Search in sources :

Example 16 with IMarkupFragment

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

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

the class WicketTester method assertMarkupLocale.

/**
 * Asserts that a component's markup has loaded with the given locale
 *
 * @param component
 *              The component which markup to check
 * @param expectedLocale
 *              The expected locale of the component's markup
 */
public void assertMarkupLocale(Component component, Locale expectedLocale) {
    Result result = Result.PASS;
    IMarkupFragment markup = getMarkupFragment(component);
    Locale actualLocale = markup.getMarkupResourceStream().getLocale();
    if (Objects.equal(expectedLocale, actualLocale) == false) {
        result = Result.fail(String.format("Wrong locale for component '%s'. Actual: '%s', expected: '%s'", component.getPageRelativePath(), actualLocale, expectedLocale));
    }
    assertResult(result);
}
Also used : Locale(java.util.Locale) IMarkupFragment(org.apache.wicket.markup.IMarkupFragment)

Example 18 with IMarkupFragment

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

the class WicketTester method assertMarkupStyle.

/**
 * Asserts that a component's markup has loaded with the given style.
 *
 * @param component
 *              The component which markup to check
 * @param expectedStyle
 *              The expected style of the component's markup.
 *              For example: <em>green</em> in <code>MyPanel_green.html</code>
 */
public void assertMarkupStyle(Component component, String expectedStyle) {
    Result result = Result.PASS;
    IMarkupFragment markup = getMarkupFragment(component);
    String actualStyle = markup.getMarkupResourceStream().getStyle();
    if (Objects.equal(expectedStyle, actualStyle) == false) {
        result = Result.fail(String.format("Wrong style for component '%s'. Actual: '%s', expected: '%s'", component.getPageRelativePath(), actualStyle, expectedStyle));
    }
    assertResult(result);
}
Also used : IMarkupFragment(org.apache.wicket.markup.IMarkupFragment)

Example 19 with IMarkupFragment

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

the class MarkupFragmentTest method border.

/**
 * @throws Exception
 */
@Test
public void border() throws Exception {
    Page page = new MyBorderPage();
    Border border = (Border) page.get("border");
    // Get the associated markup file
    IMarkupFragment markup = border.getAssociatedMarkup();
    compareMarkupWithFile(markup, "MyBorder_ExpectedResult.html", MyPage.class);
    // getMarkup() returns the "calling" tags
    markup = border.getMarkup();
    compareMarkupWithString(markup, "<span wicket:id=\"border\">test</span>");
    // getMarkup(null) returns the markup which is used to find a child component
    markup = border.getMarkup(null);
    compareMarkupWithString(markup, "<wicket:border>  111  <wicket:body/>  222</wicket:border>");
    assertNull(border.getBodyContainer().getAssociatedMarkup());
    border.dequeue();
    markup = border.getBodyContainer().getMarkup();
    compareMarkupWithString(markup, "<wicket:body/>");
    markup = border.getBodyContainer().getMarkup(null);
    compareMarkupWithString(markup, "<span wicket:id=\"border\">test</span>");
    markup = border.getBodyContainer().getParent().getMarkup(border.getBodyContainer());
    compareMarkupWithString(markup, "<wicket:body/>");
    // getMarkup(null) returns the markup which is used to find a child component
    markup = border.getBodyContainer().getMarkup(null);
    compareMarkupWithString(markup, "<span wicket:id=\"border\">test</span>");
}
Also used : Page(org.apache.wicket.Page) IMarkupFragment(org.apache.wicket.markup.IMarkupFragment) Border(org.apache.wicket.markup.html.border.Border) Test(org.junit.Test)

Example 20 with IMarkupFragment

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

the class MarkupFragmentTest method webMarkupContainer.

/**
 * @throws Exception
 */
@Test
public void webMarkupContainer() throws Exception {
    MarkupContainer container = (MarkupContainer) new MyPage().get("container");
    IMarkupFragment markup = container.getMarkup();
    compareMarkupWithString(markup, "<span wicket:id=\"container\">text</span>");
    // The container doesn't have an external markup file
    markup = container.getAssociatedMarkup();
    assertNull(markup);
    // Get the markup which is used to search for children.
    markup = container.getMarkup(null);
    compareMarkupWithString(markup, "<span wicket:id=\"container\">text</span>");
}
Also used : MarkupContainer(org.apache.wicket.MarkupContainer) IMarkupFragment(org.apache.wicket.markup.IMarkupFragment) Test(org.junit.Test)

Aggregations

IMarkupFragment (org.apache.wicket.markup.IMarkupFragment)26 Test (org.junit.Test)8 Page (org.apache.wicket.Page)6 ComponentTag (org.apache.wicket.markup.ComponentTag)6 MarkupStream (org.apache.wicket.markup.MarkupStream)6 MarkupContainer (org.apache.wicket.MarkupContainer)5 MarkupException (org.apache.wicket.markup.MarkupException)4 Component (org.apache.wicket.Component)3 MarkupElement (org.apache.wicket.markup.MarkupElement)3 MarkupNotFoundException (org.apache.wicket.markup.MarkupNotFoundException)3 ArrayList (java.util.ArrayList)2 Locale (java.util.Locale)2 WicketRuntimeException (org.apache.wicket.WicketRuntimeException)2 MarkupResourceStream (org.apache.wicket.markup.MarkupResourceStream)2 WicketTag (org.apache.wicket.markup.WicketTag)2 Border (org.apache.wicket.markup.html.border.Border)2 Panel (org.apache.wicket.markup.html.panel.Panel)2 IComponentResolver (org.apache.wicket.markup.resolver.IComponentResolver)2 IOException (java.io.IOException)1 List (java.util.List)1