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 2012/07/16 14:22:31 UTC

svn commit: r1361989 - in /webservices/commons/trunk/modules/axiom/modules: axiom-api/src/main/java/org/apache/axiom/om/ axiom-api/src/main/java/org/apache/axiom/om/impl/builder/ axiom-common-impl/src/main/java/org/apache/axiom/om/impl/common/ axiom-do...

Author: veithen
Date: Mon Jul 16 12:22:31 2012
New Revision: 1361989

URL: http://svn.apache.org/viewvc?rev=1361989&view=rev
Log:
Added a removeChildren method to OMContainer.

Added:
    webservices/commons/trunk/modules/axiom/modules/axiom-common-impl/src/main/java/org/apache/axiom/om/impl/common/IElement.java   (with props)
    webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/document/TestRemoveChildren.java   (with props)
    webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/element/TestRemoveChildren.java   (with props)
    webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/sourcedelement/TestRemoveChildrenUnexpanded.java   (with props)
Modified:
    webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/OMContainer.java
    webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/OMXMLParserWrapper.java
    webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/builder/StAXBuilder.java
    webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/builder/StAXOMBuilder.java
    webservices/commons/trunk/modules/axiom/modules/axiom-common-impl/src/main/java/org/apache/axiom/om/impl/common/OMContainerHelper.java
    webservices/commons/trunk/modules/axiom/modules/axiom-common-impl/src/main/java/org/apache/axiom/om/impl/common/OMElementImplUtil.java
    webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/DocumentFragmentImpl.java
    webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/DocumentImpl.java
    webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/ElementImpl.java
    webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMDocumentImpl.java
    webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMElementImpl.java
    webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMSourcedElementImpl.java
    webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/OMTestSuiteBuilder.java

Modified: webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/OMContainer.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/OMContainer.java?rev=1361989&r1=1361988&r2=1361989&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/OMContainer.java (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/OMContainer.java Mon Jul 16 12:22:31 2012
@@ -127,6 +127,21 @@ public interface OMContainer extends OMS
     OMNode getFirstOMChild();
 
     /**
+     * Remove all children from this container. This method has the same effect as the following
+     * code:
+     * 
+     * <pre>
+     * for (Iterator it = container.getChildren(); it.hasNext(); ) {
+     *     it.next();
+     *     it.remove();
+     * }</pre>
+     * 
+     * However, the implementation may do this in an optimized way. In particular, if the node is
+     * incomplete, it may choose not to instantiate child node that would become unreachable anyway.
+     */
+    void removeChildren();
+    
+    /**
      * Serialize the node with caching enabled.
      * <p>
      * This method will always serialize the infoset as plain XML. In particular, any {@link OMText}

Modified: webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/OMXMLParserWrapper.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/OMXMLParserWrapper.java?rev=1361989&r1=1361988&r2=1361989&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/OMXMLParserWrapper.java (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/OMXMLParserWrapper.java Mon Jul 16 12:22:31 2012
@@ -32,12 +32,7 @@ public interface OMXMLParserWrapper {
     int next() throws OMException;
 
     /**
-     * Discards the current element. This will remove the given element and its descendants.
-     *
-     * @param el
-     * @throws OMException
-     *
-     * @throws OMException
+     * @deprecated Use {@link OMNode#discard()} to discard elements.
      */
     void discard(OMElement el) throws OMException;
 

Modified: webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/builder/StAXBuilder.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/builder/StAXBuilder.java?rev=1361989&r1=1361988&r2=1361989&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/builder/StAXBuilder.java (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/builder/StAXBuilder.java Mon Jul 16 12:22:31 2012
@@ -40,6 +40,7 @@ import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
 import javax.xml.namespace.QName;
+import javax.xml.stream.XMLStreamConstants;
 import javax.xml.stream.XMLStreamException;
 import javax.xml.stream.XMLStreamReader;
 
@@ -272,48 +273,60 @@ public abstract class StAXBuilder implem
         }
     }
 
-    /**
-     * Method discard.
-     *
-     * @param element
-     * @throws OMException
-     */
+    // For compatibility only
     public void discard(OMElement element) throws OMException {
+        discard((OMContainer)element);
+        element.discard();
+    }
+    
+    public void discard(OMContainer container) throws OMException {
 
 //        if (element.isComplete() || !cache) {
 //            throw new OMException();
 //        }
         try {
 
-            // Calculate the depth of the element to be discarded. This determines how many
-            // END_ELEMENT events we need to consume.
-            int targetDepth = elementLevel-1;
-            OMContainerEx current = target;
-            while (current != element) {
-                if (current instanceof OMElement) {
-                    targetDepth--;
-                    current = (OMContainerEx)((OMElement)current).getParent();
-                } else {
-                    throw new OMException("Called discard for an element that is not being built by this builder");
+            if (container instanceof OMDocument) {
+                if (container != document) {
+                    throw new OMException("Called discard for a document that is not being built by this builder");
+                }
+                while (parserNext() != XMLStreamConstants.END_DOCUMENT) {
+                    // Just loop
+                }
+            } else {
+                // Calculate the depth of the element to be discarded. This determines how many
+                // END_ELEMENT events we need to consume.
+                int targetDepth = elementLevel-1;
+                OMContainerEx current = target;
+                while (current != container) {
+                    if (current instanceof OMElement) {
+                        targetDepth--;
+                        current = (OMContainerEx)((OMElement)current).getParent();
+                    } else {
+                        throw new OMException("Called discard for an element that is not being built by this builder");
+                    }
+                }
+                while (elementLevel > targetDepth) {
+                    parserNext();
                 }
-            }
-            
-            while (elementLevel > targetDepth) {
-                parserNext();
             }
 
             // Mark nodes as discarded
-            current = target;
+            OMContainerEx current = target;
             while (true) {
                 current.discarded();
-                if (current == element) {
+                if (current == container) {
                     break;
                 }
                 current = (OMContainerEx)((OMElement)current).getParent();
             }
             
-            target = (OMContainerEx)element.getParent();
-            element.detach();
+            if (container instanceof OMDocument) {
+                target = null;
+                done = true;
+            } else {
+                target = (OMContainerEx)((OMElement)container).getParent();
+            }
         } catch (XMLStreamException e) {
             throw new OMException(e);
         } 

Modified: webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/builder/StAXOMBuilder.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/builder/StAXOMBuilder.java?rev=1361989&r1=1361988&r2=1361989&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/builder/StAXOMBuilder.java (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/builder/StAXOMBuilder.java Mon Jul 16 12:22:31 2012
@@ -238,6 +238,7 @@ public class StAXOMBuilder extends StAXB
                     case XMLStreamConstants.END_DOCUMENT:
                         done = true;
                         ((OMContainerEx) this.document).setComplete(true);
+                        target = null;
                         break;
                     case XMLStreamConstants.SPACE:
                         try {

Added: webservices/commons/trunk/modules/axiom/modules/axiom-common-impl/src/main/java/org/apache/axiom/om/impl/common/IElement.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-common-impl/src/main/java/org/apache/axiom/om/impl/common/IElement.java?rev=1361989&view=auto
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-common-impl/src/main/java/org/apache/axiom/om/impl/common/IElement.java (added)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-common-impl/src/main/java/org/apache/axiom/om/impl/common/IElement.java Mon Jul 16 12:22:31 2012
@@ -0,0 +1,25 @@
+/*
+ * 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.axiom.om.impl.common;
+
+import org.apache.axiom.om.impl.OMElementEx;
+
+public interface IElement extends OMElementEx, IContainer, IChildNode {
+
+}

Propchange: webservices/commons/trunk/modules/axiom/modules/axiom-common-impl/src/main/java/org/apache/axiom/om/impl/common/IElement.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: webservices/commons/trunk/modules/axiom/modules/axiom-common-impl/src/main/java/org/apache/axiom/om/impl/common/OMContainerHelper.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-common-impl/src/main/java/org/apache/axiom/om/impl/common/OMContainerHelper.java?rev=1361989&r1=1361988&r2=1361989&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-common-impl/src/main/java/org/apache/axiom/om/impl/common/OMContainerHelper.java (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-common-impl/src/main/java/org/apache/axiom/om/impl/common/OMContainerHelper.java Mon Jul 16 12:22:31 2012
@@ -175,4 +175,31 @@ public final class OMContainerHelper {
         }
         return firstChild;
     }
+    
+    public static void removeChildren(IContainer that) {
+        boolean updateState;
+        if (that.getState() == IParentNode.INCOMPLETE && that.getBuilder() != null) {
+            OMNode lastKnownChild = that.getLastKnownOMChild();
+            if (lastKnownChild != null) {
+                lastKnownChild.build();
+            }
+            ((StAXOMBuilder)that.getBuilder()).discard(that);
+            updateState = true;
+        } else {
+            updateState = false;
+        }
+        IChildNode child = (IChildNode)that.getFirstOMChildIfAvailable();
+        while (child != null) {
+            IChildNode nextSibling = (IChildNode)child.getNextOMSiblingIfAvailable();
+            child.setPreviousOMSibling(null);
+            child.setNextOMSibling(null);
+            child.setParent(null);
+            child = nextSibling;
+        }
+        that.setFirstChild(null);
+        that.setLastChild(null);
+        if (updateState) {
+            that.setComplete(true);
+        }
+    }
 }

Modified: webservices/commons/trunk/modules/axiom/modules/axiom-common-impl/src/main/java/org/apache/axiom/om/impl/common/OMElementImplUtil.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-common-impl/src/main/java/org/apache/axiom/om/impl/common/OMElementImplUtil.java?rev=1361989&r1=1361988&r2=1361989&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-common-impl/src/main/java/org/apache/axiom/om/impl/common/OMElementImplUtil.java (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-common-impl/src/main/java/org/apache/axiom/om/impl/common/OMElementImplUtil.java Mon Jul 16 12:22:31 2012
@@ -30,12 +30,14 @@ import javax.xml.namespace.NamespaceCont
 import javax.xml.stream.XMLStreamException;
 import javax.xml.stream.XMLStreamReader;
 
+import org.apache.axiom.om.OMContainer;
 import org.apache.axiom.om.OMElement;
 import org.apache.axiom.om.OMException;
 import org.apache.axiom.om.OMNamespace;
 import org.apache.axiom.om.OMNode;
 import org.apache.axiom.om.OMSourcedElement;
 import org.apache.axiom.om.OMText;
+import org.apache.axiom.om.impl.builder.StAXOMBuilder;
 import org.apache.axiom.util.namespace.MapBasedNamespaceContext;
 import org.apache.axiom.util.stax.XMLStreamReaderUtils;
 
@@ -144,4 +146,11 @@ public class OMElementImplUtil {
             throw new OMException(ex);
         }
     }
+    
+    public static void discard(IElement that) {
+        if (that.getState() == IParentNode.INCOMPLETE && that.getBuilder() != null) {
+            ((StAXOMBuilder)that.getBuilder()).discard((OMContainer)that);
+        }
+        that.detach();
+    }
 }

Modified: webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/DocumentFragmentImpl.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/DocumentFragmentImpl.java?rev=1361989&r1=1361988&r2=1361989&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/DocumentFragmentImpl.java (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/DocumentFragmentImpl.java Mon Jul 16 12:22:31 2012
@@ -23,6 +23,7 @@ import org.apache.axiom.om.OMCloneOption
 import org.apache.axiom.om.OMFactory;
 import org.apache.axiom.om.OMXMLParserWrapper;
 import org.apache.axiom.om.impl.common.IContainer;
+import org.apache.axiom.om.impl.common.OMContainerHelper;
 import org.w3c.dom.DocumentFragment;
 import org.w3c.dom.Node;
 
@@ -115,4 +116,9 @@ public class DocumentFragmentImpl extend
     public final Node getNextSibling() {
         return null;
     }
+
+    // TODO: we really shouldn't implement IContainer, but only IParentNode
+    public void removeChildren() {
+        OMContainerHelper.removeChildren(this);
+    }
 }

Modified: webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/DocumentImpl.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/DocumentImpl.java?rev=1361989&r1=1361988&r2=1361989&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/DocumentImpl.java (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/DocumentImpl.java Mon Jul 16 12:22:31 2012
@@ -625,4 +625,8 @@ public class DocumentImpl extends RootNo
     public final Node getNextSibling() {
         return null;
     }
+    
+    public final void removeChildren() {
+        OMContainerHelper.removeChildren(this);
+    }
 }

Modified: webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/ElementImpl.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/ElementImpl.java?rev=1361989&r1=1361988&r2=1361989&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/ElementImpl.java (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/ElementImpl.java Mon Jul 16 12:22:31 2012
@@ -28,9 +28,7 @@ import org.apache.axiom.om.OMFactory;
 import org.apache.axiom.om.OMNamespace;
 import org.apache.axiom.om.OMNode;
 import org.apache.axiom.om.OMXMLParserWrapper;
-import org.apache.axiom.om.impl.OMElementEx;
-import org.apache.axiom.om.impl.common.IChildNode;
-import org.apache.axiom.om.impl.common.IContainer;
+import org.apache.axiom.om.impl.common.IElement;
 import org.apache.axiom.om.impl.common.IParentNode;
 import org.apache.axiom.om.impl.common.NamespaceIterator;
 import org.apache.axiom.om.impl.common.OMChildElementIterator;
@@ -65,8 +63,8 @@ import java.util.HashMap;
 import java.util.Iterator;
 
 /** Implementation of the org.w3c.dom.Element and org.apache.axiom.om.Element interfaces. */
-public class ElementImpl extends ParentNode implements Element, OMElementEx, IChildNode, NamedNode,
-        OMConstants, IContainer {
+public class ElementImpl extends ParentNode implements Element, IElement, NamedNode,
+        OMConstants {
 
     private static final Log log = LogFactory.getLog(ElementImpl.class);
     
@@ -1217,11 +1215,7 @@ public class ElementImpl extends ParentN
     }
 
     public void discard() throws OMException {
-        if (state == COMPLETE) {
-            this.detach();
-        } else {
-            builder.discard(this);
-        }
+        OMElementImplUtil.discard(this);
     }
 
     /*
@@ -1381,4 +1375,8 @@ public class ElementImpl extends ParentN
     public final IParentNode getIParentNode() {
         return parentNode();
     }
+    
+    public final void removeChildren() {
+        OMContainerHelper.removeChildren(this);
+    }
 }

Modified: webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMDocumentImpl.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMDocumentImpl.java?rev=1361989&r1=1361988&r2=1361989&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMDocumentImpl.java (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMDocumentImpl.java Mon Jul 16 12:22:31 2012
@@ -334,4 +334,8 @@ public class OMDocumentImpl extends OMSe
     public void build() {
         OMContainerHelper.build(this);
     }
+    
+    public void removeChildren() {
+        OMContainerHelper.removeChildren(this);
+    }
 }

Modified: webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMElementImpl.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMElementImpl.java?rev=1361989&r1=1361988&r2=1361989&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMElementImpl.java (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMElementImpl.java Mon Jul 16 12:22:31 2012
@@ -30,9 +30,9 @@ import org.apache.axiom.om.OMNamespace;
 import org.apache.axiom.om.OMNode;
 import org.apache.axiom.om.OMXMLParserWrapper;
 import org.apache.axiom.om.OMXMLStreamReaderConfiguration;
-import org.apache.axiom.om.impl.OMElementEx;
 import org.apache.axiom.om.impl.OMNodeEx;
 import org.apache.axiom.om.impl.common.IContainer;
+import org.apache.axiom.om.impl.common.IElement;
 import org.apache.axiom.om.impl.common.NamespaceIterator;
 import org.apache.axiom.om.impl.common.OMChildElementIterator;
 import org.apache.axiom.om.impl.common.OMChildrenLegacyQNameIterator;
@@ -68,7 +68,7 @@ import java.util.LinkedHashMap;
 
 /** Class OMElementImpl */
 public class OMElementImpl extends OMNodeImpl
-        implements OMElementEx, OMConstants, IContainer {
+        implements IElement, OMConstants {
 
     private static final Log log = LogFactory.getLog(OMElementImpl.class);
     
@@ -915,11 +915,7 @@ public class OMElementImpl extends OMNod
      * @throws OMException
      */
     public void discard() throws OMException {
-        if (state == COMPLETE || builder == null) {
-            this.detach();
-        } else {
-            builder.discard(this);
-        }
+        OMElementImplUtil.discard(this);
     }
 
     public QName resolveQName(String qname) {
@@ -1013,5 +1009,9 @@ public class OMElementImpl extends OMNod
     public SAXSource getSAXSource(boolean cache) {
         return new OMSource(this);
     }
+    
+    public void removeChildren() {
+        OMContainerHelper.removeChildren(this);
+    }
 }
 

Modified: webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMSourcedElementImpl.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMSourcedElementImpl.java?rev=1361989&r1=1361988&r2=1361989&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMSourcedElementImpl.java (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMSourcedElementImpl.java Mon Jul 16 12:22:31 2012
@@ -1120,4 +1120,14 @@ public class OMSourcedElementImpl extend
             return ((OMDataSourceExt)dataSource).getObject();
         }
     }
+
+    public void removeChildren() {
+        // One might think that if the element is not expanded, we don't need to expand it because
+        // we are going to remove the children anyway. However, this is not true for two reasons:
+        //  * The element may have attributes and they must be available after removeChildren().
+        //  * The local name, namespace URI and/or prefix of the element may be unknown. In that
+        //    case, we need to expand the element to make this information available.
+        forceExpand();
+        super.removeChildren();
+    }
 }

Modified: webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/OMTestSuiteBuilder.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/OMTestSuiteBuilder.java?rev=1361989&r1=1361988&r2=1361989&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/OMTestSuiteBuilder.java (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/OMTestSuiteBuilder.java Mon Jul 16 12:22:31 2012
@@ -111,6 +111,10 @@ public class OMTestSuiteBuilder extends 
         addTest(new org.apache.axiom.ts.om.document.TestGetOMDocumentElementAfterDetach(metaFactory));
         addTest(new org.apache.axiom.ts.om.document.TestGetOMDocumentElementWithParser(metaFactory));
         addTest(new org.apache.axiom.ts.om.document.TestIsCompleteAfterAddingIncompleteChild(metaFactory));
+        addTest(new org.apache.axiom.ts.om.document.TestRemoveChildren(metaFactory, true, false));
+        addTest(new org.apache.axiom.ts.om.document.TestRemoveChildren(metaFactory, true, true));
+        addTest(new org.apache.axiom.ts.om.document.TestRemoveChildren(metaFactory, false, false));
+        addTest(new org.apache.axiom.ts.om.document.TestRemoveChildren(metaFactory, false, true));
         addTest(new org.apache.axiom.ts.om.document.TestSerializeAndConsume(metaFactory));
         addTest(new org.apache.axiom.ts.om.document.TestSerializeAndConsumeWithIncompleteDescendant(metaFactory));
         addTest(new org.apache.axiom.ts.om.document.TestSetOMDocumentElementNew(metaFactory));
@@ -236,6 +240,8 @@ public class OMTestSuiteBuilder extends 
         addTest(new org.apache.axiom.ts.om.element.TestMultipleDefaultNS(metaFactory));
         addTest(new org.apache.axiom.ts.om.element.TestRemoveAttribute(metaFactory));
         addTest(new org.apache.axiom.ts.om.element.TestRemoveAttributeNotOwner(metaFactory));
+        addTest(new org.apache.axiom.ts.om.element.TestRemoveChildren(metaFactory, true));
+        addTest(new org.apache.axiom.ts.om.element.TestRemoveChildren(metaFactory, false));
         addTest(new org.apache.axiom.ts.om.element.TestResolveQNameWithDefaultNamespace(metaFactory));
         addTest(new org.apache.axiom.ts.om.element.TestResolveQNameWithNonDefaultNamespace(metaFactory));
         addTest(new org.apache.axiom.ts.om.element.TestResolveQNameWithoutNamespace(metaFactory));
@@ -386,6 +392,7 @@ public class OMTestSuiteBuilder extends 
             addTest(new org.apache.axiom.ts.om.sourcedelement.TestName4DefaultPrefix(metaFactory));
             addTest(new org.apache.axiom.ts.om.sourcedelement.TestName4QualifiedPrefix(metaFactory));
             addTest(new org.apache.axiom.ts.om.sourcedelement.TestName4Unqualified(metaFactory));
+            addTest(new org.apache.axiom.ts.om.sourcedelement.TestRemoveChildrenUnexpanded(metaFactory));
             addTest(new org.apache.axiom.ts.om.sourcedelement.TestSerializeAndConsumeToStream(metaFactory));
             addTest(new org.apache.axiom.ts.om.sourcedelement.TestSerializeAndConsumeToWriter(metaFactory));
             addTest(new org.apache.axiom.ts.om.sourcedelement.TestSerializeAndConsumeToXMLWriter(metaFactory));

Added: webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/document/TestRemoveChildren.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/document/TestRemoveChildren.java?rev=1361989&view=auto
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/document/TestRemoveChildren.java (added)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/document/TestRemoveChildren.java Mon Jul 16 12:22:31 2012
@@ -0,0 +1,86 @@
+/*
+ * 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.axiom.ts.om.document;
+
+import java.io.StringReader;
+import java.io.StringWriter;
+
+import org.apache.axiom.om.OMContainer;
+import org.apache.axiom.om.OMDocument;
+import org.apache.axiom.om.OMElement;
+import org.apache.axiom.om.OMFactory;
+import org.apache.axiom.om.OMMetaFactory;
+import org.apache.axiom.om.OMProcessingInstruction;
+import org.apache.axiom.om.OMXMLBuilderFactory;
+import org.apache.axiom.ts.AxiomTestCase;
+import org.custommonkey.xmlunit.XMLAssert;
+
+/**
+ * Tests {@link OMContainer#removeChildren()} on an {@link OMContainer}.
+ */
+public class TestRemoveChildren extends AxiomTestCase {
+    private final boolean complete;
+    private final boolean accessDocumentElement;
+
+    public TestRemoveChildren(OMMetaFactory metaFactory, boolean complete, boolean accessDocumentElement) {
+        super(metaFactory);
+        this.complete = complete;
+        this.accessDocumentElement = accessDocumentElement;
+        addTestProperty("complete", Boolean.toString(complete));
+        addTestProperty("accessDocumentElement", Boolean.toString(complete));
+    }
+
+    protected void runTest() throws Throwable {
+        OMFactory factory = metaFactory.getOMFactory();
+        OMDocument document = OMXMLBuilderFactory.createOMBuilder(factory,
+                new StringReader("<?pi data?><root>text</root>")).getDocument();
+        if (complete) {
+            document.build();
+        }
+        OMProcessingInstruction firstChild = (OMProcessingInstruction)document.getFirstOMChild();
+        OMElement documentElement;
+        if (accessDocumentElement) {
+            documentElement = document.getOMDocumentElement();
+            assertEquals(complete, documentElement.isComplete());
+        } else {
+            documentElement = null;
+        }
+        document.removeChildren();
+        // Test that the child has been detached correctly.
+        assertNull(firstChild.getParent());
+        assertNull(firstChild.getPreviousOMSibling());
+        assertNull(firstChild.getNextOMSibling());
+        if (documentElement != null) {
+            // Test that the child has been detached correctly.
+            assertNull(documentElement.getParent());
+            assertNull(documentElement.getPreviousOMSibling());
+            assertNull(documentElement.getNextOMSibling());
+            // Test that we can still get the content of the document element.
+            assertEquals("text", documentElement.getText());
+        }
+        // Test that the document is now empty.
+        assertNull(document.getFirstOMChild());
+        // Check that the document is in a clean state and that we are able to add
+        // new children.
+        document.addChild(factory.createOMElement("newroot", null));
+        StringWriter out = new StringWriter();
+        document.serialize(out);
+        XMLAssert.assertXMLEqual("<newroot/>", out.toString());
+    }
+}

Propchange: webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/document/TestRemoveChildren.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/element/TestRemoveChildren.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/element/TestRemoveChildren.java?rev=1361989&view=auto
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/element/TestRemoveChildren.java (added)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/element/TestRemoveChildren.java Mon Jul 16 12:22:31 2012
@@ -0,0 +1,68 @@
+/*
+ * 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.axiom.ts.om.element;
+
+import java.io.StringReader;
+
+import org.apache.axiom.om.OMContainer;
+import org.apache.axiom.om.OMElement;
+import org.apache.axiom.om.OMFactory;
+import org.apache.axiom.om.OMMetaFactory;
+import org.apache.axiom.om.OMXMLBuilderFactory;
+import org.apache.axiom.ts.AxiomTestCase;
+import org.custommonkey.xmlunit.XMLAssert;
+
+/**
+ * Tests {@link OMContainer#removeChildren()} on an {@link OMElement}.
+ */
+public class TestRemoveChildren extends AxiomTestCase {
+    private final boolean complete;
+    
+    public TestRemoveChildren(OMMetaFactory metaFactory, boolean complete) {
+        super(metaFactory);
+        this.complete = complete;
+        addTestProperty("complete", Boolean.toString(complete));
+    }
+
+    protected void runTest() throws Throwable {
+        OMFactory factory = metaFactory.getOMFactory();
+        OMElement element = OMXMLBuilderFactory.createOMBuilder(factory,
+                new StringReader("<root><a>A</a><b>B</b></root>")).getDocumentElement();
+        if (complete) {
+            element.build();
+        }
+        OMElement firstChild = (OMElement)element.getFirstOMChild();
+        assertEquals(complete, element.isComplete());
+        assertEquals(complete, firstChild.isComplete());
+        element.removeChildren();
+        // We still need to be able to get the content of the child we retrieved before
+        // calling removeChildren.
+        assertEquals("A", firstChild.getText());
+        // Test that the child has been detached correctly.
+        assertNull(firstChild.getParent());
+        assertNull(firstChild.getPreviousOMSibling());
+        assertNull(firstChild.getNextOMSibling());
+        // Test that the element is now empty.
+        assertNull(element.getFirstOMChild());
+        // Check that the element is in a clean state and that we are able to add
+        // new children.
+        element.addChild(factory.createOMElement("c", null));
+        XMLAssert.assertXMLEqual("<root><c/></root>", element.toString());
+    }
+}

Propchange: webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/element/TestRemoveChildren.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/sourcedelement/TestRemoveChildrenUnexpanded.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/sourcedelement/TestRemoveChildrenUnexpanded.java?rev=1361989&view=auto
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/sourcedelement/TestRemoveChildrenUnexpanded.java (added)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/sourcedelement/TestRemoveChildrenUnexpanded.java Mon Jul 16 12:22:31 2012
@@ -0,0 +1,56 @@
+/*
+ * 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.axiom.ts.om.sourcedelement;
+
+import java.util.Iterator;
+
+import org.apache.axiom.om.OMAttribute;
+import org.apache.axiom.om.OMContainer;
+import org.apache.axiom.om.OMFactory;
+import org.apache.axiom.om.OMMetaFactory;
+import org.apache.axiom.om.OMSourcedElement;
+import org.apache.axiom.om.ds.ByteArrayDataSource;
+import org.apache.axiom.ts.AxiomTestCase;
+
+/**
+ * Tests {@link OMContainer#removeChildren()} on an {@link OMSourcedElement} that is not expanded.
+ * In this case the sourced element needs to be expanded to build any attributes present on the
+ * element and to ensure that the information about the name of the element is complete.
+ */
+public class TestRemoveChildrenUnexpanded extends AxiomTestCase {
+    public TestRemoveChildrenUnexpanded(OMMetaFactory metaFactory) {
+        super(metaFactory);
+    }
+
+    protected void runTest() throws Throwable {
+        OMFactory factory = metaFactory.getOMFactory();
+        OMSourcedElement element = factory.createOMElement(new ByteArrayDataSource(
+                "<element attr='value'><a/></element>".getBytes("utf-8"), "utf-8"));
+        element.removeChildren();
+        // Check that the attribute has been added
+        Iterator it = element.getAllAttributes();
+        assertTrue(it.hasNext());
+        OMAttribute attr = (OMAttribute)it.next();
+        assertEquals("attr", attr.getLocalName());
+        assertEquals("value", attr.getAttributeValue());
+        assertFalse(it.hasNext());
+        // Check that the element is empty
+        assertNull(element.getFirstOMChild());
+    }
+}

Propchange: webservices/commons/trunk/modules/axiom/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/sourcedelement/TestRemoveChildrenUnexpanded.java
------------------------------------------------------------------------------
    svn:eol-style = native