Search in sources :

Example 16 with MarkupStream

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

the class Component method toString.

/**
 * @param detailed
 *            True if a detailed string is desired
 * @return The string
 */
public String toString(final boolean detailed) {
    try {
        final StringBuilder buffer = new StringBuilder();
        buffer.append("[Component id = ").append(getId());
        if (detailed) {
            final Page page = findPage();
            if (page == null) {
                buffer.append(", page = <No Page>, path = ").append(getPath()).append('.').append(Classes.simpleName(getClass()));
            } else {
                buffer.append(", page = ").append(Classes.name(getPage().getPageClass())).append(", path = ").append(getPageRelativePath()).append(", type = ").append(Classes.name(getClass())).append(", isVisible = ").append((determineVisibility())).append(", isVersioned = ").append(isVersioned());
            }
            if (markup != null) {
                buffer.append(", markup = ").append(new MarkupStream(getMarkup()).toString());
            }
        }
        buffer.append(']');
        return buffer.toString();
    } catch (Exception e) {
        log.warn("Error while building toString()", e);
        return String.format("[Component id = %s <attributes are not available because exception %s was thrown during toString()>]", getId(), e.getClass().getName());
    }
}
Also used : IRequestablePage(org.apache.wicket.request.component.IRequestablePage) MarkupStream(org.apache.wicket.markup.MarkupStream) AuthorizationException(org.apache.wicket.authorization.AuthorizationException) MarkupNotFoundException(org.apache.wicket.markup.MarkupNotFoundException) UnauthorizedActionException(org.apache.wicket.authorization.UnauthorizedActionException) MarkupException(org.apache.wicket.markup.MarkupException)

Example 17 with MarkupStream

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

the class MergedMarkupTest method test2.

/**
 * test2()
 */
@Test
public void test2() {
    Page page = new SubPageWithMarkup();
    MarkupStream markup = page.getAssociatedMarkupStream(true);
    assertEquals("utf-8", markup.getEncoding());
    assertEquals(MarkupParser.WICKET, markup.getWicketNamespace());
}
Also used : Page(org.apache.wicket.Page) MarkupStream(org.apache.wicket.markup.MarkupStream) Test(org.junit.Test)

Example 18 with MarkupStream

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

the class BorderBehavior method beforeRender.

@Override
public void beforeRender(final Component component) {
    final MarkupStream stream = getMarkupStream(component);
    final Response response = component.getResponse();
    stream.setCurrentIndex(0);
    boolean insideBorderMarkup = false;
    while (stream.isCurrentIndexInsideTheStream()) {
        MarkupElement elem = stream.get();
        stream.next();
        if (elem instanceof WicketTag) {
            WicketTag wTag = (WicketTag) elem;
            if (!insideBorderMarkup) {
                if (wTag.isBorderTag() && wTag.isOpen()) {
                    insideBorderMarkup = true;
                    continue;
                } else {
                    throw new WicketRuntimeException("Unexpected tag encountered in markup of component border " + getClass().getName() + ". Tag: " + wTag.toString() + ", expected tag: <wicket:border>");
                }
            } else {
                if (wTag.isBodyTag()) {
                    break;
                } else {
                    throw new WicketRuntimeException("Unexpected tag encountered in markup of component border " + getClass().getName() + ". Tag: " + wTag.toString() + ", expected tag: <wicket:body> or </wicket:body>");
                }
            }
        }
        if (insideBorderMarkup) {
            response.write(elem.toCharSequence());
        }
    }
    if (!stream.isCurrentIndexInsideTheStream()) {
        throw new WicketRuntimeException("Markup for component border " + getClass().getName() + " ended prematurely, was expecting </wicket:border>");
    }
}
Also used : Response(org.apache.wicket.request.Response) WicketTag(org.apache.wicket.markup.WicketTag) WicketRuntimeException(org.apache.wicket.WicketRuntimeException) MarkupStream(org.apache.wicket.markup.MarkupStream) MarkupElement(org.apache.wicket.markup.MarkupElement)

Example 19 with MarkupStream

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

use of org.apache.wicket.markup.MarkupStream 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

MarkupStream (org.apache.wicket.markup.MarkupStream)24 ComponentTag (org.apache.wicket.markup.ComponentTag)14 IMarkupFragment (org.apache.wicket.markup.IMarkupFragment)6 MarkupException (org.apache.wicket.markup.MarkupException)6 WicketRuntimeException (org.apache.wicket.WicketRuntimeException)5 MarkupElement (org.apache.wicket.markup.MarkupElement)5 WicketTag (org.apache.wicket.markup.WicketTag)5 Component (org.apache.wicket.Component)3 MarkupContainer (org.apache.wicket.MarkupContainer)3 IOException (java.io.IOException)2 MarkupNotFoundException (org.apache.wicket.markup.MarkupNotFoundException)2 Label (org.apache.wicket.markup.html.basic.Label)2 Response (org.apache.wicket.request.Response)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Locale (java.util.Locale)1 Page (org.apache.wicket.Page)1 AuthorizationException (org.apache.wicket.authorization.AuthorizationException)1 UnauthorizedActionException (org.apache.wicket.authorization.UnauthorizedActionException)1 IResourceStreamLocator (org.apache.wicket.core.util.resource.locator.IResourceStreamLocator)1