use of org.w3c.dom.NodeList in project camel by apache.
the class CamelNamespaceHandler method injectNamespaces.
protected void injectNamespaces(Element element, Binder<Node> binder) {
NodeList list = element.getChildNodes();
Namespaces namespaces = null;
int size = list.getLength();
for (int i = 0; i < size; i++) {
Node child = list.item(i);
if (child instanceof Element) {
Element childElement = (Element) child;
Object object = binder.getJAXBNode(child);
if (object instanceof NamespaceAware) {
NamespaceAware namespaceAware = (NamespaceAware) object;
if (namespaces == null) {
namespaces = new Namespaces(element);
}
namespaces.configure(namespaceAware);
}
injectNamespaces(childElement, binder);
}
}
}
use of org.w3c.dom.NodeList 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.NodeList in project camel by apache.
the class BacklogTracerTest method testBacklogTracerEventMessageAsXml.
public void testBacklogTracerEventMessageAsXml() throws Exception {
MBeanServer mbeanServer = getMBeanServer();
ObjectName on = new ObjectName("org.apache.camel:context=camel-1,type=tracer,name=BacklogTracer");
assertNotNull(on);
mbeanServer.isRegistered(on);
Boolean enabled = (Boolean) mbeanServer.getAttribute(on, "Enabled");
assertEquals("Should not be enabled", Boolean.FALSE, enabled);
Integer size = (Integer) mbeanServer.getAttribute(on, "BacklogSize");
assertEquals("Should be 1000", 1000, size.intValue());
// enable it
mbeanServer.setAttribute(on, new Attribute("Enabled", Boolean.TRUE));
getMockEndpoint("mock:foo").expectedMessageCount(2);
getMockEndpoint("mock:bar").expectedMessageCount(2);
template.sendBody("direct:start", "Hello World");
template.sendBody("direct:start", "Bye World");
assertMockEndpointsSatisfied();
String events = (String) mbeanServer.invoke(on, "dumpTracedMessagesAsXml", new Object[] { "foo" }, new String[] { "java.lang.String" });
assertNotNull(events);
log.info(events);
// should be valid XML
Document dom = context.getTypeConverter().convertTo(Document.class, events);
assertNotNull(dom);
NodeList list = dom.getElementsByTagName("backlogTracerEventMessage");
assertEquals(2, list.getLength());
}
use of org.w3c.dom.NodeList in project camel by apache.
the class BacklogTracerTest method testBacklogTracerEventMessageDumpAllAsXml.
public void testBacklogTracerEventMessageDumpAllAsXml() throws Exception {
MBeanServer mbeanServer = getMBeanServer();
ObjectName on = new ObjectName("org.apache.camel:context=camel-1,type=tracer,name=BacklogTracer");
assertNotNull(on);
mbeanServer.isRegistered(on);
Boolean enabled = (Boolean) mbeanServer.getAttribute(on, "Enabled");
assertEquals("Should not be enabled", Boolean.FALSE, enabled);
// enable it
mbeanServer.setAttribute(on, new Attribute("Enabled", Boolean.TRUE));
getMockEndpoint("mock:foo").expectedMessageCount(2);
getMockEndpoint("mock:bar").expectedMessageCount(2);
template.sendBody("direct:start", "Hello World");
template.sendBody("direct:start", "Bye World");
assertMockEndpointsSatisfied();
String events = (String) mbeanServer.invoke(on, "dumpAllTracedMessagesAsXml", null, null);
assertNotNull(events);
log.info(events);
// should be valid XML
Document dom = context.getTypeConverter().convertTo(Document.class, events);
assertNotNull(dom);
NodeList list = dom.getElementsByTagName("backlogTracerEventMessage");
assertEquals(6, list.getLength());
}
use of org.w3c.dom.NodeList in project camel by apache.
the class TransformXpathTest method testTransformWithXpath.
public void testTransformWithXpath() throws Exception {
MockEndpoint mock = getMockEndpoint("mock:result");
mock.expectedMessageCount(1);
mock.message(0).body().isInstanceOf(NodeList.class);
String xml = context.getTypeConverter().convertTo(String.class, new File("src/test/resources/org/apache/camel/processor/students.xml"));
template.sendBody("direct:start", xml);
assertMockEndpointsSatisfied();
NodeList list = mock.getReceivedExchanges().get(0).getIn().getBody(NodeList.class);
assertEquals(2, list.getLength());
assertEquals("Claus", context.getTypeConverter().convertTo(String.class, list.item(0).getTextContent().trim()));
assertEquals("Hadrian", context.getTypeConverter().convertTo(String.class, list.item(1).getTextContent().trim()));
}
Aggregations