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));
}
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);
}
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));
}
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);
}
}
}
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();
}
Aggregations