Search in sources :

Example 66 with StAXSource

use of javax.xml.transform.stax.StAXSource in project tomee by apache.

the class StaxUtils method createXMLStreamReader.

public static XMLStreamReader createXMLStreamReader(Source source) {
    try {
        if (source instanceof DOMSource) {
            DOMSource ds = (DOMSource) source;
            Node nd = ds.getNode();
            Element el = null;
            if (nd instanceof Document) {
                el = ((Document) nd).getDocumentElement();
            } else if (nd instanceof Element) {
                el = (Element) nd;
            }
            if (null != el) {
                return new W3CDOMStreamReader(el, source.getSystemId());
            }
        } else if (source instanceof StAXSource) {
            return ((StAXSource) source).getXMLStreamReader();
        } else if (source instanceof StaxSource) {
            return ((StaxSource) source).getXMLStreamReader();
        } else if (source instanceof SAXSource) {
            SAXSource ss = (SAXSource) source;
            if (ss.getXMLReader() == null) {
                return createXMLStreamReader(((SAXSource) source).getInputSource());
            }
        }
        XMLInputFactory factory = getXMLInputFactory();
        try {
            XMLStreamReader reader = null;
            try {
                reader = factory.createXMLStreamReader(source);
            } catch (UnsupportedOperationException e) {
            // ignore
            }
            if (reader == null && source instanceof StreamSource) {
                // createXMLStreamReader from Source is optional, we'll try and map it
                StreamSource ss = (StreamSource) source;
                if (ss.getInputStream() != null) {
                    reader = factory.createXMLStreamReader(ss.getSystemId(), ss.getInputStream());
                } else {
                    reader = factory.createXMLStreamReader(ss.getSystemId(), ss.getReader());
                }
            }
            return reader;
        } finally {
            returnXMLInputFactory(factory);
        }
    } catch (XMLStreamException e) {
        throw new RuntimeException("Couldn't parse stream.", e);
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) XMLStreamReader(javax.xml.stream.XMLStreamReader) Node(org.w3c.dom.Node) StartElement(javax.xml.stream.events.StartElement) Element(org.w3c.dom.Element) StreamSource(javax.xml.transform.stream.StreamSource) StAXSource(javax.xml.transform.stax.StAXSource) Document(org.w3c.dom.Document) StartDocument(javax.xml.stream.events.StartDocument) SAXSource(javax.xml.transform.sax.SAXSource) XMLStreamException(javax.xml.stream.XMLStreamException) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Aggregations

StAXSource (javax.xml.transform.stax.StAXSource)66 XMLStreamReader (javax.xml.stream.XMLStreamReader)27 XMLStreamException (javax.xml.stream.XMLStreamException)24 StringReader (java.io.StringReader)19 XMLInputFactory (javax.xml.stream.XMLInputFactory)19 XMLEventReader (javax.xml.stream.XMLEventReader)16 StreamSource (javax.xml.transform.stream.StreamSource)15 DOMSource (javax.xml.transform.dom.DOMSource)14 Source (javax.xml.transform.Source)13 SAXSource (javax.xml.transform.sax.SAXSource)12 Test (org.junit.Test)12 StreamResult (javax.xml.transform.stream.StreamResult)10 InputStream (java.io.InputStream)9 TransformerException (javax.xml.transform.TransformerException)8 IOException (java.io.IOException)7 Transformer (javax.xml.transform.Transformer)7 Test (org.junit.jupiter.api.Test)7 Document (org.w3c.dom.Document)7 InputSource (org.xml.sax.InputSource)7 StringWriter (java.io.StringWriter)6