You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-dev@axis.apache.org by ch...@apache.org on 2005/08/04 10:42:19 UTC

svn commit: r227324 - in /webservices/axis/trunk/java/modules/xml: src/org/apache/axis2/om/ src/org/apache/axis2/om/impl/llom/ src/org/apache/axis2/om/impl/llom/factory/ src/org/apache/axis2/soap/impl/llom/ test/org/apache/axis2/om/

Author: chinthaka
Date: Thu Aug  4 01:41:46 2005
New Revision: 227324

URL: http://svn.apache.org/viewcvs?rev=227324&view=rev
Log:
- Fixing some OM Bugs
- adding more tests on OM

Modified:
    webservices/axis/trunk/java/modules/xml/src/org/apache/axis2/om/OMFactory.java
    webservices/axis/trunk/java/modules/xml/src/org/apache/axis2/om/impl/llom/OMAttributeImpl.java
    webservices/axis/trunk/java/modules/xml/src/org/apache/axis2/om/impl/llom/OMElementImpl.java
    webservices/axis/trunk/java/modules/xml/src/org/apache/axis2/om/impl/llom/factory/OMLinkedListImplFactory.java
    webservices/axis/trunk/java/modules/xml/src/org/apache/axis2/soap/impl/llom/SOAPElement.java
    webservices/axis/trunk/java/modules/xml/test/org/apache/axis2/om/OMElementTest.java

Modified: webservices/axis/trunk/java/modules/xml/src/org/apache/axis2/om/OMFactory.java
URL: http://svn.apache.org/viewcvs/webservices/axis/trunk/java/modules/xml/src/org/apache/axis2/om/OMFactory.java?rev=227324&r1=227323&r2=227324&view=diff
==============================================================================
--- webservices/axis/trunk/java/modules/xml/src/org/apache/axis2/om/OMFactory.java (original)
+++ webservices/axis/trunk/java/modules/xml/src/org/apache/axis2/om/OMFactory.java Thu Aug  4 01:41:46 2005
@@ -32,6 +32,7 @@
      * @return
      */
     public OMElement createOMElement(String localName, OMNamespace ns);
+    public OMElement createOMElement(String localName, OMNamespace ns, OMContainer parent);
 
     /**
      * @param localName

Modified: webservices/axis/trunk/java/modules/xml/src/org/apache/axis2/om/impl/llom/OMAttributeImpl.java
URL: http://svn.apache.org/viewcvs/webservices/axis/trunk/java/modules/xml/src/org/apache/axis2/om/impl/llom/OMAttributeImpl.java?rev=227324&r1=227323&r2=227324&view=diff
==============================================================================
--- webservices/axis/trunk/java/modules/xml/src/org/apache/axis2/om/impl/llom/OMAttributeImpl.java (original)
+++ webservices/axis/trunk/java/modules/xml/src/org/apache/axis2/om/impl/llom/OMAttributeImpl.java Thu Aug  4 01:41:46 2005
@@ -55,15 +55,15 @@
     }
 
     /**
-     * Method getQName
      *
      * @return qname
      */
     public QName getQName() {
-        String namespaceName = (namespace != null)
-                ? namespace.getName()
-                : null;
-        return new QName(namespaceName, localName);
+        if(namespace != null){
+            return new QName(namespace.getName(), localName, namespace.getPrefix());
+        }else{
+            return new QName(localName);
+        }
     }
 
     // -------- Getters and Setters

Modified: webservices/axis/trunk/java/modules/xml/src/org/apache/axis2/om/impl/llom/OMElementImpl.java
URL: http://svn.apache.org/viewcvs/webservices/axis/trunk/java/modules/xml/src/org/apache/axis2/om/impl/llom/OMElementImpl.java?rev=227324&r1=227323&r2=227324&view=diff
==============================================================================
--- webservices/axis/trunk/java/modules/xml/src/org/apache/axis2/om/impl/llom/OMElementImpl.java (original)
+++ webservices/axis/trunk/java/modules/xml/src/org/apache/axis2/om/impl/llom/OMElementImpl.java Thu Aug  4 01:41:46 2005
@@ -76,20 +76,12 @@
         super(parent);
         this.localName = localName;
         if (ns != null) {
-            setNamespace(handleNamespace(ns));
+            setNamespace(ns);
         }
         this.builder = builder;
         firstChild = null;
     }
 
-    /**
-     * @param parent
-     * @param parent
-     */
-    protected OMElementImpl(OMContainer parent) {
-        super(parent);
-        this.done = true;
-    }
 
     /**
      * Constructor OMElementImpl
@@ -98,45 +90,39 @@
      * @param ns
      */
     public OMElementImpl(String localName, OMNamespace ns) {
-        super(null);
-        if (localName == null || localName.trim().length() == 0) {
-            throw new OMException("localname can not be null or empty");
-        }
-        this.localName = localName;
-        this.done = true;
-        if (ns != null) {
-            setNamespace(handleNamespace(ns));
-        }
+        this(localName, ns, null);
     }
 
     /**
-     * Constructor OMElementImpl
+     * This is the basic constructor for OMElement. All the other constructors within this class
+     * will depend on this.
      *
-     * @param localName
-     * @param ns
-     * @param parent
+     * @param localName - this MUST always be not null
+     * @param ns - can be null
+     * @param parent - this should be an OMContainer
      */
     public OMElementImpl(String localName, OMNamespace ns, OMContainer parent) {
         super(parent);
+        if (localName == null || localName.trim().length() == 0) {
+            throw new OMException("localname can not be null or empty");
+        }
         this.localName = localName;
         this.done = true;
         if (ns != null) {
-            setNamespace(handleNamespace(ns));
+            setNamespace(ns);
         }
     }
 
     /**
      * Here it is assumed that this QName passed, at least contains the localName for this element
      *
-     * @param qname
+     * @param qname - this should be valid qname according to javax.xml.namespace.QName
      * @param parent
      * @throws OMException
      */
     public OMElementImpl(QName qname, OMContainer parent) throws OMException {
-        super(parent);
-        this.localName = qname.getLocalPart();
-        this.done = true;
-        handleNamespace(qname);
+       this(qname.getLocalPart(), null, parent);
+        this.ns = handleNamespace(qname);
     }
 
     /**
@@ -144,8 +130,8 @@
      *
      * @param qname
      */
-    private void handleNamespace(QName qname) {
-        OMNamespace ns;
+    private OMNamespace handleNamespace(QName qname) {
+        OMNamespace ns = null;
 
         // first try to find a namespace from the scope
         String namespaceURI = qname.getNamespaceURI();
@@ -158,7 +144,7 @@
              *  1. nsURI = null & parent != null, but ns = null
              *  2. nsURI != null, (parent doesn't have an ns with given URI), but ns = null
              */
-            if ((ns == null) && !"".equals(namespaceURI)) {
+            if (ns == null) {
                 String prefix = qname.getPrefix();
                 if (!"".equals(prefix)) {
                     ns = declareNamespace(namespaceURI, prefix);
@@ -169,9 +155,13 @@
                 }
             }
             if (ns != null) {
-                this.setNamespace(ns);
+                this.ns = (ns);
             }
+        }else{
+           // no namespace URI in the given QName. No need to bother about this ??
         }
+
+        return ns;
     }
 
     /**
@@ -508,8 +498,8 @@
     public void setFirstChild(OMNode firstChild) {
         if (firstChild != null) {
             firstChild.setParent(this);
-            this.firstChild = firstChild;
         }
+        this.firstChild = firstChild;
 //
 //        OMNode currentFirstChild = getFirstChild();
 //        if (currentFirstChild != null) {
@@ -780,14 +770,11 @@
      * @param namespace
      */
     public void setNamespace(OMNamespace namespace) {
-        if (ns != null) {
-            OMNamespace ns = findNamespace(namespace.getName(),
-                    namespace.getPrefix());
-            if (ns == null) {
-                declareNamespace(namespace);
-            }
+        OMNamespace nsObject = null;
+        if (namespace != null) {
+            nsObject = handleNamespace(namespace);
         }
-        this.ns = namespace;
+        this.ns = nsObject;
     }
 
     /**

Modified: webservices/axis/trunk/java/modules/xml/src/org/apache/axis2/om/impl/llom/factory/OMLinkedListImplFactory.java
URL: http://svn.apache.org/viewcvs/webservices/axis/trunk/java/modules/xml/src/org/apache/axis2/om/impl/llom/factory/OMLinkedListImplFactory.java?rev=227324&r1=227323&r2=227324&view=diff
==============================================================================
--- webservices/axis/trunk/java/modules/xml/src/org/apache/axis2/om/impl/llom/factory/OMLinkedListImplFactory.java (original)
+++ webservices/axis/trunk/java/modules/xml/src/org/apache/axis2/om/impl/llom/factory/OMLinkedListImplFactory.java Thu Aug  4 01:41:46 2005
@@ -57,6 +57,10 @@
         return new OMElementImpl(localName, ns);
     }
 
+    public OMElement createOMElement(String localName, OMNamespace ns, OMContainer parent) {
+        return new OMElementImpl(localName, ns, parent);
+    }
+
     /**
      * Method createOMElement
      *

Modified: webservices/axis/trunk/java/modules/xml/src/org/apache/axis2/soap/impl/llom/SOAPElement.java
URL: http://svn.apache.org/viewcvs/webservices/axis/trunk/java/modules/xml/src/org/apache/axis2/soap/impl/llom/SOAPElement.java?rev=227324&r1=227323&r2=227324&view=diff
==============================================================================
--- webservices/axis/trunk/java/modules/xml/src/org/apache/axis2/soap/impl/llom/SOAPElement.java (original)
+++ webservices/axis/trunk/java/modules/xml/src/org/apache/axis2/soap/impl/llom/SOAPElement.java Thu Aug  4 01:41:46 2005
@@ -36,7 +36,7 @@
     protected SOAPElement(OMElement parent,
                           String localName,
                           boolean extractNamespaceFromParent) throws SOAPProcessingException {
-        super(parent);
+        super(localName, null, parent);
         if (parent == null) {
             throw new SOAPProcessingException(
                     " Can not create " + localName +

Modified: webservices/axis/trunk/java/modules/xml/test/org/apache/axis2/om/OMElementTest.java
URL: http://svn.apache.org/viewcvs/webservices/axis/trunk/java/modules/xml/test/org/apache/axis2/om/OMElementTest.java?rev=227324&r1=227323&r2=227324&view=diff
==============================================================================
--- webservices/axis/trunk/java/modules/xml/test/org/apache/axis2/om/OMElementTest.java (original)
+++ webservices/axis/trunk/java/modules/xml/test/org/apache/axis2/om/OMElementTest.java Thu Aug  4 01:41:46 2005
@@ -6,6 +6,7 @@
 import org.apache.commons.logging.LogFactory;
 
 import javax.xml.namespace.QName;
+import java.util.Iterator;
 
 /**
  * Copyright 2001-2004 The Apache Software Foundation.
@@ -28,6 +29,11 @@
     private static final String WSA_TO = "To";
     private Log log = LogFactory.getLog(getClass());
 
+    OMFactory factory = OMAbstractFactory.getOMFactory();
+    private OMElement firstElement;
+    private OMElement secondElement;
+
+
     public OMElementTest(String testName) {
         super(testName);
     }
@@ -36,7 +42,11 @@
      * @see TestCase#setUp()
      */
     protected void setUp() throws Exception {
-
+       OMNamespace testingNamespace = factory.createOMNamespace(
+                        "http://testing.axis2.org", "axis2");
+        firstElement = factory.createOMElement("FirstElement", testingNamespace);
+        secondElement = factory.createOMElement("SecondElement", factory.createOMNamespace(
+                                "http://testing.axis2.org", "axis2"), firstElement);
     }
 
     public void testGetText() {
@@ -54,6 +64,58 @@
         } catch (Exception e) {
             log.info(e.getMessage());
         }
+    }
+
+    public void testConstructors(){
+
+        try {
+            OMElement elementWithNoLocalName = factory.createOMElement("", null);
+            fail("This should fail as OMElement should not be allowed to create without a local name ");
+        } catch (Exception e) {
+            assertTrue(true);
+        }
+
+        assertEquals("Namespace having same information, declared in the same context, should share" +
+                " the same namespace object",firstElement.getNamespace(), secondElement.getNamespace());
+        assertEquals("OMElement children addition has not worked properly", secondElement, firstElement.getFirstElement());
+
+        OMNamespace testNamespace2 = factory.createOMNamespace("ftp://anotherTest.axis2.org", "axis2");
+        firstElement.declareNamespace(testNamespace2);
+
+        OMNamespace inheritedSecondNamespace = secondElement.findNamespace(testNamespace2.getName(),
+                testNamespace2.getPrefix());
+        assertNotNull("Children should inherit namespaces declared in parent", inheritedSecondNamespace);
+        assertEquals("inherited namespace uri should be equal", inheritedSecondNamespace.getName(), testNamespace2.getName());
+        assertEquals("inherited namespace prefix should be equal", inheritedSecondNamespace.getPrefix(), testNamespace2.getPrefix());
+
+
+    }
+
+    public void testChildDetachment() {
+        OMNamespace testNamespace2 = factory.createOMNamespace("ftp://anotherTest.axis2.org", "axis2");
+        
+        secondElement.detach();
+        assertTrue("OMElement children detachment has not worked properly", !secondElement.equals(firstElement.getFirstElement()));
+        assertNull("First Element should not contain elements after detaching. ", firstElement.getFirstElement());
+        assertNull("First Element should not contain elements after detaching. ", firstElement.getFirstChild());
+        assertNull(secondElement.findNamespace(testNamespace2.getName(), testNamespace2.getPrefix()));
+
+        firstElement.addChild(secondElement);
+        firstElement.setText("Some Sample Text");
+
+        assertTrue("First added child must be the first child", secondElement.equals(firstElement.getFirstChild()));
+        Iterator children = firstElement.getChildren();
+        int childCount = 0;
+        while (children.hasNext()) {
+            Object o = children.next();
+            childCount++;
+        }
+        assertEquals("Children count should be two", childCount, 2);
+
+        secondElement.detach();
+        assertTrue("First child should be the text child", firstElement.getFirstChild() instanceof OMText);
+
+
     }
 
 }