Search in sources :

Example 16 with DOMSource

use of javax.xml.transform.dom.DOMSource in project camel by apache.

the class XmlConverterTest method testToDomSourceFromInputStream.

public void testToDomSourceFromInputStream() throws Exception {
    XmlConverter conv = new XmlConverter();
    InputStream is = context.getTypeConverter().convertTo(InputStream.class, "<foo>bar</foo>");
    DOMSource out = conv.toDOMSource(is);
    assertNotNull(out);
    assertEquals("<foo>bar</foo>", context.getTypeConverter().convertTo(String.class, out));
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) InputStream(java.io.InputStream)

Example 17 with DOMSource

use of javax.xml.transform.dom.DOMSource in project camel by apache.

the class XmlConverterTest method testToDomSourceByCustomSource.

public void testToDomSourceByCustomSource() throws Exception {
    XmlConverter conv = new XmlConverter();
    Source dummy = new Source() {

        public String getSystemId() {
            return null;
        }

        public void setSystemId(String s) {
        }
    };
    DOMSource out = conv.toDOMSource(dummy);
    assertNull(out);
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) DOMSource(javax.xml.transform.dom.DOMSource) InputSource(org.xml.sax.InputSource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) SAXSource(javax.xml.transform.sax.SAXSource) StAXSource(javax.xml.transform.stax.StAXSource) BytesSource(org.apache.camel.BytesSource)

Example 18 with DOMSource

use of javax.xml.transform.dom.DOMSource in project camel by apache.

the class XmlConverterTest method testToSaxSourceByDomSource.

public void testToSaxSourceByDomSource() throws Exception {
    XmlConverter conv = new XmlConverter();
    DOMSource source = conv.toDOMSource("<foo>bar</foo>");
    SAXSource out = conv.toSAXSource(source, null);
    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 19 with DOMSource

use of javax.xml.transform.dom.DOMSource in project camel by apache.

the class CacheBasedXPathReplacer method process.

public void process(Exchange exchange) throws Exception {
    String cacheKey = key.evaluate(exchange, String.class);
    if (isValid(cacheManager, cacheName, cacheKey)) {
        Ehcache cache = cacheManager.getCache(cacheName);
        if (LOG.isDebugEnabled()) {
            LOG.debug("Replacing XPath value {} in Message with value stored against key {} in CacheName {}", new Object[] { xpath, cacheKey, cacheName });
        }
        exchange.getIn().setHeader(CacheConstants.CACHE_KEY, cacheKey);
        Object body = exchange.getIn().getBody();
        InputStream is = exchange.getContext().getTypeConverter().convertTo(InputStream.class, body);
        Document document;
        try {
            document = exchange.getContext().getTypeConverter().convertTo(Document.class, exchange, is);
        } finally {
            IOHelper.close(is, "is", LOG);
        }
        InputStream cis = exchange.getContext().getTypeConverter().convertTo(InputStream.class, cache.get(cacheKey).getObjectValue());
        try {
            Document cacheValueDocument = exchange.getContext().getTypeConverter().convertTo(Document.class, exchange, cis);
            // Create/setup the Transformer
            XmlConverter xmlConverter = new XmlConverter();
            String xslString = IOConverter.toString(new File("./src/main/resources/xpathreplacer.xsl"), exchange);
            xslString = xslString.replace("##match_token##", xpath);
            Source xslSource = xmlConverter.toStreamSource(new StringReader(xslString));
            TransformerFactory transformerFactory = xmlConverter.createTransformerFactory();
            Transformer transformer = transformerFactory.newTransformer(xslSource);
            DOMSource source = xmlConverter.toDOMSource(document);
            DOMResult result = new DOMResult();
            transformer.setParameter("cacheValue", cacheValueDocument);
            transformer.transform(source, result);
            // DOMSource can be converted to byte[] by camel type converter mechanism
            DOMSource dom = new DOMSource(result.getNode());
            exchange.getIn().setBody(dom, byte[].class);
        } finally {
            IOHelper.close(cis, "cis", LOG);
        }
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) DOMResult(javax.xml.transform.dom.DOMResult) InputStream(java.io.InputStream) Document(org.w3c.dom.Document) DOMSource(javax.xml.transform.dom.DOMSource) Source(javax.xml.transform.Source) XmlConverter(org.apache.camel.converter.jaxp.XmlConverter) StringReader(java.io.StringReader) Ehcache(net.sf.ehcache.Ehcache) File(java.io.File)

Example 20 with DOMSource

use of javax.xml.transform.dom.DOMSource in project camel by apache.

the class XmlFixture method transform.

protected static Document transform(Document aDocument, String aResourcePath) throws Exception {
    TransformerFactory tf = TransformerFactory.newInstance();
    InputStream in = XmlFixture.class.getResourceAsStream(aResourcePath);
    Source src = new StreamSource(in);
    src.setSystemId(XmlFixture.class.getResource(aResourcePath).toExternalForm());
    Transformer t = tf.newTransformer(src);
    DOMResult result = new DOMResult();
    t.transform(new DOMSource(aDocument), result);
    return (Document) result.getNode();
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) DOMResult(javax.xml.transform.dom.DOMResult) InputStream(java.io.InputStream) StreamSource(javax.xml.transform.stream.StreamSource) Document(org.w3c.dom.Document) DOMSource(javax.xml.transform.dom.DOMSource) InputSource(org.xml.sax.InputSource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source)

Aggregations

DOMSource (javax.xml.transform.dom.DOMSource)392 StreamResult (javax.xml.transform.stream.StreamResult)231 Transformer (javax.xml.transform.Transformer)204 Document (org.w3c.dom.Document)161 TransformerFactory (javax.xml.transform.TransformerFactory)112 TransformerException (javax.xml.transform.TransformerException)107 DocumentBuilder (javax.xml.parsers.DocumentBuilder)102 StringWriter (java.io.StringWriter)97 IOException (java.io.IOException)93 Element (org.w3c.dom.Element)81 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)77 Source (javax.xml.transform.Source)67 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)62 SAXException (org.xml.sax.SAXException)56 File (java.io.File)55 InputSource (org.xml.sax.InputSource)50 StreamSource (javax.xml.transform.stream.StreamSource)47 Node (org.w3c.dom.Node)45 InputStream (java.io.InputStream)35 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)35