You are viewing a plain text version of this content. The canonical link for it is here.
Posted to wsrf-dev@ws.apache.org by ip...@apache.org on 2005/05/28 15:52:15 UTC

svn commit: r178871 - in /incubator/apollo/trunk/src: java/org/apache/ws/util/XmlBeanUtils.java test/org/apache/ws/util/XmlBeanUtilsTestCase.java

Author: ips
Date: Sat May 28 06:52:12 2005
New Revision: 178871

URL: http://svn.apache.org/viewcvs?rev=178871&view=rev
Log:
fixed the addChildElement() "any" bug!!!!!

Modified:
    incubator/apollo/trunk/src/java/org/apache/ws/util/XmlBeanUtils.java
    incubator/apollo/trunk/src/test/org/apache/ws/util/XmlBeanUtilsTestCase.java

Modified: incubator/apollo/trunk/src/java/org/apache/ws/util/XmlBeanUtils.java
URL: http://svn.apache.org/viewcvs/incubator/apollo/trunk/src/java/org/apache/ws/util/XmlBeanUtils.java?rev=178871&r1=178870&r2=178871&view=diff
==============================================================================
--- incubator/apollo/trunk/src/java/org/apache/ws/util/XmlBeanUtils.java (original)
+++ incubator/apollo/trunk/src/java/org/apache/ws/util/XmlBeanUtils.java Sat May 28 06:52:12 2005
@@ -41,9 +41,10 @@
 import java.util.Map;
 
 /**
- * LOG-DONE Generic utility methods for working with Apache XMLBeans.
+ * Generic utility methods for working with Apache XMLBeans.
  *
- * @author Ian P. Springer, Sal Campana
+ * @author Ian Springer
+ * @author Sal Campana
  */
 public abstract class XmlBeanUtils
 {
@@ -328,45 +329,39 @@
     }
 
     /**
-     * Adds a copy of the specified XmlBean as the last child element of the
-     * specified parent XmlBean.
+     * Adds a copy of the XmlBean <code>newChild</code> as the last child
+     * of the XmlBean <code>parent</code>.
      *
-     * @param parentXBean
-     * @param xBean
+     * @param parent the XmlBean to be added to; must represent an element or a document
+     * @param newChild the XmlBean to be added; must represent an element or a document
      */
-    public static XmlObject addChildElement( XmlObject parentXBean,
-                                             XmlObject xBean )
+    public static XmlObject addChildElement( XmlObject parent,
+                                             XmlObject newChild )
     {
-        parentXBean = getRootElement( parentXBean );
-        xBean = getRootElement( xBean );
-        XmlCursor parentCursor = parentXBean.newCursor();
-        if ( parentCursor.toLastChild() ) // has children
+        parent = getRootElement( parent );
+        Node parentNode = parent.getDomNode();
+        Node newChildNode = newChild.getDomNode(); // no need to use newDomNode() because importNode() will make a copy
+        if ( newChildNode.getNodeType() == Node.DOCUMENT_NODE )
         {
-            parentCursor.toEndToken();
-            parentCursor.toNextToken();
+            newChildNode = ((Document)newChildNode).getDocumentElement();
         }
-        else                              // childless
-        {
-            parentCursor.toEndToken();
-        }
-        parentCursor.insertElement( getName( xBean ) );
-        parentCursor.toPrevSibling();
-        XmlObject childXBean = parentCursor.getObject();
-        parentCursor.dispose();
-        return childXBean.set( xBean );
+        newChildNode = parentNode.getOwnerDocument().importNode( newChildNode, true ); //
+        parentNode.appendChild( newChildNode );
+        return newChild;
     }
 
     /**
-     * Adds an XmlBean with the specified name as the last of the specified parent XmlBean.
+     * Creates a new XmlBean named <code>name</code> and adds it as the
+     * last child of the XmlBean <code>parent</code>.
      *
-     * @param parentXBean
-     * @param name
+     * @param parent the XmlBean to be added to; must represent an element or a document
+     * @param name the name of the new XmlBean to be added
      */
-    public static XmlObject addChildElement( XmlObject parentXBean,
+    public static XmlObject addChildElement( XmlObject parent,
                                              QName name )
     {
-        parentXBean = getRootElement( parentXBean );
-        XmlCursor parentCursor = parentXBean.newCursor();
+        parent = getRootElement( parent );
+        XmlCursor parentCursor = parent.newCursor();
         if ( parentCursor.toLastChild() )  // has children
         {
             parentCursor.toEndToken();
@@ -378,9 +373,9 @@
         }
         parentCursor.insertElement( name );
         parentCursor.toPrevSibling();
-        XmlObject childXBean = parentCursor.getObject();
+        XmlObject newChild = parentCursor.getObject();
         parentCursor.dispose();
-        return childXBean;        
+        return newChild;
     }
 
     /**

Modified: incubator/apollo/trunk/src/test/org/apache/ws/util/XmlBeanUtilsTestCase.java
URL: http://svn.apache.org/viewcvs/incubator/apollo/trunk/src/test/org/apache/ws/util/XmlBeanUtilsTestCase.java?rev=178871&r1=178870&r2=178871&view=diff
==============================================================================
--- incubator/apollo/trunk/src/test/org/apache/ws/util/XmlBeanUtilsTestCase.java (original)
+++ incubator/apollo/trunk/src/test/org/apache/ws/util/XmlBeanUtilsTestCase.java Sat May 28 06:52:12 2005
@@ -71,12 +71,15 @@
     {
         ResourcePropertyValueChangeNotificationType resourcePropertyValueChangeNotificationType = ResourcePropertyValueChangeNotificationType.Factory.newInstance();
         ResourcePropertyValueChangeNotificationType.OldValue oldValue = resourcePropertyValueChangeNotificationType.addNewOldValue();
-        XmlBeanUtils.addChildElement( oldValue, XmlObject.Factory.parse( "<foo/>" ) );
-        XmlObject[] childElems = XmlBeanUtils.getChildElements( oldValue, new QName( "foo" ) );
-        assertEquals( 1, childElems.length );
-        XmlObject fooElem = childElems[0];
-        childElems = XmlBeanUtils.getChildElements( fooElem, new QName( "foo" ) );
-        //assertEquals( 0, childElems.length );  // a bug in XmlBeans causes this assertion to fail
+        XmlBeanUtils.addChildElement( oldValue, XmlObject.Factory.parse( "<fo>fum</fo>" ) );
+        // add a second one, so we can make sure they get added in the right order...
+        XmlBeanUtils.addChildElement( oldValue, XmlObject.Factory.parse( "<fo>nub</fo>" ) );
+        XmlObject[] childElems = XmlBeanUtils.getChildElements( oldValue, new QName( "fo" ) );
+        assertEquals( 2, childElems.length );
+        XmlObject[] grandchildElems = XmlBeanUtils.getChildElements( childElems[0], new QName( "fo" ) );
+        assertEquals( 0, grandchildElems.length );  // this tests the nefarious "redundant grandchild" cursor bug
+        assertEquals( "fum", XmlBeanUtils.getValue( childElems[0] ) );
+        assertEquals( "nub", XmlBeanUtils.getValue( childElems[1] ) );
 
         resourcePropertyValueChangeNotificationType = ResourcePropertyValueChangeNotificationType.Factory.newInstance();
         oldValue = resourcePropertyValueChangeNotificationType.addNewOldValue();



---------------------------------------------------------------------
To unsubscribe, e-mail: apollo-dev-unsubscribe@ws.apache.org
For additional commands, e-mail: apollo-dev-help@ws.apache.org