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

davsclaus pushed a commit to branch camel-2.22.x
in repository https://gitbox.apache.org/repos/asf/camel.git

commit 06ed50793fb21131be76c89b351dfd66bb3586ef
Author: Claus Ibsen <claus.ib...@gmail.com>
AuthorDate: Sat Sep 8 17:34:27 2018 +0200

    CAMEL-12114: Have XmlLineNumberParser respecting namespace uri also for 
anonymous namespaces (without prefix) from its parent+ tag
---
 .../camel/parser/helper/XmlLineNumberParser.java   | 29 ++++++++++++++--
 .../camel/parser/java/XmlLineNumberParserTest.java | 39 ++++++++++++++++++++++
 2 files changed, 66 insertions(+), 2 deletions(-)

diff --git 
a/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/helper/XmlLineNumberParser.java
 
b/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/helper/XmlLineNumberParser.java
index bdc7890..edf8312 100644
--- 
a/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/helper/XmlLineNumberParser.java
+++ 
b/tooling/camel-route-parser/src/main/java/org/apache/camel/parser/helper/XmlLineNumberParser.java
@@ -19,8 +19,15 @@ package org.apache.camel.parser.helper;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.StringReader;
+import java.util.ArrayDeque;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Deque;
 import java.util.HashMap;
+import java.util.LinkedHashMap;
+import java.util.List;
 import java.util.Map;
+import java.util.Queue;
 import java.util.Stack;
 import javax.xml.parsers.DocumentBuilder;
 import javax.xml.parsers.DocumentBuilderFactory;
@@ -102,6 +109,7 @@ public final class XmlLineNumberParser {
             private Locator locator;
             private boolean found;
             private final Map<String, String> localNs = new HashMap<>();
+            private final Map<String, String> anonymousNs = new 
LinkedHashMap<>();
 
             @Override
             public void setDocumentLocator(final Locator locator) {
@@ -146,6 +154,19 @@ public final class XmlLineNumberParser {
                                     ns = localNs.get(prefix);
                                 }
                             }
+                        } else {
+                            // maybe there is an anonymous namespace (xmlns)
+                            if (attributes != null) {
+                                ns = attributes.getValue("xmlns");
+                                if (ns != null) {
+                                    anonymousNs.put(qName, ns);
+                                } else if (!anonymousNs.isEmpty()) {
+                                    // grab latest anonymous namespace to use 
as the namespace as
+                                    // this child tag should use the parents+ 
namespace
+                                    List<String> values = new 
ArrayList<>(anonymousNs.values());
+                                    ns = values.get(values.size() - 1);
+                                }
+                            }
                         }
                         if (ns != null) {
                             el = doc.createElementNS(ns, qName);
@@ -154,8 +175,10 @@ public final class XmlLineNumberParser {
                         }
                     }
 
-                    for (int i = 0; i < attributes.getLength(); i++) {
-                        el.setAttribute(attributes.getQName(i), 
attributes.getValue(i));
+                    if (attributes != null) {
+                        for (int i = 0; i < attributes.getLength(); i++) {
+                            el.setAttribute(attributes.getQName(i), 
attributes.getValue(i));
+                        }
                     }
 
                     el.setUserData(LINE_NUMBER, 
String.valueOf(this.locator.getLineNumber()), null);
@@ -185,6 +208,8 @@ public final class XmlLineNumberParser {
                     closedEl.setUserData(LINE_NUMBER_END, 
String.valueOf(this.locator.getLineNumber()), null);
                     closedEl.setUserData(COLUMN_NUMBER_END, 
String.valueOf(this.locator.getColumnNumber()), null);
                 }
+
+                anonymousNs.remove(qName);
             }
 
             @Override
diff --git 
a/tooling/camel-route-parser/src/test/java/org/apache/camel/parser/java/XmlLineNumberParserTest.java
 
b/tooling/camel-route-parser/src/test/java/org/apache/camel/parser/java/XmlLineNumberParserTest.java
new file mode 100644
index 0000000..bec0696
--- /dev/null
+++ 
b/tooling/camel-route-parser/src/test/java/org/apache/camel/parser/java/XmlLineNumberParserTest.java
@@ -0,0 +1,39 @@
+/**
+ * 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.java;
+
+import java.io.FileInputStream;
+import java.io.InputStream;
+
+import org.w3c.dom.Document;
+import org.w3c.dom.NodeList;
+
+import org.apache.camel.parser.helper.XmlLineNumberParser;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+
+public class XmlLineNumberParserTest {
+
+    @Test
+    public void testRespectNamespace() throws Exception {
+        InputStream is = new 
FileInputStream("src/test/resources/org/apache/camel/parser/xml/mycamel.xml");
+        Document parsedXml = XmlLineNumberParser.parseXml(is);
+        NodeList fromCamelWithNamespace = 
parsedXml.getElementsByTagNameNS("http://camel.apache.org/schema/spring";, 
"from");
+        assertEquals(1, fromCamelWithNamespace.getLength());
+    }
+}

Reply via email to