You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commons-dev@ws.apache.org by sc...@apache.org on 2006/08/21 19:11:44 UTC

svn commit: r433288 - in /webservices/commons/trunk/modules/axiom/modules: axiom-api/src/main/java/org/apache/axiom/om/impl/serialize/ axiom-api/src/main/java/org/apache/axiom/om/impl/util/ axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/ axiom-...

Author: scheu
Date: Mon Aug 21 10:11:43 2006
New Revision: 433288

URL: http://svn.apache.org/viewvc?rev=433288&view=rev
Log:
WSCOMMONS-74
Fix OM Serialization to prevent unnecessary namespace declaration duplication
Contributor:Rich Scheuerle

Added:
    webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/om/SerializationTest.java
Modified:
    webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/serialize/StreamingOMSerializer.java
    webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/util/OMSerializerUtil.java
    webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMElementImpl.java

Modified: webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/serialize/StreamingOMSerializer.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/serialize/StreamingOMSerializer.java?rev=433288&r1=433287&r2=433288&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/serialize/StreamingOMSerializer.java (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/serialize/StreamingOMSerializer.java Mon Aug 21 10:11:43 2006
@@ -114,22 +114,21 @@
     	// Note: To serialize the start tag, we must follow the order dictated by the JSR-173 (StAX) specification.
     	// Please keep this code in sync with the code in OMSerializerUtil.serializeStartpart
     
-        // The algorithm is:
+    	// The algorithm is:
         // ... generate setPrefix/setDefaultNamespace for each namespace declaration if the prefix is unassociated.
     	// ... generate setPrefix/setDefaultNamespace if the prefix of the element is unassociated
     	// ... generate setPrefix/setDefaultNamespace for each unassociated prefix of the attributes.
     	//
     	// ... generate writeStartElement (See NOTE_A)
     	//
-    	// ... generate writeNamespace/writerDefaultNamespace for each namespace declaration on the element
-    	// ... generate writeNamespace/writeDefaultNamespace for any new "autogen" namespace/prefixes
-    	// ... generate writeAttribute for each attribute	
+    	// ... generate writeNamespace/writerDefaultNamespace for the new namespace declarations determine during the "set" processing
+    	// ... generate writeAttribute for each attribute
     	
     	// NOTE_A: To confuse matters, some StAX vendors (including woodstox), believe that the setPrefix bindings 
     	// should occur after the writeStartElement.  If this is the case, the writeStartElement is generated first.
     	
-    	ArrayList  prefixList = null;
-    	ArrayList  nsList = null;
+    	ArrayList  writePrefixList = null;
+    	ArrayList  writeNSList = null;
     	
 
     	// Get the prefix and namespace of the element.  "" and null are identical.
@@ -160,26 +159,37 @@
         	String namespace = reader.getNamespaceURI(i);
         	namespace = (namespace != null && namespace.length() == 0) ? null : namespace;
         	
-        	generateSetPrefix(prefix, namespace, writer);
+        	String newPrefix = generateSetPrefix(prefix, namespace, writer);
+        	// If this is a new association, remember it so that it can written out later
+        	if (newPrefix != null) {
+        		if (writePrefixList == null) {
+        			writePrefixList= new ArrayList();
+        			writeNSList = new ArrayList();
+        		}
+        		if (!writePrefixList.contains(newPrefix)) {
+        			writePrefixList.add(newPrefix);
+        			writeNSList.add(namespace);
+        		}
+        	}
         }
         
         // Generate setPrefix for the element
         // If the prefix is not associated with a namespace yet, remember it so that we can
     	// write out a namespace declaration
     	String newPrefix = generateSetPrefix(ePrefix, eNamespace, writer);
-    	
+    	// If this is a new association, remember it so that it can written out later
     	if (newPrefix != null) {
-    		if (prefixList == null) {
-    			prefixList= new ArrayList();
-    			nsList = new ArrayList();
+    		if (writePrefixList == null) {
+    			writePrefixList= new ArrayList();
+    			writeNSList = new ArrayList();
     		}
-    		if (!prefixList.contains(newPrefix)) {
-    			prefixList.add(newPrefix);
-    			nsList.add(eNamespace);
+    		if (!writePrefixList.contains(newPrefix)) {
+    			writePrefixList.add(newPrefix);
+    			writeNSList.add(eNamespace);
     		}
     	}
     
-        // Now write the namespaces for each attribute
+        // Now Generate setPrefix for each attribute
         count = reader.getAttributeCount();
         for (int i = 0; i < count; i++) {
             String prefix = reader.getAttributePrefix(i);
@@ -199,13 +209,13 @@
             // If the prefix is not associated with a namespace yet, remember it so that we can
         	// write out a namespace declaration
         	if (newPrefix != null) {
-        		if (prefixList == null) {
-        			prefixList= new ArrayList();
-        			nsList = new ArrayList();
-        		}
-        		if (!prefixList.contains(newPrefix)) {
-        			prefixList.add(newPrefix);
-        			nsList.add(namespace);
+        		if (writePrefixList == null) {
+        			writePrefixList= new ArrayList();
+        			writeNSList = new ArrayList();
+        		}
+        		if (!writePrefixList.contains(newPrefix)) {
+        			writePrefixList.add(newPrefix);
+        			writeNSList.add(namespace);
         		}
         	}
         }
@@ -223,35 +233,12 @@
         	}
         }
 
-
-        // add the namespaces
-        count = reader.getNamespaceCount();
-        String namespacePrefix;
-        for (int i = 0; i < count; i++) {
-        	String prefix = reader.getNamespacePrefix(i);
-        	prefix = (prefix != null && prefix.length() == 0) ? null : prefix;
-        	String namespace = reader.getNamespaceURI(i);
-        	if (prefix != null && namespace != null) {
-        		writer.writeNamespace(prefix, namespace);
-        	} else {
-        		writer.writeDefaultNamespace(namespace);
-        	}
-        	// If this prefix is in the unassociated list, remove it.
-        	if (prefixList != null) {
-        		int j = prefixList.indexOf((prefix == null)?"":prefix);
-        		if (j >= 0) {
-        			prefixList.remove(j);
-        			nsList.remove(j);
-        		}
-        	}
-        }
-        
-        // Now write out the namespaces that for prefixes that are not associated
-        if (prefixList != null) {
-        	for (int i=0; i<prefixList.size(); i++) {
-        		String prefix = (String) prefixList.get(i);
-        		String namespace = (String) nsList.get(i);
-        		
+        // Now write out the list of namespace declarations in this list that we constructed
+    	// while doing the "set" processing.
+    	if (writePrefixList != null) {
+        	for (int i=0; i<writePrefixList.size(); i++) {
+        		String prefix = (String) writePrefixList.get(i);
+        		String namespace = (String) writeNSList.get(i);	
         		if (prefix != null) {
             		writer.writeNamespace(prefix, namespace);
             	} else {
@@ -259,7 +246,7 @@
             	}
         	}
         }
-        
+    	
         // Now write the attributes
         count = reader.getAttributeCount();
         for (int i = 0; i < count; i++) {

Modified: webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/util/OMSerializerUtil.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/util/OMSerializerUtil.java?rev=433288&r1=433287&r2=433288&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/util/OMSerializerUtil.java (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/util/OMSerializerUtil.java Mon Aug 21 10:11:43 2006
@@ -193,16 +193,14 @@
     	//
     	// ... generate writeStartElement (See NOTE_A)
     	//
-    	// ... generate writeNamespace/writerDefaultNamespace for each namespace declaration on the element
-    	// ... generate writeNamespace/writeDefaultNamespace for any new "autogen" namespace/prefixes
+    	// ... generate writeNamespace/writerDefaultNamespace for the new namespace declarations determine during the "set" processing
     	// ... generate writeAttribute for each attribute	
     	
     	// NOTE_A: To confuse matters, some StAX vendors (including woodstox), believe that the setPrefix bindings 
     	// should occur after the writeStartElement.  If this is the case, the writeStartElement is generated first.
     	
-    
-    	ArrayList  prefixList = null;
-    	ArrayList  nsList = null;
+    	ArrayList  writePrefixList = null;
+    	ArrayList  writeNSList = null;
     
     	// Get the namespace and prefix of the element
     	OMNamespace eOMNamespace = element.getNamespace();
@@ -242,21 +240,33 @@
         	prefix = (prefix != null && prefix.length() == 0) ? null : prefix;
         	namespace = (namespace != null && namespace.length() == 0) ? null : namespace;
         	
-        	generateSetPrefix(prefix, namespace, writer);
+        	
+        	String newPrefix = generateSetPrefix(prefix, namespace, writer);
+        	// If this is a new association, remember it so that it can written out later
+        	if (newPrefix != null) {
+        		if (writePrefixList == null) {
+        			writePrefixList= new ArrayList();
+        			writeNSList = new ArrayList();
+        		}
+        		if (!writePrefixList.contains(newPrefix)) {
+        			writePrefixList.add(newPrefix);
+        			writeNSList.add(namespace);
+        		}
+        	}
         }
     	
     	// Generate setPrefix for the element
     	// Get the prefix and namespace of the element.  "" and null are identical.
-    	
     	String newPrefix = generateSetPrefix(ePrefix, eNamespace, writer);
+    	// If this is a new association, remember it so that it can written out later
     	if (newPrefix != null) {
-    		if (prefixList == null) {
-    			prefixList= new ArrayList();
-    			nsList = new ArrayList();
+    		if (writePrefixList == null) {
+    			writePrefixList= new ArrayList();
+    			writeNSList = new ArrayList();
     		}
-    		if (!prefixList.contains(newPrefix)) {
-    			prefixList.add(newPrefix);
-    			nsList.add(eNamespace);
+    		if (!writePrefixList.contains(newPrefix)) {
+    			writePrefixList.add(newPrefix);
+    			writeNSList.add(eNamespace);
     		}
     	}
     	
@@ -285,13 +295,13 @@
             // If the prefix is not associated with a namespace yet, remember it so that we can
         	// write out a namespace declaration
         	if (newPrefix != null) {
-        		if (prefixList == null) {
-        			prefixList= new ArrayList();
-        			nsList = new ArrayList();
+        		if (writePrefixList == null) {
+        			writePrefixList= new ArrayList();
+        			writeNSList = new ArrayList();
         		}
-        		if (!prefixList.contains(newPrefix)) {
-        			prefixList.add(newPrefix);
-        			nsList.add(namespace);
+        		if (!writePrefixList.contains(newPrefix)) {
+        			writePrefixList.add(newPrefix);
+        			writeNSList.add(namespace);
         		}
         	}
         }
@@ -309,43 +319,12 @@
         	}
         }
         
-        // Now write the namespace declarations
-    	it = element.getAllDeclaredNamespaces();
-    	while (it != null && it.hasNext()) {
-    		OMNamespace omNamespace = (OMNamespace) it.next();
-    		String prefix = null;
-    		String namespace = null;
-    		if (omNamespace != null) {
-    			prefix = omNamespace.getPrefix();
-    			namespace = omNamespace.getNamespaceURI();
-    		}
-        	prefix = (prefix != null && prefix.length() == 0) ? null : prefix;
-        	namespace = (namespace != null && namespace.length() == 0) ? null : namespace;
-        	if (prefix != null) {
-        		// For some reason, prefixes are sometimes bound to an empty namespace on the OMElement.
-        		// This does not seem correct, the following code is a safeguard against errors.
-        		if (namespace != null) {
-        			writer.writeNamespace(prefix, namespace);
-        		}
-        	} else {
-        		writer.writeDefaultNamespace(namespace);
-        	}
-        	
-        	// If this prefix is in the unassociated list, remove it.
-        	if (prefixList != null) {
-        		int i = prefixList.indexOf((prefix == null)?"":prefix);
-        		if (i >= 0) {
-        			prefixList.remove(i);
-        			nsList.remove(i);
-        		}
-        	}
-        }
-    	
-    	// Now write out the namespaces that for prefixes that are not associated (i.e. auto-generated)
-        if (prefixList != null) {
-        	for (int i=0; i<prefixList.size(); i++) {
-        		String prefix = (String) prefixList.get(i);
-        		String namespace = (String) nsList.get(i);	
+        // Now write out the list of namespace declarations in this list that we constructed
+    	// while doing the "set" processing.
+    	if (writePrefixList != null) {
+        	for (int i=0; i<writePrefixList.size(); i++) {
+        		String prefix = (String) writePrefixList.get(i);
+        		String namespace = (String) writeNSList.get(i);	
         		if (prefix != null) {
             		writer.writeNamespace(prefix, namespace);
             	} else {
@@ -516,7 +495,7 @@
     	// Generate setPrefix/setDefaultNamespace if the prefix is not associated.
     	String newPrefix = null;
         if (namespace != null) {
-        	
+        	// Qualified Namespace
         	
         	// Get the namespace associated with this writer
         	String writerNS = writer.getNamespaceContext().getNamespaceURI((prefix==null) ? "" : prefix);
@@ -541,8 +520,15 @@
         		// No Action needed..The writer already has associated this prefix to this namespace
         	}
         } else {
-        	// Disable the default namespace
-        	writer.setDefaultNamespace("");
+        	// Unqualified Namespace
+        	
+        	// Make sure the default namespace is either not used or disabled (set to "")
+        	String writerNS = writer.getNamespaceContext().getNamespaceURI("");
+        	if (writerNS != null && writerNS.length() > 0) {
+        		// Disable the default namespace
+        		writer.setDefaultNamespace("");
+        		newPrefix = "";
+        	}
         }
         return newPrefix;
     }

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=433288&r1=433287&r2=433288&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 Aug 21 10:11:43 2006
@@ -87,6 +87,7 @@
 
     /**
      * Constructor OMElementImpl.
+     * A null namespace indicates that the default namespace in scope is used
      */
     public OMElementImpl(String localName, OMNamespace ns, OMContainer parent,
                          OMXMLParserWrapper builder, OMFactory factory) {
@@ -115,6 +116,8 @@
      * @param ns        - can be null
      * @param parent    - this should be an OMContainer
      * @param factory   - factory that created this OMElement
+     * 
+     * A null namespace indicates that the default namespace in scope is used
      */
     public OMElementImpl(String localName, OMNamespace ns, OMContainer parent,
                          OMFactory factory) {

Added: webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/om/SerializationTest.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/om/SerializationTest.java?rev=433288&view=auto
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/om/SerializationTest.java (added)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/om/SerializationTest.java Mon Aug 21 10:11:43 2006
@@ -0,0 +1,305 @@
+/*
+ * Copyright 2004,2005 The Apache Software Foundation.
+ *
+ * Licensed 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;
+
+import junit.framework.TestCase;
+
+/**
+ * Each of the following tests have a parent "person" and children "name", "age", "weight".
+ * The parent is defined as either:
+ *   a) a qualified name (QParent)
+ *   b) an unqualified name (UParent)
+ *   c) a qualified name using the default namespace (DParent)
+ * 
+ * Likewise the children are defined as either:
+ *   a) qualified names (QChildren)
+ *   b) unqualified children (UChildren) 
+ *   c) qualified using the default namespace (DChildren)
+ * 
+ *
+ */
+public class SerializationTest extends TestCase {
+
+	private static final String NS = "http://ws.apache.org/axis2/apacheconasia/06";
+	private static final String PREFIX = "prefix";
+	
+    public void testDParentDChildren() {
+        OMFactory fac = OMAbstractFactory.getOMFactory();
+        
+        OMNamespace nsParent = fac.createOMNamespace(NS, "");
+        OMNamespace nsChildren = fac.createOMNamespace(NS, "");
+        
+        OMElement personElem = fac.createOMElement("person", nsParent);
+        OMElement nameElem = fac.createOMElement("name", nsChildren);
+        nameElem.setText("John");
+        
+        OMElement ageElem = fac.createOMElement("age", nsChildren);
+        ageElem.setText("34");
+        
+        OMElement weightElem = fac.createOMElement("weight", nsChildren);
+        weightElem.setText("50");
+        
+        //Add children to the person element
+        personElem.addChild(nameElem);
+        personElem.addChild(ageElem);
+        personElem.addChild(weightElem);
+        
+        String xml = personElem.toString();
+        
+        assertEquals("Incorrect namespace serialization",2, xml.split("http://ws.apache.org/axis2/apacheconasia/06").length);
+    }
+    
+    public void testDParentUChildren() {
+        OMFactory fac = OMAbstractFactory.getOMFactory();
+        
+        OMNamespace nsParent = fac.createOMNamespace(NS, "");
+        OMNamespace nsChildren = fac.createOMNamespace("", "");
+        
+        OMElement personElem = fac.createOMElement("person", nsParent);
+        OMElement nameElem = fac.createOMElement("name", nsChildren);
+        nameElem.setText("John");
+        
+        OMElement ageElem = fac.createOMElement("age", nsChildren);
+        ageElem.setText("34");
+        
+        OMElement weightElem = fac.createOMElement("weight", nsChildren);
+        weightElem.setText("50");
+        
+        //Add children to the person element
+        personElem.addChild(nameElem);
+        personElem.addChild(ageElem);
+        personElem.addChild(weightElem);
+        
+        String xml = personElem.toString();
+        
+        assertEquals("Incorrect namespace serialization",2, xml.split("http://ws.apache.org/axis2/apacheconasia/06").length);
+        assertEquals("Incorrect namespace serialization",4, xml.split("\"\"").length);
+    }
+    
+    public void testDParentQChildren() {
+        OMFactory fac = OMAbstractFactory.getOMFactory();
+        
+        OMNamespace nsParent = fac.createOMNamespace(NS, "");
+        OMNamespace nsChildren = fac.createOMNamespace(NS, PREFIX);
+        
+        OMElement personElem = fac.createOMElement("person", nsParent);
+        OMElement nameElem = fac.createOMElement("name", nsChildren);
+        nameElem.setText("John");
+        
+        OMElement ageElem = fac.createOMElement("age", nsChildren);
+        ageElem.setText("34");
+        
+        OMElement weightElem = fac.createOMElement("weight", nsChildren);
+        weightElem.setText("50");
+        
+        //Add children to the person element
+        personElem.addChild(nameElem);
+        personElem.addChild(ageElem);
+        personElem.addChild(weightElem);
+        
+        String xml = personElem.toString();
+        
+        assertEquals("Incorrect namespace serialization",5, xml.split("http://ws.apache.org/axis2/apacheconasia/06").length);
+    }
+    
+    
+    public void testQParentQChildren() {
+        OMFactory fac = OMAbstractFactory.getOMFactory();
+        
+        OMNamespace nsParent = fac.createOMNamespace(NS, PREFIX);
+        OMNamespace nsChildren = fac.createOMNamespace(NS, PREFIX);
+        
+        OMElement personElem = fac.createOMElement("person", nsParent);
+        OMElement nameElem = fac.createOMElement("name", nsChildren);
+        nameElem.setText("John");
+        
+        OMElement ageElem = fac.createOMElement("age", nsChildren);
+        ageElem.setText("34");
+        
+        OMElement weightElem = fac.createOMElement("weight", nsChildren);
+        weightElem.setText("50");
+        
+        //Add children to the person element
+        personElem.addChild(nameElem);
+        personElem.addChild(ageElem);
+        personElem.addChild(weightElem);
+        
+        String xml = personElem.toString();
+        
+        assertEquals("Incorrect namespace serialization",2, xml.split("http://ws.apache.org/axis2/apacheconasia/06").length);
+    }
+    
+    public void testQParentUChildren() {
+        OMFactory fac = OMAbstractFactory.getOMFactory();
+        
+        OMNamespace nsParent = fac.createOMNamespace(NS, PREFIX);
+        OMNamespace nsChildren = fac.createOMNamespace("", "");
+        
+        OMElement personElem = fac.createOMElement("person", nsParent);
+        OMElement nameElem = fac.createOMElement("name", nsChildren);
+        nameElem.setText("John");
+        
+        OMElement ageElem = fac.createOMElement("age", nsChildren);
+        ageElem.setText("34");
+        
+        OMElement weightElem = fac.createOMElement("weight", nsChildren);
+        weightElem.setText("50");
+        
+        //Add children to the person element
+        personElem.addChild(nameElem);
+        personElem.addChild(ageElem);
+        personElem.addChild(weightElem);
+        
+        String xml = personElem.toString();
+        
+        assertEquals("Incorrect default namespace serialization",2, xml.split("http://ws.apache.org/axis2/apacheconasia/06").length);
+        assertEquals("Incorrect namespace serialization",1, xml.split("\"\"").length);
+    }
+    
+    public void testQParentDChildren() {
+        OMFactory fac = OMAbstractFactory.getOMFactory();
+        
+        OMNamespace nsParent = fac.createOMNamespace(NS, PREFIX);
+        OMNamespace nsChildren = fac.createOMNamespace(NS, "");
+        
+        OMElement personElem = fac.createOMElement("person", nsParent);
+        OMElement nameElem = fac.createOMElement("name", nsChildren);
+        nameElem.setText("John");
+        
+        OMElement ageElem = fac.createOMElement("age", nsChildren);
+        ageElem.setText("34");
+        
+        OMElement weightElem = fac.createOMElement("weight", nsChildren);
+        weightElem.setText("50");
+        
+        //Add children to the person element
+        personElem.addChild(nameElem);
+        personElem.addChild(ageElem);
+        personElem.addChild(weightElem);
+        
+        String xml = personElem.toString();
+        
+        assertEquals("Incorrect default namespace serialization",5, xml.split("http://ws.apache.org/axis2/apacheconasia/06").length);
+    }
+    
+    public void testUParentUChildren() {
+        OMFactory fac = OMAbstractFactory.getOMFactory();
+        
+        OMNamespace nsParent = fac.createOMNamespace("", "");
+        OMNamespace nsChildren = fac.createOMNamespace("", "");
+        
+        OMElement personElem = fac.createOMElement("person", nsParent);
+        OMElement nameElem = fac.createOMElement("name", nsChildren);
+        nameElem.setText("John");
+        
+        OMElement ageElem = fac.createOMElement("age", nsChildren);
+        ageElem.setText("34");
+        
+        OMElement weightElem = fac.createOMElement("weight", nsChildren);
+        weightElem.setText("50");
+        
+        //Add children to the person element
+        personElem.addChild(nameElem);
+        personElem.addChild(ageElem);
+        personElem.addChild(weightElem);
+        
+        String xml = personElem.toString();
+        
+        assertEquals("Incorrect default namespace serialization",1, xml.split("http://ws.apache.org/axis2/apacheconasia/06").length);
+        assertEquals("Incorrect namespace serialization",1, xml.split("\"\"").length);
+    }
+    
+    public void testUParentQChildren() {
+        OMFactory fac = OMAbstractFactory.getOMFactory();
+        
+        OMNamespace nsParent = fac.createOMNamespace("", "");
+        OMNamespace nsChildren = fac.createOMNamespace(NS, PREFIX);
+        
+        OMElement personElem = fac.createOMElement("person", nsParent);
+        OMElement nameElem = fac.createOMElement("name", nsChildren);
+        nameElem.setText("John");
+        
+        OMElement ageElem = fac.createOMElement("age", nsChildren);
+        ageElem.setText("34");
+        
+        OMElement weightElem = fac.createOMElement("weight", nsChildren);
+        weightElem.setText("50");
+        
+        //Add children to the person element
+        personElem.addChild(nameElem);
+        personElem.addChild(ageElem);
+        personElem.addChild(weightElem);
+        
+        String xml = personElem.toString();
+        
+        assertEquals("Incorrect default namespace serialization",4, xml.split("http://ws.apache.org/axis2/apacheconasia/06").length);
+        assertEquals("Incorrect namespace serialization",1, xml.split("\"\"").length);
+    }
+    
+    public void testUParentDChildren() {
+        OMFactory fac = OMAbstractFactory.getOMFactory();
+        
+        OMNamespace nsParent = fac.createOMNamespace("", "");
+        OMNamespace nsChildren = fac.createOMNamespace(NS, PREFIX);
+        
+        OMElement personElem = fac.createOMElement("person", nsParent);
+        OMElement nameElem = fac.createOMElement("name", nsChildren);
+        nameElem.setText("John");
+        
+        OMElement ageElem = fac.createOMElement("age", nsChildren);
+        ageElem.setText("34");
+        
+        OMElement weightElem = fac.createOMElement("weight", nsChildren);
+        weightElem.setText("50");
+        
+        //Add children to the person element
+        personElem.addChild(nameElem);
+        personElem.addChild(ageElem);
+        personElem.addChild(weightElem);
+        
+        String xml = personElem.toString();
+        
+        assertEquals("Incorrect namespace serialization",4, xml.split("http://ws.apache.org/axis2/apacheconasia/06").length);
+        assertEquals("Incorrect namespace serialization",1, xml.split("\"\"").length);
+    }
+    
+    
+    
+   
+    
+    /**
+     * Special case when OMElement is created with a null OMNamespace.
+     * In this case, the created OMElement uses that default namespace of the parent.
+     */
+    public void testNullOMNamespace() {
+        OMFactory fac = OMAbstractFactory.getOMFactory();
+        OMNamespace ns = fac.createOMNamespace("http://ws.apache.org/axis2/apacheconasia/06", "");
+        OMElement personElem = fac.createOMElement("person", ns);
+
+        //Create and add using null namespace...this should pick up default namespace from parent
+        OMElement nameElem = fac.createOMElement("name", null);
+        nameElem.setText("John");
+        personElem.addChild(nameElem);
+
+        String xml = personElem.toString();
+        
+        assertEquals("Incorrect namespace serialization",2, xml.split("http://ws.apache.org/axis2/apacheconasia/06").length);
+        assertEquals("Incorrect serialization", 1, xml.split("xmlns=\"\"").length);
+        
+    }
+    
+}



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