camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject camel git commit: CAMEL-8309: Camel XML DSL - Allow to specify uri attributes in multi lines to make long urs easier to read and maintain
Date Mon, 02 Feb 2015 20:53:50 GMT
Repository: camel
Updated Branches:
  refs/heads/master 75bb8f88a -> ea258fb3f


CAMEL-8309: Camel XML DSL - Allow to specify uri attributes in multi lines to make long urs
easier to read and maintain


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/ea258fb3
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/ea258fb3
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/ea258fb3

Branch: refs/heads/master
Commit: ea258fb3fb34620bedc29fdc5a84483b008f1fe1
Parents: 75bb8f8
Author: Claus Ibsen <davsclaus@apache.org>
Authored: Mon Feb 2 21:53:50 2015 +0100
Committer: Claus Ibsen <davsclaus@apache.org>
Committed: Mon Feb 2 21:53:50 2015 +0100

----------------------------------------------------------------------
 .../handler/CamelNamespaceHandler.java          | 31 ++++++++++++--
 .../camel/blueprint/BlueprintJaxbRestTest.java  |  2 +-
 .../camel/blueprint/BlueprintJaxbTest.java      |  2 +-
 .../spring/handler/CamelNamespaceHandler.java   | 45 +++++++++++++++-----
 .../properties/SpringAttributeNewLineTest.java  | 43 +++++++++++++++++++
 .../properties/SpringAttributeNewLineTest.xml   | 42 ++++++++++++++++++
 .../test/blueprint/AttributeNewLineTest.java    | 37 ++++++++++++++++
 .../test/blueprint/AttributeNewLineTest.xml     | 40 +++++++++++++++++
 8 files changed, 225 insertions(+), 17 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/ea258fb3/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/handler/CamelNamespaceHandler.java
----------------------------------------------------------------------
diff --git a/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/handler/CamelNamespaceHandler.java
b/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/handler/CamelNamespaceHandler.java
index 58649c9..c2e55fc 100644
--- a/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/handler/CamelNamespaceHandler.java
+++ b/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/handler/CamelNamespaceHandler.java
@@ -35,6 +35,7 @@ import javax.xml.bind.JAXBException;
 
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
+import org.w3c.dom.NamedNodeMap;
 import org.w3c.dom.Node;
 import org.w3c.dom.NodeList;
 
@@ -131,16 +132,38 @@ public class CamelNamespaceHandler implements NamespaceHandler {
 
     private JAXBContext jaxbContext;
 
-    public static void renameNamespaceRecursive(Node node, String fromNamespace, String toNamespace)
{
+    /**
+     * 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 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 ("uri".equals(att.getNodeName()) || "url".equals(att.getNodeName())) {
+
+                    String value = att.getNodeValue();
+                    // remove all double spaces
+                    String changed = value.replaceAll("\\s{2,}", "");
+
+                    if (!value.equals(changed)) {
+                        LOG.debug("Removing whitespace noise from attribute {} -> {}",
value, changed);
+                        att.setNodeValue(changed);
+                    }
+                }
+            }
         }
         NodeList list = node.getChildNodes();
         for (int i = 0; i < list.getLength(); ++i) {
-            renameNamespaceRecursive(list.item(i), fromNamespace, toNamespace);
+            doBeforeParse(list.item(i), fromNamespace, toNamespace);
         }
     }
 
@@ -158,7 +181,7 @@ public class CamelNamespaceHandler implements NamespaceHandler {
 
         try {
             // as the camel-core model namespace is Spring we need to rename from blueprint
to spring
-            renameNamespaceRecursive(element, BLUEPRINT_NS, SPRING_NS);
+            doBeforeParse(element, BLUEPRINT_NS, SPRING_NS);
 
             if (element.getLocalName().equals(CAMEL_CONTEXT)) {
                 return parseCamelContextNode(element, context);
@@ -180,7 +203,7 @@ public class CamelNamespaceHandler implements NamespaceHandler {
             }
         } finally {
             // make sure to rename back so we leave the DOM as-is
-            renameNamespaceRecursive(element, SPRING_NS, BLUEPRINT_NS);
+            doBeforeParse(element, SPRING_NS, BLUEPRINT_NS);
         }
 
         return null;

http://git-wip-us.apache.org/repos/asf/camel/blob/ea258fb3/components/camel-blueprint/src/test/java/org/apache/camel/blueprint/BlueprintJaxbRestTest.java
----------------------------------------------------------------------
diff --git a/components/camel-blueprint/src/test/java/org/apache/camel/blueprint/BlueprintJaxbRestTest.java
b/components/camel-blueprint/src/test/java/org/apache/camel/blueprint/BlueprintJaxbRestTest.java
index bc38614..d303cfa 100644
--- a/components/camel-blueprint/src/test/java/org/apache/camel/blueprint/BlueprintJaxbRestTest.java
+++ b/components/camel-blueprint/src/test/java/org/apache/camel/blueprint/BlueprintJaxbRestTest.java
@@ -52,7 +52,7 @@ public class BlueprintJaxbRestTest extends TestSupport {
                 break;
             }
         }
-        CamelNamespaceHandler.renameNamespaceRecursive(elem, CamelNamespaceHandler.BLUEPRINT_NS,
CamelNamespaceHandler.SPRING_NS);
+        CamelNamespaceHandler.doBeforeParse(elem, CamelNamespaceHandler.BLUEPRINT_NS, CamelNamespaceHandler.SPRING_NS);
 
         JAXBContext context = JAXBContext.newInstance("org.apache.camel.blueprint:"
                                                         + "org.apache.camel:org.apache.camel.model:"

http://git-wip-us.apache.org/repos/asf/camel/blob/ea258fb3/components/camel-blueprint/src/test/java/org/apache/camel/blueprint/BlueprintJaxbTest.java
----------------------------------------------------------------------
diff --git a/components/camel-blueprint/src/test/java/org/apache/camel/blueprint/BlueprintJaxbTest.java
b/components/camel-blueprint/src/test/java/org/apache/camel/blueprint/BlueprintJaxbTest.java
index aabf472..d79fc96 100644
--- a/components/camel-blueprint/src/test/java/org/apache/camel/blueprint/BlueprintJaxbTest.java
+++ b/components/camel-blueprint/src/test/java/org/apache/camel/blueprint/BlueprintJaxbTest.java
@@ -52,7 +52,7 @@ public class BlueprintJaxbTest extends TestSupport {
                 break;
             }
         }
-        CamelNamespaceHandler.renameNamespaceRecursive(elem, CamelNamespaceHandler.BLUEPRINT_NS,
CamelNamespaceHandler.SPRING_NS);
+        CamelNamespaceHandler.doBeforeParse(elem, CamelNamespaceHandler.BLUEPRINT_NS, CamelNamespaceHandler.SPRING_NS);
 
         JAXBContext context = JAXBContext.newInstance("org.apache.camel.blueprint:"
                                                         + "org.apache.camel:org.apache.camel.model:"

http://git-wip-us.apache.org/repos/asf/camel/blob/ea258fb3/components/camel-spring/src/main/java/org/apache/camel/spring/handler/CamelNamespaceHandler.java
----------------------------------------------------------------------
diff --git a/components/camel-spring/src/main/java/org/apache/camel/spring/handler/CamelNamespaceHandler.java
b/components/camel-spring/src/main/java/org/apache/camel/spring/handler/CamelNamespaceHandler.java
index 87b1f4c..ebb3893 100644
--- a/components/camel-spring/src/main/java/org/apache/camel/spring/handler/CamelNamespaceHandler.java
+++ b/components/camel-spring/src/main/java/org/apache/camel/spring/handler/CamelNamespaceHandler.java
@@ -21,16 +21,10 @@ import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Map;
 import java.util.Set;
-
 import javax.xml.bind.Binder;
 import javax.xml.bind.JAXBContext;
 import javax.xml.bind.JAXBException;
 
-import org.w3c.dom.Document;
-import org.w3c.dom.Element;
-import org.w3c.dom.Node;
-import org.w3c.dom.NodeList;
-
 import org.apache.camel.builder.xml.Namespaces;
 import org.apache.camel.core.xml.CamelJMXAgentDefinition;
 import org.apache.camel.core.xml.CamelPropertyPlaceholderDefinition;
@@ -66,6 +60,11 @@ import org.springframework.beans.factory.parsing.BeanComponentDefinition;
 import org.springframework.beans.factory.support.BeanDefinitionBuilder;
 import org.springframework.beans.factory.xml.NamespaceHandlerSupport;
 import org.springframework.beans.factory.xml.ParserContext;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.NamedNodeMap;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
 
 /**
  * Camel namespace for the spring XML configuration file.
@@ -81,16 +80,40 @@ public class CamelNamespaceHandler extends NamespaceHandlerSupport {
     private JAXBContext jaxbContext;
     private Map<String, BeanDefinition> autoRegisterMap = new HashMap<String, BeanDefinition>();
 
-    public static void renameNamespaceRecursive(Node node) {
+    /**
+     * Prepares the nodes before parsing.
+     */
+    public static void doBeforeParse(Node node) {
         if (node.getNodeType() == Node.ELEMENT_NODE) {
+
+            // ensure namespace with versions etc is renamed to be same namespace so we can
parse using this handler
             Document doc = node.getOwnerDocument();
             if (node.getNamespaceURI().startsWith(SPRING_NS + "/v")) {
                 doc.renameNode(node, SPRING_NS, node.getNodeName());
             }
+
+            // remove whitespace noise from uri 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 ("uri".equals(att.getNodeName()) || "url".equals(att.getNodeName())) {
+
+                    String value = att.getNodeValue();
+                    // remove all double spaces
+                    String changed = value.replaceAll("\\s{2,}", "");
+
+                    if (!value.equals(changed)) {
+                        LOG.debug("Removing whitespace noise from attribute {} -> {}",
value, changed);
+                        att.setNodeValue(changed);
+                    }
+                }
+            }
         }
         NodeList list = node.getChildNodes();
         for (int i = 0; i < list.getLength(); ++i) {
-            renameNamespaceRecursive(list.item(i));
+            doBeforeParse(list.item(i));
         }
     }
 
@@ -257,7 +280,7 @@ public class CamelNamespaceHandler extends NamespaceHandlerSupport {
 
         @Override
         protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder
builder) {
-            renameNamespaceRecursive(element);
+            doBeforeParse(element);
             super.doParse(element, parserContext, builder);
 
             // now lets parse the routes with JAXB
@@ -287,7 +310,7 @@ public class CamelNamespaceHandler extends NamespaceHandlerSupport {
 
         @Override
         protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder
builder) {
-            renameNamespaceRecursive(element);
+            doBeforeParse(element);
             super.doParse(element, parserContext, builder);
 
             // now lets parse the routes with JAXB
@@ -317,7 +340,7 @@ public class CamelNamespaceHandler extends NamespaceHandlerSupport {
 
         @Override
         protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder
builder) {
-            renameNamespaceRecursive(element);
+            doBeforeParse(element);
             super.doParse(element, parserContext, builder);
 
             String contextId = element.getAttribute("id");

http://git-wip-us.apache.org/repos/asf/camel/blob/ea258fb3/components/camel-spring/src/test/java/org/apache/camel/component/properties/SpringAttributeNewLineTest.java
----------------------------------------------------------------------
diff --git a/components/camel-spring/src/test/java/org/apache/camel/component/properties/SpringAttributeNewLineTest.java
b/components/camel-spring/src/test/java/org/apache/camel/component/properties/SpringAttributeNewLineTest.java
new file mode 100644
index 0000000..96aa640
--- /dev/null
+++ b/components/camel-spring/src/test/java/org/apache/camel/component/properties/SpringAttributeNewLineTest.java
@@ -0,0 +1,43 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.properties;
+
+import org.apache.camel.spring.SpringTestSupport;
+import org.junit.Test;
+import org.springframework.context.support.AbstractXmlApplicationContext;
+import org.springframework.context.support.ClassPathXmlApplicationContext;
+
+/**
+ * @version 
+ */
+public class SpringAttributeNewLineTest extends SpringTestSupport {
+
+    @Override
+    protected AbstractXmlApplicationContext createApplicationContext() {
+        return new ClassPathXmlApplicationContext("org/apache/camel/component/properties/SpringAttributeNewLineTest.xml");
+    }
+
+    @Test
+    public void testSpringAttributeNewLineTest() throws Exception {
+        getMockEndpoint("mock:bar").expectedMessageCount(1);
+
+        template.sendBody("direct:start", "Hello World");
+
+        assertMockEndpointsSatisfied();
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/ea258fb3/components/camel-spring/src/test/resources/org/apache/camel/component/properties/SpringAttributeNewLineTest.xml
----------------------------------------------------------------------
diff --git a/components/camel-spring/src/test/resources/org/apache/camel/component/properties/SpringAttributeNewLineTest.xml
b/components/camel-spring/src/test/resources/org/apache/camel/component/properties/SpringAttributeNewLineTest.xml
new file mode 100644
index 0000000..ca56aa2
--- /dev/null
+++ b/components/camel-spring/src/test/resources/org/apache/camel/component/properties/SpringAttributeNewLineTest.xml
@@ -0,0 +1,42 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+    Licensed to the Apache Software Foundation (ASF) under one or more
+    contributor license agreements.  See the NOTICE file distributed with
+    this work for additional information regarding copyright ownership.
+    The ASF licenses this file to You under the Apache License, Version 2.0
+    (the "License"); you may not use this file except in compliance with
+    the License.  You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+    Unless required by applicable law or agreed to in writing, software
+    distributed under the License is distributed on an "AS IS" BASIS,
+    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+    See the License for the specific language governing permissions and
+    limitations under the License.
+-->
+<beans xmlns="http://www.springframework.org/schema/beans"
+       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+       xsi:schemaLocation="
+       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
+       http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
+    ">
+
+    <camelContext xmlns="http://camel.apache.org/schema/spring">
+
+        <route>
+            <from uri="direct:start"/>
+            <to uri="seda:bar?timeout=10000
+              &amp;blockWhenFull=true"/>
+        </route>
+
+        <route>
+            <from uri="seda:bar?
+              timeout=10000&amp;
+              blockWhenFull=true"/>
+            <to uri="mock:bar"/>
+        </route>
+
+    </camelContext>
+
+</beans>

http://git-wip-us.apache.org/repos/asf/camel/blob/ea258fb3/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/AttributeNewLineTest.java
----------------------------------------------------------------------
diff --git a/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/AttributeNewLineTest.java
b/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/AttributeNewLineTest.java
new file mode 100644
index 0000000..9e74889
--- /dev/null
+++ b/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/AttributeNewLineTest.java
@@ -0,0 +1,37 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.test.blueprint;
+
+import org.junit.Test;
+
+public class AttributeNewLineTest extends CamelBlueprintTestSupport {
+
+    @Override
+    protected String getBlueprintDescriptor() {
+        return "org/apache/camel/test/blueprint/AttributeNewLineTest.xml";
+    }
+
+    @Test
+    public void testAttributeNewLineTest() throws Exception {
+        getMockEndpoint("mock:bar").expectedMessageCount(1);
+
+        template.sendBody("direct:start", "Hello World");
+
+        assertMockEndpointsSatisfied();
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/ea258fb3/components/camel-test-blueprint/src/test/resources/org/apache/camel/test/blueprint/AttributeNewLineTest.xml
----------------------------------------------------------------------
diff --git a/components/camel-test-blueprint/src/test/resources/org/apache/camel/test/blueprint/AttributeNewLineTest.xml
b/components/camel-test-blueprint/src/test/resources/org/apache/camel/test/blueprint/AttributeNewLineTest.xml
new file mode 100644
index 0000000..10dae5a
--- /dev/null
+++ b/components/camel-test-blueprint/src/test/resources/org/apache/camel/test/blueprint/AttributeNewLineTest.xml
@@ -0,0 +1,40 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+    Licensed to the Apache Software Foundation (ASF) under one or more
+    contributor license agreements.  See the NOTICE file distributed with
+    this work for additional information regarding copyright ownership.
+    The ASF licenses this file to You under the Apache License, Version 2.0
+    (the "License"); you may not use this file except in compliance with
+    the License.  You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+    Unless required by applicable law or agreed to in writing, software
+    distributed under the License is distributed on an "AS IS" BASIS,
+    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+    See the License for the specific language governing permissions and
+    limitations under the License.
+-->
+<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
+           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+           xsi:schemaLocation="
+             http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd">
+
+  <camelContext xmlns="http://camel.apache.org/schema/blueprint">
+
+    <route>
+      <from uri="direct:start"/>
+      <to uri="seda:bar?timeout=10000
+              &amp;blockWhenFull=true"/>
+    </route>
+
+    <route>
+      <from uri="seda:bar?
+              timeout=10000&amp;
+              blockWhenFull=true"/>
+      <to uri="mock:bar"/>
+    </route>
+
+  </camelContext>
+
+</blueprint>
\ No newline at end of file


Mime
View raw message