use of org.w3c.dom.Document 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.w3c.dom.Document 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.w3c.dom.Document 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.w3c.dom.Document in project camel by apache.
the class CamelNamespaceHandler method doBeforeParse.
/**
* Prepares the nodes before parsing.
*/
public static void doBeforeParse(Node node, String fromNamespace, String toNamespace) {
if (node.getNodeType() == Node.ELEMENT_NODE) {
Document doc = node.getOwnerDocument();
if (node.getNamespaceURI().equals(fromNamespace)) {
doc.renameNode(node, toNamespace, node.getLocalName());
}
// remove whitespace noise from uri, xxxUri attributes, eg new lines, and tabs etc, which allows end users to format
// their Camel routes in more human readable format, but at runtime those attributes must be trimmed
// the parser removes most of the noise, but keeps double spaces in the attribute values
NamedNodeMap map = node.getAttributes();
for (int i = 0; i < map.getLength(); i++) {
Node att = map.item(i);
if (att.getNodeName().equals("uri") || att.getNodeName().endsWith("Uri")) {
final String value = att.getNodeValue();
String before = ObjectHelper.before(value, "?");
String after = ObjectHelper.after(value, "?");
if (before != null && after != null) {
// remove all double spaces in the uri parameters
String changed = after.replaceAll("\\s{2,}", "");
if (!after.equals(changed)) {
String newAtr = before.trim() + "?" + changed.trim();
LOG.debug("Removed whitespace noise from attribute {} -> {}", value, newAtr);
att.setNodeValue(newAtr);
}
}
}
}
}
NodeList list = node.getChildNodes();
for (int i = 0; i < list.getLength(); ++i) {
doBeforeParse(list.item(i), fromNamespace, toNamespace);
}
}
use of org.w3c.dom.Document 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