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;
}
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;
}
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;
}
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);
}
Aggregations