You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ws.apache.org by ve...@apache.org on 2014/05/06 23:56:20 UTC

svn commit: r1592903 - in /webservices/axiom/trunk/modules: axiom-api/src/main/java/org/apache/axiom/om/ axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/ axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/ axiom-testsuite/src/main/java/org/apa...

Author: veithen
Date: Tue May  6 21:56:19 2014
New Revision: 1592903

URL: http://svn.apache.org/r1592903
Log:
Improve consistency of the OMElement#addAttribute(String, String, OMNamespace) API:
* Let LLOM's addAttribute implementation generate a prefix (instead of throwing a NPE) if the prefix is null and no namespace declaration for the given namespace URI is found.
* Add test coverage for the scenario where the prefix is null and a namespace declaration for the given URI is found (in which case the corresponding prefix is reused). This worked correctly in LLOM.
* Make DOOM's behavior consistent with LLOM.
* Update Javadoc of the addAttribute method.

Added:
    webservices/axiom/trunk/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/element/TestAddAttributeGeneratedPrefix.java
    webservices/axiom/trunk/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/element/TestAddAttributeReuseExistingPrefix.java
Modified:
    webservices/axiom/trunk/modules/axiom-api/src/main/java/org/apache/axiom/om/OMElement.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/OMElementImpl.java
    webservices/axiom/trunk/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/OMTestSuiteBuilder.java

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=1592903&r1=1592902&r2=1592903&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 Tue May  6 21:56:19 2014
@@ -365,9 +365,11 @@ public interface OMElement extends OMNod
      *            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.
+     *            The namespace for the attribute. If no corresponding namespace declaration is in
+     *            scope, then a new namespace declaration will be added to the element. The
+     *            {@link OMNamespace} may have a <code>null</code> prefix, in which case the method
+     *            will generate a prefix (if no namespace declaration for the given namespace URI is
+     *            in scope) or use an existing one.
      * @return Returns the added attribute.
      * @throws IllegalArgumentException
      *             if an attempt is made to create a prefixed attribute with an empty namespace name

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=1592903&r1=1592902&r2=1592903&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 Tue May  6 21:56:19 2014
@@ -59,6 +59,7 @@ import javax.xml.XMLConstants;
 import javax.xml.namespace.NamespaceContext;
 import javax.xml.namespace.QName;
 import javax.xml.stream.XMLStreamException;
+
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.Reader;
@@ -531,17 +532,16 @@ public class ElementImpl extends ParentN
 
     public OMAttribute addAttribute(String localName, String value,
                                     OMNamespace ns) {
+        OMNamespace namespace = null;
         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);
-                }
+            String namespaceURI = ns.getNamespaceURI();
+            String prefix = ns.getPrefix();
+            namespace = findNamespace(namespaceURI, prefix);
+            if (namespace == null) {
+                namespace = new OMNamespaceImpl(namespaceURI, prefix != null ? prefix : OMSerializerUtil.getNextNSPrefix());
             }
         }
-        return addAttribute(new AttrImpl(null, localName, ns, value, factory));
+        return addAttribute(new AttrImpl(null, localName, namespace, value, factory));
     }
 
     public OMNamespace addNamespaceDeclaration(String uri, String prefix) {

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=1592903&r1=1592902&r2=1592903&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 Tue May  6 21:56:19 2014
@@ -593,7 +593,7 @@ public class OMElementImpl extends OMNod
         attributes.remove(attr.getQName());
     }
 
-    public OMAttribute addAttribute(String attributeName, String value,
+    public OMAttribute addAttribute(String localName, String value,
                                     OMNamespace ns) {
         OMNamespace namespace = null;
         if (ns != null) {
@@ -601,10 +601,10 @@ public class OMElementImpl extends OMNod
             String prefix = ns.getPrefix();
             namespace = findNamespace(namespaceURI, prefix);
             if (namespace == null) {
-                namespace = new OMNamespaceImpl(namespaceURI, prefix);
+                namespace = new OMNamespaceImpl(namespaceURI, prefix != null ? prefix : OMSerializerUtil.getNextNSPrefix());
             }
         }
-        return addAttribute(new OMAttributeImpl(attributeName, namespace, value, this.factory));
+        return addAttribute(new OMAttributeImpl(localName, namespace, value, this.factory));
     }
 
     /**

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=1592903&r1=1592902&r2=1592903&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 Tue May  6 21:56:19 2014
@@ -198,6 +198,8 @@ public class OMTestSuiteBuilder extends 
         addTest(new org.apache.axiom.ts.om.document.sr.TestDTDReaderFromParser(metaFactory, false, false));
         addTest(new org.apache.axiom.ts.om.element.TestAddAttributeAlreadyOwnedByElement(metaFactory));
         addTest(new org.apache.axiom.ts.om.element.TestAddAttributeAlreadyOwnedByOtherElement(metaFactory));
+        addTest(new org.apache.axiom.ts.om.element.TestAddAttributeGeneratedPrefix(metaFactory));
+        addTest(new org.apache.axiom.ts.om.element.TestAddAttributeReuseExistingPrefix(metaFactory));
         addTest(new org.apache.axiom.ts.om.element.TestAddAttributeWithInvalidNamespace1(metaFactory));
         addTest(new org.apache.axiom.ts.om.element.TestAddAttributeWithInvalidNamespace2(metaFactory));
         for (int i=0; i<addAttributeStrategies.length; i++) {

Added: webservices/axiom/trunk/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/element/TestAddAttributeGeneratedPrefix.java
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/element/TestAddAttributeGeneratedPrefix.java?rev=1592903&view=auto
==============================================================================
--- webservices/axiom/trunk/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/element/TestAddAttributeGeneratedPrefix.java (added)
+++ webservices/axiom/trunk/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/element/TestAddAttributeGeneratedPrefix.java Tue May  6 21:56:19 2014
@@ -0,0 +1,51 @@
+/*
+ * 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.element;
+
+import java.util.Iterator;
+
+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.OMNamespace;
+import org.apache.axiom.ts.AxiomTestCase;
+
+/**
+ * Tests that when {@link OMElement#addAttribute(String, String, OMNamespace)} is called with an
+ * {@link OMNamespace} with a <code>null</code> prefix and no namespace declaration for the given
+ * namespace URI is in scope, the method generates a prefix.
+ */
+public class TestAddAttributeGeneratedPrefix extends AxiomTestCase {
+    public TestAddAttributeGeneratedPrefix(OMMetaFactory metaFactory) {
+        super(metaFactory);
+    }
+
+    protected void runTest() throws Throwable {
+        OMFactory factory = metaFactory.getOMFactory();
+        OMElement element = factory.createOMElement("test", null);
+        OMAttribute attr = element.addAttribute("attr", "value", factory.createOMNamespace("urn:test", null));
+        OMNamespace ns = attr.getNamespace();
+        assertTrue(ns.getPrefix().length() > 0);
+        Iterator it = element.getAllDeclaredNamespaces();
+        assertTrue(it.hasNext());
+        assertEquals(ns, it.next());
+        assertFalse(it.hasNext());
+    }
+}

Added: webservices/axiom/trunk/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/element/TestAddAttributeReuseExistingPrefix.java
URL: http://svn.apache.org/viewvc/webservices/axiom/trunk/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/element/TestAddAttributeReuseExistingPrefix.java?rev=1592903&view=auto
==============================================================================
--- webservices/axiom/trunk/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/element/TestAddAttributeReuseExistingPrefix.java (added)
+++ webservices/axiom/trunk/modules/axiom-testsuite/src/main/java/org/apache/axiom/ts/om/element/TestAddAttributeReuseExistingPrefix.java Tue May  6 21:56:19 2014
@@ -0,0 +1,47 @@
+/*
+ * 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.element;
+
+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.OMNamespace;
+import org.apache.axiom.ts.AxiomTestCase;
+
+/**
+ * Tests that when {@link OMElement#addAttribute(String, String, OMNamespace)} is called with an
+ * {@link OMNamespace} with a <code>null</code> prefix and a namespace declaration for the given
+ * namespace URI is in scope, the method reuses the existing prefix instead of generating one.
+ */
+public class TestAddAttributeReuseExistingPrefix extends AxiomTestCase {
+    public TestAddAttributeReuseExistingPrefix(OMMetaFactory metaFactory) {
+        super(metaFactory);
+    }
+
+    protected void runTest() throws Throwable {
+        OMFactory factory = metaFactory.getOMFactory();
+        OMElement parent = factory.createOMElement("parent", null);
+        OMElement element = factory.createOMElement("element", null, parent);
+        parent.declareNamespace("urn:test", "p");
+        OMAttribute attr = element.addAttribute("attr", "test", factory.createOMNamespace("urn:test", null));
+        assertEquals("p", attr.getPrefix());
+        assertFalse(element.getAllDeclaredNamespaces().hasNext());
+    }
+}