Author: chinthaka
Date: Fri Apr 21 11:13:19 2006
New Revision: 395976

URL: http://svn.apache.org/viewcvs?rev=395976&view=rev
Log:
Fixing one more namespace scenario. Thanks Ruchith for pointing this out. 
This includes a test for that as well.


Modified:
    webservices/commons/trunk/modules/axiom/maven.xml
    
webservices/commons/trunk/modules/axiom/src/org/apache/axiom/om/impl/util/OMSerializerUtil.java
    
webservices/commons/trunk/modules/axiom/test/org/apache/axiom/om/NamespaceTest.java

Modified: webservices/commons/trunk/modules/axiom/maven.xml
URL: 
http://svn.apache.org/viewcvs/webservices/commons/trunk/modules/axiom/maven.xml?rev=395976&r1=395975&r2=395976&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/maven.xml (original)
+++ webservices/commons/trunk/modules/axiom/maven.xml Fri Apr 21 11:13:19 2006
@@ -114,11 +114,11 @@
         </ant:copy>
 
         <!--add documents-->
-        <ant:copy toDir="${dist}/docs">
-            <ant:fileset dir="xdocs">
-                <ant:exclude name="**/.svn/**"/>
-            </ant:fileset>
-        </ant:copy>
+        <!--<ant:copy toDir="${dist}/docs">-->
+            <!--<ant:fileset dir="xdocs">-->
+                <!--<ant:exclude name="**/.svn/**"/>-->
+            <!--</ant:fileset>-->
+        <!--</ant:copy>-->
 
         <!--copy release notes, etc-->
         <ant:copy toDir="${dist}">

Modified: 
webservices/commons/trunk/modules/axiom/src/org/apache/axiom/om/impl/util/OMSerializerUtil.java
URL: 
http://svn.apache.org/viewcvs/webservices/commons/trunk/modules/axiom/src/org/apache/axiom/om/impl/util/OMSerializerUtil.java?rev=395976&r1=395975&r2=395976&view=diff
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/src/org/apache/axiom/om/impl/util/OMSerializerUtil.java
 (original)
+++ 
webservices/commons/trunk/modules/axiom/src/org/apache/axiom/om/impl/util/OMSerializerUtil.java
 Fri Apr 21 11:13:19 2006
@@ -103,8 +103,9 @@
             // Case 2 :
             //        The passed in namespace is a default ns, but there is a 
non-default ns declared
             //        in the current scope.
-            if ( ("".equals(prefix) && "".equals(prefixFromWriter) && 
!uri.equals(writer.getNamespaceContext().getNamespaceURI(""))) ||
-                 (prefix != null && "".equals(prefix) && (prefixFromWriter == 
null || !prefix.equals(prefixFromWriter)))){
+            if (("".equals(prefix) && "".equals(prefixFromWriter) && 
!uri.equals(writer.getNamespaceContext().getNamespaceURI(""))) ||
+                    (prefix != null && "".equals(prefix) && (prefixFromWriter 
== null || !prefix.equals(prefixFromWriter))))
+            {
                 // this has not been declared earlier
                 writer.writeDefaultNamespace(uri);
                 writer.setDefaultNamespace(uri);
@@ -184,25 +185,35 @@
                         writer.writeDefaultNamespace(nameSpaceName);
                         writer.setDefaultNamespace(nameSpaceName);
                     } else {
-                        writer.writeStartElement(prefix, 
element.getLocalName(),
-                                nameSpaceName);
-                        writer.writeNamespace(prefix, nameSpaceName);
-                        writer.setPrefix(prefix, nameSpaceName);
+                        // now the left scenario is this
+                        // 1. prefix != "" && writer_prefix != "" but 
writer_prefix != prefix
+                        // 2. prefix != "" && writer_prefix == ""
+
+                        // In both the above cases this xml may contain more 
than one prefix for the
+                        // same URI. Check them all.
+
+                        Iterator prefixesIter = 
writer.getNamespaceContext().getPrefixes(nameSpaceName);
+                        boolean found = false;                                 
                   sen
+                        while (prefixesIter.hasNext()) {
+                            String prefix_w = (String) prefixesIter.next();
+                            if (prefix_w.equals(prefix)) {
+                                // if found do not declare the ns
+                                writer.writeStartElement(nameSpaceName, 
element.getLocalName());
+                                found = true;
+                            }
+                        }
+
+                        if (!found) {
+                            // seems we haven't found one in the current 
scope. So declare it.
+                            writer.writeStartElement(prefix, 
element.getLocalName(),
+                                    nameSpaceName);
+                            writer.writeNamespace(prefix, nameSpaceName);
+                            writer.setPrefix(prefix, nameSpaceName);
+                        }
+
                     }
                 }
 
-//                if (writer_prefix != null) {
-//                    writer.writeStartElement(nameSpaceName,
-//                            element.getLocalName());
-//                } else {
-//                    prefix = (prefix == null) ? getNextNSPrefix() : prefix;
-//                    writer.writeStartElement(prefix, element.getLocalName(),
-//                            nameSpaceName);
-//                    writer.writeNamespace(prefix, nameSpaceName);
-//                    writer.setPrefix(prefix, nameSpaceName);
-//
-//                }
-                
             } else {
                 writer.writeStartElement(element.getLocalName());
             }
@@ -304,7 +315,7 @@
 
     public static String getNextNSPrefix(XMLStreamWriter writer) {
         String prefix = getNextNSPrefix();
-        while (writer.getNamespaceContext().getNamespaceURI(prefix) != null){
+        while (writer.getNamespaceContext().getNamespaceURI(prefix) != null) {
             prefix = getNextNSPrefix();
         }
 

Modified: 
webservices/commons/trunk/modules/axiom/test/org/apache/axiom/om/NamespaceTest.java
URL: 
http://svn.apache.org/viewcvs/webservices/commons/trunk/modules/axiom/test/org/apache/axiom/om/NamespaceTest.java?rev=395976&r1=395975&r2=395976&view=diff
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/test/org/apache/axiom/om/NamespaceTest.java
 (original)
+++ 
webservices/commons/trunk/modules/axiom/test/org/apache/axiom/om/NamespaceTest.java
 Fri Apr 21 11:13:19 2006
@@ -121,11 +121,42 @@
         OMElement childTwo = omFac.createOMElement("ChildElementTwo", null, 
childOne);
         childTwo.declareDefaultNamespace("http://one.org";);
 
-        StringBuffer element = new 
StringBuffer(documentElement.toStringWithConsume());
 
-        int firstIndex = element.indexOf("xmlns=\"http://one.org\"";);
-        assertTrue(firstIndex > -1);
-        assertTrue(element.lastIndexOf("xmlns=\"http://one.org\"";) != 
firstIndex);
+        assertEquals(2, 
getNumberOfOccurrences(documentElement.toStringWithConsume(), 
"xmlns=\"http://one.org\"";));
+    }
+
+    public void testNamespaceProblem3() throws XMLStreamException {
+
+        /**
+         * <RootElement xmlns:ns1="http://one.org"; xmlns:ns2="http://one.org";>
+         *   <ns2:ChildElementOne xmlns="http://one.org";>
+         *      <ns2:ChildElementTwo />
+         *   </ns2:ChildElementOne>
+         * </RootElement>
+         */
+
+        OMFactory omFac = OMAbstractFactory.getOMFactory();
+
+        OMElement documentElement = omFac.createOMElement("RootElement", null);
+        OMNamespace ns1 = documentElement.declareNamespace("http://one.org";, 
"ns1");
+        OMNamespace ns2 = documentElement.declareNamespace("http://one.org";, 
"ns2");
+
+        OMElement childOne = omFac.createOMElement("ChildElementOne", ns2, 
documentElement);
+        childOne.declareDefaultNamespace("http://one.org";);
+
+        OMElement childTwo = omFac.createOMElement("ChildElementTwo", ns1, 
childOne);
+
+        assertEquals(1, 
getNumberOfOccurrences(documentElement.toStringWithConsume(), 
"xmlns:ns2=\"http://one.org\"";));
+    }
+
+    private int getNumberOfOccurrences(String xml, String pattern){
+        int index = -1;
+        int count = 0;
+        while ((index = xml.indexOf(pattern, index + 1)) != -1) {
+            count ++;
+        }
+
+        return count;
     }
 
 


Reply via email to