Search in sources :

Example 1 with MarkupStream

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

the class HtmlHeaderContainer method getMarkup.

@Override
public IMarkupFragment getMarkup() {
    if (getParent() == null) {
        throw new WicketRuntimeException("Bug: The Wicket internal instance of HtmlHeaderContainer is not connected to a parent");
    }
    // Get the page markup
    IMarkupFragment markup = getPage().getMarkup();
    if (markup == null) {
        throw new MarkupException("Unable to get page markup: " + getPage().toString());
    }
    // Find the markup fragment
    MarkupStream stream = new MarkupStream(markup);
    IMarkupFragment headerMarkup = null;
    while (stream.skipUntil(ComponentTag.class)) {
        ComponentTag tag = stream.getTag();
        if (tag.isOpen() || tag.isOpenClose()) {
            if (tag instanceof WicketTag) {
                WicketTag wtag = (WicketTag) tag;
                if (wtag.isHeadTag() || wtag.isHeaderItemsTag()) {
                    headerMarkup = stream.getMarkupFragment();
                    break;
                }
            } else if (tag.getName().equalsIgnoreCase("head") && tag.isAutoComponentTag()) {
                headerMarkup = stream.getMarkupFragment();
                break;
            }
        }
        stream.next();
    }
    setMarkup(headerMarkup);
    return headerMarkup;
}
Also used : WicketTag(org.apache.wicket.markup.WicketTag) ComponentTag(org.apache.wicket.markup.ComponentTag) WicketRuntimeException(org.apache.wicket.WicketRuntimeException) MarkupStream(org.apache.wicket.markup.MarkupStream) IMarkupFragment(org.apache.wicket.markup.IMarkupFragment) MarkupException(org.apache.wicket.markup.MarkupException)

Example 2 with MarkupStream

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

the class AssociatedMarkupSourcingStrategy method renderHeadFromAssociatedMarkupFile.

/**
 * Called by components like Panel and Border which have associated Markup and which may have a
 * <wicket:head> tag.
 * <p>
 * Whereas 'this' might be a Panel or Border, the HtmlHeaderContainer parameter has been added
 * to the Page as a container for all headers any of its components might wish to contribute to.
 * <p>
 * The headers contributed are rendered in the standard way.
 *
 * @param container
 * @param htmlContainer
 *            The HtmlHeaderContainer added to the Page
 */
public final void renderHeadFromAssociatedMarkupFile(final WebMarkupContainer container, final HtmlHeaderContainer htmlContainer) {
    // reset for each render in case the strategy is re-used
    noMoreWicketHeadTagsAllowed = false;
    // Gracefully getAssociateMarkupStream. Throws no exception in case
    // markup is not found
    final MarkupStream markupStream = container.getAssociatedMarkupStream(false);
    if (markupStream == null) {
        return;
    }
    // Position pointer at current (first) header
    noMoreWicketHeadTagsAllowed = false;
    while (nextHeaderMarkup(markupStream) != -1) {
        // found <wicket:head>
        String headerId = getHeaderId(container, markupStream);
        // Create a HeaderPartContainer and associate the markup
        HeaderPartContainer headerPart = getHeaderPart(container, headerId, markupStream.getMarkupFragment());
        if (headerPart != null) {
            // to the page or any other container in the hierarchy.
            if (htmlContainer.okToRenderComponent(headerPart.getScope(), headerPart.getId())) {
                // make sure the Page is accessible
                headerPart.setParent(htmlContainer);
                headerPart.render();
            }
        }
        // Position the stream after <wicket:head>
        markupStream.skipComponent();
    }
}
Also used : MarkupStream(org.apache.wicket.markup.MarkupStream) HeaderPartContainer(org.apache.wicket.markup.html.HeaderPartContainer)

Example 3 with MarkupStream

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

the class AssociatedMarkupSourcingStrategy method findMarkupInAssociatedFileHeader.

/**
 * Search the child's markup in the header section of the markup
 *
 * @param container
 * @param child
 * @return Null, if not found
 */
public IMarkupFragment findMarkupInAssociatedFileHeader(final MarkupContainer container, final Component child) {
    // Get the associated markup
    IMarkupFragment markup = container.getAssociatedMarkup();
    IMarkupFragment childMarkup = null;
    // MarkupStream is good at searching markup
    MarkupStream stream = new MarkupStream(markup);
    while (stream.skipUntil(ComponentTag.class) && (childMarkup == null)) {
        ComponentTag tag = stream.getTag();
        if (TagUtils.isWicketHeadTag(tag)) {
            if (tag.getMarkupClass() == null) {
                // find() can still fail an return null => continue the search
                childMarkup = stream.getMarkupFragment().find(child.getId());
            }
        } else if (TagUtils.isHeadTag(tag)) {
            // find() can still fail an return null => continue the search
            childMarkup = stream.getMarkupFragment().find(child.getId());
        }
        // Must be a direct child. We are not interested in grand children
        if (tag.isOpen() && !tag.hasNoCloseTag()) {
            stream.skipToMatchingCloseTag(tag);
        }
        stream.next();
    }
    return childMarkup;
}
Also used : ComponentTag(org.apache.wicket.markup.ComponentTag) MarkupStream(org.apache.wicket.markup.MarkupStream) IMarkupFragment(org.apache.wicket.markup.IMarkupFragment)

Example 4 with MarkupStream

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

the class MarkupUtil method findStartTag.

/**
 * Searches for {@code tagName} in the given {@code markup}.
 *
 * @param markup
 * @param tagName
 * @return The {@link IMarkupFragment} corresponding to {@code tagName}. Null, if such {@code tagName} is not found
 */
public static IMarkupFragment findStartTag(final IMarkupFragment markup, final String tagName) {
    MarkupStream stream = new MarkupStream(markup);
    while (stream.skipUntil(WicketTag.class)) {
        ComponentTag tag = stream.getTag();
        if (tag.isOpen() || tag.isOpenClose()) {
            WicketTag wtag = (WicketTag) tag;
            if (tagName.equalsIgnoreCase(wtag.getName())) {
                return stream.getMarkupFragment();
            }
            stream.skipToMatchingCloseTag(tag);
        }
        stream.next();
    }
    return null;
}
Also used : WicketTag(org.apache.wicket.markup.WicketTag) ComponentTag(org.apache.wicket.markup.ComponentTag) MarkupStream(org.apache.wicket.markup.MarkupStream)

Example 5 with MarkupStream

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

the class TransparentWebMarkupContainer method renderHeadForInnerSiblings.

/**
 * Renders head for embedded component, i.e. those who are not added
 * directly to this container but have the markup inside it.
 *
 * @param container
 * 				The HtmlHeaderContainer
 */
private void renderHeadForInnerSiblings(HtmlHeaderContainer container) {
    MarkupStream stream = new MarkupStream(getMarkup());
    while (stream.isCurrentIndexInsideTheStream()) {
        MarkupElement childOpenTag = stream.nextOpenTag();
        if ((childOpenTag instanceof ComponentTag) && !stream.atCloseTag()) {
            // Get element as tag
            final ComponentTag tag = (ComponentTag) childOpenTag;
            // Get component id
            final String id = tag.getId();
            Component component = null;
            if (get(id) == null) {
                component = ComponentResolvers.resolveByComponentHierarchy(this, stream, tag);
            }
            if (component != null) {
                component.internalRenderHead(container);
            }
            // consider just direct children
            stream.skipToMatchingCloseTag(tag);
        }
    }
}
Also used : ComponentTag(org.apache.wicket.markup.ComponentTag) MarkupStream(org.apache.wicket.markup.MarkupStream) MarkupElement(org.apache.wicket.markup.MarkupElement) Component(org.apache.wicket.Component)

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