Example usage for org.apache.wicket.markup MarkupParser parse

List of usage examples for org.apache.wicket.markup MarkupParser parse

Introduction

In this page you can find the example usage for org.apache.wicket.markup MarkupParser parse.

Prototype

public final Markup parse() throws IOException, ResourceStreamNotFoundException 

Source Link

Document

Reads and parses markup from a file.

Usage

From source file:com.premiumminds.webapp.wicket.testing.ExtendedWicketTester.java

License:Open Source License

/**
 * 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 #createFormPageMarkup(String)}.
 * <p>/*  w  w w .  j  av  a 2 s . c  o  m*/
 *     <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 inputType
 *            the type of HTML input to be associated with 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
 */
@SuppressWarnings("deprecation")
public final <C extends Component> C startComponentInForm(final C component, final String inputType,
        IMarkupFragment pageMarkup) {
    Args.notNull(component, "component");

    // Create a page object and assign the markup
    Page page = createFormPage();
    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 = createFormPageMarkup(component.getId(), inputType);
        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) {
            fail("Error while parsing the markup for the autogenerated page: " + e.getMessage());
        }
    }

    if (page instanceof StartComponentInForm) {
        ((StartComponentInForm) page).setPageMarkup(pageMarkup);
    } else {
        page.setMarkup(pageMarkup);
    }

    // Add the child component
    Form<Void> form = new Form<Void>("form");
    page.add(form);
    form.add(component);

    // Preserve 'componentInForm' because #startPage() needs to null-fy it
    ComponentInForm oldComponentInForm = componentInForm;

    // Process the page
    startPage(page);

    // Remember the "root" component processes and return it
    if (oldComponentInForm != null) {
        componentInForm = oldComponentInForm;
    } else {
        componentInForm = new ComponentInForm();
        componentInForm.component = component;
    }
    return component;
}

From source file:org.opensingular.lib.wicket.util.bootstrap.layout.TemplatePanel.java

License:Apache License

@Override
protected IMarkupSourcingStrategy newMarkupSourcingStrategy() {
    return new PanelMarkupSourcingStrategy(false) {
        @Override//from ww  w.  j  a v a2 s .co m
        public IMarkupFragment getMarkup(MarkupContainer parent, Component child) {
            // corrige o problema de encoding
            StringResourceStream stringResourceStream = new StringResourceStream(
                    "<wicket:panel>" + getTemplateFunction().apply(TemplatePanel.this) + "</wicket:panel>",
                    "text/html");
            stringResourceStream.setCharset(Charset.forName(
                    Optional.ofNullable(Application.get().getMarkupSettings().getDefaultMarkupEncoding())
                            .orElse(StandardCharsets.UTF_8.name())));

            MarkupParser markupParser = new MarkupParser(new MarkupResourceStream(stringResourceStream));
            markupParser.setWicketNamespace(MarkupParser.WICKET);
            Markup markup;
            try {
                markup = markupParser.parse();
            } catch (Exception e) {
                throw SingularUtil.propagate(e);
            }

            // If child == null, than return the markup fragment starting
            // with <wicket:panel>
            if (child == null) {
                return markup;
            }

            // Copiado da superclasse. buscando markup do child
            IMarkupFragment associatedMarkup = markup.find(child.getId());
            if (associatedMarkup != null) {
                return associatedMarkup;
            }
            associatedMarkup = searchMarkupInTransparentResolvers(parent, parent.getMarkup(), child);
            if (associatedMarkup != null) {
                return associatedMarkup;
            }
            return findMarkupInAssociatedFileHeader(parent, child);
        }

        @Override
        public void onComponentTagBody(Component component, MarkupStream markupStream, ComponentTag openTag) {
            TemplatePanel.this.onBeforeComponentTagBody(markupStream, openTag);
            super.onComponentTagBody(component, markupStream, openTag);
            TemplatePanel.this.onAfterComponentTagBody(markupStream, openTag);
        }
    };
}

From source file:sf.wicklet.wicketext.test.TestMarkupParser01.java

License:Apache License

@Test
public void test01() throws Exception {
    final String input = ResourceUtil.readString(
            new PackageResourceStream(getClass(), "sf/wicklet/wicketext/test/TestMarkupParser01Test01.html"));
    final MarkupParser parser = new MarkupParser(input);
    final IRootMarkup ret = parser.parse();
    final MarkupStat stat = WicketExtrasUtil.dump(System.out, DEBUG, ret);
    assertEquals(17, stat.ctags);// ww  w  . j a v  a 2  s. c  om
    assertEquals(18, stat.rtags);
    assertEquals(0, stat.others);
}

From source file:sf.wicklet.wicketext.wicket.core.test.MarkupParser01.java

License:Apache License

@Test
public void test01() throws IOException, ResourceStreamNotFoundException {
    final String DATA = "/sf/wicklet/wicketext/wicket/core/test/MarkupParser01Test01.html";
    final String content = FileUtil.getResourceAsString(DATA);
    new WicketTester();
    final MarkupParser parser = new MarkupParser(content);
    final IMarkupFragment ret = parser.parse();
    int components = 0;
    if (DEBUG) {/* w w w  .ja v a  2  s . c o  m*/
        System.out.println("### Markup: " + ret.size());
    }
    for (final IMarkupElement e : ret) {
        if (DEBUG) {
            System.out.println(e.getClass().getName() + ": " + e.toUserDebugString());
        }
        if (e instanceof ComponentTag) {
            ++components;
        }
    }
    assertEquals(7, components);
}