Search in sources :

Example 6 with XmlTag

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

the class AttributeModifierTest method testNewValueForAttributeValue.

/**
 * Test using newValue for appending to the current attribute value.
 */
@Test
public void testNewValueForAttributeValue() {
    AttributeModifier modifier = new AttributeModifier("test", null) {

        private static final long serialVersionUID = 1L;

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

Example 7 with XmlTag

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

the class AttributeModifierTest method testNewValueForModelValue.

/**
 * Test using newValue for appending to the model value.
 */
@Test
public void testNewValueForModelValue() {
    AttributeModifier modifier = new AttributeModifier("test", Model.of("happy")) {

        private static final long serialVersionUID = 1L;

        @Override
        protected String newValue(String currentValue, String replacementValue) {
            return replacementValue + " together";
        }
    };
    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("happy together", replacement);
}
Also used : ComponentTag(org.apache.wicket.markup.ComponentTag) XmlTag(org.apache.wicket.markup.parser.XmlTag) Test(org.junit.Test)

Example 8 with XmlTag

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

the class AttributeModifierTest method appendSpecialAttribute.

/**
 * Add an attribute with name equal (Object#equals()) to the special
 * {@link AttributeModifier#VALUELESS_ATTRIBUTE_REMOVE} but not identity equal
 *
 * https://issues.apache.org/jira/browse/WICKET-3934
 */
@Test
public void appendSpecialAttribute() {
    String attrName = "attrName";
    AttributeModifier appender = AttributeModifier.append(attrName, "VA_REMOVE");
    XmlTag xmlTag = new XmlTag();
    ComponentTag tag = new ComponentTag(xmlTag);
    Map<String, Object> attributes = tag.getAttributes();
    attributes.put(attrName, "VA_REMOVE");
    appender.replaceAttributeValue(null, tag);
    assertFalse(attributes.isEmpty());
    assertEquals("VA_REMOVE VA_REMOVE", attributes.get(attrName));
}
Also used : ComponentTag(org.apache.wicket.markup.ComponentTag) XmlTag(org.apache.wicket.markup.parser.XmlTag) Test(org.junit.Test)

Example 9 with XmlTag

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

the class TagTester method hasChildTag.

/**
 * Checks if the tag has a child with the given <code>tagName</code>.
 *
 * @param tagName
 *            the tag name to search for
 * @return <code>true</code> if this tag has a child with the given <code>tagName</code>.
 */
public boolean hasChildTag(String tagName) {
    Args.notEmpty(tagName, "tagName");
    boolean hasChild = false;
    if (openTag.isOpen()) {
        try {
            // Get the content of the tag
            int startPos = openTag.getPos() + openTag.getLength();
            int endPos = closeTag.getPos();
            String markup = parser.getInput(startPos, endPos).toString();
            if (Strings.isEmpty(markup) == false) {
                XmlPullParser p = new XmlPullParser();
                p.parse(markup);
                XmlTag tag;
                while ((tag = p.nextTag()) != null) {
                    if (tagName.equalsIgnoreCase(tag.getName())) {
                        hasChild = true;
                        break;
                    }
                }
            }
        } catch (Exception e) {
            throw new WicketRuntimeException(e);
        }
    }
    return hasChild;
}
Also used : WicketRuntimeException(org.apache.wicket.WicketRuntimeException) XmlPullParser(org.apache.wicket.markup.parser.XmlPullParser) WicketRuntimeException(org.apache.wicket.WicketRuntimeException) XmlTag(org.apache.wicket.markup.parser.XmlTag)

Example 10 with XmlTag

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

the class TagTester method createTags.

public static List<TagTester> createTags(String markup, Function<XmlTag, Boolean> accept, boolean stopAfterFirst) {
    List<TagTester> testers = new ArrayList<>();
    if ((Strings.isEmpty(markup) == false)) {
        try {
            // remove the CDATA and
            // the id attribute of the component because it is often the same as the element's id
            markup = AJAX_COMPONENT_CDATA_OPEN.matcher(markup).replaceAll("<component>");
            markup = AJAX_COMPONENT_CDATA_CLOSE.matcher(markup).replaceAll("</component>");
            XmlPullParser parser = new XmlPullParser();
            parser.parse(markup);
            XmlTag openTag = null;
            XmlTag closeTag = null;
            // temporary Tag-Hierarchy after openTag
            Stack<XmlTag> stack = new Stack<>();
            while (true) {
                XmlTag xmlTag = parser.nextTag();
                if (xmlTag == null) {
                    break;
                }
                if (openTag == null) {
                    if (accept.apply(xmlTag)) {
                        if (xmlTag.isOpen()) {
                            openTag = xmlTag;
                        } else if (xmlTag.isOpenClose()) {
                            openTag = xmlTag;
                            closeTag = xmlTag;
                        }
                    }
                } else {
                    if (xmlTag.isOpen() && !xmlTag.isOpenClose()) {
                        stack.push(xmlTag);
                    }
                    if (xmlTag.isClose()) {
                        XmlTag foundTag = findOpenTag(xmlTag, stack);
                        if (foundTag == null) {
                            if (xmlTag.getName().equals(openTag.getName())) {
                                closeTag = xmlTag;
                                closeTag.setOpenTag(openTag);
                            } else if (requiresCloseTag(openTag.getName()) == false) {
                                // no closeTag for current openTag (allowed)
                                closeTag = openTag;
                            } else {
                                // no closeTag for current openTag (invalid structure)
                                // thus reset state
                                openTag = null;
                                closeTag = null;
                            }
                        }
                    }
                }
                if ((openTag != null) && (closeTag != null)) {
                    TagTester tester = new TagTester(parser, openTag, closeTag);
                    testers.add(tester);
                    openTag = null;
                    closeTag = null;
                }
                if (stopAfterFirst && (closeTag != null)) {
                    break;
                }
            }
        } catch (Exception e) {
            throw new WicketRuntimeException(e);
        }
    }
    return testers;
}
Also used : WicketRuntimeException(org.apache.wicket.WicketRuntimeException) ArrayList(java.util.ArrayList) XmlPullParser(org.apache.wicket.markup.parser.XmlPullParser) WicketRuntimeException(org.apache.wicket.WicketRuntimeException) XmlTag(org.apache.wicket.markup.parser.XmlTag) Stack(java.util.Stack)

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