Search in sources :

Example 31 with XMLStreamReader

use of javax.xml.stream.XMLStreamReader in project felix by apache.

the class StaxParser method parseRepository.

public RepositoryImpl parseRepository(InputStream is, URI baseUri) throws Exception {
    XMLStreamReader reader = getFactory().createXMLStreamReader(is);
    int event = reader.nextTag();
    if (event != XMLStreamConstants.START_ELEMENT || !REPOSITORY.equals(reader.getLocalName())) {
        throw new Exception("Expected element 'repository' at the root of the document");
    }
    return parseRepository(reader);
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) XMLStreamException(javax.xml.stream.XMLStreamException)

Example 32 with XMLStreamReader

use of javax.xml.stream.XMLStreamReader in project felix by apache.

the class StaxParser method parseProperty.

public PropertyImpl parseProperty(Reader r) throws Exception {
    XMLStreamReader reader = getFactory().createXMLStreamReader(r);
    int event = reader.nextTag();
    if (event != XMLStreamConstants.START_ELEMENT || !P.equals(reader.getLocalName())) {
        throw new Exception("Expected element 'p'");
    }
    return parseProperty(reader);
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) XMLStreamException(javax.xml.stream.XMLStreamException)

Example 33 with XMLStreamReader

use of javax.xml.stream.XMLStreamReader in project felix by apache.

the class StaxParser method parseResource.

public ResourceImpl parseResource(Reader r) throws Exception {
    XMLStreamReader reader = getFactory().createXMLStreamReader(r);
    int event = reader.nextTag();
    if (event != XMLStreamConstants.START_ELEMENT || !RESOURCE.equals(reader.getLocalName())) {
        throw new Exception("Expected element 'resource'");
    }
    return parseResource(reader);
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) XMLStreamException(javax.xml.stream.XMLStreamException)

Example 34 with XMLStreamReader

use of javax.xml.stream.XMLStreamReader in project cxf by apache.

the class ToolsStaxUtils method getTags.

public static List<Tag> getTags(final File source) throws Exception {
    List<Tag> tags = new ArrayList<>();
    List<String> ignoreEmptyTags = Arrays.asList(new String[] { "sequence" });
    try (InputStream is = new BufferedInputStream(Files.newInputStream(source.toPath()))) {
        XMLStreamReader reader = StaxUtils.createXMLStreamReader(is);
        Tag newTag = null;
        int count = 0;
        QName checkingPoint = null;
        Stack<Tag> stack = new Stack<Tag>();
        while (reader.hasNext()) {
            int event = reader.next();
            if (checkingPoint != null) {
                count++;
            }
            if (event == XMLStreamConstants.START_ELEMENT) {
                newTag = new Tag();
                newTag.setName(reader.getName());
                if (ignoreEmptyTags.contains(reader.getLocalName())) {
                    checkingPoint = reader.getName();
                }
                for (int i = 0; i < reader.getAttributeCount(); i++) {
                    newTag.getAttributes().put(reader.getAttributeName(i), reader.getAttributeValue(i));
                }
                stack.push(newTag);
            }
            if (event == XMLStreamConstants.CHARACTERS) {
                newTag.setText(reader.getText());
            }
            if (event == XMLStreamConstants.END_ELEMENT) {
                Tag startTag = stack.pop();
                if (checkingPoint != null && checkingPoint.equals(reader.getName())) {
                    if (count == 1) {
                    // Tag is empty, and it's in the ignore collection, so we just skip this tag
                    } else {
                        tags.add(startTag);
                    }
                    count = 0;
                    checkingPoint = null;
                } else {
                    tags.add(startTag);
                }
            }
        }
        reader.close();
    }
    return tags;
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) QName(javax.xml.namespace.QName) ArrayList(java.util.ArrayList) Tag(org.apache.cxf.tools.common.Tag) Stack(java.util.Stack)

Example 35 with XMLStreamReader

use of javax.xml.stream.XMLStreamReader in project cxf by apache.

the class ToolsStaxUtils method getTagTree.

public static Tag getTagTree(final InputStream is, final List<String> ignoreAttr, Map<QName, Set<String>> types) throws Exception {
    Tag root = new Tag();
    root.setName(new QName("root", "root"));
    XMLStreamReader reader = StaxUtils.createXMLStreamReader(is);
    Tag newTag = null;
    Tag currentTag = root;
    while (reader.hasNext()) {
        int event = reader.next();
        if (event == XMLStreamConstants.START_ELEMENT) {
            newTag = new Tag();
            newTag.setName(reader.getName());
            if (!ignoreAttr.isEmpty()) {
                newTag.getIgnoreAttr().addAll(ignoreAttr);
            }
            for (int i = 0; i < reader.getAttributeCount(); i++) {
                // probably a qname to a type, pull namespace in differently
                String tp = reader.getAttributeValue(i);
                if (isType(types, reader.getName(), reader.getAttributeName(i))) {
                    int idx = tp.indexOf(':');
                    if (idx > 0 && tp.length() > idx && tp.substring(idx + 1).indexOf(':') == -1) {
                        String pfx = tp.substring(0, idx);
                        String ns = reader.getNamespaceURI(pfx);
                        if (ns != null) {
                            tp = "{" + ns + "}" + tp.substring(idx + 1);
                        }
                    } else {
                        String ns = reader.getNamespaceURI("");
                        if (ns != null) {
                            tp = "{" + ns + "}" + tp.substring(idx + 1);
                        }
                    }
                }
                newTag.getAttributes().put(reader.getAttributeName(i), tp);
            }
            newTag.setParent(currentTag);
            currentTag.getTags().add(newTag);
            currentTag = newTag;
        }
        if (event == XMLStreamConstants.CHARACTERS) {
            newTag.setText(reader.getText());
        }
        if (event == XMLStreamConstants.END_ELEMENT) {
            currentTag = currentTag.getParent();
        }
    }
    reader.close();
    return root;
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) QName(javax.xml.namespace.QName) Tag(org.apache.cxf.tools.common.Tag)

Aggregations

XMLStreamReader (javax.xml.stream.XMLStreamReader)1074 Test (org.junit.Test)486 InputStream (java.io.InputStream)451 ByteArrayInputStream (java.io.ByteArrayInputStream)379 ByteArrayOutputStream (java.io.ByteArrayOutputStream)334 Document (org.w3c.dom.Document)311 XMLStreamException (javax.xml.stream.XMLStreamException)288 ArrayList (java.util.ArrayList)270 XMLSecurityProperties (org.apache.xml.security.stax.ext.XMLSecurityProperties)242 XMLInputFactory (javax.xml.stream.XMLInputFactory)211 QName (javax.xml.namespace.QName)208 DOMSource (javax.xml.transform.dom.DOMSource)206 StringReader (java.io.StringReader)196 SecretKey (javax.crypto.SecretKey)188 StreamResult (javax.xml.transform.stream.StreamResult)183 DocumentBuilder (javax.xml.parsers.DocumentBuilder)178 XMLStreamWriter (javax.xml.stream.XMLStreamWriter)160 InboundXMLSec (org.apache.xml.security.stax.ext.InboundXMLSec)155 IOException (java.io.IOException)144 Key (java.security.Key)103