camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject [03/13] camel git commit: Camel java dsl parser prototype
Date Sun, 08 Oct 2017 12:02:46 GMT
Camel java dsl parser prototype


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

Branch: refs/heads/parser2
Commit: 7aef65670715aa28c838440c5ed8e39cbb8a9488
Parents: 3d3bc4b
Author: Claus Ibsen <davsclaus@apache.org>
Authored: Sat Oct 7 17:55:08 2017 +0200
Committer: Claus Ibsen <davsclaus@apache.org>
Committed: Sat Oct 7 17:55:08 2017 +0200

----------------------------------------------------------------------
 .../parser/AdvancedRouteBuilderParser.java      |  5 +-
 .../helper/AdvancedCamelJavaParserHelper.java   | 73 ++++++++++++++++----
 .../camel/parser/model/CamelNodeDetails.java    | 41 ++++++++++-
 .../parser/model/CamelNodeDetailsFactory.java   |  4 ++
 .../camel/parser/java/RoasterJavaDslTest.java   |  5 +-
 5 files changed, 110 insertions(+), 18 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/7aef6567/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/AdvancedRouteBuilderParser.java
----------------------------------------------------------------------
diff --git a/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/AdvancedRouteBuilderParser.java
b/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/AdvancedRouteBuilderParser.java
index e3fb6d7..9159203 100644
--- a/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/AdvancedRouteBuilderParser.java
+++ b/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/AdvancedRouteBuilderParser.java
@@ -31,7 +31,8 @@ import org.jboss.forge.roaster.model.source.MethodSource;
 public class AdvancedRouteBuilderParser {
 
     // TODO: list of details, on per route
-    public static CamelNodeDetails parseRouteBuilder(JavaClassSource clazz, boolean includeInlinedRouteBuilders)
{
+    public static CamelNodeDetails parseRouteBuilder(JavaClassSource clazz, String baseDir,
String fullyQualifiedFileName,
+                                                     boolean includeInlinedRouteBuilders)
{
         AdvancedCamelJavaParserHelper parser = new AdvancedCamelJavaParserHelper();
 
         List<MethodSource<JavaClassSource>> methods = new ArrayList<>();
@@ -47,7 +48,7 @@ public class AdvancedRouteBuilderParser {
         }
 
         for (MethodSource<JavaClassSource> configureMethod : methods) {
-            CamelNodeDetails details = parser.parseCamelRoute(clazz, configureMethod);
+            CamelNodeDetails details = parser.parseCamelRoute(clazz, baseDir, fullyQualifiedFileName,
configureMethod);
             return details;
         }
 

http://git-wip-us.apache.org/repos/asf/camel/blob/7aef6567/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/helper/AdvancedCamelJavaParserHelper.java
----------------------------------------------------------------------
diff --git a/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/helper/AdvancedCamelJavaParserHelper.java
b/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/helper/AdvancedCamelJavaParserHelper.java
index c264930..322997b 100644
--- a/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/helper/AdvancedCamelJavaParserHelper.java
+++ b/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/helper/AdvancedCamelJavaParserHelper.java
@@ -16,6 +16,9 @@
  */
 package org.apache.camel.parser.helper;
 
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileReader;
 import java.util.List;
 import java.util.Map;
 
@@ -57,11 +60,13 @@ import org.jboss.forge.roaster.model.source.MethodSource;
  * <p/>
  * This implementation is lower level details. For a higher level parser see {@link RouteBuilderParser}.
  */
+// TODO: rename this class
 public final class AdvancedCamelJavaParserHelper {
 
     private CamelCatalog camelCatalog = new DefaultCamelCatalog(true);
 
-    public CamelNodeDetails parseCamelRoute(JavaClassSource clazz, MethodSource<JavaClassSource>
method) {
+    public CamelNodeDetails parseCamelRoute(JavaClassSource clazz, String baseDir, String
fullyQualifiedFileName,
+                                            MethodSource<JavaClassSource> method) {
 
         // find any from which is the start of the route
         CamelNodeDetailsFactory nodeFactory = CamelNodeDetailsFactory.newInstance();
@@ -77,7 +82,7 @@ public final class AdvancedCamelJavaParserHelper {
                     if (statement instanceof ExpressionStatement) {
                         ExpressionStatement es = (ExpressionStatement) statement;
                         Expression exp = es.getExpression();
-                        parseExpression(nodeFactory, clazz, block, exp, route);
+                        parseExpression(nodeFactory, fullyQualifiedFileName, clazz, block,
exp, route);
                     }
                 }
             }
@@ -86,28 +91,32 @@ public final class AdvancedCamelJavaParserHelper {
         // now parse the route node and build a tree structure of the EIPs
         String root = route.getOutputs().get(0).getName();
 
-        CamelNodeDetails answer = nodeFactory.newNode(null, root);
-        CamelNodeDetails parent = answer;
+        // re-create factory as we rebuild the tree
+        nodeFactory = CamelNodeDetailsFactory.newInstance();
 
-        // TODO: use camel catalog to know about these types and when to do what
+        CamelNodeDetails answer = nodeFactory.newNode(null, root);
+        answer.setFileName(fullyQualifiedFileName);
 
+        CamelNodeDetails parent = answer;
         for (int i = 1; i < route.getOutputs().size(); i++) {
+
             CamelNodeDetails node = route.getOutputs().get(i);
             String name = node.getName();
 
+            // TODO: use camel catalog to know about these types and when to do what
             // special for some EIPs
             if ("choice".equals(name)) {
-                CamelNodeDetails output = nodeFactory.newNode(parent, name);
+                CamelNodeDetails output = nodeFactory.copyNode(parent, name, node);
                 parent.addOutput(output);
                 parent = output;
             } else if ("when".equals(name)) {
                 parent = grandParent(parent, "choice");
-                CamelNodeDetails output = nodeFactory.newNode(parent, name);
+                CamelNodeDetails output = nodeFactory.copyNode(parent, name, node);
                 parent.addOutput(output);
                 parent = output;
             } else if ("otherwise".equals(name)) {
                 parent = grandParent(parent, "choice");
-                CamelNodeDetails output = nodeFactory.newNode(parent, name);
+                CamelNodeDetails output = nodeFactory.copyNode(parent, name, node);
                 parent.addOutput(output);
                 parent = output;
             } else if ("end".equals(name) || "endChoice".equals(name) || "endDoTry".equals(name))
{
@@ -121,12 +130,12 @@ public final class AdvancedCamelJavaParserHelper {
                 boolean hasOutput = hasOutput(name);
                 if (hasOutput) {
                     // has output so add as new child node
-                    CamelNodeDetails output = nodeFactory.newNode(parent, name);
+                    CamelNodeDetails output = nodeFactory.copyNode(parent, name, node);
                     parent.addOutput(output);
                     parent = output;
                 } else {
                     // add straight to itself
-                    CamelNodeDetails output = nodeFactory.newNode(parent, name);
+                    CamelNodeDetails output = nodeFactory.copyNode(parent, name, node);
                     parent.addOutput(output);
                 }
             }
@@ -161,21 +170,21 @@ public final class AdvancedCamelJavaParserHelper {
         }
     }
 
-    private void parseExpression(CamelNodeDetailsFactory nodeFactory,
+    private void parseExpression(CamelNodeDetailsFactory nodeFactory, String fullyQualifiedFileName,
                                  JavaClassSource clazz, Block block, Expression exp, CamelNodeDetails
node) {
         if (exp == null) {
             return;
         }
         if (exp instanceof MethodInvocation) {
             MethodInvocation mi = (MethodInvocation) exp;
-            node = doParseCamelModels(nodeFactory, clazz, block, mi, node);
+            node = doParseCamelModels(nodeFactory, fullyQualifiedFileName, clazz, block,
mi, node);
             // if the method was called on another method, then recursive
             exp = mi.getExpression();
-            parseExpression(nodeFactory, clazz, block, exp, node);
+            parseExpression(nodeFactory, fullyQualifiedFileName, clazz, block, exp, node);
         }
     }
 
-    private CamelNodeDetails doParseCamelModels(CamelNodeDetailsFactory nodeFactory,
+    private CamelNodeDetails doParseCamelModels(CamelNodeDetailsFactory nodeFactory, String
fullyQualifiedFileName,
                                                 JavaClassSource clazz, Block block, MethodInvocation
mi, CamelNodeDetails node) {
         String name = mi.getName().getIdentifier();
 
@@ -185,6 +194,15 @@ public final class AdvancedCamelJavaParserHelper {
         // only include if its a known Camel model
         if (isEnd || camelCatalog.findModelNames().contains(name)) {
             CamelNodeDetails newNode = nodeFactory.newNode(node, name);
+
+            // include source code details
+            int pos = mi.getName().getStartPosition();
+            int line = findLineNumber(fullyQualifiedFileName, pos);
+            if (line > -1) {
+                newNode.setLineNumber("" + line);
+            }
+            newNode.setFileName(fullyQualifiedFileName);
+
             node.addPreliminaryOutput(newNode);
             return node;
         }
@@ -251,6 +269,10 @@ public final class AdvancedCamelJavaParserHelper {
         return null;
     }
 
+    /**
+     * @deprecated currently not in use
+     */
+    @Deprecated
     public static String getLiteralValue(JavaClassSource clazz, Block block, Expression expression)
{
         // unwrap parenthesis
         if (expression instanceof ParenthesizedExpression) {
@@ -392,4 +414,27 @@ public final class AdvancedCamelJavaParserHelper {
         return false;
     }
 
+    private static int findLineNumber(String fullyQualifiedFileName, int position) {
+        int lines = 0;
+
+        try {
+            int current = 0;
+            try (BufferedReader br = new BufferedReader(new FileReader(new File(fullyQualifiedFileName))))
{
+                String line;
+                while ((line = br.readLine()) != null) {
+                    lines++;
+                    current += line.length() + 1; // add 1 for line feed
+                    if (current >= position) {
+                        return lines;
+                    }
+                }
+            }
+        } catch (Exception e) {
+            // ignore
+            return -1;
+        }
+
+        return lines;
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/7aef6567/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/model/CamelNodeDetails.java
----------------------------------------------------------------------
diff --git a/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/model/CamelNodeDetails.java
b/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/model/CamelNodeDetails.java
index 9712347..7167eb8 100644
--- a/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/model/CamelNodeDetails.java
+++ b/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/model/CamelNodeDetails.java
@@ -21,11 +21,26 @@ import java.util.List;
 
 public class CamelNodeDetails {
 
+    // source code details
+    private String fileName;
+    private String lineNumber;
+    private String lineNumberEnd;
+
+    // camel node details
     private final CamelNodeDetails parent;
     private final String name;
     private final int order;
     private List<CamelNodeDetails> outputs;
 
+    public CamelNodeDetails(CamelNodeDetails parent, String name, int order, CamelNodeDetails
copy) {
+        this.parent = parent;
+        this.name = name;
+        this.order = order;
+        this.fileName = copy.getFileName();
+        this.lineNumber = copy.getLineNumber();
+        this.lineNumberEnd = copy.getLineNumberEnd();
+    }
+
     public CamelNodeDetails(CamelNodeDetails parent, String name, int order) {
         this.parent = parent;
         this.name = name;
@@ -63,13 +78,37 @@ public class CamelNodeDetails {
         return outputs;
     }
 
+    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 toString() {
         return name;
     }
 
     public String dump(int level) {
         StringBuilder sb = new StringBuilder();
-        sb.append(order);
+        sb.append(lineNumber);
         sb.append("\t");
         sb.append(padString(level));
         sb.append(name);

http://git-wip-us.apache.org/repos/asf/camel/blob/7aef6567/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/model/CamelNodeDetailsFactory.java
----------------------------------------------------------------------
diff --git a/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/model/CamelNodeDetailsFactory.java
b/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/model/CamelNodeDetailsFactory.java
index 502c2c7..33b15c8 100644
--- a/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/model/CamelNodeDetailsFactory.java
+++ b/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/model/CamelNodeDetailsFactory.java
@@ -30,4 +30,8 @@ public class CamelNodeDetailsFactory {
     public CamelNodeDetails newNode(CamelNodeDetails parent, String name) {
         return new CamelNodeDetails(parent, name, ++order);
     }
+
+    public CamelNodeDetails copyNode(CamelNodeDetails parent, String name, CamelNodeDetails
copoy) {
+        return new CamelNodeDetails(parent, name, ++order, copoy);
+    }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/7aef6567/tooling/camel-route-parser/src/test/java/org/apache/camel/parser/java/RoasterJavaDslTest.java
----------------------------------------------------------------------
diff --git a/tooling/camel-route-parser/src/test/java/org/apache/camel/parser/java/RoasterJavaDslTest.java
b/tooling/camel-route-parser/src/test/java/org/apache/camel/parser/java/RoasterJavaDslTest.java
index 48e3a3c..928ac87 100644
--- a/tooling/camel-route-parser/src/test/java/org/apache/camel/parser/java/RoasterJavaDslTest.java
+++ b/tooling/camel-route-parser/src/test/java/org/apache/camel/parser/java/RoasterJavaDslTest.java
@@ -40,10 +40,13 @@ public class RoasterJavaDslTest extends CamelTestSupport {
     public void parse() throws Exception {
         JavaClassSource clazz = (JavaClassSource) Roaster.parse(new File("src/test/java/org/apache/camel/parser/java/MyJavaDslRouteBuilder.java"));
 
-        CamelNodeDetails details = AdvancedRouteBuilderParser.parseRouteBuilder(clazz, true);
+        CamelNodeDetails details = AdvancedRouteBuilderParser.parseRouteBuilder(clazz, ".",
+            "src/test/java/org/apache/camel/parser/java/MyJavaDslRouteBuilder.java",true);
         String tree = details.dump(0);
         LOG.info("\n" + tree);
 
+        String name = details.getFileName();
+        System.out.println(name);
         System.out.println(tree);
     }
 


Mime
View raw message