You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ws.apache.org by ve...@apache.org on 2014/07/22 00:55:57 UTC

svn commit: r1612425 - in /webservices/axiom/trunk/implementations/axiom-dom/src: main/java/org/apache/axiom/om/impl/dom/ParentNode.java test/java/org/apache/axiom/om/impl/dom/DOMImplementationTest.java

Author: veithen
Date: Mon Jul 21 22:55:57 2014
New Revision: 1612425

URL: http://svn.apache.org/r1612425
Log:
Rewrite DOOM's replaceChild implementation using existing primitives. This also fixes a couple of issues.

Modified:
    webservices/axiom/trunk/implementations/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/ParentNode.java
    webservices/axiom/trunk/implementations/axiom-dom/src/test/java/org/apache/axiom/om/impl/dom/DOMImplementationTest.java

Modified: webservices/axiom/trunk/implementations/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/ParentNode.java
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/implementations/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/ParentNode.java?rev=1612425&r1=1612424&r2=1612425&view=diff
==============================================================================
--- webservices/axiom/trunk/implementations/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/ParentNode.java (original)
+++ webservices/axiom/trunk/implementations/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/ParentNode.java Mon Jul 21 22:55:57 2014
@@ -28,6 +28,7 @@ import org.apache.axiom.om.OMCloneOption
 import org.apache.axiom.om.OMFactory;
 import org.w3c.dom.DOMException;
 import org.w3c.dom.Document;
+import org.w3c.dom.Element;
 import org.w3c.dom.Node;
 
 public abstract class ParentNode extends NodeImpl implements DOMParentNode {
@@ -40,7 +41,7 @@ public abstract class ParentNode extends
     // /
 
     public final Node appendChild(Node newChild) throws DOMException {
-        checkNewChild(newChild);
+        checkNewChild(newChild, null);
         if (newChild instanceof CoreChildNode) {
             coreAppendChild((CoreChildNode)newChild, false);
         } else if (newChild instanceof CoreDocumentFragment) {
@@ -51,7 +52,7 @@ public abstract class ParentNode extends
         return newChild;
     }
 
-    private void checkNewChild(Node newChild) {
+    private void checkNewChild(Node newChild, Node replacedChild) {
         NodeImpl newDomChild = (NodeImpl) newChild;
         
         checkSameOwnerDocument(newDomChild);
@@ -62,7 +63,7 @@ public abstract class ParentNode extends
 
         if (this instanceof Document) {
             if (newDomChild instanceof ElementImpl) {
-                if (((DocumentImpl) this).getOMDocumentElement() != null) {
+                if (!(replacedChild instanceof Element) && ((DocumentImpl) this).getOMDocumentElement() != null) {
                     // Throw exception since there cannot be two document elements
                     throw newDOMException(DOMException.HIERARCHY_REQUEST_ERR);
                 }
@@ -90,7 +91,7 @@ public abstract class ParentNode extends
             if (!(refChild instanceof CoreChildNode && ((CoreChildNode)refChild).coreGetParent() == this)) {
                 throw newDOMException(DOMException.NOT_FOUND_ERR);
             }
-            checkNewChild(newChild);
+            checkNewChild(newChild, null);
             if (newChild instanceof CoreChildNode) {
                 ((CoreChildNode)refChild).coreInsertSiblingBefore((CoreChildNode)newChild);
             } else if (newChild instanceof CoreDocumentFragment) {
@@ -103,81 +104,33 @@ public abstract class ParentNode extends
     }
 
     /** Replaces the oldChild with the newChild. */
-    public final Node replaceChild(Node newChild, Node oldChild) throws DOMException {
-        NodeImpl newDomChild = (NodeImpl) newChild;
-        NodeImpl oldDomChild = (NodeImpl) oldChild;
-
-        if (newChild == null) {
-            throw new IllegalArgumentException("newChild can't be null");
+    public final Node replaceChild(Node newChild, Node _oldChild) throws DOMException {
+        if (!(_oldChild instanceof CoreChildNode)) {
+            throw newDOMException(DOMException.NOT_FOUND_ERR);
         }
-
-        if (isAncestorOrSelf(newChild)) {
-            throw newDOMException(DOMException.HIERARCHY_REQUEST_ERR);
+        CoreChildNode oldChild = (CoreChildNode)_oldChild;
+        if (oldChild.coreGetParent() != this) {
+            throw newDOMException(DOMException.NOT_FOUND_ERR);
         }
-
-        checkSameOwnerDocument(newDomChild);
-
-        NodeImpl tempNode = (NodeImpl)getFirstChild();
-        boolean found = false;
-        while (!found && tempNode != null) {
-            if (tempNode == oldChild) {
-                NodeImpl head; // The first child to insert
-                NodeImpl tail; // The last child to insert
-                
-                if (newChild instanceof DocumentFragmentImpl) {
-                    DocumentFragmentImpl docFrag =
-                            (DocumentFragmentImpl) newDomChild;
-                    head = (NodeImpl)docFrag.getFirstChild();
-                    tail = (NodeImpl)docFrag.getLastChild();
-                    
-                    NodeImpl child = (NodeImpl) docFrag.getFirstChild();
-                    //set the parent of all kids to me
-                    while(child != null) {
-                        child.setParent(this);
-                        child = child.internalGetNextSibling();
-                    }
-
-                    docFrag.coreSetFirstChild(null);
-                    docFrag.coreSetLastChild(null);
-                } else {
-                    head = newDomChild;
-                    tail = newDomChild;
-                    newDomChild.setParent(this);
-                }
-                
-                // We use getNextSibling here to force bulding the node if necessary
-                NodeImpl nextSibling = (NodeImpl)oldDomChild.getNextSibling();
-                NodeImpl previousSibling = oldDomChild.internalGetPreviousSibling();
-                
-                tail.internalSetNextSibling(nextSibling);
-                head.internalSetPreviousSibling(previousSibling);
-                
-                if (previousSibling != null) {
-                    previousSibling.internalSetNextSibling(head);
-                } else {
-                    coreSetFirstChild((CoreChildNode)head);
-                }
-
-                if (nextSibling != null) {
-                    nextSibling.internalSetPreviousSibling(tail);
-                } else {
-                    coreSetLastChild((CoreChildNode)tail);
-                }
-                
-                found = true;
-
-                // remove the old child's references to this tree
-                oldDomChild.internalSetNextSibling(null);
-                oldDomChild.internalSetPreviousSibling(null);
-                oldDomChild.setParent(null);
+        checkNewChild(newChild, _oldChild);
+        CoreChildNode nextSibling = oldChild.coreGetNextSibling();
+        oldChild.coreDetach(coreGetOwnerDocument(true));
+        if (newChild instanceof CoreChildNode) {
+            if (nextSibling == null) {
+                coreAppendChild((CoreChildNode)newChild, false);
+            } else {
+                nextSibling.coreInsertSiblingBefore((CoreChildNode)newChild);
             }
-            tempNode = (NodeImpl)tempNode.getNextSibling();
+        } else if (newChild instanceof CoreDocumentFragment) {
+            if (nextSibling == null) {
+                coreAppendChildren((CoreDocumentFragment)newChild);
+            } else {
+                nextSibling.coreInsertSiblingsBefore((CoreDocumentFragment)newChild);
+            }
+        } else {
+            throw newDOMException(DOMException.HIERARCHY_REQUEST_ERR);
         }
-
-        if (!found)
-            throw newDOMException(DOMException.NOT_FOUND_ERR);
-
-        return oldChild;
+        return _oldChild;
     }
 
     /**

Modified: webservices/axiom/trunk/implementations/axiom-dom/src/test/java/org/apache/axiom/om/impl/dom/DOMImplementationTest.java
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/implementations/axiom-dom/src/test/java/org/apache/axiom/om/impl/dom/DOMImplementationTest.java?rev=1612425&r1=1612424&r2=1612425&view=diff
==============================================================================
--- webservices/axiom/trunk/implementations/axiom-dom/src/test/java/org/apache/axiom/om/impl/dom/DOMImplementationTest.java (original)
+++ webservices/axiom/trunk/implementations/axiom-dom/src/test/java/org/apache/axiom/om/impl/dom/DOMImplementationTest.java Mon Jul 21 22:55:57 2014
@@ -55,8 +55,6 @@ public class DOMImplementationTest exten
         builder.exclude(W3CTestCase.class, "(id=http://www.w3.org/2001/DOM-Test-Suite/level1/core/elementsetattributenomodificationallowederr)");
         builder.exclude(W3CTestCase.class, "(id=http://www.w3.org/2001/DOM-Test-Suite/level1/core/namednodemapremovenameditem)");
         builder.exclude(W3CTestCase.class, "(id=http://www.w3.org/2001/DOM-Test-Suite/level1/core/namednodemapremovenameditemgetvalue)");
-        builder.exclude(W3CTestCase.class, "(id=http://www.w3.org/2001/DOM-Test-Suite/level1/core/nodereplacechildinvalidnodetype)");
-        builder.exclude(W3CTestCase.class, "(id=http://www.w3.org/2001/DOM-Test-Suite/level1/core/nodereplacechildnewchildexists)");
         builder.exclude(W3CTestCase.class, "(id=http://www.w3.org/2001/DOM-Test-Suite/level1/core/processinginstructionsetdatanomodificationallowederr)");
         builder.exclude(W3CTestCase.class, "(id=http://www.w3.org/2001/DOM-Test-Suite/level1/core/hc_documentinvalidcharacterexceptioncreateattribute)");
         builder.exclude(W3CTestCase.class, "(id=http://www.w3.org/2001/DOM-Test-Suite/level1/core/hc_documentinvalidcharacterexceptioncreateattribute1)");
@@ -68,8 +66,6 @@ public class DOMImplementationTest exten
         builder.exclude(W3CTestCase.class, "(id=http://www.w3.org/2001/DOM-Test-Suite/level1/core/hc_elementnormalize2)");
         builder.exclude(W3CTestCase.class, "(id=http://www.w3.org/2001/DOM-Test-Suite/level1/core/hc_elementremoveattributeaftercreate)");
         builder.exclude(W3CTestCase.class, "(id=http://www.w3.org/2001/DOM-Test-Suite/level1/core/hc_elementreplaceattributewithself)");
-        builder.exclude(W3CTestCase.class, "(id=http://www.w3.org/2001/DOM-Test-Suite/level1/core/hc_nodereplacechildinvalidnodetype)");
-        builder.exclude(W3CTestCase.class, "(id=http://www.w3.org/2001/DOM-Test-Suite/level1/core/hc_nodereplacechildnewchildexists)");
         builder.exclude(W3CTestCase.class, "(id=http://www.w3.org/2001/DOM-Test-Suite/level1/core/hc_attrappendchild2)");
         builder.exclude(W3CTestCase.class, "(id=http://www.w3.org/2001/DOM-Test-Suite/level1/core/hc_attrappendchild4)");
         builder.exclude(W3CTestCase.class, "(id=http://www.w3.org/2001/DOM-Test-Suite/level1/core/hc_attrgetvalue2)");
@@ -381,7 +377,6 @@ public class DOMImplementationTest exten
         builder.exclude(W3CTestCase.class, "(id=http://www.w3.org/2001/DOM-Test-Suite/level3/core/nodereplacechild31)");
         builder.exclude(W3CTestCase.class, "(id=http://www.w3.org/2001/DOM-Test-Suite/level3/core/nodereplacechild32)");
         builder.exclude(W3CTestCase.class, "(id=http://www.w3.org/2001/DOM-Test-Suite/level3/core/nodereplacechild35)");
-        builder.exclude(W3CTestCase.class, "(id=http://www.w3.org/2001/DOM-Test-Suite/level3/core/nodereplacechild39)");
         builder.exclude(W3CTestCase.class, "(id=http://www.w3.org/2001/DOM-Test-Suite/level3/core/nodereplacechild40)");
         builder.exclude(W3CTestCase.class, "(id=http://www.w3.org/2001/DOM-Test-Suite/level3/core/nodesettextcontent02)");
         builder.exclude(W3CTestCase.class, "(id=http://www.w3.org/2001/DOM-Test-Suite/level3/core/nodesettextcontent12)");