You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2017/01/12 11:17:10 UTC

camel git commit: camel maven plugin validate goal should also support simple predicate parsing for EIPs that uses simple as predicates.

Repository: camel
Updated Branches:
  refs/heads/master 0d02e9e3c -> ab530bfe7


camel maven plugin validate goal should also support simple predicate parsing for EIPs that uses simple as predicates.


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

Branch: refs/heads/master
Commit: ab530bfe742e8de6936670c9467c93dc8109e261
Parents: 0d02e9e
Author: Claus Ibsen <da...@apache.org>
Authored: Thu Jan 12 12:14:06 2017 +0100
Committer: Claus Ibsen <da...@apache.org>
Committed: Thu Jan 12 12:14:06 2017 +0100

----------------------------------------------------------------------
 .../org/apache/camel/parser/XmlRouteParser.java | 45 ++++++++++++++++
 .../camel/parser/xml/XmlFilterRouteTest.java    | 54 ++++++++++++++++++++
 .../apache/camel/parser/xml/myfiltercamel.xml   | 18 +++++++
 3 files changed, 117 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/ab530bfe/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 2cca9df..eb883d4 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
@@ -141,11 +141,56 @@ public final class XmlRouteParser {
                 detail.setLineNumber(lineNumber);
                 detail.setLineNumberEnd(lineNumberEnd);
                 detail.setSimple(simple);
+
+                // is it used as predicate or not
+                boolean asPredicate = isSimplePredicate(node);
+                detail.setPredicate(asPredicate);
+                detail.setExpression(!asPredicate);
+
                 simpleExpressions.add(detail);
             }
         }
     }
 
+    /**
+     * Using simple expressions in the XML DSL may be used in certain places as predicate only
+     */
+    private static boolean isSimplePredicate(Node node) {
+        // need to check the parent
+        Node parent = node.getParentNode();
+        if (parent == null) {
+            return false;
+        }
+        String name = parent.getNodeName();
+
+        if (name == null) {
+            return false;
+        }
+        if (name.equals("completionPredicate") || name.equals("completion")) {
+            return true;
+        }
+        if (name.equals("onWhen") || name.equals("when") || name.equals("handled") || name.equals("continued")) {
+            return true;
+        }
+        if (name.equals("retryWhile") || name.equals("filter") || name.equals("validate")) {
+            return true;
+        }
+        // special for loop
+        if (name.equals("loop")) {
+            String doWhile = null;
+            if (parent.getAttributes() != null) {
+                Node attr = parent.getAttributes().getNamedItem("doWhile");
+                if (attr != null) {
+                    doWhile = attr.getTextContent();
+                }
+            }
+            if ("true".equalsIgnoreCase(doWhile)) {
+                return true;
+            }
+        }
+        return false;
+    }
+
     private static String endpointComponentName(String uri) {
         if (uri != null) {
             int idx = uri.indexOf(":");

http://git-wip-us.apache.org/repos/asf/camel/blob/ab530bfe/tooling/camel-route-parser/src/test/java/org/apache/camel/parser/xml/XmlFilterRouteTest.java
----------------------------------------------------------------------
diff --git a/tooling/camel-route-parser/src/test/java/org/apache/camel/parser/xml/XmlFilterRouteTest.java b/tooling/camel-route-parser/src/test/java/org/apache/camel/parser/xml/XmlFilterRouteTest.java
new file mode 100644
index 0000000..3158358
--- /dev/null
+++ b/tooling/camel-route-parser/src/test/java/org/apache/camel/parser/xml/XmlFilterRouteTest.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.CamelSimpleExpressionDetails;
+import org.junit.Assert;
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class XmlFilterRouteTest {
+
+    private static final Logger LOG = LoggerFactory.getLogger(XmlFilterRouteTest.class);
+
+    @Test
+    public void testXml() throws Exception {
+        List<CamelSimpleExpressionDetails> list = new ArrayList<>();
+
+        InputStream is = new FileInputStream("src/test/resources/org/apache/camel/parser/xml/myfiltercamel.xml");
+        String fqn = "src/test/resources/org/apache/camel/camel/parser/xml/myfiltercamel.xml";
+        String baseDir = "src/test/resources";
+        XmlRouteParser.parseXmlRouteSimpleExpressions(is, baseDir, fqn, list);
+
+        for (CamelSimpleExpressionDetails detail : list) {
+            LOG.info(detail.getSimple());
+        }
+
+        Assert.assertEquals(1, list.size());
+        Assert.assertEquals("${body} == 'Camel'", list.get(0).getSimple());
+        Assert.assertTrue(list.get(0).isPredicate());
+        Assert.assertFalse(list.get(0).isExpression());
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/ab530bfe/tooling/camel-route-parser/src/test/resources/org/apache/camel/parser/xml/myfiltercamel.xml
----------------------------------------------------------------------
diff --git a/tooling/camel-route-parser/src/test/resources/org/apache/camel/parser/xml/myfiltercamel.xml b/tooling/camel-route-parser/src/test/resources/org/apache/camel/parser/xml/myfiltercamel.xml
new file mode 100644
index 0000000..3062e8c
--- /dev/null
+++ b/tooling/camel-route-parser/src/test/resources/org/apache/camel/parser/xml/myfiltercamel.xml
@@ -0,0 +1,18 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<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"/>
+      <filter>
+        <simple>${body} == 'Camel'</simple>
+        <to uri="mock:camel"/>
+      </filter>
+    </route>
+  </camelContext>
+
+</beans>