use of org.kxml2.kdom.Element in project android by JetBrains.
the class DomPullParserTest method compareParsers.
private static void compareParsers(PsiFile file, NextEventType nextEventType) throws Exception {
assertTrue(file instanceof XmlFile);
Document document = XmlUtils.parseDocumentSilently(file.getText(), true);
assertNotNull(document);
KXmlParser referenceParser = createReferenceParser(file);
DomPullParser parser = new DomPullParser(document.getDocumentElement());
assertEquals("Expected " + name(referenceParser.getEventType()) + " but was " + name(parser.getEventType()) + " (at line:column " + describePosition(referenceParser) + ")", referenceParser.getEventType(), parser.getEventType());
while (true) {
int expected, next;
switch(nextEventType) {
case NEXT:
expected = referenceParser.next();
next = parser.next();
break;
case NEXT_TOKEN:
expected = referenceParser.nextToken();
next = parser.nextToken();
break;
case NEXT_TAG:
{
try {
expected = referenceParser.nextTag();
} catch (Exception e) {
expected = referenceParser.getEventType();
}
try {
next = parser.nextTag();
} catch (Exception e) {
next = parser.getEventType();
}
break;
}
default:
throw new AssertionError("Unexpected type");
}
Element element = null;
if (expected == XmlPullParser.START_TAG) {
assertNotNull(parser.getViewCookie());
assertTrue(parser.getViewCookie() == null || parser.getViewCookie() instanceof Element);
element = (Element) parser.getViewCookie();
}
if (expected == XmlPullParser.START_TAG) {
assertEquals(referenceParser.getName(), parser.getName());
if (element != document.getDocumentElement()) {
// KXmlParser seems to not include xmlns: attributes on the root tag!
SortedSet<String> referenceAttributes = new TreeSet<>();
SortedSet<String> attributes = new TreeSet<>();
for (int i = 0; i < referenceParser.getAttributeCount(); i++) {
String s = referenceParser.getAttributePrefix(i) + ':' + referenceParser.getAttributeName(i) + '=' + referenceParser.getAttributeValue(i);
referenceAttributes.add(s);
}
for (int i = 0; i < parser.getAttributeCount(); i++) {
String s = parser.getAttributePrefix(i) + ':' + parser.getAttributeName(i) + '=' + parser.getAttributeValue(i);
attributes.add(s);
if (parser.getAttributeNamespace(i) != null) {
//noinspection ConstantConditions
assertEquals(normalizeValue(parser.getAttributeValue(i)), normalizeValue(parser.getAttributeValue(parser.getAttributeNamespace(i), parser.getAttributeName(i))));
}
}
assertEquals(referenceAttributes, attributes);
}
assertEquals(referenceParser.isEmptyElementTag(), parser.isEmptyElementTag());
if (element instanceof XmlTag) {
XmlTag tag = (XmlTag) element;
for (XmlAttribute attribute : tag.getAttributes()) {
String namespace = attribute.getNamespace();
String name = attribute.getLocalName();
assertEquals(namespace + ':' + name + " in element " + parser.getName(), normalizeValue(referenceParser.getAttributeValue(namespace, name)), normalizeValue(parser.getAttributeValue(namespace, name)));
}
}
} else if (expected == XmlPullParser.TEXT || expected == XmlPullParser.COMMENT) {
assertEquals(StringUtil.notNullize(referenceParser.getText()).trim(), StringUtil.notNullize(parser.getText()).trim());
}
if (expected != next) {
assertEquals("Expected " + name(expected) + " but was " + name(next) + "(At " + describePosition(referenceParser) + ")", expected, next);
}
if (expected == XmlPullParser.END_DOCUMENT) {
break;
}
}
}
use of org.kxml2.kdom.Element in project collect by opendatakit.
the class FileUtils method getChildElement.
// needed because element.getelement fails when there are attributes
private static Element getChildElement(Element parent, String childName) {
Element e = null;
int c = parent.getChildCount();
int i = 0;
for (i = 0; i < c; i++) {
if (parent.getType(i) == Node.ELEMENT) {
if (parent.getElement(i).getName().equalsIgnoreCase(childName)) {
return parent.getElement(i);
}
}
}
return e;
}
use of org.kxml2.kdom.Element in project felix by apache.
the class PullParser method parseProperty.
public PropertyImpl parseProperty(Reader r) throws Exception {
XmlPullParser reader = new KXmlParser();
reader.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
reader.setInput(r);
int event = reader.nextTag();
if (event != XmlPullParser.START_TAG || !P.equals(reader.getName())) {
throw new Exception("Expected element 'p'");
}
return parseProperty(reader);
}
use of org.kxml2.kdom.Element in project felix by apache.
the class PullParser method parseRepository.
public RepositoryImpl parseRepository(Reader r) throws Exception {
XmlPullParser reader = new KXmlParser();
reader.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
reader.setInput(r);
int event = reader.nextTag();
if (event != XmlPullParser.START_TAG || !REPOSITORY.equals(reader.getName())) {
throw new Exception("Expected element 'repository' at the root of the document");
}
return parse(reader);
}
use of org.kxml2.kdom.Element in project felix by apache.
the class PullParser method parseCapability.
public CapabilityImpl parseCapability(Reader r) throws Exception {
XmlPullParser reader = new KXmlParser();
reader.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
reader.setInput(r);
int event = reader.nextTag();
if (event != XmlPullParser.START_TAG || !CAPABILITY.equals(reader.getName())) {
throw new Exception("Expected element 'capability'");
}
return parseCapability(reader);
}
Aggregations