Search in sources :

Example 41 with SAXSource

use of javax.xml.transform.sax.SAXSource 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 42 with SAXSource

use of javax.xml.transform.sax.SAXSource in project tomee by apache.

the class JaxbOpenejb method unmarshal.

public static <T> T unmarshal(final Class<T> type, final InputStream in, final boolean filter) throws ParserConfigurationException, SAXException, JAXBException {
    final InputSource inputSource = new InputSource(in);
    final SAXParser parser = Saxs.namespaceAwareFactory().newSAXParser();
    final JAXBContext ctx = getContext(type);
    final Unmarshaller unmarshaller = ctx.createUnmarshaller();
    unmarshaller.setEventHandler(new ValidationEventHandler() {

        public boolean handleEvent(final ValidationEvent validationEvent) {
            System.out.println(validationEvent);
            return false;
        }
    });
    final SAXSource source;
    if (filter) {
        final NamespaceFilter xmlFilter = new NamespaceFilter(parser.getXMLReader());
        xmlFilter.setContentHandler(unmarshaller.getUnmarshallerHandler());
        source = new SAXSource(xmlFilter, inputSource);
    } else {
        source = new SAXSource(inputSource);
    }
    currentPublicId.set(new TreeSet<String>());
    try {
        return unmarshaller.unmarshal(source, type).getValue();
    } finally {
        currentPublicId.set(null);
    }
}
Also used : InputSource(org.xml.sax.InputSource) ValidationEventHandler(javax.xml.bind.ValidationEventHandler) SAXSource(javax.xml.transform.sax.SAXSource) ValidationEvent(javax.xml.bind.ValidationEvent) SAXParser(javax.xml.parsers.SAXParser) JAXBContext(javax.xml.bind.JAXBContext) Unmarshaller(javax.xml.bind.Unmarshaller)

Example 43 with SAXSource

use of javax.xml.transform.sax.SAXSource 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 44 with SAXSource

use of javax.xml.transform.sax.SAXSource in project webservices-axiom by apache.

the class SAXBuilderFactory method getBuilder.

@Override
public OMXMLParserWrapper getBuilder(OMMetaFactory metaFactory, InputSource inputSource) throws Exception {
    SAXParserFactory factory = implementation.newSAXParserFactory();
    factory.setNamespaceAware(true);
    factory.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
    SAXParser parser = factory.newSAXParser();
    SAXSource source = new SAXSource(new CoalescingXMLFilter(parser.getXMLReader()), inputSource);
    return OMXMLBuilderFactory.createOMBuilder(metaFactory.getOMFactory(), source, false);
}
Also used : SAXSource(javax.xml.transform.sax.SAXSource) SAXParser(javax.xml.parsers.SAXParser) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Example 45 with SAXSource

use of javax.xml.transform.sax.SAXSource 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)

Aggregations

SAXSource (javax.xml.transform.sax.SAXSource)111 InputSource (org.xml.sax.InputSource)81 XMLReader (org.xml.sax.XMLReader)38 Source (javax.xml.transform.Source)28 StreamSource (javax.xml.transform.stream.StreamSource)28 DOMSource (javax.xml.transform.dom.DOMSource)27 SAXException (org.xml.sax.SAXException)26 TransformerException (javax.xml.transform.TransformerException)24 SAXParserFactory (javax.xml.parsers.SAXParserFactory)20 Unmarshaller (javax.xml.bind.Unmarshaller)17 SAXParser (javax.xml.parsers.SAXParser)17 Transformer (javax.xml.transform.Transformer)17 StreamResult (javax.xml.transform.stream.StreamResult)16 Test (org.junit.Test)16 StringReader (java.io.StringReader)15 IOException (java.io.IOException)14 JAXBContext (javax.xml.bind.JAXBContext)14 ByteArrayInputStream (java.io.ByteArrayInputStream)12 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)11 ValidationEvent (javax.xml.bind.ValidationEvent)10