Search in sources :

Example 1 with Element

use of org.kxml2.kdom.Element in project android by JetBrains.

the class LayoutPsiPullParserTest method compareParsers.

private void compareParsers(PsiFile file, NextEventType nextEventType) throws Exception {
    assertTrue(file instanceof XmlFile);
    XmlFile xmlFile = (XmlFile) file;
    KXmlParser referenceParser = createReferenceParser(file);
    LayoutPsiPullParser parser = LayoutPsiPullParser.create(xmlFile, new RenderLogger("test", myModule));
    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");
        }
        PsiElement element = null;
        if (expected == START_TAG) {
            assertNotNull(parser.getViewCookie());
            assertTrue(parser.getViewCookie() instanceof TagSnapshot);
            element = ((TagSnapshot) parser.getViewCookie()).tag;
        }
        if (expected == START_TAG) {
            assertEquals(referenceParser.getName(), parser.getName());
            if (element != xmlFile.getRootTag()) {
                // 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);
            }
            if (element instanceof XmlTag) {
                XmlTag tag = (XmlTag) element;
                for (XmlAttribute attribute : tag.getAttributes()) {
                    String namespace = attribute.getNamespace();
                    String name = attribute.getLocalName();
                    if (namespace.isEmpty()) {
                        String prefix = attribute.getNamespacePrefix();
                        if (!prefix.isEmpty()) {
                            name = prefix + ":" + prefix;
                        }
                    }
                    //noinspection ConstantConditions
                    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;
        }
    }
}
Also used : XmlAttribute(com.intellij.psi.xml.XmlAttribute) XmlFile(com.intellij.psi.xml.XmlFile) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) KXmlParser(org.kxml2.io.KXmlParser) TreeSet(java.util.TreeSet) PsiElement(com.intellij.psi.PsiElement) XmlTag(com.intellij.psi.xml.XmlTag)

Example 2 with Element

use of org.kxml2.kdom.Element in project felix by apache.

the class PullParser method parseRepository.

public RepositoryImpl parseRepository(InputStream is, URI baseUri) throws Exception {
    XmlPullParser reader = new KXmlParser();
    // The spec-based Repository XML uses namespaces, so switch this on...
    reader.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
    reader.setInput(is, null);
    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");
    }
    RepositoryImpl repo;
    if ("http://www.osgi.org/xmlns/repository/v1.0.0".equals(reader.getNamespace())) {
        // how its initiated.
        return SpecXMLPullParser.parse(reader, baseUri);
    } else {
        // We're parsing the old
        return parse(reader);
    }
}
Also used : KXmlParser(org.kxml2.io.KXmlParser) XmlPullParser(org.xmlpull.v1.XmlPullParser) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) IOException(java.io.IOException)

Example 3 with Element

use of org.kxml2.kdom.Element in project felix by apache.

the class PullParser method parseRequirement.

public RequirementImpl parseRequirement(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 || !REQUIRE.equals(reader.getName())) {
        throw new Exception("Expected element 'require'");
    }
    return parseRequire(reader);
}
Also used : KXmlParser(org.kxml2.io.KXmlParser) XmlPullParser(org.xmlpull.v1.XmlPullParser) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) IOException(java.io.IOException)

Example 4 with Element

use of org.kxml2.kdom.Element in project felix by apache.

the class PullParser method parseResource.

public ResourceImpl parseResource(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 || !RESOURCE.equals(reader.getName())) {
        throw new Exception("Expected element 'resource'");
    }
    return parseResource(reader);
}
Also used : KXmlParser(org.kxml2.io.KXmlParser) XmlPullParser(org.xmlpull.v1.XmlPullParser) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) IOException(java.io.IOException)

Example 5 with Element

use of org.kxml2.kdom.Element in project felix by apache.

the class ObrUpdate method parseObrXml.

/**
 * put the information from obr.xml into ressourceBundle object.
 */
private void parseObrXml() throws MojoExecutionException {
    try {
        InputStream is = new FileInputStream(new File(m_obrXml));
        try {
            KXmlParser kxp = new KXmlParser();
            kxp.setInput(is, null);
            // skip top level element
            kxp.nextTag();
            // go to first child element
            kxp.nextTag();
            parseObrXml(kxp);
        } finally {
            is.close();
        }
    } catch (Exception e) {
        throw new MojoExecutionException("Unable to parse obr xml: " + m_obrXml, e);
    }
}
Also used : KXmlParser(org.kxml2.io.KXmlParser) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) File(java.io.File) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException)

Aggregations

Element (org.kxml2.kdom.Element)50 TreeElement (org.javarosa.core.model.instance.TreeElement)25 AbstractTreeElement (org.javarosa.core.model.instance.AbstractTreeElement)23 ArrayList (java.util.ArrayList)21 IOException (java.io.IOException)17 IFormElement (org.javarosa.core.model.IFormElement)17 KXmlParser (org.kxml2.io.KXmlParser)12 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)12 File (java.io.File)8 Test (org.junit.Test)7 ParsingException (org.opendatakit.briefcase.model.ParsingException)7 XmlPullParser (org.xmlpull.v1.XmlPullParser)7 Document (org.kxml2.kdom.Document)6 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 FileInputStream (java.io.FileInputStream)3 FileNotFoundException (java.io.FileNotFoundException)3 OutputStreamWriter (java.io.OutputStreamWriter)3 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)3 HashMap (java.util.HashMap)3 KXmlSerializer (org.kxml2.io.KXmlSerializer)3