Author: veithen
Date: Fri Feb  7 15:19:26 2014
New Revision: 1565684

URL: http://svn.apache.org/r1565684
Log:
Unified the set(OM)Namespace methods and added setNamespace(OMNamespace, 
boolean) to OMNamedInformationItem.

Added:
    
webservices/axiom/trunk/modules/axiom-common-impl/src/main/java/org/apache/axiom/om/impl/common/OMNamedInformationItemHelper.java
   (with props)
    
webservices/axiom/trunk/modules/axiom-dom-testsuite/src/main/java/org/apache/axiom/ts/dom/attr/TestSetPrefixNullWithNamespace.java
   (contents, props changed)
      - copied, changed from r1565683, 
webservices/axiom/trunk/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/element/TestSetNamespaceWithNullOMNamespace.java
    
webservices/axiom/trunk/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/SetNamespaceTestCase.java
   (with props)
    
webservices/axiom/trunk/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/attribute/TestSetNamespace.java
   (with props)
Removed:
    
webservices/axiom/trunk/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/element/TestSetNamespaceInvalid.java
    
webservices/axiom/trunk/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/element/TestSetNamespaceWithMatchingBindingInScope.java
    
webservices/axiom/trunk/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/element/TestSetNamespaceWithNullOMNamespace.java
    
webservices/axiom/trunk/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/element/TestSetNamespaceWithNullPrefix.java
Modified:
    
webservices/axiom/trunk/modules/axiom-api/src/main/java/org/apache/axiom/om/OMAttribute.java
    
webservices/axiom/trunk/modules/axiom-api/src/main/java/org/apache/axiom/om/OMElement.java
    
webservices/axiom/trunk/modules/axiom-api/src/main/java/org/apache/axiom/om/OMNamedInformationItem.java
    
webservices/axiom/trunk/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/builder/BuilderUtil.java
    
webservices/axiom/trunk/modules/axiom-common-impl/src/main/java/org/apache/axiom/om/impl/common/IElement.java
    
webservices/axiom/trunk/modules/axiom-dom-testsuite/src/main/java/org/apache/axiom/ts/dom/DOMTestSuiteBuilder.java
    
webservices/axiom/trunk/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/AttrImpl.java
    
webservices/axiom/trunk/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/ElementImpl.java
    
webservices/axiom/trunk/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMAttributeImpl.java
    
webservices/axiom/trunk/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMElementImpl.java
    
webservices/axiom/trunk/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMSourcedElementImpl.java
    
webservices/axiom/trunk/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/PushOMBuilder.java
    
webservices/axiom/trunk/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/OMTestSuiteBuilder.java
    
webservices/axiom/trunk/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/element/TestSetNamespace.java

Modified: 
webservices/axiom/trunk/modules/axiom-api/src/main/java/org/apache/axiom/om/OMAttribute.java
URL: 
http://svn.apache.org/viewvc/webservices/axiom/trunk/modules/axiom-api/src/main/java/org/apache/axiom/om/OMAttribute.java?rev=1565684&r1=1565683&r2=1565684&view=diff
==============================================================================
--- 
webservices/axiom/trunk/modules/axiom-api/src/main/java/org/apache/axiom/om/OMAttribute.java
 (original)
+++ 
webservices/axiom/trunk/modules/axiom-api/src/main/java/org/apache/axiom/om/OMAttribute.java
 Fri Feb  7 15:19:26 2014
@@ -41,11 +41,9 @@ public interface OMAttribute extends OMN
     /** @param value  */
     void setAttributeType(String value);
 
-    /** @param omNamespace  */
-    // TODO: ill-defined API:
-    //  * if the attribute has an owner, should a namespace declaration be 
added (if no matching declaration is in scope)?
-    //  * what if prefix is null?
-    //  * constraints on OMNamespace not defined (e.g. empty prefix not 
allowed if namespace URI not empty)
+    /**
+     * @deprecated Use {@link OMNamedInformationItem#setNamespace(OMNamespace, 
boolean)} instead.
+     */
     void setOMNamespace(OMNamespace omNamespace);
 
     /**

Modified: 
webservices/axiom/trunk/modules/axiom-api/src/main/java/org/apache/axiom/om/OMElement.java
URL: 
http://svn.apache.org/viewvc/webservices/axiom/trunk/modules/axiom-api/src/main/java/org/apache/axiom/om/OMElement.java?rev=1565684&r1=1565683&r2=1565684&view=diff
==============================================================================
--- 
webservices/axiom/trunk/modules/axiom-api/src/main/java/org/apache/axiom/om/OMElement.java
 (original)
+++ 
webservices/axiom/trunk/modules/axiom-api/src/main/java/org/apache/axiom/om/OMElement.java
 Fri Feb  7 15:19:26 2014
@@ -78,7 +78,7 @@ import java.util.Iterator;
  * <li>More precisely, Axiom will always make sure that any {@link OMElement} 
or {@link OMAttribute}
  * node will keep the namespace URI that has been assigned to the node at 
creation time, unless the
  * namespace is explicitly changed using {@link #setNamespace(OMNamespace)} or
- * {@link OMAttribute#setOMNamespace(OMNamespace)}.
+ * {@link OMNamedInformationItem#setNamespace(OMNamespace, boolean)}.
  * </ul>
  */
 public interface OMElement extends OMNode, OMContainer, OMNamedInformationItem 
{
@@ -511,10 +511,8 @@ public interface OMElement extends OMNod
     void setNamespace(OMNamespace namespace);
 
     /**
-     * This will not search the namespace in the scope nor will declare in the 
current element, as
-     * in setNamespace(OMNamespace). This will just assign the given namespace 
to the element.
-     *
-     * @param namespace
+     * @deprecated Use {@link OMNamedInformationItem#setNamespace(OMNamespace, 
boolean)} with
+     *             <code>declare</code> set to <code>true</code>.
      */
     void setNamespaceWithNoFindInCurrentScope(OMNamespace namespace);
 

Modified: 
webservices/axiom/trunk/modules/axiom-api/src/main/java/org/apache/axiom/om/OMNamedInformationItem.java
URL: 
http://svn.apache.org/viewvc/webservices/axiom/trunk/modules/axiom-api/src/main/java/org/apache/axiom/om/OMNamedInformationItem.java?rev=1565684&r1=1565683&r2=1565684&view=diff
==============================================================================
--- 
webservices/axiom/trunk/modules/axiom-api/src/main/java/org/apache/axiom/om/OMNamedInformationItem.java
 (original)
+++ 
webservices/axiom/trunk/modules/axiom-api/src/main/java/org/apache/axiom/om/OMNamedInformationItem.java
 Fri Feb  7 15:19:26 2014
@@ -52,6 +52,30 @@ public interface OMNamedInformationItem 
      *         prefix is returned.
      */
     OMNamespace getNamespace();
+    
+    /**
+     * Set the namespace for this information item. This will change the 
namespace URI and the
+     * prefix of the information item. In addition, if <code>declare</code> is 
<code>true</code>
+     * this method ensures that a corresponding namespace declaration exists: 
if no corresponding
+     * namespace declaration is already in scope, then a new one will be added 
to the nearest
+     * element (i.e. the element itself if this information item is an element 
or the owner element
+     * if this information item is an attribute).
+     * 
+     * @param namespace
+     *            The new namespace for this information item, or 
<code>null</code> to remove the
+     *            namespace from this information item. If an {@link 
OMNamespace} instance with a
+     *            <code>null</code> prefix is given, then a prefix will be 
generated automatically.
+     *            In this case, the generated prefix can be determined using 
{@link #getNamespace()}
+     *            method.
+     * @param declare
+     *            Indicates whether a namespace declaration should be 
generated if necessary;
+     *            ignored if the information item is an attribute without 
owner element.
+     * @throws IllegalArgumentException
+     *             if an attempt is made to change the namespace of the 
information item in such a
+     *             way that it would make the document ill-formed with respect 
to namespaces (e.g.
+     *             binding a prefix to the empty namespace name)
+     */
+    void setNamespace(OMNamespace namespace, boolean declare);
 
     /**
      * Get the QName of this information item.

Modified: 
webservices/axiom/trunk/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/builder/BuilderUtil.java
URL: 
http://svn.apache.org/viewvc/webservices/axiom/trunk/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/builder/BuilderUtil.java?rev=1565684&r1=1565683&r2=1565684&view=diff
==============================================================================
--- 
webservices/axiom/trunk/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/builder/BuilderUtil.java
 (original)
+++ 
webservices/axiom/trunk/modules/axiom-api/src/main/java/org/apache/axiom/om/impl/builder/BuilderUtil.java
 Fri Feb  7 15:19:26 2014
@@ -49,7 +49,7 @@ public class BuilderUtil {
             namespace = 
((OMElementEx)element).addNamespaceDeclaration(namespaceURI, prefix);
         }
         if (namespace != null && namespaceURI.length() > 0) {
-            element.setNamespaceWithNoFindInCurrentScope(namespace);
+            element.setNamespace(namespace, false);
         }
     }
 }

Modified: 
webservices/axiom/trunk/modules/axiom-common-impl/src/main/java/org/apache/axiom/om/impl/common/IElement.java
URL: 
http://svn.apache.org/viewvc/webservices/axiom/trunk/modules/axiom-common-impl/src/main/java/org/apache/axiom/om/impl/common/IElement.java?rev=1565684&r1=1565683&r2=1565684&view=diff
==============================================================================
--- 
webservices/axiom/trunk/modules/axiom-common-impl/src/main/java/org/apache/axiom/om/impl/common/IElement.java
 (original)
+++ 
webservices/axiom/trunk/modules/axiom-common-impl/src/main/java/org/apache/axiom/om/impl/common/IElement.java
 Fri Feb  7 15:19:26 2014
@@ -18,8 +18,9 @@
  */
 package org.apache.axiom.om.impl.common;
 
+import org.apache.axiom.om.OMNamespace;
 import org.apache.axiom.om.impl.OMElementEx;
 
 public interface IElement extends OMElementEx, IContainer, IChildNode {
-
+    void addNamespaceDeclaration(OMNamespace ns);
 }

Added: 
webservices/axiom/trunk/modules/axiom-common-impl/src/main/java/org/apache/axiom/om/impl/common/OMNamedInformationItemHelper.java
URL: 
http://svn.apache.org/viewvc/webservices/axiom/trunk/modules/axiom-common-impl/src/main/java/org/apache/axiom/om/impl/common/OMNamedInformationItemHelper.java?rev=1565684&view=auto
==============================================================================
--- 
webservices/axiom/trunk/modules/axiom-common-impl/src/main/java/org/apache/axiom/om/impl/common/OMNamedInformationItemHelper.java
 (added)
+++ 
webservices/axiom/trunk/modules/axiom-common-impl/src/main/java/org/apache/axiom/om/impl/common/OMNamedInformationItemHelper.java
 Fri Feb  7 15:19:26 2014
@@ -0,0 +1,69 @@
+/*
+ * 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.OMNamespace;
+import org.apache.axiom.om.impl.util.OMSerializerUtil;
+
+public final class OMNamedInformationItemHelper {
+    private OMNamedInformationItemHelper() {}
+
+    public static OMNamespace handleNamespace(IElement context, OMNamespace 
ns, boolean attr, boolean declare) {
+        String namespaceURI = ns == null ? "" : ns.getNamespaceURI();
+        String prefix = ns == null ? "" : ns.getPrefix();
+        if (namespaceURI.length() == 0) {
+            if (prefix != null && prefix.length() != 0) {
+                throw new IllegalArgumentException("Cannot bind a prefix to 
the empty namespace name");
+            }
+            if (!attr && declare) {
+                // Special case: no namespace; we need to generate a namespace 
declaration only if
+                // there is a conflicting namespace declaration (i.e. a 
declaration for the default
+                // namespace with a non empty URI) is in scope
+                if (context.getDefaultNamespace() != null) {
+                    context.declareDefaultNamespace("");
+                }
+            }
+            return null;
+        } else {
+            if (attr && prefix != null && prefix.length() == 0) {
+                throw new IllegalArgumentException("An attribute with a 
namespace must be prefixed");
+            }
+            boolean addNSDecl = false;
+            if (context != null && (declare || prefix == null)) {
+                OMNamespace existingNSDecl = 
context.findNamespace(namespaceURI, prefix);
+                if (existingNSDecl == null
+                        || (prefix != null && 
!existingNSDecl.getPrefix().equals(prefix))
+                        || (prefix == null && attr && 
existingNSDecl.getPrefix().length() == 0)) {
+                    addNSDecl = declare;
+                } else {
+                    prefix = existingNSDecl.getPrefix();
+                    ns = existingNSDecl;
+                }
+            }
+            if (prefix == null) {
+                prefix = OMSerializerUtil.getNextNSPrefix();
+                ns = new OMNamespaceImpl(namespaceURI, prefix);
+            }
+            if (addNSDecl) {
+                context.addNamespaceDeclaration(ns);
+            }
+            return ns;
+        }
+    }
+}

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

Modified: 
webservices/axiom/trunk/modules/axiom-dom-testsuite/src/main/java/org/apache/axiom/ts/dom/DOMTestSuiteBuilder.java
URL: 
http://svn.apache.org/viewvc/webservices/axiom/trunk/modules/axiom-dom-testsuite/src/main/java/org/apache/axiom/ts/dom/DOMTestSuiteBuilder.java?rev=1565684&r1=1565683&r2=1565684&view=diff
==============================================================================
--- 
webservices/axiom/trunk/modules/axiom-dom-testsuite/src/main/java/org/apache/axiom/ts/dom/DOMTestSuiteBuilder.java
 (original)
+++ 
webservices/axiom/trunk/modules/axiom-dom-testsuite/src/main/java/org/apache/axiom/ts/dom/DOMTestSuiteBuilder.java
 Fri Feb  7 15:19:26 2014
@@ -58,6 +58,7 @@ public class DOMTestSuiteBuilder extends
         addTest(new 
org.apache.axiom.ts.dom.attr.TestLookupNamespaceURIWithoutOwnerElement(dbf));
         addTest(new 
org.apache.axiom.ts.dom.attr.TestSetPrefixNotNullWithNamespace(dbf));
         addTest(new 
org.apache.axiom.ts.dom.attr.TestSetPrefixNotNullWithoutNamespace(dbf));
+        addTest(new 
org.apache.axiom.ts.dom.attr.TestSetPrefixNullWithNamespace(dbf));
         addTest(new org.apache.axiom.ts.dom.builder.TestParseURI(dbf));
         addTest(new 
org.apache.axiom.ts.dom.builder.TestWhitespaceAroundDocumentElement(dbf));
         addTest(new org.apache.axiom.ts.dom.document.TestAdoptNode(dbf));

Copied: 
webservices/axiom/trunk/modules/axiom-dom-testsuite/src/main/java/org/apache/axiom/ts/dom/attr/TestSetPrefixNullWithNamespace.java
 (from r1565683, 
webservices/axiom/trunk/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/element/TestSetNamespaceWithNullOMNamespace.java)
URL: 
http://svn.apache.org/viewvc/webservices/axiom/trunk/modules/axiom-dom-testsuite/src/main/java/org/apache/axiom/ts/dom/attr/TestSetPrefixNullWithNamespace.java?p2=webservices/axiom/trunk/modules/axiom-dom-testsuite/src/main/java/org/apache/axiom/ts/dom/attr/TestSetPrefixNullWithNamespace.java&p1=webservices/axiom/trunk/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/element/TestSetNamespaceWithNullOMNamespace.java&r1=1565683&r2=1565684&rev=1565684&view=diff
==============================================================================
--- 
webservices/axiom/trunk/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/element/TestSetNamespaceWithNullOMNamespace.java
 (original)
+++ 
webservices/axiom/trunk/modules/axiom-dom-testsuite/src/main/java/org/apache/axiom/ts/dom/attr/TestSetPrefixNullWithNamespace.java
 Fri Feb  7 15:19:26 2014
@@ -16,30 +16,29 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-package org.apache.axiom.ts.om.element;
+package org.apache.axiom.ts.dom.attr;
 
-import javax.xml.namespace.QName;
+import javax.xml.parsers.DocumentBuilderFactory;
 
-import org.apache.axiom.om.OMElement;
-import org.apache.axiom.om.OMFactory;
-import org.apache.axiom.om.OMMetaFactory;
-import org.apache.axiom.om.OMNamespace;
-import org.apache.axiom.ts.AxiomTestCase;
+import org.apache.axiom.ts.dom.DOMTestCase;
+import org.w3c.dom.Attr;
+import org.w3c.dom.Document;
+import org.w3c.dom.Node;
 
 /**
- * Tests the behavior of {@link OMElement#setNamespace(OMNamespace)} if the 
{@link OMNamespace}
- * parameter is <code>null</code>.
+ * Tests the behavior of {@link Node#setPrefix(String)} when used to remove 
the prefix on an
+ * {@link Attr} that has a namespace. Although this results in an attribute 
that is invalid with
+ * respect to namespaces, no exception is thrown.
  */
-public class TestSetNamespaceWithNullOMNamespace extends AxiomTestCase {
-    public TestSetNamespaceWithNullOMNamespace(OMMetaFactory metaFactory) {
-        super(metaFactory);
+public class TestSetPrefixNullWithNamespace extends DOMTestCase {
+    public TestSetPrefixNullWithNamespace(DocumentBuilderFactory dbf) {
+        super(dbf);
     }
 
     protected void runTest() throws Throwable {
-        OMFactory factory = metaFactory.getOMFactory();
-        OMElement element = factory.createOMElement("test", "urn:test", "p");
-        element.setNamespace(null);
-        assertNull(element.getNamespace());
-        assertEquals(new QName("test"), element.getQName());
+        Document document = dbf.newDocumentBuilder().newDocument();
+        Attr attr = document.createAttributeNS("urn:test", "p:test");
+        attr.setPrefix(null);
+        assertNull(attr.getPrefix());
     }
 }

Propchange: 
webservices/axiom/trunk/modules/axiom-dom-testsuite/src/main/java/org/apache/axiom/ts/dom/attr/TestSetPrefixNullWithNamespace.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: 
webservices/axiom/trunk/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/AttrImpl.java
URL: 
http://svn.apache.org/viewvc/webservices/axiom/trunk/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/AttrImpl.java?rev=1565684&r1=1565683&r2=1565684&view=diff
==============================================================================
--- 
webservices/axiom/trunk/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/AttrImpl.java
 (original)
+++ 
webservices/axiom/trunk/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/AttrImpl.java
 Fri Feb  7 15:19:26 2014
@@ -27,6 +27,7 @@ import org.apache.axiom.om.OMFactory;
 import org.apache.axiom.om.OMNamespace;
 import org.apache.axiom.om.OMOutputFormat;
 import org.apache.axiom.om.OMXMLParserWrapper;
+import org.apache.axiom.om.impl.common.OMNamedInformationItemHelper;
 import org.apache.axiom.om.impl.common.OMNamespaceImpl;
 import org.apache.axiom.om.impl.common.serializer.push.Serializer;
 import org.w3c.dom.Attr;
@@ -305,6 +306,10 @@ public class AttrImpl extends RootNode i
         this.namespace = namespace;
     }
 
+    public void setNamespace(OMNamespace namespace, boolean declare) {
+        this.namespace = OMNamedInformationItemHelper.handleNamespace(owner 
instanceof ElementImpl ? (ElementImpl)owner : null, namespace, true, declare);
+    }
+
     /**
      * Sets the namespace of this attribute node.
      *

Modified: 
webservices/axiom/trunk/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/ElementImpl.java
URL: 
http://svn.apache.org/viewvc/webservices/axiom/trunk/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/ElementImpl.java?rev=1565684&r1=1565683&r2=1565684&view=diff
==============================================================================
--- 
webservices/axiom/trunk/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/ElementImpl.java
 (original)
+++ 
webservices/axiom/trunk/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/ElementImpl.java
 Fri Feb  7 15:19:26 2014
@@ -35,6 +35,7 @@ import org.apache.axiom.om.impl.common.N
 import org.apache.axiom.om.impl.common.OMChildElementIterator;
 import org.apache.axiom.om.impl.common.OMContainerHelper;
 import org.apache.axiom.om.impl.common.OMElementHelper;
+import org.apache.axiom.om.impl.common.OMNamedInformationItemHelper;
 import org.apache.axiom.om.impl.common.OMNamespaceImpl;
 import org.apache.axiom.om.impl.common.OMNodeHelper;
 import org.apache.axiom.om.impl.common.serializer.push.OutputException;
@@ -113,7 +114,7 @@ public class ElementImpl extends ParentN
             parentNode.addChild(this, builder != null);
         }
         this.attributes = new AttributeMap(this);
-        namespace = generateNSDecl ? handleNamespace(ns) : ns;
+        namespace = generateNSDecl ? 
OMNamedInformationItemHelper.handleNamespace(this, ns, false, true) : ns;
     }
 
     final ParentNode internalGetOwnerNode() {
@@ -140,29 +141,6 @@ public class ElementImpl extends ParentN
         this.nextSibling = nextSibling;
     }
 
-    private OMNamespace handleNamespace(OMNamespace ns) {
-        String namespaceURI = ns == null ? "" : ns.getNamespaceURI();
-        String prefix = ns == null ? "" : ns.getPrefix();
-        if (namespaceURI.length() == 0 && prefix != null && prefix.length() > 
0) {
-            throw new IllegalArgumentException("Cannot create a prefixed 
element with an empty namespace name");
-        }
-        if (namespaceURI.length() == 0) {
-            // Special case: no namespace; we need to generate a namespace 
declaration only if
-            // there is a conflicting namespace declaration (i.e. a 
declaration for the default
-            // namespace with a non empty URI) is in scope
-            if (getDefaultNamespace() != null) {
-                declareDefaultNamespace("");
-            }
-            return null;
-        } else {
-            OMNamespace namespace = findNamespace(namespaceURI, prefix);
-            if (namespace == null || (prefix != null && 
!namespace.getPrefix().equals(prefix))) {
-                namespace = declareNamespace(ns);
-            }
-            return namespace;
-        }
-    }
-
     OMNamespace handleNamespace(String namespaceURI, String prefix) {
         if (prefix.length() == 0 && namespaceURI.length() == 0) {
             OMNamespace namespace = getDefaultNamespace();
@@ -462,7 +440,7 @@ public class ElementImpl extends ParentN
         } else {
             if (namespaceURI != null) {
                 attr = new AttrImpl(ownerDocument(), localName, value, 
this.factory);
-                attr.setOMNamespace(new OMNamespaceImpl(namespaceURI, prefix 
== null ? "" : prefix));
+                attr.internalSetNamespace(new OMNamespaceImpl(namespaceURI, 
prefix == null ? "" : prefix));
     
                 this.setAttributeNodeNS(attr);
             } else {
@@ -567,8 +545,14 @@ public class ElementImpl extends ParentN
     }
 
     public OMNamespace addNamespaceDeclaration(String uri, String prefix) {
-        setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, prefix.length() == 
0 ? XMLConstants.XMLNS_ATTRIBUTE : XMLConstants.XMLNS_ATTRIBUTE + ":" + prefix, 
uri);
-        return new OMNamespaceImpl(uri, prefix);
+        OMNamespace ns = new OMNamespaceImpl(uri, prefix);
+        addNamespaceDeclaration(ns);
+        return ns;
+    }
+    
+    public void addNamespaceDeclaration(OMNamespace ns) {
+        String prefix = ns.getPrefix();
+        setAttributeNS(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, prefix.length() == 
0 ? XMLConstants.XMLNS_ATTRIBUTE : XMLConstants.XMLNS_ATTRIBUTE + ":" + prefix, 
ns.getNamespaceURI());
     }
 
     /**
@@ -845,8 +829,12 @@ public class ElementImpl extends ParentN
         this.namespace = namespace;
     }
 
+    public void setNamespace(OMNamespace namespace, boolean declare) {
+        this.namespace = OMNamedInformationItemHelper.handleNamespace(this, 
namespace, false, declare);
+    }
+
     public void setNamespace(OMNamespace namespace) {
-        internalSetNamespace(handleNamespace(namespace));
+        setNamespace(namespace, true);
     }
 
     public void setNamespaceWithNoFindInCurrentScope(OMNamespace namespace) {

Modified: 
webservices/axiom/trunk/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMAttributeImpl.java
URL: 
http://svn.apache.org/viewvc/webservices/axiom/trunk/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMAttributeImpl.java?rev=1565684&r1=1565683&r2=1565684&view=diff
==============================================================================
--- 
webservices/axiom/trunk/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMAttributeImpl.java
 (original)
+++ 
webservices/axiom/trunk/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMAttributeImpl.java
 Fri Feb  7 15:19:26 2014
@@ -26,6 +26,7 @@ import org.apache.axiom.om.OMFactory;
 import org.apache.axiom.om.OMInformationItem;
 import org.apache.axiom.om.OMNamespace;
 import org.apache.axiom.om.OMElement;
+import org.apache.axiom.om.impl.common.OMNamedInformationItemHelper;
 
 import javax.xml.namespace.QName;
 
@@ -57,7 +58,7 @@ public class OMAttributeImpl implements 
     private OMFactory factory;
 
     // Keep track of the owner of the attribute
-    protected OMElement owner;
+    OMElementImpl owner;
 
     /**
      * Constructor OMAttributeImpl.
@@ -159,6 +160,11 @@ public class OMAttributeImpl implements 
         this.type = type;
     }
 
+    public void setNamespace(OMNamespace namespace, boolean declare) {
+        this.namespace = OMNamedInformationItemHelper.handleNamespace(owner, 
namespace, true, declare);
+        this.qName = null;
+    }
+
     /**
      * Method setOMNamespace.
      *

Modified: 
webservices/axiom/trunk/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMElementImpl.java
URL: 
http://svn.apache.org/viewvc/webservices/axiom/trunk/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMElementImpl.java?rev=1565684&r1=1565683&r2=1565684&view=diff
==============================================================================
--- 
webservices/axiom/trunk/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMElementImpl.java
 (original)
+++ 
webservices/axiom/trunk/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMElementImpl.java
 Fri Feb  7 15:19:26 2014
@@ -43,6 +43,7 @@ import org.apache.axiom.om.impl.common.O
 import org.apache.axiom.om.impl.common.OMContainerHelper;
 import org.apache.axiom.om.impl.common.OMDescendantsIterator;
 import org.apache.axiom.om.impl.common.OMElementHelper;
+import org.apache.axiom.om.impl.common.OMNamedInformationItemHelper;
 import org.apache.axiom.om.impl.common.OMNamespaceImpl;
 import org.apache.axiom.om.impl.common.SAXResultContentHandler;
 import org.apache.axiom.om.impl.common.serializer.push.OutputException;
@@ -126,7 +127,7 @@ public class OMElementImpl extends OMNod
         if (parent != null) {
             ((IContainer)parent).addChild(this, builder != null);
         }
-        this.ns = generateNSDecl ? handleNamespace(ns) : ns;
+        this.ns = generateNSDecl ? 
OMNamedInformationItemHelper.handleNamespace(this, ns, false, true) : ns;
     }
 
     /**
@@ -183,29 +184,6 @@ public class OMElementImpl extends OMNod
         return ns;
     }
 
-    private OMNamespace handleNamespace(OMNamespace ns) {
-        String namespaceURI = ns == null ? "" : ns.getNamespaceURI();
-        String prefix = ns == null ? "" : ns.getPrefix();
-        if (namespaceURI.length() == 0 && prefix != null && prefix.length() > 
0) {
-            throw new IllegalArgumentException("Cannot create a prefixed 
element with an empty namespace name");
-        }
-        if (namespaceURI.length() == 0) {
-            // Special case: no namespace; we need to generate a namespace 
declaration only if
-            // there is a conflicting namespace declaration (i.e. a 
declaration for the default
-            // namespace with a non empty URI) is in scope
-            if (getDefaultNamespace() != null) {
-                declareDefaultNamespace("");
-            }
-            return null;
-        } else {
-            OMNamespace namespace = findNamespace(namespaceURI, prefix);
-            if (namespace == null || (prefix != null && 
!namespace.getPrefix().equals(prefix))) {
-                namespace = declareNamespace(ns);
-            }
-            return namespace;
-        }
-    }
-
     OMNamespace handleNamespace(String namespaceURI, String prefix) {
         if (prefix.length() == 0 && namespaceURI.length() == 0) {
             OMNamespace namespace = getDefaultNamespace();
@@ -362,7 +340,7 @@ public class OMElementImpl extends OMNod
         return ns;
     }
     
-    void addNamespaceDeclaration(OMNamespace ns) {
+    public void addNamespaceDeclaration(OMNamespace ns) {
         if (namespaces == null) {
             this.namespaces = new HashMap(5);
         }
@@ -854,8 +832,7 @@ public class OMElementImpl extends OMNod
     }
 
     public void setNamespace(OMNamespace namespace) {
-        this.ns = handleNamespace(namespace);
-        this.qName = null;
+        setNamespace(namespace, true);
     }
 
     public void setNamespaceWithNoFindInCurrentScope(OMNamespace namespace) {
@@ -863,6 +840,11 @@ public class OMElementImpl extends OMNod
         this.qName = null;
     }
 
+    public void setNamespace(OMNamespace namespace, boolean declare) {
+        this.ns = OMNamedInformationItemHelper.handleNamespace(this, 
namespace, false, declare);
+        this.qName = null;
+    }
+
     /**
      * Method getQName.
      *

Modified: 
webservices/axiom/trunk/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMSourcedElementImpl.java
URL: 
http://svn.apache.org/viewvc/webservices/axiom/trunk/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMSourcedElementImpl.java?rev=1565684&r1=1565683&r2=1565684&view=diff
==============================================================================
--- 
webservices/axiom/trunk/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMSourcedElementImpl.java
 (original)
+++ 
webservices/axiom/trunk/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMSourcedElementImpl.java
 Fri Feb  7 15:19:26 2014
@@ -369,7 +369,7 @@ public class OMSourcedElementImpl extend
         return super.addNamespaceDeclaration(uri, prefix);
     }
 
-    void addNamespaceDeclaration(OMNamespace ns) {
+    public void addNamespaceDeclaration(OMNamespace ns) {
         super.addNamespaceDeclaration(ns);
     }
 
@@ -596,6 +596,11 @@ public class OMSourcedElementImpl extend
         super.setNamespaceWithNoFindInCurrentScope(namespace);
     }
 
+    public void setNamespace(OMNamespace namespace, boolean declare) {
+        forceExpand();
+        super.setNamespace(namespace, declare);
+    }
+
     public QName getQName() {
         if (isExpanded()) {
             return super.getQName();

Modified: 
webservices/axiom/trunk/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/PushOMBuilder.java
URL: 
http://svn.apache.org/viewvc/webservices/axiom/trunk/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/PushOMBuilder.java?rev=1565684&r1=1565683&r2=1565684&view=diff
==============================================================================
--- 
webservices/axiom/trunk/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/PushOMBuilder.java
 (original)
+++ 
webservices/axiom/trunk/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/PushOMBuilder.java
 Fri Feb  7 15:19:26 2014
@@ -115,7 +115,7 @@ public class PushOMBuilder extends Abstr
             parent = factory.createOMElement(localName, parent, null);
         }
         if (ns != null) {
-            parent.setNamespaceWithNoFindInCurrentScope(ns);
+            parent.setNamespace(ns, false);
         }
     }
 

Modified: 
webservices/axiom/trunk/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/OMTestSuiteBuilder.java
URL: 
http://svn.apache.org/viewvc/webservices/axiom/trunk/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/OMTestSuiteBuilder.java?rev=1565684&r1=1565683&r2=1565684&view=diff
==============================================================================
--- 
webservices/axiom/trunk/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/OMTestSuiteBuilder.java
 (original)
+++ 
webservices/axiom/trunk/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/OMTestSuiteBuilder.java
 Fri Feb  7 15:19:26 2014
@@ -82,6 +82,25 @@ public class OMTestSuiteBuilder extends 
         addTest(new 
org.apache.axiom.ts.om.attribute.TestHasNameWithNamespace(metaFactory));
         addTest(new 
org.apache.axiom.ts.om.attribute.TestHasNameWithoutNamespace(metaFactory));
         addTest(new 
org.apache.axiom.ts.om.attribute.TestSetLocalName(metaFactory));
+        for (int i=0; i<4; i++) {
+            boolean declare = (i & 1) != 0;
+            boolean owner = (i & 2) != 0;
+            // Valid
+            addTest(new 
org.apache.axiom.ts.om.attribute.TestSetNamespace(metaFactory, "urn:test", "p", 
declare, owner, null, false, "p", declare && owner));
+            addTest(new 
org.apache.axiom.ts.om.attribute.TestSetNamespace(metaFactory, "urn:test", 
null, declare, owner, null, false, null, declare && owner));
+            if (owner) {
+                addTest(new 
org.apache.axiom.ts.om.attribute.TestSetNamespace(metaFactory, "urn:test", "p", 
declare, true, "p", false, "p", false));
+                addTest(new 
org.apache.axiom.ts.om.attribute.TestSetNamespace(metaFactory, "urn:test", "p", 
declare, true, "q", false, "p", declare));
+                addTest(new 
org.apache.axiom.ts.om.attribute.TestSetNamespace(metaFactory, "urn:test", 
null, declare, true, "p", false, "p", false));
+                addTest(new 
org.apache.axiom.ts.om.attribute.TestSetNamespace(metaFactory, "urn:test", 
null, declare, true, "", false, null, declare));
+            }
+            addTest(new 
org.apache.axiom.ts.om.attribute.TestSetNamespace(metaFactory, "", "", declare, 
owner, null, false, "", false));
+            addTest(new 
org.apache.axiom.ts.om.attribute.TestSetNamespace(metaFactory, "", null, 
declare, owner, null, false, "", false));
+            addTest(new 
org.apache.axiom.ts.om.attribute.TestSetNamespace(metaFactory, null, null, 
declare, owner, null, false, "", false));
+            // Invalid
+            addTest(new 
org.apache.axiom.ts.om.attribute.TestSetNamespace(metaFactory, "urn:test", "", 
declare, owner, null, true, null, false));
+            addTest(new 
org.apache.axiom.ts.om.attribute.TestSetNamespace(metaFactory, "", "p", 
declare, owner, null, true, null, false));
+        }
         addTest(new 
org.apache.axiom.ts.om.builder.TestCloseWithInputStream(metaFactory));
         addTest(new 
org.apache.axiom.ts.om.builder.TestCloseWithReader(metaFactory));
         addTest(new 
org.apache.axiom.ts.om.builder.TestCloseWithXMLStreamReader(metaFactory));
@@ -340,11 +359,24 @@ public class OMTestSuiteBuilder extends 
         addTest(new 
org.apache.axiom.ts.om.element.TestSerializeAndConsumeConsumed(metaFactory));
         addTest(new 
org.apache.axiom.ts.om.element.TestSerializeAndConsumePartiallyBuilt(metaFactory));
         addTest(new 
org.apache.axiom.ts.om.element.TestSerializeAndConsumeWithIncompleteDescendant(metaFactory));
-        addTest(new 
org.apache.axiom.ts.om.element.TestSetNamespace(metaFactory));
-        addTest(new 
org.apache.axiom.ts.om.element.TestSetNamespaceInvalid(metaFactory));
-        addTest(new 
org.apache.axiom.ts.om.element.TestSetNamespaceWithMatchingBindingInScope(metaFactory));
-        addTest(new 
org.apache.axiom.ts.om.element.TestSetNamespaceWithNullOMNamespace(metaFactory));
-        addTest(new 
org.apache.axiom.ts.om.element.TestSetNamespaceWithNullPrefix(metaFactory));
+        for (int i=0; i<3; i++) {
+            Boolean declare = i == 0 ? null : Boolean.valueOf(i==2);
+            boolean implicitDeclare = declare == null || 
declare.booleanValue();
+            // Valid
+            addTest(new 
org.apache.axiom.ts.om.element.TestSetNamespace(metaFactory, "urn:test", "p", 
declare, null, false, "p", implicitDeclare));
+            addTest(new 
org.apache.axiom.ts.om.element.TestSetNamespace(metaFactory, "urn:test", null, 
declare, null, false, null, implicitDeclare));
+            addTest(new 
org.apache.axiom.ts.om.element.TestSetNamespace(metaFactory, "urn:test", "p", 
declare, "p", false, "p", false));
+            addTest(new 
org.apache.axiom.ts.om.element.TestSetNamespace(metaFactory, "urn:test", "p", 
declare, "q", false, "p", implicitDeclare));
+            addTest(new 
org.apache.axiom.ts.om.element.TestSetNamespace(metaFactory, "urn:test", null, 
declare, "p", false, "p", false));
+            addTest(new 
org.apache.axiom.ts.om.element.TestSetNamespace(metaFactory, "urn:test", null, 
declare, "", false, "", false));
+            addTest(new 
org.apache.axiom.ts.om.element.TestSetNamespace(metaFactory, "urn:test", "", 
declare, null, false, "", implicitDeclare));
+            addTest(new 
org.apache.axiom.ts.om.element.TestSetNamespace(metaFactory, "urn:test", "", 
declare, "", false, "", false));
+            addTest(new 
org.apache.axiom.ts.om.element.TestSetNamespace(metaFactory, "", "", declare, 
null, false, "", false));
+            addTest(new 
org.apache.axiom.ts.om.element.TestSetNamespace(metaFactory, "", null, declare, 
null, false, "", false));
+            addTest(new 
org.apache.axiom.ts.om.element.TestSetNamespace(metaFactory, null, null, 
declare, null, false, "", false));
+            // Invalid
+            addTest(new 
org.apache.axiom.ts.om.element.TestSetNamespace(metaFactory, "", "p", declare, 
null, true, null, false));
+        }
         addTest(new org.apache.axiom.ts.om.element.TestSetText(metaFactory));
         addTest(new 
org.apache.axiom.ts.om.element.TestSetTextEmptyString(metaFactory));
         addTest(new 
org.apache.axiom.ts.om.element.TestSetTextNull(metaFactory));

Added: 
webservices/axiom/trunk/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/SetNamespaceTestCase.java
URL: 
http://svn.apache.org/viewvc/webservices/axiom/trunk/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/SetNamespaceTestCase.java?rev=1565684&view=auto
==============================================================================
--- 
webservices/axiom/trunk/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/SetNamespaceTestCase.java
 (added)
+++ 
webservices/axiom/trunk/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/SetNamespaceTestCase.java
 Fri Feb  7 15:19:26 2014
@@ -0,0 +1,161 @@
+/*
+ * 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;
+
+import java.util.Iterator;
+
+import javax.xml.namespace.QName;
+
+import org.apache.axiom.om.OMElement;
+import org.apache.axiom.om.OMFactory;
+import org.apache.axiom.om.OMMetaFactory;
+import org.apache.axiom.om.OMNamedInformationItem;
+import org.apache.axiom.om.OMNamespace;
+import org.apache.axiom.ts.AxiomTestCase;
+
+public abstract class SetNamespaceTestCase extends AxiomTestCase {
+    private final String namespaceURI;
+    private final String prefix;
+    private final String prefixInScope;
+    private final boolean invalid;
+    private final String expectedPrefix;
+    private final boolean expectNSDecl;
+    
+    /**
+     * Constructor.
+     * 
+     * @param metaFactory
+     *            the meta factory
+     * @param namespaceURI
+     *            the namespace URI to set or <code>null</code> to set a 
<code>null</code>
+     *            {@link OMNamespace}
+     * @param prefix
+     *            the prefix to set or <code>null</code> to generate a prefix
+     * @param prefixInScope
+     *            the prefix of an existing namespace declaration in scope for 
the given namespace
+     *            URI, or <code>null</code> if no matching namespace 
declaration is in scope
+     * @param invalid
+     *            flag indicating whether the namespace is invalid and
+     *            {@link OMNamedInformationItem#setNamespace(OMNamespace, 
boolean)} is expected to
+     *            throw an {@link IllegalArgumentException}
+     * @param expectedPrefix
+     *            the expected prefix of the attribute after the invocation of
+     *            {@link OMNamedInformationItem#setNamespace(OMNamespace, 
boolean)}, or null if the
+     *            method is expected to generate a prefix
+     * @param expectNSDecl
+     *            indicates whether {@link 
OMNamedInformationItem#setNamespace(OMNamespace, boolean)}
+     *            is expected to generate a namespace declaration on the owner 
element
+     */
+    public SetNamespaceTestCase(OMMetaFactory metaFactory, String 
namespaceURI, String prefix, String prefixInScope, boolean invalid, String 
expectedPrefix, boolean expectNSDecl) {
+        super(metaFactory);
+        this.namespaceURI = namespaceURI;
+        this.prefix = prefix;
+        this.prefixInScope = prefixInScope;
+        this.invalid = invalid;
+        this.expectedPrefix = expectedPrefix;
+        this.expectNSDecl = expectNSDecl;
+        if (namespaceURI != null) {
+            addTestParameter("uri", namespaceURI);
+        }
+        if (prefix != null) {
+            addTestParameter("prefix", prefix);
+        }
+        if (prefixInScope != null) {
+            addTestParameter("prefixInScope", prefixInScope);
+        }
+        addTestParameter("invalid", invalid);
+    }
+
+    protected abstract boolean context();
+    protected abstract OMNamedInformationItem node(OMFactory factory, 
OMElement context);
+    protected abstract void setNamespace(OMNamedInformationItem node, 
OMNamespace ns);
+    
+    protected final void runTest() throws Throwable {
+        OMFactory factory = metaFactory.getOMFactory();
+        OMElement element;
+        OMNamedInformationItem node;
+        if (context()) {
+            // To avoid collisions if prefixInScope is the empty string
+            OMNamespace dummyNS = factory.createOMNamespace("__dummy__", 
"__dummy__");
+            OMElement parent = factory.createOMElement("parent", dummyNS);
+            element = factory.createOMElement("test", dummyNS, parent);
+            if (prefixInScope != null) {
+                if (prefixInScope.length() == 0) {
+                    parent.declareDefaultNamespace(namespaceURI);
+                } else {
+                    parent.declareNamespace(namespaceURI, prefixInScope);
+                }
+            }
+        } else {
+            element = null;
+        }
+        node = node(factory, element);
+        OMNamespace ns = namespaceURI == null ? null : 
factory.createOMNamespace(namespaceURI, prefix);
+        try {
+            setNamespace(node, ns);
+            if (invalid) {
+                fail("Expected IllegalArgumentException");
+            }
+        } catch (IllegalArgumentException ex) {
+            if (invalid) {
+                return;
+            } else {
+                throw ex;
+            }
+        }
+        String expectedPrefix;
+        if (this.expectedPrefix == null) {
+            expectedPrefix = node.getPrefix();
+            assertNotNull(expectedPrefix);
+            assertFalse(expectedPrefix.length() == 0);
+        } else {
+            expectedPrefix = this.expectedPrefix;
+            if (expectedPrefix.length() == 0) {
+                assertNull(node.getPrefix());
+            } else {
+                assertEquals(expectedPrefix, node.getPrefix());
+            }
+        }
+        if (namespaceURI == null || namespaceURI.length() == 0) {
+            assertNull(node.getNamespace());
+        } else {
+            OMNamespace actualNS = node.getNamespace();
+            assertEquals(expectedPrefix, actualNS.getPrefix());
+            assertEquals(namespaceURI, actualNS.getNamespaceURI());
+        }
+        if (namespaceURI == null || namespaceURI.length() == 0) {
+            assertNull(node.getNamespaceURI());
+        } else {
+            assertEquals(namespaceURI, node.getNamespaceURI());
+        }
+        QName qname = node.getQName();
+        assertEquals(expectedPrefix, qname.getPrefix());
+        assertEquals(namespaceURI == null ? "" : namespaceURI, 
qname.getNamespaceURI());
+        if (element != null) {
+            Iterator it = element.getAllDeclaredNamespaces();
+            if (expectNSDecl) {
+                assertTrue(it.hasNext());
+                OMNamespace decl = (OMNamespace)it.next();
+                assertEquals(expectedPrefix, decl.getPrefix());
+                assertEquals(namespaceURI == null ? "" : namespaceURI, 
decl.getNamespaceURI());
+            }
+            assertFalse(it.hasNext());
+        }
+    }
+}

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

Added: 
webservices/axiom/trunk/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/attribute/TestSetNamespace.java
URL: 
http://svn.apache.org/viewvc/webservices/axiom/trunk/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/attribute/TestSetNamespace.java?rev=1565684&view=auto
==============================================================================
--- 
webservices/axiom/trunk/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/attribute/TestSetNamespace.java
 (added)
+++ 
webservices/axiom/trunk/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/attribute/TestSetNamespace.java
 Fri Feb  7 15:19:26 2014
@@ -0,0 +1,89 @@
+/*
+ * 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.attribute;
+
+import org.apache.axiom.om.OMAttribute;
+import org.apache.axiom.om.OMElement;
+import org.apache.axiom.om.OMFactory;
+import org.apache.axiom.om.OMMetaFactory;
+import org.apache.axiom.om.OMNamedInformationItem;
+import org.apache.axiom.om.OMNamespace;
+import org.apache.axiom.ts.om.SetNamespaceTestCase;
+
+/**
+ * Test the behavior of {@link 
OMNamedInformationItem#setNamespace(OMNamespace, boolean)} on an
+ * {@link OMAttribute}.
+ */
+public class TestSetNamespace extends SetNamespaceTestCase {
+    private final boolean declare;
+    private final boolean owner;
+    
+    /**
+     * Constructor.
+     * 
+     * @param metaFactory
+     *            the meta factory
+     * @param namespaceURI
+     *            the namespace URI to set or <code>null</code> to set a 
<code>null</code>
+     *            {@link OMNamespace}
+     * @param prefix
+     *            the prefix to set or <code>null</code> to generate a prefix
+     * @param declare
+     *            the value of the <code>declare</code> argument
+     * @param owner
+     *            flag indicating whether the attribute should have an owner 
element
+     * @param prefixInScope
+     *            the prefix of an existing namespace declaration in scope for 
the given namespace
+     *            URI, or <code>null</code> if no matching namespace 
declaration is in scope
+     * @param invalid
+     *            flag indicating whether the namespace is invalid and
+     *            {@link OMNamedInformationItem#setNamespace(OMNamespace, 
boolean)} is expected to
+     *            throw an {@link IllegalArgumentException}
+     * @param expectedPrefix
+     *            the expected prefix of the attribute after the invocation of
+     *            {@link OMNamedInformationItem#setNamespace(OMNamespace, 
boolean)}, or null if the
+     *            method is expected to generate a prefix
+     * @param expectNSDecl
+     *            indicates whether {@link 
OMNamedInformationItem#setNamespace(OMNamespace, boolean)}
+     *            is expected to generate a namespace declaration on the owner 
element
+     */
+    public TestSetNamespace(OMMetaFactory metaFactory, String namespaceURI, 
String prefix, boolean declare, boolean owner, String prefixInScope, boolean 
invalid, String expectedPrefix, boolean expectNSDecl) {
+        super(metaFactory, namespaceURI, prefix, prefixInScope, invalid, 
expectedPrefix, expectNSDecl);
+        this.declare = declare;
+        this.owner = owner;
+        addTestParameter("declare", declare);
+        addTestParameter("owner", owner);
+    }
+
+    protected boolean context() {
+        return owner;
+    }
+
+    protected OMNamedInformationItem node(OMFactory factory, OMElement 
context) {
+        if (context != null) {
+            return context.addAttribute("attr", "value", null);
+        } else {
+            return factory.createOMAttribute("attr", null, "value");
+        }
+    }
+
+    protected void setNamespace(OMNamedInformationItem node, OMNamespace ns) {
+        node.setNamespace(ns, declare);
+    }
+}

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

Modified: 
webservices/axiom/trunk/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/element/TestSetNamespace.java
URL: 
http://svn.apache.org/viewvc/webservices/axiom/trunk/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/element/TestSetNamespace.java?rev=1565684&r1=1565683&r2=1565684&view=diff
==============================================================================
--- 
webservices/axiom/trunk/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/element/TestSetNamespace.java
 (original)
+++ 
webservices/axiom/trunk/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/element/TestSetNamespace.java
 Fri Feb  7 15:19:26 2014
@@ -18,35 +18,41 @@
  */
 package org.apache.axiom.ts.om.element;
 
-import java.util.Iterator;
-
-import javax.xml.namespace.QName;
-
 import org.apache.axiom.om.OMElement;
 import org.apache.axiom.om.OMFactory;
 import org.apache.axiom.om.OMMetaFactory;
+import org.apache.axiom.om.OMNamedInformationItem;
 import org.apache.axiom.om.OMNamespace;
-import org.apache.axiom.ts.AxiomTestCase;
+import org.apache.axiom.ts.om.SetNamespaceTestCase;
 
 /**
- * Tests the behavior of {@link OMElement#setNamespace(OMNamespace)} if no 
matching namespace
- * binding is in scope. In this case, the method must add a new namespace 
declaration.
+ * Tests the behavior of {@link OMElement#setNamespace(OMNamespace)} and
+ * {@link OMNamedInformationItem#setNamespace(OMNamespace, boolean)}.
  */
-public class TestSetNamespace extends AxiomTestCase {
-    public TestSetNamespace(OMMetaFactory metaFactory) {
-        super(metaFactory);
+public class TestSetNamespace extends SetNamespaceTestCase {
+    private final Boolean declare;
+    
+    public TestSetNamespace(OMMetaFactory metaFactory, String namespaceURI, 
String prefix, Boolean declare, String prefixInScope, boolean invalid, String 
expectedPrefix, boolean expectNSDecl) {
+        super(metaFactory, namespaceURI, prefix, prefixInScope, invalid, 
expectedPrefix, expectNSDecl);
+        this.declare = declare;
+        if (declare != null) {
+            addTestParameter("declare", declare.booleanValue());
+        }
+    }
+
+    protected boolean context() {
+        return true;
+    }
+
+    protected OMNamedInformationItem node(OMFactory factory, OMElement 
context) {
+        return context;
     }
 
-    protected void runTest() throws Throwable {
-        OMFactory factory = metaFactory.getOMFactory();
-        OMElement element = factory.createOMElement(new QName("test"));
-        OMNamespace ns = factory.createOMNamespace("urn:test", "p");
-        element.setNamespace(ns);
-        assertEquals(new QName("urn:test", "test"), element.getQName());
-        assertEquals(ns, element.getNamespace());
-        Iterator it = element.getAllDeclaredNamespaces();
-        assertTrue(it.hasNext());
-        assertEquals(ns, it.next());
-        assertFalse(it.hasNext());
+    protected void setNamespace(OMNamedInformationItem node, OMNamespace ns) {
+        if (declare == null) {
+            ((OMElement)node).setNamespace(ns);
+        } else {
+            node.setNamespace(ns, declare.booleanValue());
+        }
     }
 }


Reply via email to