Search in sources :

Example 1 with MarkupResourceStream

use of org.apache.wicket.markup.MarkupResourceStream in project wicket by apache.

the class MarkupUtil method isMarkupHtml5Compliant.

/**
 * @param container
 * @return True if the Page and all it's Panels, Borders etc. have HTML5 compliant markup. HTML5
 *         markup is identified by <DOCTYPE html>
 */
public static boolean isMarkupHtml5Compliant(final MarkupContainer container) {
    Args.notNull(container, "container");
    Page page = container.getPage();
    final boolean[] rtn = new boolean[] { true };
    page.visitChildren(MarkupContainer.class, new IVisitor<MarkupContainer, Void>() {

        @Override
        public void component(final MarkupContainer comp, final IVisit<Void> visit) {
            IMarkupFragment associatedMarkup = comp.getAssociatedMarkup();
            if (associatedMarkup != null) {
                MarkupResourceStream rs = associatedMarkup.getMarkupResourceStream();
                if (rs.isHtml5() == false) {
                    rtn[0] = false;
                    visit.stop();
                }
            }
        }
    });
    return rtn[0];
}
Also used : MarkupContainer(org.apache.wicket.MarkupContainer) Page(org.apache.wicket.Page) MarkupResourceStream(org.apache.wicket.markup.MarkupResourceStream) IMarkupFragment(org.apache.wicket.markup.IMarkupFragment)

Example 2 with MarkupResourceStream

use of org.apache.wicket.markup.MarkupResourceStream in project wicket by apache.

the class BorderBehavior method findMarkupStream.

/**
 * @param owner
 * @return markup stream
 */
private MarkupStream findMarkupStream(final Component owner) {
    final MarkupType markupType = getMarkupType(owner);
    if (markupType == null) {
        return null;
    }
    // TODO we need to expose this functionality for any class not just for
    // markupcontainers in markupcache so we don't have to replicate this
    // logic here
    // Get locator to search for the resource
    final IResourceStreamLocator locator = Application.get().getResourceSettings().getResourceStreamLocator();
    final String style = owner.getStyle();
    final String variation = owner.getVariation();
    final Locale locale = owner.getLocale();
    MarkupResourceStream markupResourceStream = null;
    Class<?> containerClass = getClass();
    while (!(containerClass.equals(BorderBehavior.class))) {
        String path = containerClass.getName().replace('.', '/');
        IResourceStream resourceStream = locator.locate(containerClass, path, style, variation, locale, markupType.getExtension(), false);
        // Did we find it already?
        if (resourceStream != null) {
            ContainerInfo ci = new ContainerInfo(containerClass, locale, style, variation, markupType);
            markupResourceStream = new MarkupResourceStream(resourceStream, ci, containerClass);
            break;
        }
        // Walk up the class hierarchy one level, if markup has not
        // yet been found
        containerClass = containerClass.getSuperclass();
    }
    if (markupResourceStream == null) {
        throw new WicketRuntimeException("Could not find markup for component border `" + getClass().getName() + "`");
    }
    try {
        IMarkupFragment markup = MarkupFactory.get().newMarkupParser(markupResourceStream).parse();
        return new MarkupStream(markup);
    } catch (Exception e) {
        throw new WicketRuntimeException("Could not parse markup from markup resource stream: " + markupResourceStream.toString(), e);
    } finally {
        try {
            markupResourceStream.close();
        } catch (IOException e) {
            throw new WicketRuntimeException("Cannot close markup resource stream: " + markupResourceStream, e);
        }
    }
}
Also used : Locale(java.util.Locale) IResourceStream(org.apache.wicket.util.resource.IResourceStream) WicketRuntimeException(org.apache.wicket.WicketRuntimeException) IResourceStreamLocator(org.apache.wicket.core.util.resource.locator.IResourceStreamLocator) MarkupResourceStream(org.apache.wicket.markup.MarkupResourceStream) MarkupStream(org.apache.wicket.markup.MarkupStream) IOException(java.io.IOException) WicketRuntimeException(org.apache.wicket.WicketRuntimeException) IOException(java.io.IOException) ContainerInfo(org.apache.wicket.markup.ContainerInfo) IMarkupFragment(org.apache.wicket.markup.IMarkupFragment) MarkupType(org.apache.wicket.markup.MarkupType)

Example 3 with MarkupResourceStream

use of org.apache.wicket.markup.MarkupResourceStream in project wicket by apache.

the class BaseWicketTester method startComponentInPage.

/**
 * Process a component. A web page will be automatically created with the {@code pageMarkup}
 * provided. In case {@code pageMarkup} is null, the markup will be automatically created with
 * {@link #createPageMarkup(String)}.
 * <p>
 * <strong>Note</strong>: the component id is set by the user. To reach any of its children use
 * this id + their relative path to the component itself. For example if the started component
 * has id <em>compId</em> and a Link child component component with id "link" then after
 * starting the component you can click it with: <code>tester.clickLink("compId:link")</code>
 * </p>
 *
 * @param <C>
 *            the type of the component
 * @param component
 *            the component to be tested
 * @param pageMarkup
 *            the markup for the Page that will be automatically created. May be {@code null}.
 * @return The component processed
 */
public final <C extends Component> C startComponentInPage(final C component, IMarkupFragment pageMarkup) {
    Args.notNull(component, "component");
    // Create a page object and assign the markup
    Page page = createPage();
    if (page == null) {
        fail("The automatically created page should not be null.");
    }
    // Automatically create the page markup if not provided
    if (pageMarkup == null) {
        String markup = createPageMarkup(component.getId());
        if (markup == null) {
            fail("The markup for the automatically created page should not be null.");
        }
        try {
            // set a ContainerInfo to be able to use HtmlHeaderContainer so header contribution
            // still work. WICKET-3700
            ContainerInfo containerInfo = new ContainerInfo(page);
            MarkupResourceStream markupResourceStream = new MarkupResourceStream(new StringResourceStream(markup), containerInfo, page.getClass());
            MarkupParser markupParser = getApplication().getMarkupSettings().getMarkupFactory().newMarkupParser(markupResourceStream);
            pageMarkup = markupParser.parse();
        } catch (Exception e) {
            String errorMessage = "Error while parsing the markup for the autogenerated page: " + e.getMessage();
            log.error(errorMessage, e);
            fail(errorMessage);
        }
    }
    if (page instanceof StartComponentInPage) {
        ((StartComponentInPage) page).setPageMarkup(pageMarkup);
    } else {
        page.setMarkup(pageMarkup);
    }
    // Add the child component
    page.add(component);
    // Preserve 'componentInPage' because #startPage() needs to null-fy it
    ComponentInPage oldComponentInPage = componentInPage;
    // Process the page
    startPage(page);
    // Remember the "root" component processes and return it
    if (oldComponentInPage != null) {
        componentInPage = oldComponentInPage;
    } else {
        componentInPage = new ComponentInPage();
        componentInPage.component = component;
    }
    return component;
}
Also used : ContainerInfo(org.apache.wicket.markup.ContainerInfo) WebPage(org.apache.wicket.markup.html.WebPage) Page(org.apache.wicket.Page) MarkupResourceStream(org.apache.wicket.markup.MarkupResourceStream) StringResourceStream(org.apache.wicket.util.resource.StringResourceStream) WicketRuntimeException(org.apache.wicket.WicketRuntimeException) IOException(java.io.IOException) ParseException(java.text.ParseException) ResourceStreamNotFoundException(org.apache.wicket.util.resource.ResourceStreamNotFoundException) MarkupParser(org.apache.wicket.markup.MarkupParser)

Example 4 with MarkupResourceStream

use of org.apache.wicket.markup.MarkupResourceStream in project wicket by apache.

the class MergedMarkupTest method test1.

/**
 * test1()
 */
@Test
public void test1() {
    Page page = new SubPageWithoutMarkup();
    Markup markup = page.getAssociatedMarkup();
    MarkupResourceStream stream = markup.getMarkupResourceStream();
    assertEquals("utf-8", stream.getEncoding());
    assertEquals(MarkupParser.WICKET, stream.getWicketNamespace());
}
Also used : Markup(org.apache.wicket.markup.Markup) Page(org.apache.wicket.Page) MarkupResourceStream(org.apache.wicket.markup.MarkupResourceStream) Test(org.junit.Test)

Aggregations

MarkupResourceStream (org.apache.wicket.markup.MarkupResourceStream)4 Page (org.apache.wicket.Page)3 IOException (java.io.IOException)2 WicketRuntimeException (org.apache.wicket.WicketRuntimeException)2 ContainerInfo (org.apache.wicket.markup.ContainerInfo)2 IMarkupFragment (org.apache.wicket.markup.IMarkupFragment)2 ParseException (java.text.ParseException)1 Locale (java.util.Locale)1 MarkupContainer (org.apache.wicket.MarkupContainer)1 IResourceStreamLocator (org.apache.wicket.core.util.resource.locator.IResourceStreamLocator)1 Markup (org.apache.wicket.markup.Markup)1 MarkupParser (org.apache.wicket.markup.MarkupParser)1 MarkupStream (org.apache.wicket.markup.MarkupStream)1 MarkupType (org.apache.wicket.markup.MarkupType)1 WebPage (org.apache.wicket.markup.html.WebPage)1 IResourceStream (org.apache.wicket.util.resource.IResourceStream)1 ResourceStreamNotFoundException (org.apache.wicket.util.resource.ResourceStreamNotFoundException)1 StringResourceStream (org.apache.wicket.util.resource.StringResourceStream)1 Test (org.junit.Test)1