Repository: camel
Updated Branches:
  refs/heads/master cd54a9665 -> a724619a0


CAMEL-11296: camel-maven-plugin:validate - Allow to detect duplicate route ids


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

Branch: refs/heads/master
Commit: a0126b4c61b445ecb8600fe09a1afad350f3f162
Parents: cd54a96
Author: Claus Ibsen <davscl...@apache.org>
Authored: Sun May 21 09:36:19 2017 +0200
Committer: Claus Ibsen <davscl...@apache.org>
Committed: Sun May 21 09:36:19 2017 +0200

----------------------------------------------------------------------
 .../org/apache/camel/parser/XmlRouteParser.java |  43 ++++++
 .../camel/parser/helper/CamelXmlHelper.java     |  14 ++
 .../camel/parser/model/CamelRouteDetails.java   | 133 +++++++++++++++++++
 .../camel/parser/xml/DuplicateRouteIdsTest.java |  54 ++++++++
 .../camel/parser/xml/myduplicateroutes.xml      |  33 +++++
 5 files changed, 277 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/a0126b4c/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/XmlRouteParser.java
----------------------------------------------------------------------
diff --git 
a/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/XmlRouteParser.java
 
b/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/XmlRouteParser.java
index eb883d4..25ed77a 100644
--- 
a/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/XmlRouteParser.java
+++ 
b/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/XmlRouteParser.java
@@ -27,6 +27,7 @@ import org.apache.camel.parser.helper.CamelXmlHelper;
 import org.apache.camel.parser.helper.XmlLineNumberParser;
 
 import org.apache.camel.parser.model.CamelEndpointDetails;
+import org.apache.camel.parser.model.CamelRouteDetails;
 import org.apache.camel.parser.model.CamelSimpleExpressionDetails;
 import org.jboss.forge.roaster.model.util.Strings;
 
@@ -153,6 +154,48 @@ public final class XmlRouteParser {
     }
 
     /**
+     * Parses the XML source to discover Camel routes.
+     *
+     * @param xml                     the xml file as input stream
+     * @param baseDir                 the base of the source code
+     * @param fullyQualifiedFileName  the fully qualified source code file name
+     * @param routes                  list to add discovered and parsed routes
+     */
+    public static void parseXmlRouteRoutes(InputStream xml, String baseDir, 
String fullyQualifiedFileName,
+                                           List<CamelRouteDetails> routes) 
throws Exception {
+
+        // find all the endpoints (currently only <route> and within <route>)
+        // try parse it as dom
+        Document dom = null;
+        try {
+            dom = XmlLineNumberParser.parseXml(xml);
+        } catch (Exception e) {
+            // ignore as the xml file may not be valid at this point
+        }
+        if (dom != null) {
+            List<Node> nodes = CamelXmlHelper.findAllRoutes(dom);
+            for (Node node : nodes) {
+                String id = getSafeAttribute(node, "id");
+                String lineNumber = (String) 
node.getUserData(XmlLineNumberParser.LINE_NUMBER);
+                String lineNumberEnd = (String) 
node.getUserData(XmlLineNumberParser.LINE_NUMBER_END);
+
+                // we only want the relative dir name from the resource 
directory, eg META-INF/spring/foo.xml
+                String fileName = fullyQualifiedFileName;
+                if (fileName.startsWith(baseDir)) {
+                    fileName = fileName.substring(baseDir.length() + 1);
+                }
+
+                CamelRouteDetails detail = new CamelRouteDetails();
+                detail.setFileName(fileName);
+                detail.setLineNumber(lineNumber);
+                detail.setLineNumberEnd(lineNumberEnd);
+                detail.setRouteId(id);
+                routes.add(detail);
+            }
+        }
+    }
+
+    /**
      * Using simple expressions in the XML DSL may be used in certain places 
as predicate only
      */
     private static boolean isSimplePredicate(Node node) {

http://git-wip-us.apache.org/repos/asf/camel/blob/a0126b4c/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/helper/CamelXmlHelper.java
----------------------------------------------------------------------
diff --git 
a/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/helper/CamelXmlHelper.java
 
b/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/helper/CamelXmlHelper.java
index 53d4783..88e181a 100644
--- 
a/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/helper/CamelXmlHelper.java
+++ 
b/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/helper/CamelXmlHelper.java
@@ -135,6 +135,20 @@ public final class CamelXmlHelper {
         }
     }
 
+    public static List<Node> findAllRoutes(Document dom) {
+        List<Node> nodes = new ArrayList<>();
+
+        NodeList list = dom.getElementsByTagName("route");
+        for (int i = 0; i < list.getLength(); i++) {
+            Node child = list.item(i);
+            if ("route".equals(child.getNodeName())) {
+                nodes.add(child);
+            }
+        }
+
+        return nodes;
+    }
+
     public static List<Node> findAllSimpleExpressions(Document dom) {
         List<Node> nodes = new ArrayList<>();
 

http://git-wip-us.apache.org/repos/asf/camel/blob/a0126b4c/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/model/CamelRouteDetails.java
----------------------------------------------------------------------
diff --git 
a/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/model/CamelRouteDetails.java
 
b/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/model/CamelRouteDetails.java
new file mode 100644
index 0000000..f87463a
--- /dev/null
+++ 
b/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/model/CamelRouteDetails.java
@@ -0,0 +1,133 @@
+/**
+ * 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.parser.model;
+
+/**
+ * Details about a parsed and discovered Camel route.
+ */
+public class CamelRouteDetails {
+
+    private String fileName;
+    private String lineNumber;
+    private String lineNumberEnd;
+    private String className;
+    private String methodName;
+    private String routeId;
+
+    public String getFileName() {
+        return fileName;
+    }
+
+    public void setFileName(String fileName) {
+        this.fileName = fileName;
+    }
+
+    public String getLineNumber() {
+        return lineNumber;
+    }
+
+    public void setLineNumber(String lineNumber) {
+        this.lineNumber = lineNumber;
+    }
+
+    public String getLineNumberEnd() {
+        return lineNumberEnd;
+    }
+
+    public void setLineNumberEnd(String lineNumberEnd) {
+        this.lineNumberEnd = lineNumberEnd;
+    }
+
+    public String getClassName() {
+        return className;
+    }
+
+    public void setClassName(String className) {
+        this.className = className;
+    }
+
+    public String getMethodName() {
+        return methodName;
+    }
+
+    public void setMethodName(String methodName) {
+        this.methodName = methodName;
+    }
+
+    public String getRouteId() {
+        return routeId;
+    }
+
+    public void setRouteId(String routeId) {
+        this.routeId = routeId;
+    }
+
+    @Override
+    public boolean equals(Object o) {
+        if (this == o) {
+            return true;
+        }
+        if (o == null || getClass() != o.getClass()) {
+            return false;
+        }
+
+        CamelRouteDetails that = (CamelRouteDetails) o;
+
+        if (!fileName.equals(that.fileName)) {
+            return false;
+        }
+        if (lineNumber != null ? !lineNumber.equals(that.lineNumber) : 
that.lineNumber != null) {
+            return false;
+        }
+        if (lineNumberEnd != null ? !lineNumberEnd.equals(that.lineNumberEnd) 
: that.lineNumberEnd != null) {
+            return false;
+        }
+        if (!className.equals(that.className)) {
+            return false;
+        }
+        if (methodName != null ? !methodName.equals(that.methodName) : 
that.methodName != null) {
+            return false;
+        }
+        if (routeId != null ? !routeId.equals(that.routeId) : that.routeId != 
null) {
+            return false;
+        }
+        return true;
+    }
+
+    @Override
+    public int hashCode() {
+        int result = fileName.hashCode();
+        result = 31 * result + (lineNumber != null ? lineNumber.hashCode() : 
0);
+        result = 31 * result + (lineNumberEnd != null ? 
lineNumberEnd.hashCode() : 0);
+        result = 31 * result + className.hashCode();
+        result = 31 * result + (methodName != null ? methodName.hashCode() : 
0);
+        result = 31 * result + (routeId != null ? routeId.hashCode() : 0);
+        return result;
+    }
+
+    @Override
+    public String toString() {
+        return "CamelRouteDetails["
+                + "fileName='" + fileName + '\''
+                + ", lineNumber='" + lineNumber + '\''
+                + ", lineNumberEnd='" + lineNumberEnd + '\''
+                + ", className='" + className + '\''
+                + ", methodName='" + methodName + '\''
+                + ", routeId='" + routeId + '\''
+                + ']';
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/a0126b4c/tooling/camel-route-parser/src/test/java/org/apache/camel/parser/xml/DuplicateRouteIdsTest.java
----------------------------------------------------------------------
diff --git 
a/tooling/camel-route-parser/src/test/java/org/apache/camel/parser/xml/DuplicateRouteIdsTest.java
 
b/tooling/camel-route-parser/src/test/java/org/apache/camel/parser/xml/DuplicateRouteIdsTest.java
new file mode 100644
index 0000000..1497a8d
--- /dev/null
+++ 
b/tooling/camel-route-parser/src/test/java/org/apache/camel/parser/xml/DuplicateRouteIdsTest.java
@@ -0,0 +1,54 @@
+/**
+ * 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.parser.xml;
+
+import java.io.FileInputStream;
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.camel.parser.XmlRouteParser;
+import org.apache.camel.parser.model.CamelRouteDetails;
+import org.junit.Assert;
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class DuplicateRouteIdsTest {
+
+    private static final Logger LOG = 
LoggerFactory.getLogger(DuplicateRouteIdsTest.class);
+
+    @Test
+    public void testXml() throws Exception {
+        List<CamelRouteDetails> list = new ArrayList<>();
+
+        InputStream is = new 
FileInputStream("src/test/resources/org/apache/camel/parser/xml/myduplicateroutes.xml");
+        String fqn = 
"src/test/resources/org/apache/camel/camel/parser/xml/myduplicateroutes.xml";
+        String baseDir = "src/test/resources";
+        XmlRouteParser.parseXmlRouteRoutes(is, baseDir, fqn, list);
+
+        for (CamelRouteDetails detail : list) {
+            LOG.info(detail.getRouteId());
+        }
+
+        Assert.assertEquals(3, list.size());
+        Assert.assertEquals("foo", list.get(0).getRouteId());
+        Assert.assertEquals("bar", list.get(1).getRouteId());
+        Assert.assertEquals("foo", list.get(2).getRouteId());
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/a0126b4c/tooling/camel-route-parser/src/test/resources/org/apache/camel/parser/xml/myduplicateroutes.xml
----------------------------------------------------------------------
diff --git 
a/tooling/camel-route-parser/src/test/resources/org/apache/camel/parser/xml/myduplicateroutes.xml
 
b/tooling/camel-route-parser/src/test/resources/org/apache/camel/parser/xml/myduplicateroutes.xml
new file mode 100644
index 0000000..6ea349a
--- /dev/null
+++ 
b/tooling/camel-route-parser/src/test/resources/org/apache/camel/parser/xml/myduplicateroutes.xml
@@ -0,0 +1,33 @@
+<?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.
+
+-->
+<routes xmlns="http://camel.apache.org/schema/spring";>
+    <route id="foo">
+      <from uri="timer:foo?period=5000"/>
+      <to uri="log:foo"/>
+    </route>
+    <route id="bar">
+      <from uri="timer:bar?period=5000"/>
+      <to uri="log:bar"/>
+    </route>
+    <route id="foo">
+      <from uri="timer:baz?period=5000"/>
+      <to uri="log:baz"/>
+    </route>
+</routes>

Reply via email to