Search in sources :

Example 11 with MarkupElement

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

the class FragmentMarkupSourcingStrategy method getMarkup.

/**
 * Search for the child's markup in the fragment markup.
 */
@Override
public IMarkupFragment getMarkup(final MarkupContainer container, final Component child) {
    // Get the markup to search for the fragment markup
    IMarkupFragment markup = chooseMarkup(container);
    if (markup == null) {
        throw new MarkupException("The fragments markup provider has no associated markup. " + "No markup to search for fragment markup with id: " + markupId);
    }
    // Search for the fragment markup
    IMarkupFragment childMarkup = TagUtils.findTagMarkup(markup, markupId, FRAGMENT, 1);
    if (childMarkup == null) {
        // There is one more option if the markup provider has associated markup
        MarkupContainer markupProvider = getMarkupProvider(container);
        Markup associatedMarkup = markupProvider.getAssociatedMarkup();
        if (associatedMarkup != null) {
            markup = associatedMarkup;
            if (markup != null) {
                childMarkup = markup.find(markupId);
            }
        }
    }
    if (childMarkup == null) {
        throw new MarkupNotFoundException("No Markup found for Fragment " + markupId + " in providing markup container " + getMarkupProvider(container));
    } else {
        MarkupElement fragmentTag = childMarkup.get(0);
        if ((fragmentTag instanceof WicketTag && ((WicketTag) fragmentTag).isFragmentTag()) == false) {
            throw new MarkupNotFoundException("Markup found for Fragment '" + markupId + "' in providing markup container " + getMarkupProvider(container) + " is not a <wicket:fragment> tag");
        }
    }
    if (child == null) {
        return childMarkup;
    }
    // search for the child inside the fragment markup
    return childMarkup.find(child.getId());
}
Also used : WicketTag(org.apache.wicket.markup.WicketTag) MarkupContainer(org.apache.wicket.MarkupContainer) Markup(org.apache.wicket.markup.Markup) MarkupNotFoundException(org.apache.wicket.markup.MarkupNotFoundException) IMarkupFragment(org.apache.wicket.markup.IMarkupFragment) MarkupElement(org.apache.wicket.markup.MarkupElement) MarkupException(org.apache.wicket.markup.MarkupException)

Example 12 with MarkupElement

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

the class MarkupContainer method renderNext.

/**
 * THIS METHOD IS NOT PART OF THE WICKET PUBLIC API. DO NOT USE OR OVERWRITE IT.
 *
 * Renders the next element of markup in the given markup stream.
 *
 * @param markupStream
 *            The markup stream
 * @return true, if element was rendered as RawMarkup
 */
protected boolean renderNext(final MarkupStream markupStream) {
    // Get the current markup element
    final MarkupElement element = markupStream.get();
    // If it's a tag like <wicket..> or <span wicket:id="..." >
    if ((element instanceof ComponentTag) && !markupStream.atCloseTag()) {
        // Get element as tag
        final ComponentTag tag = (ComponentTag) element;
        if (tag instanceof WicketTag && ((WicketTag) tag).isFragmentTag()) {
            return false;
        }
        // Get component id
        final String id = tag.getId();
        // Get the component for the id from the given container
        Component component = get(id);
        if (component == null) {
            component = ComponentResolvers.resolve(this, markupStream, tag, null);
            if ((component != null) && (component.getParent() == null)) {
                autoAdd(component, markupStream);
            } else if (component != null) {
                component.setMarkup(markupStream.getMarkupFragment());
            }
        }
        // Failed to find it?
        if (component != null) {
            component.render();
        } else if (tag.getFlag(ComponentTag.RENDER_RAW)) {
            // No component found, but "render as raw markup" flag found
            if (canRenderRawTag(tag)) {
                getResponse().write(element.toCharSequence());
            }
            return true;
        } else {
            throwException(markupStream, tag);
        }
    } else {
        // Render as raw markup
        if (canRenderRawTag(element)) {
            getResponse().write(element.toCharSequence());
        }
        return true;
    }
    return false;
}
Also used : WicketTag(org.apache.wicket.markup.WicketTag) ComponentTag(org.apache.wicket.markup.ComponentTag) MarkupElement(org.apache.wicket.markup.MarkupElement)

Example 13 with MarkupElement

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

the class MarkupContainer method renderAssociatedMarkup.

/**
 * Renders the entire associated markup for a container such as a Border or Panel. Any leading
 * or trailing raw markup in the associated markup is skipped.
 *
 * @param openTagName
 *            the tag to render the associated markup for
 * @param exceptionMessage
 *            message that will be used for exceptions
 */
public final void renderAssociatedMarkup(final String openTagName, final String exceptionMessage) {
    // Get associated markup file for the Border or Panel component
    final MarkupStream associatedMarkupStream = new MarkupStream(getMarkup(null));
    // Get open tag in associated markup of border component
    MarkupElement elem = associatedMarkupStream.get();
    if ((elem instanceof ComponentTag) == false) {
        associatedMarkupStream.throwMarkupException("Expected the open tag. " + exceptionMessage);
    }
    // Check for required open tag name
    ComponentTag associatedMarkupOpenTag = (ComponentTag) elem;
    if (!(associatedMarkupOpenTag.isOpen() && (associatedMarkupOpenTag instanceof WicketTag))) {
        associatedMarkupStream.throwMarkupException(exceptionMessage);
    }
    try {
        setIgnoreAttributeModifier(true);
        renderComponentTag(associatedMarkupOpenTag);
        associatedMarkupStream.next();
        String className = null;
        final boolean outputClassName = getApplication().getDebugSettings().isOutputMarkupContainerClassName();
        if (outputClassName) {
            className = Classes.name(getClass());
            getResponse().write("<!-- MARKUP FOR ");
            getResponse().write(className);
            getResponse().write(" BEGIN -->");
        }
        renderComponentTagBody(associatedMarkupStream, associatedMarkupOpenTag);
        if (outputClassName) {
            getResponse().write("<!-- MARKUP FOR ");
            getResponse().write(className);
            getResponse().write(" END -->");
        }
        renderClosingComponentTag(associatedMarkupStream, associatedMarkupOpenTag, false);
    } finally {
        setIgnoreAttributeModifier(false);
    }
}
Also used : WicketTag(org.apache.wicket.markup.WicketTag) ComponentTag(org.apache.wicket.markup.ComponentTag) MarkupStream(org.apache.wicket.markup.MarkupStream) MarkupElement(org.apache.wicket.markup.MarkupElement)

Example 14 with MarkupElement

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

the class DequeueContext method nextTag.

private ComponentTag nextTag() {
    if (skipFirst && first == null) {
        for (; index < markup.size(); index++) {
            MarkupElement element = markup.get(index);
            if (element instanceof ComponentTag) {
                first = (ComponentTag) element;
                index++;
                break;
            }
        }
    }
    for (; index < markup.size(); index++) {
        MarkupElement element = markup.get(index);
        if (element instanceof ComponentTag) {
            ComponentTag tag = (ComponentTag) element;
            if (tag.isOpen() || tag.isOpenClose()) {
                DequeueTagAction action = canDequeueTag(tag);
                switch(action) {
                    case IGNORE:
                        continue;
                    case DEQUEUE:
                        index++;
                        return tag;
                    case // skip to close tag
                    SKIP:
                        boolean found = false;
                        for (; index < markup.size(); index++) {
                            if ((markup.get(index) instanceof ComponentTag) && markup.get(index).closes(tag)) {
                                found = true;
                                break;
                            }
                        }
                        if (!found) {
                            throw new IllegalStateException(String.format("Could not find close tag for tag '%s' in markup: %s ", tag, markup));
                        }
                }
            } else {
                // closed tag
                ComponentTag open = tag.isClose() ? tag.getOpenTag() : tag;
                if (skipFirst && first != null && open == first) {
                    continue;
                }
                switch(canDequeueTag(open)) {
                    case DEQUEUE:
                        index++;
                        return tag;
                    case IGNORE:
                        continue;
                    case SKIP:
                        throw new IllegalStateException(String.format("Should not see closed tag of skipped open tag '%s' in markup:%s", tag, markup));
                }
            }
        }
    }
    return null;
}
Also used : ComponentTag(org.apache.wicket.markup.ComponentTag) MarkupElement(org.apache.wicket.markup.MarkupElement)

Aggregations

MarkupElement (org.apache.wicket.markup.MarkupElement)14 ComponentTag (org.apache.wicket.markup.ComponentTag)8 WicketTag (org.apache.wicket.markup.WicketTag)6 MarkupStream (org.apache.wicket.markup.MarkupStream)5 IMarkupFragment (org.apache.wicket.markup.IMarkupFragment)3 Test (org.junit.Test)3 WicketRuntimeException (org.apache.wicket.WicketRuntimeException)2 Markup (org.apache.wicket.markup.Markup)2 MarkupException (org.apache.wicket.markup.MarkupException)2 MarkupNotFoundException (org.apache.wicket.markup.MarkupNotFoundException)2 Response (org.apache.wicket.request.Response)2 Component (org.apache.wicket.Component)1 MarkupContainer (org.apache.wicket.MarkupContainer)1 MarkupFragment (org.apache.wicket.markup.MarkupFragment)1 RawMarkup (org.apache.wicket.markup.RawMarkup)1 BorderMarkupSourcingStrategy (org.apache.wicket.markup.html.panel.BorderMarkupSourcingStrategy)1 AbstractMarkupFilter (org.apache.wicket.markup.parser.AbstractMarkupFilter)1