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 sc...@apache.org on 2006/07/22 15:06:29 UTC

svn commit: r424572 - in /webservices/commons/trunk/modules/axiom: src/org/apache/axiom/om/impl/serialize/ test/org/apache/axiom/om/impl/serializer/

Author: scheu
Date: Sat Jul 22 06:06:28 2006
New Revision: 424572

URL: http://svn.apache.org/viewvc?rev=424572&view=rev
Log:
WSCOMMONS-64
StreamingOMSerializer Fix and new StreamingOMSerializerTest
Contributor: Rich Scheuerle
Analysis: Peter McCracken

Added:
    webservices/commons/trunk/modules/axiom/test/org/apache/axiom/om/impl/serializer/StreamingOMSerializerTest.java
Modified:
    webservices/commons/trunk/modules/axiom/src/org/apache/axiom/om/impl/serialize/StreamingOMSerializer.java
    webservices/commons/trunk/modules/axiom/test/org/apache/axiom/om/impl/serializer/NoNamespaceSerializerTest.java

Modified: webservices/commons/trunk/modules/axiom/src/org/apache/axiom/om/impl/serialize/StreamingOMSerializer.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/src/org/apache/axiom/om/impl/serialize/StreamingOMSerializer.java?rev=424572&r1=424571&r2=424572&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/src/org/apache/axiom/om/impl/serialize/StreamingOMSerializer.java (original)
+++ webservices/commons/trunk/modules/axiom/src/org/apache/axiom/om/impl/serialize/StreamingOMSerializer.java Sat Jul 22 06:06:28 2006
@@ -1,5 +1,6 @@
 /*
  * Copyright 2004,2005 The Apache Software Foundation.
+ * Copyright 2006 International Business Machines Corp.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -16,6 +17,8 @@
 
 package org.apache.axiom.om.impl.serialize;
 
+import java.util.ArrayList;
+
 import org.apache.axiom.om.OMSerializer;
 
 import javax.xml.namespace.NamespaceContext;
@@ -100,6 +103,7 @@
     }
 
     /**
+     * Write out the start element, its namespace/prefixes and attributes
      * @param reader
      * @param writer
      * @throws XMLStreamException
@@ -107,48 +111,177 @@
     protected void serializeElement(XMLStreamReader reader,
                                     XMLStreamWriter writer)
             throws XMLStreamException {
-        String prefix = reader.getPrefix();
-        String nameSpaceName = reader.getNamespaceURI();
-        if (nameSpaceName != null) {
-            String writer_prefix = writer.getPrefix(nameSpaceName);
-            if (writer_prefix != null) {
-                writer.writeStartElement(nameSpaceName, reader.getLocalName());
-            } else {
-                if (prefix != null) {
-                    writer.writeStartElement(prefix, reader.getLocalName(),
-                            nameSpaceName);
-                    writer.writeNamespace(prefix, nameSpaceName);
-                    writer.setPrefix(prefix, nameSpaceName);
-                } else {
-                    writer.writeStartElement(nameSpaceName,
-                            reader.getLocalName());
-                    writer.writeDefaultNamespace(nameSpaceName);
-                    writer.setDefaultNamespace(nameSpaceName);
-                }
+        
+    	ArrayList  prefixList = null;
+    	ArrayList  nsList = null;
+    	
+    	// The algorithm is:
+        // ... generate setPrefix/setDefaultNamespace for each namespace declaration if the prefix is unassociated.
+    	// ... generate setPrefix/setDefaultNamespace if the prefix of the element is unassociated
+    	// ... generate setPrefix/setDefaultNamespace for each unassociated prefix of the attributes.
+    	//
+    	// ... generate writeStartElement
+    	//
+    	// ... generate writeNamespace/writerDefaultNamespace for each namespace declaration on the element
+    	// ... generate writeNamespace/writeDefaultNamespace for any new "autogen" namespace/prefixes
+    	// ... generate writeAttribute for each attribute
+    	
+        // Generate setPrefix for the namespace declarations
+        int count = reader.getNamespaceCount();
+        for (int i = 0; i < count; i++) {
+        	String prefix = reader.getNamespacePrefix(i);
+        	prefix = (prefix != null && prefix.length() == 0) ? null : prefix;
+        	String namespace = reader.getNamespaceURI(i);
+        	namespace = (namespace != null && namespace.length() == 0) ? null : namespace;
+        	
+        	generateSetPrefix(prefix, namespace, writer);
+        }
+        
+    	// Generate setPrefix for the element
+    	// Get the prefix and namespace of the element.  "" and null are identical.
+        String ePrefix = reader.getPrefix();
+    	ePrefix = (ePrefix != null && ePrefix.length() == 0) ? null : ePrefix;
+    	String eNamespace = reader.getNamespaceURI();
+    	eNamespace = (eNamespace != null && eNamespace.length() == 0) ? null : eNamespace;
+    	String newPrefix = generateSetPrefix(ePrefix, eNamespace, writer);
+    	// If the prefix is not associated with a namespace yet, remember it so that we can
+    	// write out a namespace declaration
+    	if (newPrefix != null) {
+    		if (prefixList == null) {
+    			prefixList= new ArrayList();
+    			nsList = new ArrayList();
+    		}
+    		prefixList.add(newPrefix);
+    		nsList.add(eNamespace);
+    	}
+    
+        // Now write the namespaces for each attribute
+        count = reader.getAttributeCount();
+        for (int i = 0; i < count; i++) {
+            String prefix = reader.getAttributePrefix(i);
+            prefix = (prefix != null && prefix.length() == 0) ? null : prefix;
+            String namespace = reader.getAttributeNamespace(i);
+            namespace = (namespace != null && namespace.length() == 0) ? null : namespace;
+            
+            // Default prefix referencing is not allowed on an attribute
+            if (prefix == null && namespace != null) {
+            	String writerPrefix = writer.getPrefix(namespace);
+            	writerPrefix = (writerPrefix != null && writerPrefix.length() == 0) ? null : writerPrefix;
+            	prefix = (writerPrefix != null) ? 
+            			writerPrefix :
+            	        generateUniquePrefix(writer.getNamespaceContext());
             }
+            newPrefix = generateSetPrefix(prefix, namespace, writer);
+            // If the prefix is not associated with a namespace yet, remember it so that we can
+        	// write out a namespace declaration
+        	if (newPrefix != null) {
+        		if (prefixList == null) {
+        			prefixList= new ArrayList();
+        			nsList = new ArrayList();
+        		}
+        		prefixList.add(newPrefix);
+        		nsList.add(eNamespace);
+        	}
+        }
+        
+        // Now write the startElement
+        if (eNamespace != null) {
+        	writer.writeStartElement(ePrefix, reader.getLocalName(), eNamespace);
         } else {
-            writer.writeStartElement(reader.getLocalName());
+        	writer.writeStartElement(reader.getLocalName());
         }
-
-
-        // add the namespaces
-        int count = reader.getNamespaceCount();
-        String namespacePrefix;
+        
+        // Now write the namespace declarations
+        count = reader.getNamespaceCount();
         for (int i = 0; i < count; i++) {
-            namespacePrefix = reader.getNamespacePrefix(i);
-            if(namespacePrefix != null && namespacePrefix.length()==0)
-                continue;
-
-            serializeNamespace(namespacePrefix,
-                    reader.getNamespaceURI(i), writer);
+        	String prefix = reader.getNamespacePrefix(i);
+        	prefix = (prefix != null && prefix.length() == 0) ? null : prefix;
+        	String namespace = reader.getNamespaceURI(i);
+        	if (prefix != null) {
+        		writer.writeNamespace(prefix, namespace);
+        	} else {
+        		writer.writeDefaultNamespace(namespace);
+        	}
+        }
+        
+        // Now write out the namespaces that for prefixes that are not associated
+        if (prefixList != null) {
+        	for (int i=0; i<prefixList.size(); i++) {
+        		String prefix = (String) prefixList.get(i);
+        		String namespace = (String) nsList.get(i);
+        		if (prefix != null) {
+            		writer.writeNamespace(prefix, namespace);
+            	} else {
+            		writer.writeDefaultNamespace(namespace);
+            	}
+        	}
+        }
+        
+        // Now write the attributes
+        count = reader.getAttributeCount();
+        for (int i = 0; i < count; i++) {
+            String prefix = reader.getAttributePrefix(i);
+            prefix = (prefix != null && prefix.length() == 0) ? null : prefix;
+            String namespace = reader.getAttributeNamespace(i);
+            namespace = (namespace != null && namespace.length() == 0) ? null : namespace;
+            
+            
+            if (prefix == null && namespace != null) {
+            	// Default namespaces are not allowed on an attribute reference.
+                // Earlier in this code, a unique prefix was added for this case...now obtain and use it
+            	prefix = writer.getPrefix(namespace);
+            } else if (namespace != null) {
+            	// Use the writer's prefix if it is different
+            	String writerPrefix = writer.getPrefix(namespace);
+            	if (!prefix.equals(writerPrefix)) {
+            		prefix = writerPrefix;
+            	}
+            }
+            if (namespace != null) {
+            	// Qualified attribute
+            	writer.writeAttribute(prefix, namespace,
+                    reader.getAttributeLocalName(i),
+                    reader.getAttributeValue(i));
+            } else {
+            	// Unqualified attribute
+            	writer.writeAttribute(reader.getAttributeLocalName(i),
+                        reader.getAttributeValue(i));
+            }
         }
-
-        // add attributes
-        serializeAttributes(reader, writer);
-
     }
 
     /**
+     * Generate setPrefix/setDefaultNamespace if the prefix is not associated
+     * @param prefix
+     * @param namespace
+     * @param writer
+     * @return prefix name if a setPrefix/setDefaultNamespace is performed
+     */
+    private String generateSetPrefix(String prefix, String namespace, XMLStreamWriter writer) throws XMLStreamException {
+    	// Generate setPrefix/setDefaultNamespace if the prefix is not associated.
+        if (namespace != null) {
+        	String writerPrefix = writer.getPrefix(namespace);
+        	writerPrefix = (writerPrefix != null && writerPrefix.length() == 0) ? null : writerPrefix;
+        	// Qualified Name
+        	if (writerPrefix == prefix ||
+        	    (writerPrefix != null && writerPrefix.equals(prefix))) {
+        		// Already associated...a setPrefix is not needed
+        		return null;
+        	} else {
+        		if (prefix == null) {
+        			writer.setDefaultNamespace(namespace);
+        		} else {
+        			writer.setPrefix(prefix, namespace);
+        		}
+        		return prefix;
+        	}
+        } else {
+        	// Disable the default namespace
+        	writer.setDefaultNamespace("");
+        }
+        return null;
+    }
+    /**
      * Method serializeEndElement.
      *
      * @param writer
@@ -211,12 +344,12 @@
         for (int i = 0; i < count; i++) {
             prefix = reader.getAttributePrefix(i);
             namespaceName = reader.getAttributeNamespace(i);
-            /*
-               Due to parser implementations returning null as the namespace URI
-              (for the empty namespace) we need to make sure that we deal with
-              a namespace name that is not null. The best way to work around this
-              issue is to set the namespace uri to "" if it is null
-            */
+            //
+            //   Due to parser implementations returning null as the namespace URI
+            //  (for the empty namespace) we need to make sure that we deal with
+            //  a namespace name that is not null. The best way to work around this
+            //  issue is to set the namespace uri to "" if it is null
+           
             if (namespaceName==null) namespaceName="";
             
             writerPrefix =writer.getNamespaceContext().getPrefix(namespaceName);
@@ -272,22 +405,5 @@
         }
 
         return prefix;
-    }
-    /**
-     * Method serializeNamespace.
-     * @param prefix
-     * @param URI
-     * @param writer
-     * @throws XMLStreamException
-     */
-    private void serializeNamespace(String prefix,
-                                    String URI,
-                                    XMLStreamWriter writer)
-            throws XMLStreamException {
-        String prefix1 = writer.getPrefix(URI);
-        if (prefix1 == null) {
-            writer.writeNamespace(prefix, URI);
-            writer.setPrefix(prefix, URI);
-        }
     }
 }

Modified: webservices/commons/trunk/modules/axiom/test/org/apache/axiom/om/impl/serializer/NoNamespaceSerializerTest.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/test/org/apache/axiom/om/impl/serializer/NoNamespaceSerializerTest.java?rev=424572&r1=424571&r2=424572&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/test/org/apache/axiom/om/impl/serializer/NoNamespaceSerializerTest.java (original)
+++ webservices/commons/trunk/modules/axiom/test/org/apache/axiom/om/impl/serializer/NoNamespaceSerializerTest.java Sat Jul 22 06:06:28 2006
@@ -19,16 +19,19 @@
 import junit.framework.TestCase;
 import org.apache.axiom.om.OMAbstractFactory;
 import org.apache.axiom.om.OMElement;
+import org.apache.axiom.om.OMOutputFormat;
 import org.apache.axiom.om.OMXMLParserWrapper;
 import org.apache.axiom.om.impl.llom.factory.OMXMLBuilderFactory;
 import org.apache.axiom.soap.SOAPEnvelope;
 import org.apache.axiom.soap.SOAPFactory;
+import org.apache.axiom.soap.impl.builder.StAXSOAPModelBuilder;
 
 import javax.xml.stream.XMLInputFactory;
 import javax.xml.stream.XMLOutputFactory;
 import javax.xml.stream.XMLStreamReader;
 import javax.xml.stream.XMLStreamWriter;
 import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
 import java.io.InputStreamReader;
 
 public class NoNamespaceSerializerTest extends TestCase {
@@ -105,7 +108,7 @@
 
     }
 
-    public void submitPurchaseOrderTest()
+    public void testsubmitPurchaseOrder()
             throws Exception {
         SOAPFactory omFactory = OMAbstractFactory.getSOAP11Factory();
         SOAPEnvelope env = omFactory.getDefaultEnvelope();

Added: webservices/commons/trunk/modules/axiom/test/org/apache/axiom/om/impl/serializer/StreamingOMSerializerTest.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/test/org/apache/axiom/om/impl/serializer/StreamingOMSerializerTest.java?rev=424572&view=auto
==============================================================================
--- webservices/commons/trunk/modules/axiom/test/org/apache/axiom/om/impl/serializer/StreamingOMSerializerTest.java (added)
+++ webservices/commons/trunk/modules/axiom/test/org/apache/axiom/om/impl/serializer/StreamingOMSerializerTest.java Sat Jul 22 06:06:28 2006
@@ -0,0 +1,82 @@
+/*
+ * Copyright 2004,2005 The Apache Software Foundation.
+ * Copyright 2006 International Business Machines Corp.
+ *
+ * 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.
+ */
+package org.apache.axiom.om.impl.serializer;
+
+import java.io.StringReader;
+import java.io.StringWriter;
+
+import javax.xml.stream.XMLInputFactory;
+import javax.xml.stream.XMLOutputFactory;
+import javax.xml.stream.XMLStreamReader;
+import javax.xml.stream.XMLStreamWriter;
+
+import org.apache.axiom.om.impl.serialize.StreamingOMSerializer;
+
+import junit.framework.TestCase;
+
+/**
+ * @author scheu
+ *
+ */
+public class StreamingOMSerializerTest extends TestCase {
+
+	private final String START = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\">" +
+    "<soapenv:Body>\n";
+	private final String ELEMENT_START = "<purchase-order xmlns=\"http://openuri.org/easypo\">\n" +
+    "  <customer>\n" +
+    "    <name>Gladys Kravitz</name>\n" +
+    "    <address>Anytown, PA</address>\n" +
+    "  </customer>\n";
+    private final String ELEMENT_END ="  </purchase-order>";
+	private final String END = " </soapenv:Body></soapenv:Envelope>";
+	private final int COUNT = 10000;
+	
+	private XMLStreamReader reader = null;
+	private XMLStreamWriter writer = null;
+	
+	
+	private XMLInputFactory inputFactory= null;
+	private XMLOutputFactory outputFactory = null;
+	
+	protected void setUp() throws Exception {
+		// Get the Factories
+		inputFactory = XMLInputFactory.newInstance();
+		outputFactory = XMLOutputFactory.newInstance();
+		
+		// Build a large message
+		StringBuffer buffer = new StringBuffer();
+		buffer.append(START);
+		for (int i=0;i<COUNT; i++) {
+			buffer.append(ELEMENT_START);
+		}
+		for (int i=0;i<COUNT; i++) {
+			buffer.append(ELEMENT_END);
+		}
+		buffer.append(END);
+		StringReader sr = new StringReader(buffer.toString());
+		// Create an XMLStringReader
+		reader = inputFactory.createXMLStreamReader(sr);
+		
+		// Create an XMLStreamWriter
+		StringWriter sw = new StringWriter();
+		writer = outputFactory.createXMLStreamWriter(sw);
+	}
+	public void testLargeMessage() throws Exception {
+		StreamingOMSerializer sos = new StreamingOMSerializer();
+		sos.serialize(reader, writer);
+	}
+}



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@ws.apache.org
For additional commands, e-mail: commons-dev-help@ws.apache.org


Re: svn commit: r424572 - in /webservices/commons/trunk/modules/axiom: src/org/apache/axiom/om/impl/serialize/ test/org/apache/axiom/om/impl/serializer/

Posted by Ajith Ranabahu <aj...@gmail.com>.
Hi,
It seems you guys are adding the copyright statement to each and every
file you edit!
I'm ok with having an entry in the notice file but not each and every
file like this, specially being a one that has been created by others.

Ajith


> @@ -1,5 +1,6 @@
>  /*
>   * Copyright 2004,2005 The Apache Software Foundation.
> + * Copyright 2006 International Business Machines Corp.
>   *
>   * Licensed under the Apache License, Version 2.0 (the "License");


-- 
Ajith Ranabahu

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@ws.apache.org
For additional commands, e-mail: commons-dev-help@ws.apache.org