Author: veithen
Date: Sat Dec 11 15:57:22 2010
New Revision: 1044669
URL: http://svn.apache.org/viewvc?rev=1044669&view=rev
Log:
* Improved the specification of OMElement#addAttribute(String, String,
OMNamespace) to include a description of the expected behavior if an attribute
with the same local name and namespace URI already exists. The specification is
based on the behavior of the LLOM implementation.
* Added a test case and corrected the behavior of the DOOM implementation of
that method.
* Fixed some additional issues that were discoverd:
- DOOM didn't reset the owner when an attribute is removed from an element.
- AttributeHelper didn't import the OMNamespace correctly. This resulted in a
ClassCastException.
Modified:
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/OMElement.java
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/util/AttributeHelper.java
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/AttributeMap.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/main/java/org/apache/axiom/om/OMElement.java
URL:
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/OMElement.java?rev=1044669&r1=1044668&r2=1044669&view=diff
==============================================================================
---
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/OMElement.java
(original)
+++
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/OMElement.java
Sat Dec 11 15:57:22 2010
@@ -232,21 +232,25 @@ public interface OMElement extends OMNod
OMAttribute addAttribute(OMAttribute attr);
/**
- * Adds an attribute to the current element.
- * <p/>
- * <p>This function does not check to make sure that the given attribute
value can be serialized
- * directly as an XML value. The caller may, for example, pass a string
with the character
- * 0x01.
- *
- * @param attributeName The "local name" for the attribute.
- * @param value The string value of the attribute.
- * @param ns The namespace has to be one of the in scope
namespace. i.e. the passed
- * namespace must be declared in the parent element
of this attribute or
- * ancestors of the parent element of the attribute.
+ * Adds an attribute to this element.
+ * <p>
+ * If the element already has an attribute with the same local name and
namespace URI, then this
+ * existing attribute will be removed from the element, i.e. this method
will always create a
+ * new {...@link OMAttribute} instance and never update an existing one.
+ *
+ * @param localName
+ * The local name for the attribute.
+ * @param value
+ * The string value of the attribute. This function does not
check to make sure that
+ * the given attribute value can be serialized directly as an
XML value. The caller
+ * may, for example, pass a string with the character 0x01.
+ * @param ns
+ * The namespace has to be one of the in scope namespace. i.e.
the passed namespace
+ * must be declared in the parent element of this attribute or
ancestors of the
+ * parent element of the attribute.
* @return Returns the added attribute.
*/
- OMAttribute addAttribute(String attributeName, String value,
- OMNamespace ns);
+ OMAttribute addAttribute(String localName, String value, OMNamespace ns);
/**
* Method removeAttribute
Modified:
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/util/AttributeHelper.java
URL:
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/util/AttributeHelper.java?rev=1044669&r1=1044668&r2=1044669&view=diff
==============================================================================
---
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/util/AttributeHelper.java
(original)
+++
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/util/AttributeHelper.java
Sat Dec 11 15:57:22 2010
@@ -22,6 +22,7 @@ package org.apache.axiom.om.util;
import org.apache.axiom.om.OMAttribute;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
+import org.apache.axiom.om.OMNamespace;
/** Helper class for attributes. */
public class AttributeHelper {
@@ -37,8 +38,9 @@ public class AttributeHelper {
if
(omAttribute.getOMFactory().getClass().isInstance(omElement.getOMFactory())) {
omElement.addAttribute(omAttribute);
} else {
+ OMNamespace ns = omAttribute.getNamespace();
omElement.addAttribute(omAttribute.getLocalName(),
omAttribute.getAttributeValue(),
- omAttribute.getNamespace());
+
omElement.getOMFactory().createOMNamespace(ns.getNamespaceURI(),
ns.getPrefix()));
}
}
}
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=1044669&r1=1044668&r2=1044669&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 Dec 11 15:57:22 2010
@@ -277,7 +277,7 @@ public abstract class OMElementTestBase
* Test that {...@link OMElement#addAttribute(OMAttribute)} behaves
correctly when an attribute
* with the same name and namespace URI already exists.
*/
- public void testAddAttributeReplace() {
+ public void testAddAttributeReplace1() {
OMFactory factory = omMetaFactory.getOMFactory();
// Use same namespace URI but different prefixes
OMNamespace ns1 = factory.createOMNamespace("urn:ns", "p1");
@@ -295,6 +295,28 @@ public abstract class OMElementTestBase
assertSame(element, att2.getOwner());
}
+ /**
+ * Test that {...@link OMElement#addAttribute(String, String,
OMNamespace)} behaves correctly when
+ * an attribute with the same name and namespace URI already exists.
+ */
+ public void testAddAttributeReplace2() {
+ OMFactory factory = omMetaFactory.getOMFactory();
+ // Use same namespace URI but different prefixes
+ OMNamespace ns1 = factory.createOMNamespace("urn:ns", "p1");
+ OMNamespace ns2 = factory.createOMNamespace("urn:ns", "p2");
+ OMElement element = factory.createOMElement(new QName("test"));
+ OMAttribute att1 = element.addAttribute("test", "value1", ns1);
+ OMAttribute att2 = element.addAttribute("test", "value2", ns2);
+ Iterator it = element.getAllAttributes();
+ assertTrue(it.hasNext());
+ assertSame(att2, it.next());
+ assertFalse(it.hasNext());
+ assertNull(att1.getOwner());
+ assertSame(element, att2.getOwner());
+ assertEquals("value1", att1.getAttributeValue());
+ assertEquals("value2", att2.getAttributeValue());
+ }
+
public void testAddAttributeWithoutExistingNamespaceDeclaration() {
OMFactory factory = omMetaFactory.getOMFactory();
OMElement element = factory.createOMElement(new QName("test"));
Modified:
webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/AttributeMap.java
URL:
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/AttributeMap.java?rev=1044669&r1=1044668&r2=1044669&view=diff
==============================================================================
---
webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/AttributeMap.java
(original)
+++
webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/AttributeMap.java
Sat Dec 11 15:57:22 2010
@@ -90,7 +90,7 @@ public class AttributeMap extends NamedN
if (i >= 0) { // There's an attribute already with this attr's name
previous = (AttrImpl) nodes.elementAt(i);
nodes.setElementAt(attr, i);
- previous.parent = this.ownerNode;
+ previous.parent = null;
previous.isOwned(false);
// make sure it won't be mistaken with defaults in case it's reused
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=1044669&r1=1044668&r2=1044669&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 Dec 11 15:57:22 2010
@@ -55,7 +55,6 @@ import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
-import java.util.Vector;
/** Implementation of the org.w3c.dom.Element and org.apache.axiom.om.Element
interfaces. */
public class ElementImpl extends ParentNode implements Element, OMElement,
@@ -699,12 +698,7 @@ public class ElementImpl extends ParentN
return attr;
}
- /**
- * The behaviour of this is the same as org.w3c.dom.Element#setAttributeNS
- *
- * @see org.apache.axiom.om.OMElement#addAttribute(String, String,
org.apache.axiom.om.OMNamespace)
- */
- public OMAttribute addAttribute(String attributeName, String value,
+ public OMAttribute addAttribute(String localName, String value,
OMNamespace ns) {
if (ns != null) {
String uri = ns.getNamespaceURI();
@@ -716,13 +710,7 @@ public class ElementImpl extends ParentN
}
}
}
- if (ns != null) {
- return this.addAttribute(ns.getNamespaceURI(), ns.getPrefix() + ":"
- + attributeName, value);
- } else {
- return this.addAttribute(null, attributeName, value);
- }
-
+ return addAttribute(new AttrImpl(ownerNode, localName, ns, value,
factory));
}
/**
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=1044669&r1=1044668&r2=1044669&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 Dec 11 15:57:22 2010
@@ -619,28 +619,6 @@ public class OMElementImpl extends OMNod
}
}
- /**
- *
- * Creates an <code>OMAttributeImpl</code> instance out of the given
arguments and
- * inserts that attribute to this element. Implementor can decide as to
insert this
- * in the front or at the end of set of attributes.
- *
- * <p>The owner of the attribute is set to be the particular
<code>OMElement</code>.
- * If the attribute already has an owner then the attribute is cloned
(i.e. its name,
- * value and namespace are copied to a new attribute) and the new
attribute is added
- * to the element. It's owner is then set to be the particular
<code>OMElement</code>.
- *
- * @param attributeName The name of the attribute
- * @param value The value of the attribute
- * @param ns The namespace of the attribute
- *
- * @return The attribute that was added to the element. Note: The added
attribute
- * may not be the same instance that was given to add. This can happen if
the given
- * attribute already has an owner. In such case the returned attribute and
the given
- * attribute are <i>equal</i> but not the same instance.
- *
- * @see OMAttributeImpl#equals(Object)
- */
public OMAttribute addAttribute(String attributeName, String value,
OMNamespace ns) {
OMNamespace namespace = null;