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 2022/01/03 14:02:26 UTC

[camel] branch main updated: CAMEL-17412: camel-xml-io - Line number is the tag end line - We need a tag start line number

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

davsclaus 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 160ab64  CAMEL-17412: camel-xml-io - Line number is the tag end line - We need a tag start line number
160ab64 is described below

commit 160ab64df1193e1ad8dcf779c4f2618411eb5aa3
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Mon Jan 3 15:01:45 2022 +0100

    CAMEL-17412: camel-xml-io - Line number is the tag end line - We need a tag start line number
---
 .../java/org/apache/camel/xml/in/BaseParser.java   |  7 +++++-
 .../java/org/apache/camel/xml/io/MXParser.java     |  9 ++++++++
 .../org/apache/camel/xml/io/XmlPullParser.java     |  9 ++++++++
 .../org/apache/camel/xml/in/ModelParserTest.java   | 13 +++++++++++
 core/camel-xml-io/src/test/resources/multiline.xml | 27 ++++++++++++++++++++++
 5 files changed, 64 insertions(+), 1 deletion(-)

diff --git a/core/camel-xml-io/src/main/java/org/apache/camel/xml/in/BaseParser.java b/core/camel-xml-io/src/main/java/org/apache/camel/xml/in/BaseParser.java
index b1f8274..f80df53 100644
--- a/core/camel-xml-io/src/main/java/org/apache/camel/xml/in/BaseParser.java
+++ b/core/camel-xml-io/src/main/java/org/apache/camel/xml/in/BaseParser.java
@@ -81,7 +81,12 @@ public class BaseParser {
             T definition, AttributeHandler<T> attributeHandler, ElementHandler<T> elementHandler, ValueHandler<T> valueHandler)
             throws IOException, XmlPullParserException {
         if (definition instanceof LineNumberAware) {
-            ((LineNumberAware) definition).setLineNumber(parser.getLineNumber());
+            // we want to get the line number where the tag starts (in case its multi-line)
+            int line = parser.getStartLineNumber();
+            if (line == -1) {
+                line = parser.getLineNumber();
+            }
+            ((LineNumberAware) definition).setLineNumber(line);
             if (resource != null) {
                 ((LineNumberAware) definition).setLocation(resource.getLocation());
             }
diff --git a/core/camel-xml-io/src/main/java/org/apache/camel/xml/io/MXParser.java b/core/camel-xml-io/src/main/java/org/apache/camel/xml/io/MXParser.java
index ac8120e..e8282da 100644
--- a/core/camel-xml-io/src/main/java/org/apache/camel/xml/io/MXParser.java
+++ b/core/camel-xml-io/src/main/java/org/apache/camel/xml/io/MXParser.java
@@ -93,6 +93,7 @@ public class MXParser implements XmlPullParser {
 
     // global parser state
     protected String location;
+    protected int startLineNumber;
     protected int lineNumber;
     protected int columnNumber;
     protected boolean seenRoot;
@@ -399,6 +400,7 @@ public class MXParser implements XmlPullParser {
     protected void reset() {
         // System.out.println("reset() called");
         location = null;
+        startLineNumber = 1;
         lineNumber = 1;
         columnNumber = 0;
         seenRoot = false;
@@ -691,6 +693,10 @@ public class MXParser implements XmlPullParser {
                + getColumnNumber();
     }
 
+    public int getStartLineNumber() {
+        return startLineNumber;
+    }
+
     public int getLineNumber() {
         return lineNumber;
     }
@@ -1702,6 +1708,9 @@ public class MXParser implements XmlPullParser {
             elName[depth] = newString(buf, nameStart - bufAbsoluteStart, elLen);
         }
 
+        // remember starting line number before continuing until the end tag >
+        startLineNumber = lineNumber;
+
         while (true) {
 
             while (isS(ch)) {
diff --git a/core/camel-xml-io/src/main/java/org/apache/camel/xml/io/XmlPullParser.java b/core/camel-xml-io/src/main/java/org/apache/camel/xml/io/XmlPullParser.java
index 3a86919..a218d21 100644
--- a/core/camel-xml-io/src/main/java/org/apache/camel/xml/io/XmlPullParser.java
+++ b/core/camel-xml-io/src/main/java/org/apache/camel/xml/io/XmlPullParser.java
@@ -629,6 +629,15 @@ public interface XmlPullParser {
     String getPositionDescription();
 
     /**
+     * Returns the line number that a tag starts from (starting from 1). When the parser does
+     * not know the current line number or can not determine it, -1 is returned
+     * (e.g. for WBXML).
+     *
+     * @return starting line number or -1 if unknown.
+     */
+    int getStartLineNumber();
+
+    /**
      * Returns the current line number, starting from 1. When the parser does
      * not know the current line number or can not determine it, -1 is returned
      * (e.g. for WBXML).
diff --git a/core/camel-xml-io/src/test/java/org/apache/camel/xml/in/ModelParserTest.java b/core/camel-xml-io/src/test/java/org/apache/camel/xml/in/ModelParserTest.java
index 61a5c26..211d52f 100644
--- a/core/camel-xml-io/src/test/java/org/apache/camel/xml/in/ModelParserTest.java
+++ b/core/camel-xml-io/src/test/java/org/apache/camel/xml/in/ModelParserTest.java
@@ -131,6 +131,19 @@ public class ModelParserTest {
         Assertions.assertEquals(26, route.getOutputs().get(1).getLineNumber());
     }
 
+    @Test
+    public void testLineNumberMultiline() throws Exception {
+        Path dir = getResourceFolder();
+        Path path = new File(dir.toFile(), "multiline.xml").toPath();
+        ModelParser parser = new ModelParser(Files.newInputStream(path), NAMESPACE);
+        RoutesDefinition routes = parser.parseRoutesDefinition().orElse(null);
+        assertNotNull(routes);
+        RouteDefinition route = routes.getRoutes().get(0);
+        Assertions.assertEquals(22, route.getInput().getLineNumber());
+        Assertions.assertEquals(23, route.getOutputs().get(0).getLineNumber());
+        Assertions.assertEquals(25, route.getOutputs().get(1).getLineNumber());
+    }
+
     private Path getResourceFolder() {
         String url = getClass().getClassLoader().getResource("barInterceptorRoute.xml").toString();
         if (url.startsWith("file:")) {
diff --git a/core/camel-xml-io/src/test/resources/multiline.xml b/core/camel-xml-io/src/test/resources/multiline.xml
new file mode 100644
index 0000000..29a1b6d
--- /dev/null
+++ b/core/camel-xml-io/src/test/resources/multiline.xml
@@ -0,0 +1,27 @@
+<?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 id="camel" xmlns="http://camel.apache.org/schema/spring">
+  <route>
+    <from uri="direct:start"/>
+    <log loggingLevel="WARN"
+         message="Hello World"/>
+    <to uri="mock:end"/>
+  </route>
+</routes>