Search in sources :

Example 11 with StAXSource

use of javax.xml.transform.stax.StAXSource in project galley by Commonjava.

the class XMLInfrastructure method fallbackParseDocument.

private Document fallbackParseDocument(String xml, final Object docSource, final Exception e) throws GalleyMavenXMLException {
    logger.debug("Failed to parse: {}. DOM error: {}. Trying STaX parse with IS_REPLACING_ENTITY_REFERENCES == false...", e, docSource, e.getMessage());
    try {
        Source source;
        if (safeInputFactory != null) {
            xml = repairXmlDeclaration(xml);
            final XMLEventReader eventReader = safeInputFactory.createXMLEventReader(new StringReader(xml));
            source = new StAXSource(eventReader);
        } else {
            // Deal with ø and other undeclared entities...
            xml = escapeNonXMLEntityRefs(xml);
            final XMLReader reader = XMLReaderFactory.createXMLReader();
            reader.setFeature("http://xml.org/sax/features/validation", false);
            source = new SAXSource(reader, new InputSource(new StringReader(xml)));
        }
        final DOMResult result = new DOMResult();
        final Transformer transformer = newTransformer();
        transformer.transform(source, result);
        return (Document) result.getNode();
    } catch (final TransformerException e1) {
        throw new GalleyMavenXMLException("Failed to parse: %s. Transformer error: %s.\nOriginal DOM error: %s", e1, docSource, e1.getMessage(), e.getMessage());
    } catch (final SAXException e1) {
        throw new GalleyMavenXMLException("Failed to parse: %s. SAX error: %s.\nOriginal DOM error: %s", e1, docSource, e1.getMessage(), e.getMessage());
    } catch (final XMLStreamException e1) {
        throw new GalleyMavenXMLException("Failed to parse: %s. STaX error: %s.\nOriginal DOM error: %s", e1, docSource, e1.getMessage(), e.getMessage());
    }
}
Also used : InputSource(org.xml.sax.InputSource) DOMResult(javax.xml.transform.dom.DOMResult) Transformer(javax.xml.transform.Transformer) StAXSource(javax.xml.transform.stax.StAXSource) Document(org.w3c.dom.Document) DOMSource(javax.xml.transform.dom.DOMSource) Source(javax.xml.transform.Source) InputSource(org.xml.sax.InputSource) SAXSource(javax.xml.transform.sax.SAXSource) StAXSource(javax.xml.transform.stax.StAXSource) SAXException(org.xml.sax.SAXException) SAXSource(javax.xml.transform.sax.SAXSource) XMLStreamException(javax.xml.stream.XMLStreamException) StringReader(java.io.StringReader) XMLEventReader(javax.xml.stream.XMLEventReader) XMLReader(org.xml.sax.XMLReader) TransformerException(javax.xml.transform.TransformerException)

Example 12 with StAXSource

use of javax.xml.transform.stax.StAXSource in project webservices-axiom by apache.

the class XSLTImplementation method supportsStAXSource.

public final synchronized boolean supportsStAXSource() {
    if (supportsStAXSource == null) {
        try {
            StAXSource source = new StAXSource(XMLInputFactory.newInstance().createXMLStreamReader(new StringReader("<root/>")));
            StreamResult result = new StreamResult(new ByteArrayOutputStream());
            newTransformerFactory().newTransformer().transform(source, result);
            supportsStAXSource = true;
        } catch (Exception ex) {
            supportsStAXSource = false;
        }
    }
    return supportsStAXSource;
}
Also used : StreamResult(javax.xml.transform.stream.StreamResult) StringReader(java.io.StringReader) StAXSource(javax.xml.transform.stax.StAXSource) ByteArrayOutputStream(java.io.ByteArrayOutputStream) TransformerException(javax.xml.transform.TransformerException)

Example 13 with StAXSource

use of javax.xml.transform.stax.StAXSource in project webservices-axiom by apache.

the class BuilderSpec method from.

static BuilderSpec from(StAXParserConfiguration configuration, Source source) {
    if (source instanceof SAXSource) {
        return from((SAXSource) source, true);
    } else if (source instanceof DOMSource) {
        return from(((DOMSource) source).getNode(), true);
    } else if (source instanceof StreamSource) {
        StreamSource streamSource = (StreamSource) source;
        InputSource is = new InputSource();
        is.setByteStream(streamSource.getInputStream());
        is.setCharacterStream(streamSource.getReader());
        is.setPublicId(streamSource.getPublicId());
        is.setSystemId(streamSource.getSystemId());
        return from(configuration, is);
    } else if (source instanceof StAXSource) {
        return from(((StAXSource) source).getXMLStreamReader());
    } else {
        try {
            return new BuilderSpec(new FilteredXmlInput(new StAXPullInput(StAXUtils.getXMLInputFactory().createXMLStreamReader(source), true, null), NamespaceRepairingFilter.DEFAULT), null);
        } catch (XMLStreamException ex) {
            throw new OMException(ex);
        }
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) InputSource(org.xml.sax.InputSource) SAXSource(javax.xml.transform.sax.SAXSource) XMLStreamException(javax.xml.stream.XMLStreamException) StAXPullInput(org.apache.axiom.om.impl.stream.stax.pull.StAXPullInput) StreamSource(javax.xml.transform.stream.StreamSource) StAXSource(javax.xml.transform.stax.StAXSource) FilteredXmlInput(org.apache.axiom.core.stream.FilteredXmlInput) OMException(org.apache.axiom.om.OMException)

Example 14 with StAXSource

use of javax.xml.transform.stax.StAXSource in project webservices-axiom by apache.

the class StAXPivotTransformerTest method runTest.

@Override
protected void runTest() throws Throwable {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);
    factory.setExpandEntityReferences(false);
    factory.setCoalescing(true);
    factory.setIgnoringComments(true);
    Document document = factory.newDocumentBuilder().parse(sample.getUrl().toString());
    StAXPivot pivot = new StAXPivot(null);
    pivot.setReader(new DOMInput(document, false).createReader(pivot));
    StringWriter sw = new StringWriter();
    xsltImplementation.newTransformerFactory().newTransformer().transform(new StAXSource(pivot), new StreamResult(sw));
    assertAbout(xml()).that(sw.toString()).hasSameContentAs(document);
}
Also used : DOMInput(org.apache.axiom.core.stream.dom.DOMInput) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) StringWriter(java.io.StringWriter) StreamResult(javax.xml.transform.stream.StreamResult) StAXSource(javax.xml.transform.stax.StAXSource) Document(org.w3c.dom.Document)

Example 15 with StAXSource

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

the class StaxUtils method copy.

public static void copy(Source source, XMLStreamWriter writer) throws XMLStreamException {
    if (source instanceof StaxSource) {
        StaxSource ss = (StaxSource) source;
        if (ss.getXMLStreamReader() == null) {
            return;
        }
    } else if (source instanceof StAXSource) {
        StAXSource ss = (StAXSource) source;
        if (ss.getXMLStreamReader() == null) {
            return;
        }
    } else if (source instanceof SAXSource) {
        SAXSource ss = (SAXSource) source;
        InputSource src = ss.getInputSource();
        if (src == null || (src.getSystemId() == null && src.getPublicId() == null)) {
            if (ss.getXMLReader() != null) {
                // OK - reader is OK.  We'll use that out
                StreamWriterContentHandler ch = new StreamWriterContentHandler(writer);
                XMLReader reader = ((SAXSource) source).getXMLReader();
                reader.setContentHandler(ch);
                try {
                    try {
                        reader.setFeature("http://xml.org/sax/features/namespaces", true);
                    } catch (Throwable t) {
                    // ignore
                    }
                    try {
                        reader.setProperty("http://xml.org/sax/properties/lexical-handler", ch);
                    } catch (Throwable t) {
                    // ignore
                    }
                    reader.parse(((SAXSource) source).getInputSource());
                    return;
                } catch (Exception e) {
                    throw new XMLStreamException(e.getMessage(), e);
                }
            } else if (ss.getInputSource() == null) {
                // nothing to copy, just return
                return;
            }
        }
    } else if (source instanceof StreamSource) {
        StreamSource ss = (StreamSource) source;
        if (ss.getInputStream() == null && ss.getReader() == null && ss.getSystemId() == null) {
            // nothing to copy, just return
            return;
        }
    }
    XMLStreamReader reader = createXMLStreamReader(source);
    copy(reader, writer);
    reader.close();
}
Also used : InputSource(org.xml.sax.InputSource) SAXSource(javax.xml.transform.sax.SAXSource) XMLStreamReader(javax.xml.stream.XMLStreamReader) XMLStreamException(javax.xml.stream.XMLStreamException) StreamSource(javax.xml.transform.stream.StreamSource) StAXSource(javax.xml.transform.stax.StAXSource) XMLReader(org.xml.sax.XMLReader) XMLStreamException(javax.xml.stream.XMLStreamException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

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