Author: rr
Date: Fri May 7 19:21:38 2010
New Revision: 942185
URL: http://svn.apache.org/viewvc?rev=942185&view=rev
Log:
ODE-663: DOMUtils.cloneNode results in invalid namespace declaration (fix),
thanks to David Carver
Modified:
ode/branches/APACHE_ODE_1.X/utils/src/main/java/org/apache/ode/utils/DOMUtils.java
ode/branches/APACHE_ODE_1.X/utils/src/test/java/org/apache/ode/utils/DOMUtilsTest.java
Modified:
ode/branches/APACHE_ODE_1.X/utils/src/main/java/org/apache/ode/utils/DOMUtils.java
URL:
http://svn.apache.org/viewvc/ode/branches/APACHE_ODE_1.X/utils/src/main/java/org/apache/ode/utils/DOMUtils.java?rev=942185&r1=942184&r2=942185&view=diff
==============================================================================
---
ode/branches/APACHE_ODE_1.X/utils/src/main/java/org/apache/ode/utils/DOMUtils.java
(original)
+++
ode/branches/APACHE_ODE_1.X/utils/src/main/java/org/apache/ode/utils/DOMUtils.java
Fri May 7 19:21:38 2010
@@ -1155,16 +1155,8 @@ public class DOMUtils {
clonedNode = document.createElement(nodeName);
}
}
- // attributes are not treated as child nodes, so copy them
explicitly
- NamedNodeMap attributes = ((Element)
sourceNode).getAttributes();
- for (int i = 0; i < attributes.getLength(); i++) {
- Attr attributeClone = (Attr) cloneNode(document,
attributes.item(i));
- if (attributeClone.getNamespaceURI() == null) {
- ((Element)
clonedNode).setAttributeNode(attributeClone);
- } else {
- ((Element)
clonedNode).setAttributeNodeNS(attributeClone);
- }
- }
+
+ copyAttributes(document, sourceNode, clonedNode);
break;
case Node.ENTITY_NODE:
// TODO
@@ -1203,6 +1195,25 @@ public class DOMUtils {
return clonedNode;
}
+ private static void copyAttributes(Document document, Node sourceNode,
+ Node clonedNode) {
+ // attributes are not treated as child nodes, so copy them explicitly
+ NamedNodeMap attributes = ((Element) sourceNode).getAttributes();
+ for (int i = 0; i < attributes.getLength(); i++) {
+ Node attrNode = attributes.item(i);
+ Document nodeDoc = attrNode.getOwnerDocument();
+ Attr attributeClone = (Attr)attrNode.cloneNode(false);
+ if (nodeDoc != null && !document.equals(nodeDoc)) {
+ attributeClone = (Attr)document.importNode(attributeClone,
false);
+ }
+ if (attributeClone.getNamespaceURI() == null) {
+ ((Element) clonedNode).setAttributeNode(attributeClone);
+ } else {
+ ((Element)
clonedNode).setAttributeNodeNS(attributeClone);
+ }
+ }
+ }
+
/**
* Parse the text in the cloneChild for any embedded prefixes, and define
it in it's parent element
*
Modified:
ode/branches/APACHE_ODE_1.X/utils/src/test/java/org/apache/ode/utils/DOMUtilsTest.java
URL:
http://svn.apache.org/viewvc/ode/branches/APACHE_ODE_1.X/utils/src/test/java/org/apache/ode/utils/DOMUtilsTest.java?rev=942185&r1=942184&r2=942185&view=diff
==============================================================================
---
ode/branches/APACHE_ODE_1.X/utils/src/test/java/org/apache/ode/utils/DOMUtilsTest.java
(original)
+++
ode/branches/APACHE_ODE_1.X/utils/src/test/java/org/apache/ode/utils/DOMUtilsTest.java
Fri May 7 19:21:38 2010
@@ -20,7 +20,11 @@ package org.apache.ode.utils;
import org.apache.ode.utils.TestResources;
+import java.io.ByteArrayInputStream;
import java.io.InputStream;
+import java.util.Map;
+
+import javax.xml.parsers.DocumentBuilderFactory;
import junit.framework.TestCase;
@@ -110,5 +114,36 @@ public class DOMUtilsTest extends TestCa
}
}
}
-
+
+ public void testCloneNode() throws Exception {
+ String testString = "<ns1:parent xmlns:ns1=\"abc\">\n" +
+ " <ns1:child xmlns=\"def\">\n" +
+ " <ns2:nestedChild xmlns:ns2=\"def\"/>\n" +
+ " </ns1:child>\n" +
+ "</ns1:parent>";
+
+ Document doc = DOMUtils.parse(new
ByteArrayInputStream(testString.getBytes()));
+ Node node = doc.getFirstChild();
+ Node clonedNode = DOMUtils.cloneNode(doc, node);
+ String actualString = DOMUtils.domToString(clonedNode).replace("<?xml
version=\"1.0\" encoding=\"UTF-8\"?>\n", "");
+ assertEquals("XML Result", testString, actualString);
+ }
+
+ public void testCloneNodeNewDocument() throws Exception {
+ String testString = "<ns1:parent xmlns:ns1=\"abc\">\n" +
+ " <ns1:child xmlns=\"def\">\n" +
+ " <ns2:nestedChild xmlns:ns2=\"def\"/>\n" +
+ " </ns1:child>\n" +
+ "</ns1:parent>";
+
+ Document doc = DOMUtils.parse(new
ByteArrayInputStream(testString.getBytes()));
+ Document doc2 =
DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
+ Node node = doc.getFirstChild();
+ Node clonedNode = DOMUtils.cloneNode(doc2, node);
+
+ assertNotSame("DOM's are the same", doc, doc2);
+ String actualString = DOMUtils.domToString(clonedNode).replace("<?xml
version=\"1.0\" encoding=\"UTF-8\"?>\n", "");
+ assertEquals("XML Result", testString, actualString);
+
+ }
}