Updated Branches: refs/heads/camel-2.11.x d5e7d2567 -> aa33f1be1 refs/heads/camel-2.12.x af575cb54 -> aae06ee64 refs/heads/master 1bfb73d27 -> 9542d07c8
CAMEL-6922: Fixed xml converter toDocument to support a fallback as depending on JDK/os the TC lookup may pick method with from type as Node or NodeList. Thanks to Rene Avontuur for patch. Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/09aab55b Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/09aab55b Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/09aab55b Branch: refs/heads/master Commit: 09aab55b0df69133ad0cb0ce707c18d73c1379a7 Parents: 1bfb73d Author: Claus Ibsen <davscl...@apache.org> Authored: Tue Jan 28 16:44:23 2014 +0100 Committer: Claus Ibsen <davscl...@apache.org> Committed: Tue Jan 28 16:44:23 2014 +0100 ---------------------------------------------------------------------- .../camel/converter/jaxp/XmlConverter.java | 10 +++- .../builder/xml/NodeListToDocumentTest.java | 57 ++++++++++++++++++++ 2 files changed, 66 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/09aab55b/camel-core/src/main/java/org/apache/camel/converter/jaxp/XmlConverter.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/converter/jaxp/XmlConverter.java b/camel-core/src/main/java/org/apache/camel/converter/jaxp/XmlConverter.java index ca3df7d..b382ca5 100644 --- a/camel-core/src/main/java/org/apache/camel/converter/jaxp/XmlConverter.java +++ b/camel-core/src/main/java/org/apache/camel/converter/jaxp/XmlConverter.java @@ -690,7 +690,15 @@ public class XmlConverter { */ @Converter(allowNull = true) public Document toDOMDocumentFromSingleNodeList(NodeList nl) throws ParserConfigurationException, TransformerException { - return nl.getLength() == 1 ? toDOMDocument(nl.item(0)) : null; + if (nl.getLength() == 1) { + return toDOMDocument(nl.item(0)); + } else if (nl instanceof Node) { + // as XML parsers may often have nodes that implement both Node and NodeList then the type converter lookup + // may lookup either a type converter from NodeList or Node. So let's fallback and try with Node + return toDOMDocument((Node) nl); + } else { + return null; + } } /** http://git-wip-us.apache.org/repos/asf/camel/blob/09aab55b/camel-core/src/test/java/org/apache/camel/builder/xml/NodeListToDocumentTest.java ---------------------------------------------------------------------- diff --git a/camel-core/src/test/java/org/apache/camel/builder/xml/NodeListToDocumentTest.java b/camel-core/src/test/java/org/apache/camel/builder/xml/NodeListToDocumentTest.java new file mode 100644 index 0000000..b370307 --- /dev/null +++ b/camel-core/src/test/java/org/apache/camel/builder/xml/NodeListToDocumentTest.java @@ -0,0 +1,57 @@ +/** + * 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.builder.xml; + +import org.w3c.dom.Document; +import org.w3c.dom.NodeList; + +//import com.sun.org.apache.xerces.internal.dom.ElementNSImpl; +import org.apache.camel.ContextTestSupport; +import org.apache.camel.Exchange; +import org.junit.Ignore; + +import static org.apache.camel.builder.xml.XPathBuilder.xpath; + +@Ignore("For manual testing CAMEL-6922") +public class NodeListToDocumentTest extends ContextTestSupport { + + @Override + public boolean isUseRouteBuilder() { + return false; + } + + public void testXPathNodeResultToDocument() throws Exception { + // TODO: uses an internal nexus class which can only be tested on some platforms + /* + Object result = xpath("/foo").nodeResult().evaluate(createExchange("<foo><bar>1</bar><bar>2</bar></foo>")); + ElementNSImpl el = assertIsInstanceOf(ElementNSImpl.class, result); + assertNotNull(el); + NodeList nodeList = (NodeList) el; + assertEquals(0, nodeList.getLength()); + Document doc = context.getTypeConverter().convertTo(Document.class, nodeList); + assertNotNull(doc); + assertEquals("foo", doc.getFirstChild().getLocalName()); + */ + } + + protected Exchange createExchange(Object xml) { + Exchange exchange = createExchangeWithBody(context, xml); + exchange.getIn().setHeader("name", "James"); + return exchange; + } + +}