Search in sources :

Example 6 with SAXSource

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

the class XmlConverter method toSAXSourceFromStream.

@Converter
public SAXSource toSAXSourceFromStream(StreamSource source, Exchange exchange) throws SAXException {
    InputSource inputSource;
    if (source.getReader() != null) {
        inputSource = new InputSource(source.getReader());
    } else {
        inputSource = new InputSource(source.getInputStream());
    }
    inputSource.setSystemId(source.getSystemId());
    inputSource.setPublicId(source.getPublicId());
    XMLReader xmlReader = null;
    try {
        // use the SAXPaserFactory which is set from exchange
        if (exchange != null) {
            SAXParserFactory sfactory = exchange.getProperty(Exchange.SAXPARSER_FACTORY, SAXParserFactory.class);
            if (sfactory != null) {
                if (!sfactory.isNamespaceAware()) {
                    sfactory.setNamespaceAware(true);
                }
                xmlReader = sfactory.newSAXParser().getXMLReader();
            }
        }
        if (xmlReader == null) {
            if (xmlReaderPool == null) {
                xmlReaderPool = new XMLReaderPool(createSAXParserFactory());
            }
            xmlReader = xmlReaderPool.createXMLReader();
        }
    } catch (Exception ex) {
        LOG.warn("Cannot create the SAXParser XMLReader, due to {}", ex);
    }
    return new SAXSource(xmlReader, inputSource);
}
Also used : InputSource(org.xml.sax.InputSource) SAXSource(javax.xml.transform.sax.SAXSource) XMLReader(org.xml.sax.XMLReader) XMLStreamException(javax.xml.stream.XMLStreamException) FileNotFoundException(java.io.FileNotFoundException) SAXException(org.xml.sax.SAXException) TransformerException(javax.xml.transform.TransformerException) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXParserFactory(javax.xml.parsers.SAXParserFactory) Converter(org.apache.camel.Converter)

Example 7 with SAXSource

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

the class XmlConverter method toSAXSourceFromDOM.

@Converter
public SAXSource toSAXSourceFromDOM(DOMSource source, Exchange exchange) throws TransformerException {
    String str = toString(source, exchange);
    StringReader reader = new StringReader(str);
    return new SAXSource(new InputSource(reader));
}
Also used : InputSource(org.xml.sax.InputSource) SAXSource(javax.xml.transform.sax.SAXSource) StringReader(java.io.StringReader) Converter(org.apache.camel.Converter)

Example 8 with SAXSource

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

the class XmlConverterTest method testToSaxSourceByInputStream.

public void testToSaxSourceByInputStream() throws Exception {
    XmlConverter conv = new XmlConverter();
    InputStream is = context.getTypeConverter().convertTo(InputStream.class, "<foo>bar</foo>");
    SAXSource out = conv.toSAXSource(is, null);
    assertNotNull(out);
    assertEquals("<foo>bar</foo>", conv.toString(out, null));
}
Also used : SAXSource(javax.xml.transform.sax.SAXSource) InputStream(java.io.InputStream)

Example 9 with SAXSource

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

the class XmlConverterTest method testToDomSourceBySaxSource.

public void testToDomSourceBySaxSource() throws Exception {
    XmlConverter conv = new XmlConverter();
    SAXSource source = conv.toSAXSource("<foo>bar</foo>", null);
    DOMSource out = conv.toDOMSource(source);
    assertNotSame(source, out);
    assertEquals("<foo>bar</foo>", conv.toString(out, null));
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) SAXSource(javax.xml.transform.sax.SAXSource)

Example 10 with SAXSource

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

the class XmlConverterTest method testToSaxSourceBySaxSource.

public void testToSaxSourceBySaxSource() throws Exception {
    XmlConverter conv = new XmlConverter();
    SAXSource source = conv.toSAXSource("<foo>bar</foo>", null);
    SAXSource out = conv.toSAXSource(source, null);
    assertSame(source, out);
}
Also used : SAXSource(javax.xml.transform.sax.SAXSource)

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