Search in sources :

Example 1 with XmlPullParser

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

the class BaseWicketTester method getWicketAjaxBaseUrlEncodedInLastResponse.

/**
 * This method tries to parse the last response to return the encoded base URL and will throw an
 * exception if there none was encoded.
 *
 * @return Wicket-Ajax-BaseURL set on last response by {@link AbstractDefaultAjaxBehavior}
 * @throws IOException
 * @throws ResourceStreamNotFoundException
 * @throws ParseException
 */
public String getWicketAjaxBaseUrlEncodedInLastResponse() throws IOException, ResourceStreamNotFoundException, ParseException {
    XmlPullParser parser = new XmlPullParser();
    parser.parse(getLastResponseAsString());
    XmlTag tag;
    while ((tag = parser.nextTag()) != null) {
        if (tag.isOpen() && tag.getName().equals("script") && "wicket-ajax-base-url".equals(tag.getAttribute("id"))) {
            parser.next();
            return parser.getString().toString().split("\\\"")[1];
        }
    }
    fail("Last response has no AJAX base URL set by AbstractDefaultAjaxBehavior.");
    return null;
}
Also used : XmlPullParser(org.apache.wicket.markup.parser.XmlPullParser) XmlTag(org.apache.wicket.markup.parser.XmlTag)

Example 2 with XmlPullParser

use of org.apache.wicket.markup.parser.XmlPullParser 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 3 with XmlPullParser

use of org.apache.wicket.markup.parser.XmlPullParser 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)

Example 4 with XmlPullParser

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

the class DecoratingHeaderResponseTest method decoratedStringPrepend.

/**
 * Basic IHeaderResponseDecorator, just prepending the DECORATED string to resource name.
 *
 * @throws IOException
 * @throws ResourceStreamNotFoundException
 * @throws ParseException
 */
@Test
public void decoratedStringPrepend() throws IOException, ResourceStreamNotFoundException, ParseException {
    tester.getApplication().setHeaderResponseDecorator(new IHeaderResponseDecorator() {

        @Override
        public IHeaderResponse decorate(IHeaderResponse response) {
            return new ResourceAggregator(new DecoratingHeaderResponse(response) {

                @Override
                public void render(HeaderItem item) {
                    if (item instanceof JavaScriptReferenceHeaderItem) {
                        JavaScriptReferenceHeaderItem original = (JavaScriptReferenceHeaderItem) item;
                        item = JavaScriptHeaderItem.forReference(new PackageResourceReference("DECORATED-" + original.getReference().getName()), original.getId());
                    }
                    super.render(item);
                }
            });
        }
    });
    tester.startPage(TestPage.class);
    XmlPullParser parser = new XmlPullParser();
    parser.parse(tester.getLastResponseAsString());
    XmlTag tag = parser.nextTag();
    boolean isDecorated = false;
    do {
        if (tag.isOpen() && "script".equals(tag.getName())) {
            isDecorated = tag.getAttribute("src").toString().contains("DECORATED");
            if (!isDecorated) {
                fail();
            }
            break;
        }
    } while ((tag = parser.nextTag()) != null);
    assertTrue(isDecorated);
}
Also used : JavaScriptReferenceHeaderItem(org.apache.wicket.markup.head.JavaScriptReferenceHeaderItem) PackageResourceReference(org.apache.wicket.request.resource.PackageResourceReference) XmlPullParser(org.apache.wicket.markup.parser.XmlPullParser) IHeaderResponse(org.apache.wicket.markup.head.IHeaderResponse) ResourceAggregator(org.apache.wicket.markup.head.ResourceAggregator) HeaderItem(org.apache.wicket.markup.head.HeaderItem) JavaScriptHeaderItem(org.apache.wicket.markup.head.JavaScriptHeaderItem) JavaScriptReferenceHeaderItem(org.apache.wicket.markup.head.JavaScriptReferenceHeaderItem) XmlTag(org.apache.wicket.markup.parser.XmlTag) Test(org.junit.Test)

Aggregations

XmlPullParser (org.apache.wicket.markup.parser.XmlPullParser)4 XmlTag (org.apache.wicket.markup.parser.XmlTag)4 WicketRuntimeException (org.apache.wicket.WicketRuntimeException)2 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 Test (org.junit.Test)1