You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by or...@apache.org on 2023/03/24 13:10:20 UTC

[camel] branch main updated: (chores) camel-route-parser: consolidate duplicated code

This is an automated email from the ASF dual-hosted git repository.

orpiske pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/main by this push:
     new 07e280d7dcc (chores) camel-route-parser: consolidate duplicated code
07e280d7dcc is described below

commit 07e280d7dcc9f2503f56a26187b9fd0b0f8c93f2
Author: Otavio Rodolfo Piske <an...@gmail.com>
AuthorDate: Fri Mar 24 13:10:07 2023 +0100

    (chores) camel-route-parser: consolidate duplicated code
---
 .../org/apache/camel/parser/XmlRouteParser.java    | 80 ++++++++--------------
 1 file changed, 29 insertions(+), 51 deletions(-)

diff --git a/catalog/camel-route-parser/src/main/java/org/apache/camel/parser/XmlRouteParser.java b/catalog/camel-route-parser/src/main/java/org/apache/camel/parser/XmlRouteParser.java
index 4f47c80d714..eb4d71ab2b6 100644
--- a/catalog/camel-route-parser/src/main/java/org/apache/camel/parser/XmlRouteParser.java
+++ b/catalog/camel-route-parser/src/main/java/org/apache/camel/parser/XmlRouteParser.java
@@ -59,12 +59,7 @@ public final class XmlRouteParser {
         List<CamelNodeDetails> answer = new ArrayList<>();
 
         // 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
-        }
+        Document dom = getDocument(xml);
         if (dom != null) {
 
             // find any from which is the start of the route
@@ -80,10 +75,7 @@ public final class XmlRouteParser {
                 String lineNumberEnd = (String) route.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);
-                }
+                String fileName = getFileName(baseDir, fullyQualifiedFileName);
 
                 CamelNodeDetails node = nodeFactory.newNode(null, "route");
                 node.setRouteId(routeId);
@@ -105,6 +97,16 @@ public final class XmlRouteParser {
         return answer;
     }
 
+    private static Document getDocument(InputStream xml) {
+        Document dom = null;
+        try {
+            dom = XmlLineNumberParser.parseXml(xml);
+        } catch (Exception e) {
+            // ignore as the xml file may not be valid at this point
+        }
+        return dom;
+    }
+
     /**
      * Parses the XML source to discover Camel endpoints.
      *
@@ -119,12 +121,7 @@ public final class XmlRouteParser {
 
         // find all the endpoints (currently only <endpoint> 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
-        }
+        Document dom = getDocument(xml);
         if (dom != null) {
             List<Node> nodes = CamelXmlHelper.findAllEndpoints(dom);
             for (Node node : nodes) {
@@ -139,10 +136,7 @@ public final class XmlRouteParser {
                     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);
-                    }
+                    String fileName = getFileName(baseDir, fullyQualifiedFileName);
 
                     boolean consumerOnly = false;
                     boolean producerOnly = false;
@@ -188,12 +182,7 @@ public final class XmlRouteParser {
 
         // find all the simple expressions
         // 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
-        }
+        Document dom = getDocument(xml);
         if (dom != null) {
             List<Node> nodes = CamelXmlHelper.findAllLanguageExpressions(dom, "simple");
             for (Node node : nodes) {
@@ -201,11 +190,7 @@ public final class XmlRouteParser {
                 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);
-                }
+                String fileName = getFileName(baseDir, fullyQualifiedFileName);
 
                 CamelSimpleExpressionDetails detail = new CamelSimpleExpressionDetails();
                 detail.setFileName(fileName);
@@ -228,6 +213,15 @@ public final class XmlRouteParser {
         }
     }
 
+    private static String getFileName(String baseDir, String fullyQualifiedFileName) {
+        // 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);
+        }
+        return fileName;
+    }
+
     /**
      * Parses the XML source to discover Camel compiled simple language.
      *
@@ -242,12 +236,7 @@ public final class XmlRouteParser {
 
         // find all the simple expressions
         // 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
-        }
+        Document dom = getDocument(xml);
         if (dom != null) {
             List<Node> nodes = CamelXmlHelper.findAllLanguageExpressions(dom, "csimple");
             for (Node node : nodes) {
@@ -256,10 +245,7 @@ public final class XmlRouteParser {
                 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);
-                }
+                String fileName = getFileName(baseDir, fullyQualifiedFileName);
 
                 CamelCSimpleExpressionDetails detail = new CamelCSimpleExpressionDetails();
                 detail.setFileName(fileName);
@@ -296,12 +282,7 @@ public final class XmlRouteParser {
 
         // 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
-        }
+        Document dom = getDocument(xml);
         if (dom != null) {
             List<Node> nodes = CamelXmlHelper.findAllRoutes(dom);
             for (Node node : nodes) {
@@ -310,10 +291,7 @@ public final class XmlRouteParser {
                 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);
-                }
+                String fileName = getFileName(baseDir, fullyQualifiedFileName);
 
                 CamelRouteDetails detail = new CamelRouteDetails();
                 detail.setFileName(fileName);