Search in sources :

Example 71 with XMLReader

use of org.xml.sax.XMLReader 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 72 with XMLReader

use of org.xml.sax.XMLReader in project tika by apache.

the class ParseContext method getXMLReader.

/**
     * Returns the XMLReader specified in this parsing context. If a reader
     * is not explicitly specified, then one is created using the specified
     * or the default SAX parser.
     *
     * @see #getSAXParser()
     * @since Apache Tika 1.13
     * @return XMLReader
     * @throws TikaException
     */
public XMLReader getXMLReader() throws TikaException {
    XMLReader reader = get(XMLReader.class);
    if (reader != null) {
        return reader;
    }
    try {
        reader = getSAXParser().getXMLReader();
    } catch (SAXException e) {
        throw new TikaException("Unable to create an XMLReader", e);
    }
    reader.setEntityResolver(IGNORING_SAX_ENTITY_RESOLVER);
    return reader;
}
Also used : TikaException(org.apache.tika.exception.TikaException) XMLReader(org.xml.sax.XMLReader) SAXException(org.xml.sax.SAXException)

Example 73 with XMLReader

use of org.xml.sax.XMLReader in project webservices-axiom by apache.

the class SerializeFromSAXSource method serialize.

@Override
public XML serialize(OMContainer container) throws Exception {
    SAXSource source = container.getSAXSource(cache);
    XMLReader xmlReader = source.getXMLReader();
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    SAXSerializer serializer = new SAXSerializer();
    // A SAXSource has no way to tell its consumer about the encoding of the document.
    // Just set it to UTF-8 to have a well defined encoding.
    serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    serializer.setOutputStream(out);
    xmlReader.setContentHandler(serializer);
    xmlReader.setProperty("http://xml.org/sax/properties/lexical-handler", serializer);
    xmlReader.parse(source.getInputSource());
    return new XMLAsByteArray(out.toByteArray());
}
Also used : SAXSource(javax.xml.transform.sax.SAXSource) ByteArrayOutputStream(java.io.ByteArrayOutputStream) XMLReader(org.xml.sax.XMLReader)

Example 74 with XMLReader

use of org.xml.sax.XMLReader in project webservices-axiom by apache.

the class TestBase64StreamingWithGetSAXSource method runTest.

@Override
protected void runTest() throws Throwable {
    OMFactory factory = metaFactory.getOMFactory();
    OMElement elem = factory.createOMElement("test", null);
    // Create a data source that would eat up all memory when loaded. If the test
    // doesn't fail with an OutOfMemoryError, we know that the OMText implementation
    // supports streaming.
    DataSource ds = new RandomDataSource(654321L, Runtime.getRuntime().maxMemory());
    OMText text = factory.createOMText(new DataHandler(ds), false);
    elem.addChild(text);
    SAXSource saxSource = elem.getSAXSource(true);
    XMLReader xmlReader = saxSource.getXMLReader();
    xmlReader.setContentHandler(new Base64Comparator(ds.getInputStream()));
    xmlReader.parse(saxSource.getInputSource());
}
Also used : OMFactory(org.apache.axiom.om.OMFactory) RandomDataSource(org.apache.axiom.testutils.activation.RandomDataSource) SAXSource(javax.xml.transform.sax.SAXSource) OMText(org.apache.axiom.om.OMText) OMElement(org.apache.axiom.om.OMElement) DataHandler(javax.activation.DataHandler) XMLReader(org.xml.sax.XMLReader) RandomDataSource(org.apache.axiom.testutils.activation.RandomDataSource) DataSource(javax.activation.DataSource)

Example 75 with XMLReader

use of org.xml.sax.XMLReader in project webservices-axiom by apache.

the class SAXReader method proceed.

@Override
public boolean proceed() throws StreamException {
    XMLReader reader = source.getXMLReader();
    XmlHandlerContentHandler contentHandler = new XmlHandlerContentHandler(handler, expandEntityReferences);
    reader.setContentHandler(contentHandler);
    reader.setDTDHandler(contentHandler);
    try {
        reader.setProperty("http://xml.org/sax/properties/lexical-handler", contentHandler);
    } catch (SAXException ex) {
    // Ignore
    }
    try {
        reader.setProperty("http://xml.org/sax/properties/declaration-handler", contentHandler);
    } catch (SAXException ex) {
    // Ignore
    }
    try {
        reader.parse(source.getInputSource());
    } catch (IOException ex) {
        throw new StreamException(ex);
    } catch (SAXException ex) {
        throw new StreamException(ex);
    }
    return true;
}
Also used : IOException(java.io.IOException) XMLReader(org.xml.sax.XMLReader) SAXException(org.xml.sax.SAXException) StreamException(org.apache.axiom.core.stream.StreamException)

Aggregations

XMLReader (org.xml.sax.XMLReader)234 InputSource (org.xml.sax.InputSource)186 SAXException (org.xml.sax.SAXException)82 IOException (java.io.IOException)75 SAXParserFactory (javax.xml.parsers.SAXParserFactory)51 SAXSource (javax.xml.transform.sax.SAXSource)48 SAXParser (javax.xml.parsers.SAXParser)42 StringReader (java.io.StringReader)37 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)35 InputStream (java.io.InputStream)28 ExpatReader (org.apache.harmony.xml.ExpatReader)24 ContentHandler (org.xml.sax.ContentHandler)20 TransformerException (javax.xml.transform.TransformerException)19 DOMSource (javax.xml.transform.dom.DOMSource)18 StreamSource (javax.xml.transform.stream.StreamSource)17 ByteArrayInputStream (java.io.ByteArrayInputStream)16 FileReader (java.io.FileReader)16 InputStreamReader (java.io.InputStreamReader)12 SAXParseException (org.xml.sax.SAXParseException)11 ByteArrayOutputStream (java.io.ByteArrayOutputStream)10