Author: veithen
Date: Sat Apr 11 17:52:07 2009
New Revision: 764224

URL: http://svn.apache.org/viewvc?rev=764224&view=rev
Log:
More fixes related to namespace declarations generated by addAttribute.

Added:
    
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/resources/conformance/namespaces3.xml
   (with props)
Modified:
    
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/java/org/apache/axiom/om/OMElementTestBase.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/OMElementImpl.java

Modified: 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/java/org/apache/axiom/om/OMElementTestBase.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/java/org/apache/axiom/om/OMElementTestBase.java?rev=764224&r1=764223&r2=764224&view=diff
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/java/org/apache/axiom/om/OMElementTestBase.java
 (original)
+++ 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/java/org/apache/axiom/om/OMElementTestBase.java
 Sat Apr 11 17:52:07 2009
@@ -206,7 +206,11 @@
         assertFalse(it.hasNext());
     }
 
-    public void testAddAttributeWithExistingNamespaceDeclaration() {
+    /**
+     * Test that adding an attribute doesn't create an additional namespace 
declaration if
+     * a corresponding declaration already exists on the element.
+     */
+    public void 
testAddAttributeWithExistingNamespaceDeclarationOnSameElement() {
         OMFactory factory = getOMFactory();
         OMElement element = factory.createOMElement(new QName("test"));
         OMNamespace ns = factory.createOMNamespace("urn:ns", "p");
@@ -220,6 +224,22 @@
     }
 
     /**
+     * Test that adding an attribute doesn't create an additional namespace 
declaration if
+     * a corresponding declaration is already in scope.
+     */
+    public void testAddAttributeWithExistingNamespaceDeclarationInScope() {
+        OMFactory factory = getOMFactory();
+        OMElement root = factory.createOMElement(new QName("test"));
+        OMNamespace ns = factory.createOMNamespace("urn:ns", "p");
+        root.declareNamespace(ns);
+        OMElement child = factory.createOMElement(new QName("test"), root);
+        OMAttribute att = factory.createOMAttribute("test", ns, "test");
+        child.addAttribute(att);
+        Iterator it = child.getAllDeclaredNamespaces();
+        assertFalse(it.hasNext());
+    }
+
+    /**
      * Test checking that {...@link OMElement#addAttribute(OMAttribute)} 
correctly generates a
      * new namespace declaration if an equivalent namespace declaration exists 
but is masked.
      * The test attempts to create the following XML:

Added: 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/resources/conformance/namespaces3.xml
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/resources/conformance/namespaces3.xml?rev=764224&view=auto
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/resources/conformance/namespaces3.xml
 (added)
+++ 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/resources/conformance/namespaces3.xml
 Sat Apr 11 17:52:07 2009
@@ -0,0 +1,2 @@
+<?xml version="1.0"?>
+<a xmlns:ns="urn:ns"><b ns:attr="test"/></a>
\ No newline at end of file

Propchange: 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/resources/conformance/namespaces3.xml
------------------------------------------------------------------------------
    svn:eol-style = native

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=764224&r1=764223&r2=764224&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
 Sat Apr 11 17:52:07 2009
@@ -686,11 +686,15 @@
         }
         
         OMNamespace namespace = attr.getNamespace();
-        if (namespace != null && namespace.getNamespaceURI() != null
-                && !"".equals(namespace.getNamespaceURI())
-                && 
!namespace.getNamespaceURI().equals(this.findNamespaceURI(namespace
-                .getPrefix()))) {
-            this.declareNamespace(namespace.getNamespaceURI(), 
namespace.getPrefix());
+        if (namespace != null) {
+            String uri = namespace.getNamespaceURI();
+            if (uri.length() > 0) {
+                String prefix = namespace.getPrefix();
+                OMNamespace ns2 = findNamespaceURI(prefix);
+                if (ns2 == null || !uri.equals(ns2.getNamespaceURI())) {
+                    declareNamespace(uri, prefix);
+                }
+            }
         }
 
         if (attr.getNamespace() != null) { // If the attr has a namespace
@@ -708,8 +712,15 @@
      */
     public OMAttribute addAttribute(String attributeName, String value,
                                     OMNamespace ns) {
-        if (ns != null && findNamespace(ns.getNamespaceURI(), ns.getPrefix()) 
!= null) {
-            declareNamespace(ns);
+        if (ns != null) {
+            String uri = ns.getNamespaceURI();
+            if (uri.length() > 0) {
+                String prefix = ns.getPrefix();
+                OMNamespace ns2 = findNamespaceURI(prefix);
+                if (ns2 == null || !uri.equals(ns2.getNamespaceURI())) {
+                    declareNamespace(uri, prefix);
+                }
+            }
         }
         if (ns != null) {
             return this.addAttribute(ns.getNamespaceURI(), ns.getPrefix() + ":"

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=764224&r1=764223&r2=764224&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
 Sat Apr 11 17:52:07 2009
@@ -589,12 +589,15 @@
             this.attributes = new LinkedHashMap(5);
         }
         OMNamespace namespace = attr.getNamespace();
-        String nsURI;
-        String nsPrefix;
-        if (namespace != null && (nsURI = namespace.getNamespaceURI()) != null 
&&
-                !"".equals(nsURI) &&
-                !nsURI.equals(this.findNamespaceURI(nsPrefix = 
namespace.getPrefix()))) {
-            this.declareNamespace(nsURI, nsPrefix);
+        if (namespace != null) {
+            String uri = namespace.getNamespaceURI();
+            if (uri.length() > 0) {
+                String prefix = namespace.getPrefix();
+                OMNamespace ns2 = findNamespaceURI(prefix);
+                if (ns2 == null || !uri.equals(ns2.getNamespaceURI())) {
+                    declareNamespace(uri, prefix);
+                }
+            }
         }
 
         // Set the owner element of the attribute


Reply via email to