Search in sources :

Example 6 with InputSource

use of org.xml.sax.InputSource in project camel by apache.

the class XmlConverter method toDOMSource.

@Converter
public DOMSource toDOMSource(InputStream is, Exchange exchange) throws ParserConfigurationException, IOException, SAXException {
    InputSource source = new InputSource(is);
    String systemId = source.getSystemId();
    DocumentBuilder builder = getDocumentBuilderFactory(exchange).newDocumentBuilder();
    Document document = builder.parse(source);
    return new DOMSource(document, systemId);
}
Also used : InputSource(org.xml.sax.InputSource) DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Document(org.w3c.dom.Document) Converter(org.apache.camel.Converter)

Example 7 with InputSource

use of org.xml.sax.InputSource 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 8 with InputSource

use of org.xml.sax.InputSource 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 9 with InputSource

use of org.xml.sax.InputSource in project camel by apache.

the class XmlConverter method toDOMSourceFromStream.

@Converter
public DOMSource toDOMSourceFromStream(StreamSource source, Exchange exchange) throws ParserConfigurationException, IOException, SAXException {
    Document document;
    String systemId = source.getSystemId();
    DocumentBuilder builder = getDocumentBuilderFactory(exchange).newDocumentBuilder();
    Reader reader = source.getReader();
    if (reader != null) {
        document = builder.parse(new InputSource(reader));
    } else {
        InputStream inputStream = source.getInputStream();
        if (inputStream != null) {
            InputSource inputsource = new InputSource(inputStream);
            inputsource.setSystemId(systemId);
            document = builder.parse(inputsource);
        } else {
            throw new IOException("No input stream or reader available on StreamSource: " + source);
        }
    }
    return new DOMSource(document, systemId);
}
Also used : InputSource(org.xml.sax.InputSource) DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) XMLStreamReader(javax.xml.stream.XMLStreamReader) Reader(java.io.Reader) XMLReader(org.xml.sax.XMLReader) InputStreamReader(java.io.InputStreamReader) StringReader(java.io.StringReader) IOException(java.io.IOException) Document(org.w3c.dom.Document) Converter(org.apache.camel.Converter)

Example 10 with InputSource

use of org.xml.sax.InputSource in project camel by apache.

the class XmlConverterTest method testToInputSource.

public void testToInputSource() throws Exception {
    XmlConverter conv = new XmlConverter();
    InputStream is = context.getTypeConverter().convertTo(InputStream.class, "<foo>bar</foo>");
    InputSource out = conv.toInputSource(is, null);
    assertNotNull(out);
    assertNotNull(out.getByteStream());
}
Also used : InputSource(org.xml.sax.InputSource) InputStream(java.io.InputStream)

Aggregations

InputSource (org.xml.sax.InputSource)966 StringReader (java.io.StringReader)365 IOException (java.io.IOException)249 Document (org.w3c.dom.Document)243 DocumentBuilder (javax.xml.parsers.DocumentBuilder)237 SAXException (org.xml.sax.SAXException)230 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)190 XMLReader (org.xml.sax.XMLReader)166 Test (org.junit.Test)149 NodeList (org.w3c.dom.NodeList)131 InputStream (java.io.InputStream)126 Element (org.w3c.dom.Element)124 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)117 SAXParser (javax.xml.parsers.SAXParser)94 ByteArrayInputStream (java.io.ByteArrayInputStream)80 SAXSource (javax.xml.transform.sax.SAXSource)78 Node (org.w3c.dom.Node)75 SAXParserFactory (javax.xml.parsers.SAXParserFactory)74 File (java.io.File)64 FileInputStream (java.io.FileInputStream)55