Search in sources :

Example 1 with MarkupParser

use of org.apache.wicket.markup.MarkupParser 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 2 with MarkupParser

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

the class DiffUtil method compareMarkup.

/**
 * @param a
 *            String a
 * @param b
 *            String b
 * @return True if the two strings have the same markup tags
 */
private static boolean compareMarkup(final String a, final String b) {
    try {
        // Parse a and b into markup and compare
        final MarkupStream amarkup = new MarkupStream(new MarkupParser(a).parse());
        final MarkupStream bmarkup = new MarkupStream(new MarkupParser(b).parse());
        return amarkup.equalTo(bmarkup);
    } catch (IOException e) {
        log.error(e.getMessage(), e);
    } catch (ResourceStreamNotFoundException e) {
        log.error(e.getMessage(), e);
    }
    return false;
}
Also used : MarkupStream(org.apache.wicket.markup.MarkupStream) IOException(java.io.IOException) ResourceStreamNotFoundException(org.apache.wicket.util.resource.ResourceStreamNotFoundException) MarkupParser(org.apache.wicket.markup.MarkupParser)

Aggregations

IOException (java.io.IOException)2 MarkupParser (org.apache.wicket.markup.MarkupParser)2 ResourceStreamNotFoundException (org.apache.wicket.util.resource.ResourceStreamNotFoundException)2 ParseException (java.text.ParseException)1 Page (org.apache.wicket.Page)1 WicketRuntimeException (org.apache.wicket.WicketRuntimeException)1 ContainerInfo (org.apache.wicket.markup.ContainerInfo)1 MarkupResourceStream (org.apache.wicket.markup.MarkupResourceStream)1 MarkupStream (org.apache.wicket.markup.MarkupStream)1 WebPage (org.apache.wicket.markup.html.WebPage)1 StringResourceStream (org.apache.wicket.util.resource.StringResourceStream)1