axis-java-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aj...@apache.org
Subject svn commit: r167896 - in /webservices/axis/trunk/java/modules/xml/src/org/apache/axis/om/impl/llom: OMElementImpl.java OMSerializerUtil.java util/EmptyIterator.java
Date Tue, 03 May 2005 10:21:36 GMT
Author: ajith
Date: Tue May  3 03:21:36 2005
New Revision: 167896

URL: http://svn.apache.org/viewcvs?rev=167896&view=rev
Log:
Changing the OM Serializer implementation. Now the code is cleaner and probably slightly faster
as well

Added:
    webservices/axis/trunk/java/modules/xml/src/org/apache/axis/om/impl/llom/OMSerializerUtil.java
    webservices/axis/trunk/java/modules/xml/src/org/apache/axis/om/impl/llom/util/EmptyIterator.java
Modified:
    webservices/axis/trunk/java/modules/xml/src/org/apache/axis/om/impl/llom/OMElementImpl.java

Modified: webservices/axis/trunk/java/modules/xml/src/org/apache/axis/om/impl/llom/OMElementImpl.java
URL: http://svn.apache.org/viewcvs/webservices/axis/trunk/java/modules/xml/src/org/apache/axis/om/impl/llom/OMElementImpl.java?rev=167896&r1=167895&r2=167896&view=diff
==============================================================================
--- webservices/axis/trunk/java/modules/xml/src/org/apache/axis/om/impl/llom/OMElementImpl.java
(original)
+++ webservices/axis/trunk/java/modules/xml/src/org/apache/axis/om/impl/llom/OMElementImpl.java
Tue May  3 03:21:36 2005
@@ -20,6 +20,7 @@
 import org.apache.axis.om.impl.llom.serialize.StreamingOMSerializer;
 import org.apache.axis.om.impl.llom.traverse.OMChildrenIterator;
 import org.apache.axis.om.impl.llom.traverse.OMChildrenQNameIterator;
+import org.apache.axis.om.impl.llom.util.EmptyIterator;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
@@ -52,12 +53,12 @@
     /**
      * Field namespaces
      */
-    private HashMap namespaces = null;
+    protected HashMap namespaces = null;
 
     /**
      * Field attributes
      */
-    private HashMap attributes = null;
+    protected HashMap attributes = null;
 
     /**
      * Field log
@@ -67,7 +68,7 @@
     /**
      * Field noPrefixNamespaceCounter
      */
-    private int noPrefixNamespaceCounter = 0;
+    protected int noPrefixNamespaceCounter = 0;
 
     /**
      * Constructor OMElementImpl
@@ -241,7 +242,7 @@
 
 
         child.setParent(this);
-        
+
         child.setPreviousSibling(null);
         child.setNextSibling(firstChild);
         if (firstChild != null) {
@@ -390,26 +391,13 @@
      */
     public Iterator getAttributes() {
         if (attributes == null) {
-            return new Iterator(){
-
-                public void remove() {
-                    throw new UnsupportedOperationException();
-
-                }
-
-                public boolean hasNext() {
-                    return false;  //To change body of implemented methods use File | Settings
| File Templates.
-                }
-
-                public Object next() {
-                    throw new UnsupportedOperationException();
-                }
-            };
+            return new EmptyIterator();
         }
         return attributes.values().iterator();
     }
 
     public Iterator getAttributes(QName qname) {
+        //would there be multiple attributes with the same QName
         return null;  //ToDO
     }
 
@@ -448,7 +436,7 @@
      * @return
      */
     public OMAttribute addAttribute(String attributeName, String value,
-                                       OMNamespace ns) {
+                                    OMNamespace ns) {
         OMNamespace namespace = null;
         if (ns != null) {
             namespace = findNamespace(ns.getName(), ns.getPrefix());
@@ -579,7 +567,7 @@
      * @param text
      */
     public void setText(String text) {
-        
+
         OMNode child = this.getFirstChild();
         while(child != null){
             if(child.getType() == OMNode.TEXT_NODE){
@@ -622,9 +610,13 @@
     public void serializeWithCache(XMLStreamWriter writer)  throws XMLStreamException {
         serialize(writer,true);
     }
+
+    ///////////////////////////////////////////////////////////////////////////////////////////////
+    //////////////////////////////////////////////////////////////////////////////////////////////
+
     private void serialize(XMLStreamWriter writer,boolean cache)throws XMLStreamException
{
 
-        boolean firstElement = false;
+        // select the builder
         short builderType = PULL_TYPE_BUILDER;    // default is pull type
         if (builder != null) {
             builderType = this.builder.getBuilderType();
@@ -633,74 +625,51 @@
                 && (builder.getRegisteredContentHandler() == null)) {
             builder.registerExternalContentHandler(
                     new StreamWriterToContentHandlerConverter(writer));
-
-            // for now only SAX
         }
 
-        // Special case for the pull type building with cache off
-        // The getXMLStreamReader method returns the current elements itself.
-        if (!cache) {
-            if ((firstChild == null) && (nextSibling == null) && !isComplete()
-                    && (builderType == PULL_TYPE_BUILDER)) {
-                StreamingOMSerializer streamingOMSerializer =
-                        new StreamingOMSerializer();
-                streamingOMSerializer.serialize(this.getXMLStreamReader(!cache),
-                        writer);
-                return;
-            }
-        }
-        if (!cache) {
-            if (isComplete()) {
 
-                // serializeWithCache own normally
-                serializeNormal(writer, cache);
-                if (nextSibling != null) {
-
-                    // serilize next sibling
-                    nextSibling.serialize(writer);
-                } else {
-                    if (parent == null) {
-                        return;
-                    } else if (parent.isComplete()) {
-                        return;
-                    } else {
-
-                        // do the special serialization
-                        // Only the push serializer is left now
-                        builder.setCache(cache);
-                        builder.next();
-                    }
-                }
-            } else if (firstChild != null) {
-                serializeStartpart(writer);
-                log.info("Serializing the Element from " + localName
-                        + " the generated OM tree");
+        if (!cache) {
+            //No caching
+            if (this.firstChild!=null){
+                OMSerializerUtil.serializeStartpart(this,writer);
                 firstChild.serialize(writer);
-                serializeEndpart(writer);
-            } else {
-
-                // do the special serilization
-                // Only the push serializer is left now
-                builder.setCache(cache);
-                serializeStartpart(writer);
-                builder.next();
-                serializeEndpart(writer);
+                OMSerializerUtil.serializeEndpart(writer);
+            }else if (!this.done){
+                if (builderType==PULL_TYPE_BUILDER){
+                    OMSerializerUtil.serializeByPullStream(this,writer);
+                }else{
+                    OMSerializerUtil.serializeStartpart(this,writer);
+                    builder.setCache(cache);
+                    builder.next();
+                    OMSerializerUtil.serializeEndpart(writer);
+                }
+            }else{
+                OMSerializerUtil.serializeNormal(this,writer, cache);
             }
-        } else {
 
-            // serializeWithCache own normally
-            serializeNormal(writer, cache);
-
-            // serializeWithCache the siblings if this is not the first element
-            if (!firstElement) {
-                OMNode nextSibling = this.getNextSibling();
-                if (nextSibling != null) {
-                    nextSibling.serializeWithCache(writer);
+            //serilize siblings
+            if (this.nextSibling!=null){
+                nextSibling.serialize(writer);
+            }else if (this.parent!=null){
+                if (!this.parent.done){
+                    builder.setCache(cache);
+                    builder.next();
                 }
             }
+        }else{
+            //Cached
+            OMSerializerUtil.serializeNormal(this,writer, cache);
+            // serialize the siblings
+            OMNode nextSibling = this.getNextSibling();
+            if (nextSibling != null) {
+                nextSibling.serializeWithCache(writer);
+            }
         }
     }
 
+    ////////////////////////////////////////////////////////////////////////////////////////////////
+    ////////////////////////////////////////////////////////////////////////////////////////////////
+
     /**
      * This was requested during the second Axis2 summit. When one call this method, this
will
      * serialise without building the object structure in the memory. Misuse of this method
will
@@ -715,149 +684,6 @@
 
 
 
-    /**
-     * Method serializeStartpart
-     *
-     * @param writer
-     * @throws XMLStreamException
-     */
-    private void serializeStartpart(XMLStreamWriter writer)
-            throws XMLStreamException {
-        String nameSpaceName = null;
-        String writer_prefix = null;
-        String prefix = null;
-        if (ns != null) {
-            nameSpaceName = ns.getName();
-            writer_prefix = writer.getPrefix(nameSpaceName);
-            prefix = ns.getPrefix();
-            if (nameSpaceName != null) {
-                if (writer_prefix != null) {
-                    writer.writeStartElement(nameSpaceName,
-                            this.getLocalName());
-                } else {
-                    if (prefix != null) {
-                        writer.writeStartElement(prefix, this.getLocalName(),
-                                nameSpaceName);
-                        writer.writeNamespace(prefix, nameSpaceName);
-                        writer.setPrefix(prefix, nameSpaceName);
-                    } else {
-                        writer.writeStartElement(nameSpaceName,
-                                this.getLocalName());
-                        writer.writeDefaultNamespace(nameSpaceName);
-                        writer.setDefaultNamespace(nameSpaceName);
-                    }
-                }
-            } else {
-                writer.writeStartElement(this.getLocalName());
-//                throw new OMException(
-//                        "Non namespace qualified elements are not allowed");
-            }
-        } else {
-            writer.writeStartElement(this.getLocalName());
-//            throw new OMException(
-//                    "Non namespace qualified elements are not allowed");
-        }
-
-        // add the elements attributes
-        if (attributes != null) {
-            Iterator attributesList = attributes.values().iterator();
-            while (attributesList.hasNext()) {
-                serializeAttribute((OMAttribute) attributesList.next(), writer);
-            }
-        }
-
-        // add the namespaces
-        Iterator namespaces = this.getAllDeclaredNamespaces();
-        if (namespaces != null) {
-            while (namespaces.hasNext()) {
-                serializeNamespace((OMNamespace) namespaces.next(), writer);
-            }
-        }
-    }
-
-    /**
-     * Method serializeEndpart
-     *
-     * @param writer
-     * @throws XMLStreamException
-     */
-    private void serializeEndpart(XMLStreamWriter writer)
-            throws XMLStreamException {
-        writer.writeEndElement();
-    }
-
-    /**
-     * Method serializeNormal
-     *
-     * @param writer
-     * @param cache
-     * @throws XMLStreamException
-     */
-    private void serializeNormal(XMLStreamWriter writer, boolean cache)
-            throws XMLStreamException {
-        serializeStartpart(writer);
-        OMNode firstChild = getFirstChild();//todo
-        if (firstChild != null) {
-            if (cache){
-                firstChild.serializeWithCache(writer);
-            }else{
-                firstChild.serialize(writer);
-            }
-        }
-        serializeEndpart(writer);
-    }
-
-    /**
-     * Method serializeAttribute
-     *
-     * @param attr
-     * @param writer
-     * @throws XMLStreamException
-     */
-    protected void serializeAttribute(OMAttribute attr, XMLStreamWriter writer)
-            throws XMLStreamException {
-
-        // first check whether the attribute is associated with a namespace
-        OMNamespace ns = attr.getNamespace();
-        String prefix = null;
-        String namespaceName = null;
-        if (ns != null) {
-
-            // add the prefix if it's availble
-            prefix = ns.getPrefix();
-            namespaceName = ns.getName();
-            if (prefix != null) {
-                writer.writeAttribute(prefix, namespaceName,
-                        attr.getLocalName(), attr.getValue());
-            } else {
-                writer.writeAttribute(namespaceName, attr.getLocalName(),
-                        attr.getValue());
-            }
-        } else {
-            writer.writeAttribute(attr.getLocalName(), attr.getValue());
-        }
-    }
-
-    /**
-     * Method serializeNamespace
-     *
-     * @param namespace
-     * @param writer
-     * @throws XMLStreamException
-     */
-    protected void serializeNamespace(
-            OMNamespace namespace, XMLStreamWriter writer)
-            throws XMLStreamException {
-        if (namespace != null) {
-            String uri = namespace.getName();
-            String prefix = writer.getPrefix(uri);
-            String ns_prefix = namespace.getPrefix();
-            if (prefix == null) {
-                writer.writeNamespace(ns_prefix, namespace.getName());
-                writer.setPrefix(ns_prefix, uri);
-            }
-        }
-    }
 
     /**
      * Method getNextNamespacePrefix

Added: webservices/axis/trunk/java/modules/xml/src/org/apache/axis/om/impl/llom/OMSerializerUtil.java
URL: http://svn.apache.org/viewcvs/webservices/axis/trunk/java/modules/xml/src/org/apache/axis/om/impl/llom/OMSerializerUtil.java?rev=167896&view=auto
==============================================================================
--- webservices/axis/trunk/java/modules/xml/src/org/apache/axis/om/impl/llom/OMSerializerUtil.java
(added)
+++ webservices/axis/trunk/java/modules/xml/src/org/apache/axis/om/impl/llom/OMSerializerUtil.java
Tue May  3 03:21:36 2005
@@ -0,0 +1,188 @@
+package org.apache.axis.om.impl.llom;
+
+import org.apache.axis.om.OMAttribute;
+import org.apache.axis.om.OMNamespace;
+import org.apache.axis.om.OMElement;
+import org.apache.axis.om.OMNode;
+import org.apache.axis.om.impl.llom.serialize.StreamingOMSerializer;
+
+import javax.xml.stream.XMLStreamWriter;
+import javax.xml.stream.XMLStreamException;
+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.
+ *
+ * 
+ */
+class OMSerializerUtil {
+
+    /**
+     * Method serializeEndpart
+     *
+     * @param writer
+     * @throws javax.xml.stream.XMLStreamException
+     */
+    static void serializeEndpart(XMLStreamWriter writer)
+            throws XMLStreamException {
+        writer.writeEndElement();
+    }
+
+    /**
+     * Method serializeAttribute
+     *
+     * @param attr
+     * @param writer
+     * @throws XMLStreamException
+     */
+    static void serializeAttribute(OMAttribute attr, XMLStreamWriter writer)
+            throws XMLStreamException {
+
+        // first check whether the attribute is associated with a namespace
+        OMNamespace ns = attr.getNamespace();
+        String prefix = null;
+        String namespaceName = null;
+        if (ns != null) {
+
+            // add the prefix if it's availble
+            prefix = ns.getPrefix();
+            namespaceName = ns.getName();
+            if (prefix != null) {
+                writer.writeAttribute(prefix, namespaceName,
+                        attr.getLocalName(), attr.getValue());
+            } else {
+                writer.writeAttribute(namespaceName, attr.getLocalName(),
+                        attr.getValue());
+            }
+        } else {
+            writer.writeAttribute(attr.getLocalName(), attr.getValue());
+        }
+    }
+
+     /**
+     * Method serializeNamespace
+     *
+     * @param namespace
+     * @param writer
+     * @throws XMLStreamException
+     */
+    static void serializeNamespace(OMNamespace namespace, XMLStreamWriter writer)
+            throws XMLStreamException {
+        if (namespace != null) {
+            String uri = namespace.getName();
+            String prefix = writer.getPrefix(uri);
+            String ns_prefix = namespace.getPrefix();
+            if (prefix == null) {
+                writer.writeNamespace(ns_prefix, namespace.getName());
+                writer.setPrefix(ns_prefix, uri);
+            }
+        }
+    }
+
+
+    /**
+     * Method serializeStartpart
+     *
+     * @param writer
+     * @throws XMLStreamException
+     */
+    static void serializeStartpart(OMElementImpl element,XMLStreamWriter writer)
+            throws XMLStreamException {
+        String nameSpaceName = null;
+        String writer_prefix = null;
+        String prefix = null;
+        if (element.ns != null) {
+            nameSpaceName = element.ns.getName();
+            writer_prefix = writer.getPrefix(nameSpaceName);
+            prefix = element.ns.getPrefix();
+            if (nameSpaceName != null) {
+                if (writer_prefix != null) {
+                    writer.writeStartElement(nameSpaceName,
+                            element.getLocalName());
+                } else {
+                    if (prefix != null) {
+                        writer.writeStartElement(prefix, element.getLocalName(),
+                                nameSpaceName);
+                        writer.writeNamespace(prefix, nameSpaceName);
+                        writer.setPrefix(prefix, nameSpaceName);
+                    } else {
+                        writer.writeStartElement(nameSpaceName,
+                                element.getLocalName());
+                        writer.writeDefaultNamespace(nameSpaceName);
+                        writer.setDefaultNamespace(nameSpaceName);
+                    }
+                }
+            } else {
+                writer.writeStartElement(element.getLocalName());
+//                throw new OMException(
+//                        "Non namespace qualified elements are not allowed");
+            }
+        } else {
+            writer.writeStartElement(element.getLocalName());
+//            throw new OMException(
+//                    "Non namespace qualified elements are not allowed");
+        }
+
+        // add the elements attributes
+        if (element.attributes != null) {
+            Iterator attributesList = element.attributes.values().iterator();
+            while (attributesList.hasNext()) {
+                serializeAttribute((OMAttribute) attributesList.next(), writer);
+            }
+        }
+
+        // add the namespaces
+        Iterator namespaces = element.getAllDeclaredNamespaces();
+        if (namespaces != null) {
+            while (namespaces.hasNext()) {
+                serializeNamespace((OMNamespace) namespaces.next(), writer);
+            }
+        }
+    }
+
+
+    /**
+     * Method serializeNormal
+     *
+     * @param writer
+     * @param cache
+     * @throws XMLStreamException
+     */
+    static void serializeNormal(OMElementImpl element,XMLStreamWriter writer, boolean cache)
+            throws XMLStreamException {
+
+        if (cache){
+            element.build();
+        }
+
+        serializeStartpart(element,writer);
+        OMNode firstChild = element.firstChild;
+        if (firstChild != null) {
+            if (cache){
+                firstChild.serializeWithCache(writer);
+            }else{
+                firstChild.serialize(writer);
+            }
+        }
+        serializeEndpart(writer);
+    }
+
+    static void serializeByPullStream(OMElementImpl element,XMLStreamWriter writer) throws
XMLStreamException {
+     StreamingOMSerializer streamingOMSerializer =  new StreamingOMSerializer();
+                   streamingOMSerializer.serialize(element.getXMLStreamReaderWithoutCaching(),
+                           writer);
+                   return;
+    }
+}

Added: webservices/axis/trunk/java/modules/xml/src/org/apache/axis/om/impl/llom/util/EmptyIterator.java
URL: http://svn.apache.org/viewcvs/webservices/axis/trunk/java/modules/xml/src/org/apache/axis/om/impl/llom/util/EmptyIterator.java?rev=167896&view=auto
==============================================================================
--- webservices/axis/trunk/java/modules/xml/src/org/apache/axis/om/impl/llom/util/EmptyIterator.java
(added)
+++ webservices/axis/trunk/java/modules/xml/src/org/apache/axis/om/impl/llom/util/EmptyIterator.java
Tue May  3 03:21:36 2005
@@ -0,0 +1,36 @@
+package org.apache.axis.om.impl.llom.util;
+
+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.
+*
+* An empty Iterator implementation
+*/
+public class EmptyIterator implements Iterator {
+    public void remove() {
+        throw new UnsupportedOperationException();
+
+    }
+
+    public boolean hasNext() {
+        return false;
+    }
+
+    public Object next() {
+        throw new UnsupportedOperationException();
+    }
+}
+



Mime
View raw message