Author: vanto
Date: Tue May 11 22:58:10 2010
New Revision: 943325
URL: http://svn.apache.org/viewvc?rev=943325&view=rev
Log:
fixing ODE-663. Thanks to Dave 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=943325&r1=943324&r2=943325&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
Tue May 11 22:58:10 2010
@@ -1109,25 +1109,7 @@ public class DOMUtils {
switch (sourceNode.getNodeType()) {
case Node.ATTRIBUTE_NODE:
- if (namespaceURI == null) {
- clonedNode = document.createAttribute(nodeName);
- } else {
- String prefix = ((Attr)
sourceNode).lookupPrefix(namespaceURI);
- // the prefix for the XML namespace can't be looked up,
hence this...
- if (prefix == null &&
namespaceURI.equals(NS_URI_XMLNS)) {
- prefix = "xmlns";
- }
- // if a prefix exists, qualify the name with it
- if (prefix != null && !"".equals(prefix)) {
- nodeName = prefix + ":" + nodeName;
- }
- // create the appropriate type of attribute
- if (prefix != null) {
- clonedNode =
document.createAttributeNS(namespaceURI, nodeName);
- } else {
- clonedNode = document.createAttribute(nodeName);
- }
- }
+ clonedNode = document.importNode(sourceNode, false);
break;
case Node.CDATA_SECTION_NODE:
clonedNode = document.createCDATASection(((CDATASection)
sourceNode).getData());
@@ -1190,13 +1172,16 @@ public class DOMUtils {
NodeList sourceChildren = sourceNode.getChildNodes();
if (sourceChildren != null) {
for (int i = 0; i < sourceChildren.getLength(); i++) {
- Node sourceChild = sourceChildren.item(i);
- Node clonedChild = cloneNode(document, sourceChild);
- clonedNode.appendChild(clonedChild);
+ if (sourceNode.getNodeType() != Node.ATTRIBUTE_NODE) {
+ Node sourceChild = sourceChildren.item(i);
+ Node clonedChild = cloneNode(document, sourceChild);
+ clonedNode.appendChild(clonedChild);
+ }
+
// if the child has a textual value, parse it for any
embedded prefixes
- if (clonedChild.getNodeType() == Node.TEXT_NODE ||
- clonedChild.getNodeType() ==
Node.CDATA_SECTION_NODE) {
- parseEmbeddedPrefixes(sourceNode, clonedNode,
clonedChild);
+ if (sourceChildren.item(i).getNodeType() ==
Node.TEXT_NODE ||
+ sourceChildren.item(i).getNodeType() ==
Node.CDATA_SECTION_NODE) {
+ parseEmbeddedPrefixes(sourceNode, clonedNode,
sourceChildren.item(i));
}
}
}
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=943325&r1=943324&r2=943325&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
Tue May 11 22:58:10 2010
@@ -18,9 +18,18 @@
*/
package org.apache.ode.utils;
+import net.sf.saxon.dom.DocumentBuilderFactoryImpl;
+
import org.apache.ode.utils.TestResources;
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
import java.io.InputStream;
+import java.util.Map;
+
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
import junit.framework.TestCase;
@@ -28,12 +37,29 @@ import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
+import org.xml.sax.SAXException;
/**
* Test the {...@link DOMUtils} class.
*/
public class DOMUtilsTest extends TestCase {
-
+ private static final String SAXON_DOM_DOCUMENT_BUILDER_FACTORY =
"net.sf.saxon.dom.DocumentBuilderFactoryImpl";
+ private static final String DOCUMENT_BUILDER_FACTORY =
"javax.xml.parsers.DocumentBuilderFactory";
+ String defaultBuilderFactory = null;
+
+ @Override
+ protected void setUp() throws Exception {
+ super.setUp();
+ defaultBuilderFactory = System.getProperty(DOCUMENT_BUILDER_FACTORY,
"org.apache.xerces.jaxp.DocumentBuilderFactoryImpl");
+ }
+
+ @Override
+ protected void tearDown() throws Exception {
+ super.tearDown();
+
+ System.setProperty(DOCUMENT_BUILDER_FACTORY, defaultBuilderFactory);
+ }
+
public void testParseInputStream() throws Exception {
Document doc =
DOMUtils.parse(TestResources.getLoanApprovalProcess().openStream());
assertEquals("process", doc.getDocumentElement().getLocalName());
@@ -110,5 +136,94 @@ 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);
+
+ }
+
+ /**
+ * A Saxon's DOM is read only. cloneNode should throw an
UnsupportedOperationException.
+ *
+ * @throws Exception
+ */
+ public void testCloneNodeSaxon() 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 = createSaxonDOM(testString);
+
+ Node node = doc.getFirstChild();
+ try {
+ Node clonedNode = DOMUtils.cloneNode(doc, node);
+ } catch (UnsupportedOperationException ex) {
+
+ }
+
+ }
+
+ public void testCloneNodeNewDocumentSaxon() 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>";
+
+ String saxonString = "<ns1:parent xmlns:ns1=\"abc\">\n" +
+ " <ns1:child xmlns=\"def\">\n" +
+ " <nestedChild xmlns:ns2=\"def\"/>\n" +
+ " </ns1:child>\n" +
+ "</ns1:parent>";
+
+ Document doc = createSaxonDOM(testString);
+ Document doc2 = DOMUtils.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", saxonString, actualString);
+
+ }
+ private Document createSaxonDOM(String testString)
+ throws ParserConfigurationException, SAXException, IOException {
+ System.setProperty(DOCUMENT_BUILDER_FACTORY,
SAXON_DOM_DOCUMENT_BUILDER_FACTORY);
+ DocumentBuilderFactory factory =
DocumentBuilderFactoryImpl.newInstance();
+ factory.setNamespaceAware(true);
+ DocumentBuilder saxonBuilder = factory.newDocumentBuilder();
+ Document doc = saxonBuilder.parse(new
ByteArrayInputStream(testString.getBytes()));
+ return doc;
+ }
+
}