use of org.apache.camel.converter.jaxp.XmlConverter in project camel by apache.
the class DumpModelAsXmlTransformRouteConstantTest method testDumpModelAsXml.
public void testDumpModelAsXml() throws Exception {
String xml = ModelHelper.dumpModelAsXml(context, context.getRouteDefinition("myRoute"));
assertNotNull(xml);
log.info(xml);
Document doc = new XmlConverter().toDOMDocument(xml);
NodeList nodes = doc.getElementsByTagName("constant");
assertEquals(1, nodes.getLength());
Element node = (Element) nodes.item(0);
assertNotNull("Node <simple> expected to be instanceof Element", node);
assertEquals("Hello World", node.getTextContent());
nodes = doc.getElementsByTagName("to");
assertEquals(1, nodes.getLength());
node = (Element) nodes.item(0);
assertNotNull("Node <to> expected to be instanceof Element", node);
assertEquals("mock:result", node.getAttribute("uri"));
assertEquals("myMock", node.getAttribute("id"));
assertEquals("true", node.getAttribute("customId"));
}
use of org.apache.camel.converter.jaxp.XmlConverter in project camel by apache.
the class DumpModelAsXmlTransformRouteLanguageTest method testDumpModelAsXml.
public void testDumpModelAsXml() throws Exception {
String xml = ModelHelper.dumpModelAsXml(context, context.getRouteDefinition("myRoute"));
assertNotNull(xml);
log.info(xml);
Document doc = new XmlConverter().toDOMDocument(xml);
NodeList nodes = doc.getElementsByTagName("language");
assertEquals(1, nodes.getLength());
Element node = (Element) nodes.item(0);
assertNotNull("Node <simple> expected to be instanceof Element", node);
assertEquals("constant", node.getAttribute("language"));
assertEquals("Hello World", node.getTextContent());
nodes = doc.getElementsByTagName("to");
assertEquals(1, nodes.getLength());
node = (Element) nodes.item(0);
assertNotNull("Node <to> expected to be instanceof Element", node);
assertEquals("mock:result", node.getAttribute("uri"));
assertEquals("myMock", node.getAttribute("id"));
assertEquals("true", node.getAttribute("customId"));
}
use of org.apache.camel.converter.jaxp.XmlConverter in project camel by apache.
the class DumpModelAsXmlTransformRouteTest method testDumpModelAsXml.
public void testDumpModelAsXml() throws Exception {
String xml = ModelHelper.dumpModelAsXml(context, context.getRouteDefinition("myRoute"));
assertNotNull(xml);
log.info(xml);
Document doc = new XmlConverter().toDOMDocument(xml);
NodeList nodes = doc.getElementsByTagName("simple");
assertEquals(1, nodes.getLength());
Element node = (Element) nodes.item(0);
assertNotNull("Node <simple> expected to be instanceof Element", node);
assertEquals("Hello ${body}", node.getTextContent());
nodes = doc.getElementsByTagName("to");
assertEquals(1, nodes.getLength());
node = (Element) nodes.item(0);
assertNotNull("Node <to> expected to be instanceof Element", node);
assertEquals("mock:result", node.getAttribute("uri"));
assertEquals("myMock", node.getAttribute("id"));
assertEquals("true", node.getAttribute("customId"));
}
use of org.apache.camel.converter.jaxp.XmlConverter 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 org.apache.camel.converter.jaxp.XmlConverter in project camel by apache.
the class ModelHelper method loadRoutesDefinition.
/**
* Marshal the xml to the model definition
*
* @param context the CamelContext, if <tt>null</tt> then {@link org.apache.camel.spi.ModelJAXBContextFactory} is not in use
* @param inputStream the xml stream
* @throws Exception is thrown if an error is encountered unmarshalling from xml to model
*/
public static RoutesDefinition loadRoutesDefinition(CamelContext context, InputStream inputStream) throws Exception {
XmlConverter xmlConverter = newXmlConverter(context);
Document dom = xmlConverter.toDOMDocument(inputStream, null);
return loadRoutesDefinition(context, dom);
}
Aggregations