Search in sources :

Example 11 with XmlTag

use of org.apache.wicket.markup.parser.XmlTag in project wicket by apache.

the class MergedMarkup method merge.

/**
 * Merge inherited and base markup.
 *
 * @param markup
 *            The inherited markup
 * @param baseMarkup
 *            The base markup
 * @param extendIndex
 *            Index where <wicket:extend> has been found
 */
private void merge(final IMarkupFragment markup, final IMarkupFragment baseMarkup, int extendIndex) {
    // True if either <wicket:head> or <head> has been processed
    boolean wicketHeadProcessed = false;
    // True, if <head> was found
    boolean foundHeadTag = false;
    // Add all elements from the base markup to the new list
    // until <wicket:child/> is found. Convert <wicket:child/>
    // into <wicket:child> and add it as well.
    WicketTag childTag = null;
    int baseIndex = 0;
    MarkupResourceStream markupResourceStream = baseMarkup.getMarkupResourceStream();
    IResourceStream resource = markupResourceStream.getResource();
    Class<? extends Component> markupClass = markupResourceStream.getMarkupClass();
    for (; baseIndex < baseMarkup.size(); baseIndex++) {
        MarkupElement element = baseMarkup.get(baseIndex);
        if (element instanceof RawMarkup) {
            // Add the element to the merged list
            addMarkupElement(element);
            continue;
        }
        final ComponentTag tag = (ComponentTag) element;
        // from
        if (resource != null && tag.getMarkupClass() == null) {
            tag.setMarkupClass(markupClass);
        }
        if (element instanceof WicketTag) {
            WicketTag wtag = (WicketTag) element;
            // the deeper levels
            if (wtag.isChildTag() && tag.getMarkupClass() == markupClass) {
                if (wtag.isOpenClose()) {
                    // <wicket:child /> => <wicket:child>...</wicket:child>
                    childTag = wtag;
                    WicketTag childOpenTag = (WicketTag) wtag.mutable();
                    childOpenTag.getXmlTag().setType(TagType.OPEN);
                    childOpenTag.setMarkupClass(markupClass);
                    addMarkupElement(childOpenTag);
                    break;
                } else if (wtag.isOpen()) {
                    // <wicket:child>
                    addMarkupElement(wtag);
                    break;
                } else {
                    throw new WicketRuntimeException("Did not expect a </wicket:child> tag in " + baseMarkup.toString());
                }
            }
            // Process the head of the extended markup only once
            if (wicketHeadProcessed == false) {
                // if </wicket:head> in base markup and no <head>
                if (wtag.isClose() && wtag.isHeadTag() && (foundHeadTag == false)) {
                    wicketHeadProcessed = true;
                    // Add the current close tag
                    addMarkupElement(wtag);
                    // Add the <wicket:head> body from the derived markup.
                    copyWicketHead(markup, extendIndex);
                    // Do not add the current tag. It has already been added.
                    continue;
                }
                // if <wicket:panel> or ... in base markup
                if (wtag.isOpen() && wtag.isMajorWicketComponentTag()) {
                    wicketHeadProcessed = true;
                    // Add the <wicket:head> body from the derived markup.
                    copyWicketHead(markup, extendIndex);
                }
            }
        }
        // Process the head of the extended markup only once
        if (wicketHeadProcessed == false) {
            // Remember that we found <head> in the base markup
            if (tag.isOpen() && TagUtils.isHeadTag(tag)) {
                foundHeadTag = true;
            }
            // if <head> in base markup
            if ((tag.isClose() && TagUtils.isHeadTag(tag)) || (tag.isClose() && TagUtils.isWicketHeaderItemsTag(tag)) || (tag.isOpen() && TagUtils.isBodyTag(tag))) {
                wicketHeadProcessed = true;
                // Add the <wicket:head> body from the derived markup.
                copyWicketHead(markup, extendIndex);
            }
        }
        // Add the element to the merged list
        addMarkupElement(element);
    }
    if (baseIndex == baseMarkup.size()) {
        throw new WicketRuntimeException("Expected to find <wicket:child/> in base markup: " + baseMarkup.toString());
    }
    // <wicket:extend> until </wicket:extend> to the list
    for (; extendIndex < markup.size(); extendIndex++) {
        MarkupElement element = markup.get(extendIndex);
        addMarkupElement(element);
        if (element instanceof WicketTag) {
            WicketTag wtag = (WicketTag) element;
            if (wtag.isExtendTag() && wtag.isClose()) {
                break;
            }
        }
    }
    if (extendIndex == markup.size()) {
        throw new WicketRuntimeException("Missing close tag </wicket:extend> in derived markup: " + markup.toString());
    }
    // If <wicket:child> than skip the body and find </wicket:child>
    if (((ComponentTag) baseMarkup.get(baseIndex)).isOpen()) {
        for (baseIndex++; baseIndex < baseMarkup.size(); baseIndex++) {
            MarkupElement element = baseMarkup.get(baseIndex);
            if (element instanceof WicketTag) {
                WicketTag tag = (WicketTag) element;
                if (tag.isChildTag() && tag.isClose()) {
                    // Ok, skipped the childs content
                    tag.setMarkupClass(markupClass);
                    addMarkupElement(tag);
                    break;
                } else {
                    throw new WicketRuntimeException("Wicket tags like <wicket:xxx> are not allowed in between <wicket:child> and </wicket:child> tags: " + markup.toString());
                }
            } else if (element instanceof ComponentTag) {
                throw new WicketRuntimeException("Wicket tags identified by wicket:id are not allowed in between <wicket:child> and </wicket:child> tags: " + markup.toString());
            }
        }
        // </wicket:child> not found
        if (baseIndex == baseMarkup.size()) {
            throw new WicketRuntimeException("Expected to find </wicket:child> in base markup: " + baseMarkup.toString());
        }
    } else {
        // And now all remaining elements from the derived markup.
        // But first add </wicket:child>
        WicketTag childCloseTag = (WicketTag) childTag.mutable();
        childCloseTag.getXmlTag().setType(TagType.CLOSE);
        childCloseTag.setMarkupClass(markupClass);
        childCloseTag.setOpenTag(childTag);
        addMarkupElement(childCloseTag);
    }
    for (baseIndex++; baseIndex < baseMarkup.size(); baseIndex++) {
        MarkupElement element = baseMarkup.get(baseIndex);
        addMarkupElement(element);
        // from
        if (element instanceof ComponentTag && resource != null) {
            ComponentTag tag = (ComponentTag) element;
            if (tag.getMarkupClass() == null) {
                tag.setMarkupClass(markupClass);
            }
        }
    }
    // markup filters are not called for merged markup again, ...
    if (Page.class.isAssignableFrom(markup.getMarkupResourceStream().getMarkupClass())) {
        // Find the position inside the markup for first <wicket:head>,
        // last </wicket:head> and <head>
        int hasOpenWicketHead = -1;
        int hasCloseWicketHead = -1;
        int hasHead = -1;
        for (int i = 0; i < size(); i++) {
            MarkupElement element = get(i);
            boolean isHeadTag = (element instanceof WicketTag) && ((WicketTag) element).isHeadTag();
            if ((hasOpenWicketHead == -1) && isHeadTag) {
                hasOpenWicketHead = i;
            } else if (isHeadTag && ((ComponentTag) element).isClose()) {
                hasCloseWicketHead = i;
            } else if ((hasHead == -1) && (element instanceof ComponentTag) && TagUtils.isHeadTag(element)) {
                hasHead = i;
            } else if ((hasHead != -1) && (hasOpenWicketHead != -1)) {
                break;
            }
        }
        // If a <head> tag is missing, insert it automatically
        if ((hasOpenWicketHead != -1) && (hasHead == -1)) {
            final XmlTag headOpenTag = new XmlTag();
            headOpenTag.setName("head");
            headOpenTag.setType(TagType.OPEN);
            final ComponentTag openTag = new ComponentTag(headOpenTag);
            openTag.setId(HtmlHeaderSectionHandler.HEADER_ID);
            openTag.setAutoComponentTag(true);
            final XmlTag headCloseTag = new XmlTag();
            headCloseTag.setName(headOpenTag.getName());
            headCloseTag.setType(TagType.CLOSE);
            final ComponentTag closeTag = new ComponentTag(headCloseTag);
            closeTag.setOpenTag(openTag);
            closeTag.setId(HtmlHeaderSectionHandler.HEADER_ID);
            addMarkupElement(hasOpenWicketHead, openTag);
            addMarkupElement(hasCloseWicketHead + 2, closeTag);
        }
    }
}
Also used : IResourceStream(org.apache.wicket.util.resource.IResourceStream) WicketRuntimeException(org.apache.wicket.WicketRuntimeException) XmlTag(org.apache.wicket.markup.parser.XmlTag)

Example 12 with XmlTag

use of org.apache.wicket.markup.parser.XmlTag in project wicket by apache.

the class AttributeModifierTest method removeAttribute.

/**
 * Tests {@link AttributeModifier#remove(String)}
 *
 * https://issues.apache.org/jira/browse/WICKET-3934
 */
@Test
public void removeAttribute() {
    AttributeModifier appender = AttributeModifier.remove("class");
    XmlTag xmlTag = new XmlTag();
    ComponentTag tag = new ComponentTag(xmlTag);
    Map<String, Object> attributes = tag.getAttributes();
    attributes.put("class", "someValue");
    appender.replaceAttributeValue(null, tag);
    assertTrue(attributes.isEmpty());
}
Also used : ComponentTag(org.apache.wicket.markup.ComponentTag) XmlTag(org.apache.wicket.markup.parser.XmlTag) Test(org.junit.Test)

Example 13 with XmlTag

use of org.apache.wicket.markup.parser.XmlTag in project wicket by apache.

the class AttributeModifierTest method testModelReplacement.

/**
 * Test simple model replacement.
 */
@Test
public void testModelReplacement() {
    AttributeModifier modifier = new AttributeModifier("test", Model.of("Ellioth Smith Rocks"));
    XmlTag xmlTag = new XmlTag();
    ComponentTag tag = new ComponentTag(xmlTag);
    tag.setId("test");
    tag.setName("id");
    modifier.replaceAttributeValue(null, tag);
    Map<String, Object> attributes = tag.getAttributes();
    assertTrue(!attributes.isEmpty());
    String replacement = (String) attributes.get("test");
    assertNotNull(replacement);
    assertEquals("Ellioth Smith Rocks", replacement);
}
Also used : ComponentTag(org.apache.wicket.markup.ComponentTag) XmlTag(org.apache.wicket.markup.parser.XmlTag) Test(org.junit.Test)

Example 14 with XmlTag

use of org.apache.wicket.markup.parser.XmlTag in project wicket by apache.

the class AttributeModifierTest method testNewValue.

/**
 * Test overriding newValue (and using a null model).
 */
@Test
public void testNewValue() {
    AttributeModifier modifier = new AttributeModifier("test", null) {

        private static final long serialVersionUID = 1L;

        @Override
        protected String newValue(String currentValue, String replacementValue) {
            return "the replacement";
        }
    };
    XmlTag xmlTag = new XmlTag();
    ComponentTag tag = new ComponentTag(xmlTag);
    tag.setId("test");
    tag.setName("id");
    modifier.replaceAttributeValue(null, tag);
    Map<String, Object> attributes = tag.getAttributes();
    assertTrue(!attributes.isEmpty());
    String replacement = (String) attributes.get("test");
    assertNotNull(replacement);
    assertEquals("the replacement", replacement);
}
Also used : ComponentTag(org.apache.wicket.markup.ComponentTag) XmlTag(org.apache.wicket.markup.parser.XmlTag) Test(org.junit.Test)

Example 15 with XmlTag

use of org.apache.wicket.markup.parser.XmlTag in project wicket by apache.

the class ClassAttributeModifierTest method createTag.

private ComponentTag createTag() {
    XmlTag xmlTag = new XmlTag();
    ComponentTag tag = new ComponentTag(xmlTag);
    tag.setId("ClassAttributeModifier");
    tag.setName("test");
    return tag;
}
Also used : ComponentTag(org.apache.wicket.markup.ComponentTag) XmlTag(org.apache.wicket.markup.parser.XmlTag)

Aggregations

XmlTag (org.apache.wicket.markup.parser.XmlTag)17 ComponentTag (org.apache.wicket.markup.ComponentTag)12 Test (org.junit.Test)11 XmlPullParser (org.apache.wicket.markup.parser.XmlPullParser)4 WicketRuntimeException (org.apache.wicket.WicketRuntimeException)3 ArrayList (java.util.ArrayList)1 Stack (java.util.Stack)1 HeaderItem (org.apache.wicket.markup.head.HeaderItem)1 IHeaderResponse (org.apache.wicket.markup.head.IHeaderResponse)1 JavaScriptHeaderItem (org.apache.wicket.markup.head.JavaScriptHeaderItem)1 JavaScriptReferenceHeaderItem (org.apache.wicket.markup.head.JavaScriptReferenceHeaderItem)1 ResourceAggregator (org.apache.wicket.markup.head.ResourceAggregator)1 PackageResourceReference (org.apache.wicket.request.resource.PackageResourceReference)1 IResourceStream (org.apache.wicket.util.resource.IResourceStream)1