From commits-return-64965-archive-asf-public=cust-asf.ponee.io@camel.apache.org Sat Sep 8 17:38:33 2018 Return-Path: X-Original-To: archive-asf-public@cust-asf.ponee.io Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by mx-eu-01.ponee.io (Postfix) with SMTP id CB2F31807A5 for ; Sat, 8 Sep 2018 17:38:31 +0200 (CEST) Received: (qmail 34528 invoked by uid 500); 8 Sep 2018 15:38:30 -0000 Mailing-List: contact commits-help@camel.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@camel.apache.org Delivered-To: mailing list commits@camel.apache.org Received: (qmail 34329 invoked by uid 99); 8 Sep 2018 15:38:30 -0000 Received: from ec2-52-202-80-70.compute-1.amazonaws.com (HELO gitbox.apache.org) (52.202.80.70) by apache.org (qpsmtpd/0.29) with ESMTP; Sat, 08 Sep 2018 15:38:30 +0000 Received: by gitbox.apache.org (ASF Mail Server at gitbox.apache.org, from userid 33) id DD72E82F9E; Sat, 8 Sep 2018 15:38:29 +0000 (UTC) Date: Sat, 08 Sep 2018 15:38:28 +0000 To: "commits@camel.apache.org" Subject: [camel] 01/04: CAMEL-12114: Have XmlLineNumberParser respecting namespace uri also for anonymous namespaces (without prefix) from its parent+ tag MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit From: davsclaus@apache.org In-Reply-To: <153642110754.19903.8417670250511697391@gitbox.apache.org> References: <153642110754.19903.8417670250511697391@gitbox.apache.org> X-Git-Host: gitbox.apache.org X-Git-Repo: camel X-Git-Refname: refs/heads/master X-Git-Reftype: branch X-Git-Rev: bf416b8de7ab37b5a6bcac7ed0f6695894a48d2f X-Git-NotificationType: diff X-Git-Multimail-Version: 1.5.dev Auto-Submitted: auto-generated Message-Id: <20180908153829.DD72E82F9E@gitbox.apache.org> This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/camel.git commit bf416b8de7ab37b5a6bcac7ed0f6695894a48d2f Author: Claus Ibsen 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 9f6e63a..49088b8 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; @@ -104,6 +111,7 @@ public final class XmlLineNumberParser { private Locator locator; private boolean found; private final Map localNs = new HashMap<>(); + private final Map anonymousNs = new LinkedHashMap<>(); @Override public void setDocumentLocator(final Locator locator) { @@ -148,6 +156,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 values = new ArrayList<>(anonymousNs.values()); + ns = values.get(values.size() - 1); + } + } } if (ns != null) { el = doc.createElementNS(ns, qName); @@ -156,8 +177,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)); + } } String ln = String.valueOf(this.locator.getLineNumber()); @@ -191,6 +214,8 @@ public final class XmlLineNumberParser { closedEl.setUserData(LINE_NUMBER_END, ln, null); closedEl.setUserData(COLUMN_NUMBER_END, cn, 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()); + } +}