You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commons-dev@ws.apache.org by ch...@apache.org on 2006/03/17 05:18:08 UTC

svn commit: r386525 - in /webservices/commons/modules/axiom: src/org/apache/ws/commons/om/impl/llom/ src/org/apache/ws/commons/soap/impl/llom/builder/ test/org/apache/ws/commons/om/

Author: chinthaka
Date: Thu Mar 16 20:18:06 2006
New Revision: 386525

URL: http://svn.apache.org/viewcvs?rev=386525&view=rev
Log:
improving ns handling
removing un-necessary factory

Added:
    webservices/commons/modules/axiom/test/org/apache/ws/commons/om/NamespaceTest.java   (with props)
Modified:
    webservices/commons/modules/axiom/src/org/apache/ws/commons/om/impl/llom/OMElementImpl.java
    webservices/commons/modules/axiom/src/org/apache/ws/commons/om/impl/llom/OMSerializerUtil.java
    webservices/commons/modules/axiom/src/org/apache/ws/commons/soap/impl/llom/builder/StAXSOAPModelBuilder.java
    webservices/commons/modules/axiom/test/org/apache/ws/commons/om/AttrNsTest.java

Modified: webservices/commons/modules/axiom/src/org/apache/ws/commons/om/impl/llom/OMElementImpl.java
URL: http://svn.apache.org/viewcvs/webservices/commons/modules/axiom/src/org/apache/ws/commons/om/impl/llom/OMElementImpl.java?rev=386525&r1=386524&r2=386525&view=diff
==============================================================================
--- webservices/commons/modules/axiom/src/org/apache/ws/commons/om/impl/llom/OMElementImpl.java (original)
+++ webservices/commons/modules/axiom/src/org/apache/ws/commons/om/impl/llom/OMElementImpl.java Thu Mar 16 20:18:06 2006
@@ -16,17 +16,7 @@
 
 package org.apache.ws.commons.om.impl.llom;
 
-import org.apache.ws.commons.om.OMAbstractFactory;
-import org.apache.ws.commons.om.OMAttribute;
-import org.apache.ws.commons.om.OMConstants;
-import org.apache.ws.commons.om.OMContainer;
-import org.apache.ws.commons.om.OMElement;
-import org.apache.ws.commons.om.OMException;
-import org.apache.ws.commons.om.OMFactory;
-import org.apache.ws.commons.om.OMNamespace;
-import org.apache.ws.commons.om.OMNode;
-import org.apache.ws.commons.om.OMText;
-import org.apache.ws.commons.om.OMXMLParserWrapper;
+import org.apache.ws.commons.om.*;
 import org.apache.ws.commons.om.impl.OMContainerEx;
 import org.apache.ws.commons.om.impl.OMNodeEx;
 import org.apache.ws.commons.om.impl.OMOutputImpl;
@@ -104,7 +94,7 @@
     }
 
     /**
-     * This is the basic constructor for OMElement. All the other constructors 
+     * This is the basic constructor for OMElement. All the other constructors
      * depends on this.
      *
      * @param localName - this MUST always be not null
@@ -112,8 +102,8 @@
      * @param parent    - this should be an OMContainer
      * @param factory   - factory that created this OMElement
      */
-    public OMElementImpl(String localName, OMNamespace ns, OMContainer parent, 
-            OMFactory factory) {
+    public OMElementImpl(String localName, OMNamespace ns, OMContainer parent,
+                         OMFactory factory) {
         super(parent, factory);
         if (localName == null || localName.trim().length() == 0) {
             throw new OMException("localname can not be null or empty");
@@ -131,8 +121,8 @@
      * @param qname - this should be valid qname according to javax.xml.namespace.QName
      * @throws OMException
      */
-    public OMElementImpl(QName qname, OMContainer parent, OMFactory factory) 
-    throws OMException {
+    public OMElementImpl(QName qname, OMContainer parent, OMFactory factory)
+            throws OMException {
         this(qname.getLocalPart(), null, parent, factory);
         this.ns = handleNamespace(qname);
     }
@@ -161,9 +151,7 @@
             if (ns != null) {
                 this.ns = (ns);
             }
-        }
-
-        else
+        } else
 
         {
             // no namespace URI in the given QName. No need to bother about this ??
@@ -250,7 +238,7 @@
     }
 
     /**
-     * Gets the next sibling. This can be an OMAttribute or OMText or 
+     * Gets the next sibling. This can be an OMAttribute or OMText or
      * OMELement for others.
      *
      * @throws OMException
@@ -300,7 +288,12 @@
         if (namespaces == null) {
             this.namespaces = new HashMap(5);
         }
-        namespaces.put(namespace.getPrefix(), namespace);
+        String prefix = namespace.getPrefix();
+        if (prefix == null) {
+            prefix = OMSerializerUtil.getNextNSPrefix();
+            namespace = new OMNamespaceImpl(namespace.getName(), prefix, null);
+        }
+        namespaces.put(prefix, namespace);
         return namespace;
     }
 
@@ -351,7 +344,8 @@
         }
 
         //If the prefix is available and uri is available and its the xml namespace
-        if (prefix != null && prefix.equals(OMConstants.XMLNS_PREFIX) && uri.equals(OMConstants.XMLNS_URI)) {
+        if (prefix != null && prefix.equals(OMConstants.XMLNS_PREFIX) && uri.equals(OMConstants.XMLNS_URI))
+        {
             return new OMNamespaceImpl(uri, prefix, this.factory);
         }
 
@@ -369,13 +363,12 @@
                         (OMNamespace) namespaceListIterator.next();
                 if (omNamespace.getName() != null &&
                         omNamespace.getName().equals(uri)) {
-                       if(ns == null){
+                    if (ns == null) {
                         ns = omNamespace;
-                       }else{
-                               if(omNamespace.getPrefix() == null || omNamespace.getPrefix().length() == 0){
-                                       ns = omNamespace;
-                               }
-                       }
+                    } else if (omNamespace.getPrefix() == null || omNamespace.getPrefix().length() == 0) {
+                            ns = omNamespace;
+
+                    }
                 }
             }
             return ns;
@@ -458,7 +451,8 @@
             this.attributes = new HashMap(5);
         }
         OMNamespace namespace = attr.getNamespace();
-        if (namespace != null && this.findNamespace(namespace.getName(), namespace.getPrefix()) == null) {
+        if (namespace != null && this.findNamespace(namespace.getName(), namespace.getPrefix()) == null)
+        {
             this.declareNamespace(namespace.getName(), namespace.getPrefix());
         }
 
@@ -704,11 +698,11 @@
             if (this.done) {
                 OMSerializerUtil.serializeStartpart(this, omOutput);
                 OMNodeImpl child = (OMNodeImpl) firstChild;
-                while(child != null && ((!(child instanceof OMElement)) || child.isComplete())) {
+                while (child != null && ((!(child instanceof OMElement)) || child.isComplete())) {
                     child.serializeAndConsume(omOutput);
                     child = child.nextSibling;
                 }
-                if(child != null) {
+                if (child != null) {
                     OMElement element = (OMElement) child;
                     element.getBuilder().setCache(false);
                     OMSerializerUtil.serializeByPullStream(element, omOutput, cache);
@@ -726,10 +720,10 @@
 ////////////////////////////////////////////////////////////////////////////////////////////////
 
     /**
-     * This method serializes and consumes without building the object structure in memory. 
-     * Misuse of this method will cause loss of data. So it is advised to use 
-     * populateYourSelf() method, before calling this method, if one wants to 
-     * preserve data in the stream. This was requested during the second Axis2 summit. 
+     * This method serializes and consumes without building the object structure in memory.
+     * Misuse of this method will cause loss of data. So it is advised to use
+     * populateYourSelf() method, before calling this method, if one wants to
+     * preserve data in the stream. This was requested during the second Axis2 summit.
      *
      * @throws XMLStreamException
      */
@@ -839,7 +833,7 @@
     }
 
     /**
-     * Converts a prefix:local qname string into a proper QName, evaluating it 
+     * Converts a prefix:local qname string into a proper QName, evaluating it
      * in the OMElement context. Unprefixed qnames resolve to the local namespace.
      *
      * @param qname prefixed qname string to resolve

Modified: webservices/commons/modules/axiom/src/org/apache/ws/commons/om/impl/llom/OMSerializerUtil.java
URL: http://svn.apache.org/viewcvs/webservices/commons/modules/axiom/src/org/apache/ws/commons/om/impl/llom/OMSerializerUtil.java?rev=386525&r1=386524&r2=386525&view=diff
==============================================================================
--- webservices/commons/modules/axiom/src/org/apache/ws/commons/om/impl/llom/OMSerializerUtil.java (original)
+++ webservices/commons/modules/axiom/src/org/apache/ws/commons/om/impl/llom/OMSerializerUtil.java Thu Mar 16 20:18:06 2006
@@ -31,6 +31,8 @@
 
 public class OMSerializerUtil {
 
+    static long nsCounter = 0;
+
     /**
      * Method serializeEndpart.
      *
@@ -90,20 +92,29 @@
             String uri = namespace.getName();
             String prefix = writer.getPrefix(uri);
             String ns_prefix = namespace.getPrefix();
-            if (ns_prefix != null && !ns_prefix.equals(prefix)) {
-                writer.writeNamespace(ns_prefix, namespace.getName());
+
+            if (uri != null && !"".equals(uri)) {
+                if (prefix == null) {
+                    ns_prefix = ns_prefix == null ? getNextNSPrefix() : ns_prefix;
+                    writer.writeNamespace(ns_prefix, uri);
+                } else if (ns_prefix != null && !ns_prefix.equals(prefix)) {
+                    writer.writeNamespace(ns_prefix, uri);
+                }
             }
+
         }
     }
 
-
     /**
      * Method serializeStartpart.
      *
      * @param omOutput
      * @throws XMLStreamException
      */
-    public static void serializeStartpart(OMElement element, OMOutputImpl omOutput)
+    public static void serializeStartpart
+            (OMElement
+                    element, OMOutputImpl
+                    omOutput)
             throws XMLStreamException {
         String nameSpaceName = null;
         String writer_prefix = null;
@@ -145,8 +156,11 @@
         serializeAttributes(element, omOutput);
     }
 
-    public static void serializeNamespaces(OMElement element,
-                                           org.apache.ws.commons.om.impl.OMOutputImpl omOutput) throws XMLStreamException {
+    public static void serializeNamespaces
+            (OMElement
+                    element,
+             org.apache.ws.commons.om.impl.OMOutputImpl
+                     omOutput) throws XMLStreamException {
         Iterator namespaces = element.getAllDeclaredNamespaces();
         if (namespaces != null) {
             while (namespaces.hasNext()) {
@@ -155,8 +169,11 @@
         }
     }
 
-    public static void serializeAttributes(OMElement element,
-                                           org.apache.ws.commons.om.impl.OMOutputImpl omOutput) throws XMLStreamException {
+    public static void serializeAttributes
+            (OMElement
+                    element,
+             org.apache.ws.commons.om.impl.OMOutputImpl
+                     omOutput) throws XMLStreamException {
         if (element.getAllAttributes() != null) {
             Iterator attributesList = element.getAllAttributes();
             while (attributesList.hasNext()) {
@@ -166,7 +183,6 @@
         }
     }
 
-
     /**
      * Method serializeNormal.
      *
@@ -174,7 +190,10 @@
      * @param cache
      * @throws XMLStreamException
      */
-    public static void serializeNormal(OMElement element, OMOutputImpl omOutput, boolean cache)
+    public static void serializeNormal
+            (OMElement
+                    element, OMOutputImpl
+                    omOutput, boolean cache)
             throws XMLStreamException {
 
         if (cache) {
@@ -185,19 +204,25 @@
         OMNode firstChild = element.getFirstOMChild();
         if (firstChild != null) {
             if (cache) {
-                ((OMNodeEx)firstChild).serialize(omOutput);
+                ((OMNodeEx) firstChild).serialize(omOutput);
             } else {
-                ((OMNodeEx)firstChild).serializeAndConsume(omOutput);
+                ((OMNodeEx) firstChild).serializeAndConsume(omOutput);
             }
         }
         serializeEndpart(omOutput);
     }
 
-    public static void serializeByPullStream(OMElement element, OMOutputImpl omOutput) throws XMLStreamException {
+    public static void serializeByPullStream
+            (OMElement
+                    element, OMOutputImpl
+                    omOutput) throws XMLStreamException {
         serializeByPullStream(element, omOutput, false);
     }
 
-    public static void serializeByPullStream(OMElement element, OMOutputImpl omOutput, boolean cache) throws XMLStreamException {
+    public static void serializeByPullStream
+            (OMElement
+                    element, OMOutputImpl
+                    omOutput, boolean cache) throws XMLStreamException {
         StreamingOMSerializer streamingOMSerializer = new StreamingOMSerializer();
         if (cache) {
             streamingOMSerializer.serialize(element.getXMLStreamReader(),
@@ -207,5 +232,9 @@
             streamingOMSerializer.serialize(xmlStreamReaderWithoutCaching,
                     omOutput);
         }
+    }
+
+    public static String getNextNSPrefix() {
+        return "axis2ns" + ++nsCounter % Long.MAX_VALUE;
     }
 }

Modified: webservices/commons/modules/axiom/src/org/apache/ws/commons/soap/impl/llom/builder/StAXSOAPModelBuilder.java
URL: http://svn.apache.org/viewcvs/webservices/commons/modules/axiom/src/org/apache/ws/commons/soap/impl/llom/builder/StAXSOAPModelBuilder.java?rev=386525&r1=386524&r2=386525&view=diff
==============================================================================
--- webservices/commons/modules/axiom/src/org/apache/ws/commons/soap/impl/llom/builder/StAXSOAPModelBuilder.java (original)
+++ webservices/commons/modules/axiom/src/org/apache/ws/commons/soap/impl/llom/builder/StAXSOAPModelBuilder.java Thu Mar 16 20:18:06 2006
@@ -74,10 +74,6 @@
     private String receiverfaultCode;
     private boolean processingMandatoryFaultElements;
 
-    // We need to have soap factory, temporary, until we find out the correct SOAP version. If user has not provided
-    // a SOAP factory, internally we are creating a default one. This flag will be set if we create one internally, to
-    // warn that this should be replaced later.
-    private boolean isTempSOAPFactory = true;
 
     /**
      * Constructor StAXSOAPModelBuilder
@@ -106,12 +102,12 @@
     public StAXSOAPModelBuilder(XMLStreamReader parser, SOAPFactory factory, String soapVersion) {
         super(factory, parser);
         soapFactory = factory;
-        isTempSOAPFactory = false;
-        soapMessage = soapFactory.createSOAPMessage(this);
-        this.document = soapMessage;
         identifySOAPVersion(soapVersion);
     }
 
+    /**
+     * @param soapVersionURIFromTransport
+     */
     protected void identifySOAPVersion(String soapVersionURIFromTransport) {
 
         SOAPEnvelope soapEnvelope = getSOAPEnvelope();
@@ -128,19 +124,6 @@
                     " Message namespace URI", envelopeNamespace.getPrefix() + ":" + SOAPConstants.FAULT_CODE_VERSION_MISMATCH);
 
         }
-        if (isTempSOAPFactory) {
-            if (SOAP12Constants.SOAP_ENVELOPE_NAMESPACE_URI.equals(namespaceName)) {
-                soapFactory = OMAbstractFactory.getSOAP12Factory();
-                log.debug("Starting to process SOAP 1.2 message");
-            } else if (SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI.equals(namespaceName)) {
-                soapFactory = OMAbstractFactory.getSOAP11Factory();
-                log.debug("Starting to process SOAP 1.1 message");
-
-            } else {
-                throw new SOAPProcessingException("Only SOAP 1.1 or SOAP 1.2 messages are supported in the" +
-                        " system", SOAPConstants.FAULT_CODE_VERSION_MISMATCH);
-            }
-        }
     }
 
     /**
@@ -214,17 +197,18 @@
             }
 
             // determine SOAP version and from that determine a proper factory here.
-            String namespaceURI = this.parser.getNamespaceURI();
-            if (SOAP12Constants.SOAP_ENVELOPE_NAMESPACE_URI.equals(namespaceURI)) {
-                soapFactory = OMAbstractFactory.getSOAP12Factory();
-                log.debug("Starting to process SOAP 1.2 message");
-            } else if (SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI.equals(namespaceURI)) {
-                soapFactory = OMAbstractFactory.getSOAP11Factory();
-                log.debug("Starting to process SOAP 1.1 message");
-
-            } else {
-                throw new SOAPProcessingException("Only SOAP 1.1 or SOAP 1.2 messages are supported in the" +
-                        " system", SOAPConstants.FAULT_CODE_VERSION_MISMATCH);
+            if (soapFactory == null) {
+                String namespaceURI = this.parser.getNamespaceURI();
+                if (SOAP12Constants.SOAP_ENVELOPE_NAMESPACE_URI.equals(namespaceURI)) {
+                    soapFactory = OMAbstractFactory.getSOAP12Factory();
+                    log.debug("Starting to process SOAP 1.2 message");
+                } else if (SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI.equals(namespaceURI)) {
+                    soapFactory = OMAbstractFactory.getSOAP11Factory();
+                    log.debug("Starting to process SOAP 1.1 message");
+                } else {
+                    throw new SOAPProcessingException("Only SOAP 1.1 or SOAP 1.2 messages are supported in the" +
+                            " system", SOAPConstants.FAULT_CODE_VERSION_MISMATCH);
+                }
             }
 
             // create a SOAPMessage to hold the SOAP envelope and assign the SOAP envelope in that.

Modified: webservices/commons/modules/axiom/test/org/apache/ws/commons/om/AttrNsTest.java
URL: http://svn.apache.org/viewcvs/webservices/commons/modules/axiom/test/org/apache/ws/commons/om/AttrNsTest.java?rev=386525&r1=386524&r2=386525&view=diff
==============================================================================
--- webservices/commons/modules/axiom/test/org/apache/ws/commons/om/AttrNsTest.java (original)
+++ webservices/commons/modules/axiom/test/org/apache/ws/commons/om/AttrNsTest.java Thu Mar 16 20:18:06 2006
@@ -26,7 +26,7 @@
 public class AttrNsTest extends AbstractOMSerializationTest {
 
     private String attrNamespaceTestXML = "<?xml version='1.0' encoding='UTF-8'?>\n" +
-            "<foo xmlns:a=\"http://opensource.lk\">" +
+            "<foo xmlns:e=\"http://opensource.lk\">" +
             "    <bar1 b:attr=\"test attr value1\" xmlns:b=\"http://opensource.lk/ns1\">test1</bar1>" +
             "    <bar2 b:attr=\"test attr value2\" xmlns:b=\"http://opensource.lk/ns1\">test2</bar2>" +
             "</foo>";

Added: webservices/commons/modules/axiom/test/org/apache/ws/commons/om/NamespaceTest.java
URL: http://svn.apache.org/viewcvs/webservices/commons/modules/axiom/test/org/apache/ws/commons/om/NamespaceTest.java?rev=386525&view=auto
==============================================================================
--- webservices/commons/modules/axiom/test/org/apache/ws/commons/om/NamespaceTest.java (added)
+++ webservices/commons/modules/axiom/test/org/apache/ws/commons/om/NamespaceTest.java Thu Mar 16 20:18:06 2006
@@ -0,0 +1,78 @@
+package org.apache.ws.commons.om;
+
+import org.custommonkey.xmlunit.XMLTestCase;
+import org.xml.sax.SAXException;
+
+import javax.xml.namespace.QName;
+import javax.xml.parsers.ParserConfigurationException;
+import java.io.IOException;
+import java.util.Iterator;
+/*
+ * Copyright 2004,2005 The Apache Software Foundation.
+ *
+ * Licensed 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.
+ */
+
+public class NamespaceTest extends XMLTestCase {
+
+    public void testNoPrefixNamespaces() throws IOException, ParserConfigurationException, SAXException {
+
+        String expectedXML = "<axis2:DocumentElement xmlns:axis2=\"http://ws.apache.org/axis2\" " +
+                "xmlns:axis2ns1=\"http://undefined-ns-1.org\" xmlns:axis2ns2=\"http://undefined-ns-2.org\">" +
+                "<axis2:FirstChild /><axis2ns2:SecondChild xmlns:axis2ns2=\"http://undefined-ns-2.org\" " +
+                "axis2ns1:testAttr=\"testValue\" /></axis2:DocumentElement>";
+
+        OMFactory omFactory = OMAbstractFactory.getOMFactory();
+        OMNamespace axis2NS = omFactory.createOMNamespace("http://ws.apache.org/axis2", "axis2");
+        OMElement docElement = omFactory.createOMElement("DocumentElement", axis2NS);
+
+        OMNamespace firstOrphanNS = docElement.declareNamespace("http://undefined-ns-1.org", null);
+        docElement.declareNamespace("http://undefined-ns-2.org", null);
+
+        omFactory.createOMElement("FirstChild", axis2NS, docElement);
+
+        OMElement secondChild = omFactory.createOMElement(new QName("http://undefined-ns-2.org", "SecondChild"), docElement);
+        secondChild.addAttribute("testAttr", "testValue", firstOrphanNS);
+
+
+        Iterator allDeclaredNamespaces = docElement.getAllDeclaredNamespaces();
+        int namespaceCount = 0;
+        while (allDeclaredNamespaces.hasNext()) {
+            OMNamespace omNamespace = (OMNamespace) allDeclaredNamespaces.next();
+            namespaceCount++;
+        }
+        assertTrue(namespaceCount == 3);
+
+        assertTrue(secondChild.getNamespace().getPrefix().equals(docElement.findNamespace("http://undefined-ns-2.org", null).getPrefix()));
+
+
+    }
+
+    public void attributeNSTest() {
+        OMFactory fac = OMAbstractFactory.getOMFactory();
+        OMNamespace ns1 = fac.createOMNamespace("http://test.org", "");
+        OMNamespace ns2 = fac.createOMNamespace("http://test2.org", null);
+
+
+        OMElement elem = fac.createOMElement("test", ns1);
+        elem.addAttribute(fac.createOMAttribute("testAttr", ns2, "attrValue"));
+
+        OMNamespace namespace = elem.findNamespace("http://test.org", null);
+        assertTrue(namespace != null && namespace.getPrefix() != null && "".equals(namespace.getPrefix()));
+
+        OMNamespace namespace2 = elem.findNamespace("http://test2.org", null);
+        assertTrue(namespace2 != null && namespace2.getPrefix() != null && "".equals(namespace2.getPrefix()));
+    }
+
+
+}

Propchange: webservices/commons/modules/axiom/test/org/apache/ws/commons/om/NamespaceTest.java
------------------------------------------------------------------------------
    svn:executable = *