Author: scheu
Date: Wed Jan 31 17:34:12 2007
New Revision: 502068

URL: http://svn.apache.org/viewvc?view=rev&rev=502068
Log:
WSCOMMONS-159
Contributor:Rich Scheuerle
Fix OMSourcedElement so that it implements the proper serialize semantics as 
defined by the OMNode interface 

Modified:
    
webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMSourcedElementImpl.java
    
webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/om/impl/llom/OMSourcedElementTest.java

Modified: 
webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMSourcedElementImpl.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMSourcedElementImpl.java?view=diff&rev=502068&r1=502067&r2=502068
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMSourcedElementImpl.java
 (original)
+++ 
webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMSourcedElementImpl.java
 Wed Jan 31 17:34:12 2007
@@ -18,6 +18,7 @@
 
 import org.apache.axiom.om.*;
 import org.apache.axiom.om.util.StAXUtils;
+import org.apache.axiom.om.impl.MTOMXMLStreamWriter;
 import org.apache.axiom.om.impl.builder.StAXOMBuilder;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
@@ -107,6 +108,7 @@
 
     /**
      * Get parser from data source.
+     * Note that getDataReader may consume the underlying data source.
      * @return parser
      */
     private XMLStreamReader getDirectReader() {
@@ -116,7 +118,7 @@
                if (isDataSourceConsumed()) {
                        return super.getXMLStreamReader();
                } else {
-                       return dataSource.getReader();
+                       return dataSource.getReader();  // This call may 
consume the underlying data source
                }
         } catch (XMLStreamException e) {
             log.error("Could not get parser from data source for element " +
@@ -347,7 +349,7 @@
         if (isParserSet) {
             return super.getXMLStreamReader();
         } else {
-            return getDirectReader();
+            return getDirectReader();  // Note that this may consume the 
underlying data source
         }
     }
 
@@ -463,7 +465,7 @@
         } else {
                StringWriter writer = new StringWriter();
             XMLStreamWriter writer2 = StAXUtils.createXMLStreamWriter(writer);
-               dataSource.serialize(writer2);
+               dataSource.serialize(writer2);  // dataSource.serialize 
consumes the data
             writer2.flush();
             return writer.toString();
         }
@@ -522,10 +524,12 @@
      * @see 
org.apache.axiom.om.OMNode#internalSerialize(javax.xml.stream.XMLStreamWriter)
      */
     public void internalSerialize(javax.xml.stream.XMLStreamWriter writer) 
throws XMLStreamException {
+        // The contract of internalSerialize is to "cache" the om
        if (isDataSourceConsumed()) {
                super.internalSerialize(writer);
        } else {
-               internalSerializeAndConsume(writer);
+            forceExpand();
+            super.internalSerialize(writer);
        }
     }
 
@@ -535,7 +539,10 @@
     protected void internalSerialize(XMLStreamWriter writer, boolean cache) 
throws XMLStreamException {
        if (isDataSourceConsumed()) {
                super.internalSerialize(writer, cache);
-       } else {
+       } else if (cache) {
+            forceExpand();
+            super.internalSerialize(writer, true);
+        } else {
                internalSerializeAndConsume(writer);
        }
     }
@@ -550,7 +557,7 @@
         if (isDataSourceConsumed()) {
                super.internalSerializeAndConsume(writer);
         } else {
-               dataSource.serialize(writer);
+               dataSource.serialize(writer);  // dataSource.serialize() 
consumes the data
         }
     }
 
@@ -558,35 +565,44 @@
      * @see 
org.apache.axiom.om.OMNode#serialize(javax.xml.stream.XMLStreamWriter)
      */
     public void serialize(XMLStreamWriter xmlWriter) throws XMLStreamException 
{
-        internalSerializeAndConsume(xmlWriter);
+        // The contract is to serialize with caching
+        internalSerialize(xmlWriter);
     }
 
     /* (non-Javadoc)
      * @see org.apache.axiom.om.OMNode#serialize(java.io.OutputStream)
      */
     public void serialize(OutputStream output) throws XMLStreamException {
-        serializeAndConsume(output);
+       forceExpand();
+       XMLStreamWriter xmlStreamWriter = 
StAXUtils.createXMLStreamWriter(output);
+       serialize(xmlStreamWriter);
+       xmlStreamWriter.flush();
     }
 
     /* (non-Javadoc)
      * @see org.apache.axiom.om.OMNode#serialize(java.io.Writer)
      */
     public void serialize(Writer writer) throws XMLStreamException {
-        serializeAndConsume(writer);
+        forceExpand();
+        XMLStreamWriter xmlStreamWriter = 
StAXUtils.createXMLStreamWriter(writer);
+        serialize(xmlStreamWriter);
+        xmlStreamWriter.flush();
     }
 
     /* (non-Javadoc)
      * @see org.apache.axiom.om.OMNode#serialize(java.io.OutputStream, 
org.apache.axiom.om.OMOutputFormat)
      */
     public void serialize(OutputStream output, OMOutputFormat format) throws 
XMLStreamException {
-        serializeAndConsume(output, format);
+        forceExpand();
+        super.serialize(output, format);
     }
 
     /* (non-Javadoc)
      * @see org.apache.axiom.om.OMNode#serialize(java.io.Writer, 
org.apache.axiom.om.OMOutputFormat)
      */
     public void serialize(Writer writer, OMOutputFormat format) throws 
XMLStreamException {
-        serializeAndConsume(writer, format);
+        forceExpand();
+        super.serialize(writer, format);
     }
 
     /* (non-Javadoc)
@@ -606,7 +622,7 @@
         if (isDataSourceConsumed()) {
                super.serializeAndConsume(output, new OMOutputFormat());
         } else {
-               dataSource.serialize(output, new OMOutputFormat());
+               dataSource.serialize(output, new OMOutputFormat());  // 
consumes the datasource
         }
     }
 
@@ -620,7 +636,7 @@
         if (isDataSourceConsumed()) {
                super.serializeAndConsume(writer);
         } else {
-               dataSource.serialize(writer, new OMOutputFormat());
+               dataSource.serialize(writer, new OMOutputFormat()); // consumes 
the datasource
         }
     }
 
@@ -635,7 +651,7 @@
         if (isDataSourceConsumed()) {
                super.serializeAndConsume(output, format);
         } else {
-               dataSource.serialize(output, format);
+               dataSource.serialize(output, format); // consumes the datasource
         }
     }
 
@@ -650,7 +666,7 @@
         if (isDataSourceConsumed()) {
                super.serializeAndConsume(writer, format);
        } else {
-               dataSource.serialize(writer, format);
+               dataSource.serialize(writer, format);  // consumes the 
datasource
        }
     }
 

Modified: 
webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/om/impl/llom/OMSourcedElementTest.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/om/impl/llom/OMSourcedElementTest.java?view=diff&rev=502068&r1=502067&r2=502068
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/om/impl/llom/OMSourcedElementTest.java
 (original)
+++ 
webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/om/impl/llom/OMSourcedElementTest.java
 Wed Jan 31 17:34:12 2007
@@ -53,11 +53,11 @@
  */
 public class OMSourcedElementTest extends AbstractTestCase {
     private static String testDocument =
-        "<library xmlns='http://www.sosnoski.com/uwjws/library' books='1'>" +
-        "<type id='java' category='professional' deductable='true'>"+
-        "<name>Java Reference</name></type><type id='xml' "+
-        "category='professional' deductable='true'><name>XML Reference</name>" 
+
-        "</type><book isbn='1930110111' type='xml'><title>XSLT 
Quickly</title>" +
+        "<library xmlns=\"http://www.sosnoski.com/uwjws/library\"; 
books=\"1\">" +
+        "<type id=\"java\" category=\"professional\" deductable=\"true\">"+
+        "<name>Java Reference</name></type><type id=\"xml\" "+
+        "category=\"professional\" deductable=\"true\"><name>XML 
Reference</name>" +
+        "</type><book isbn=\"1930110111\" type=\"xml\"><title>XSLT 
Quickly</title>" +
         "<author>DuCharme, Bob</author><publisher>Manning</publisher>" +
         "<price>29.95</price></book></library>";
     
@@ -120,6 +120,26 @@
     public void testSerializeToStream() throws Exception {
         ByteArrayOutputStream bos = new ByteArrayOutputStream();
         element.serialize(bos);
+        String newText = new String(bos.toByteArray());
+        System.out.println(testDocument);
+        System.out.println(newText);
+        assertEquals("Serialized text error", testDocument, newText);
+        assertTrue("Element not expanded when serializing", 
element.isExpanded());
+        
+        bos = new ByteArrayOutputStream();
+        element.serialize(bos);
+        assertEquals("Serialized text error", testDocument,
+            new String(bos.toByteArray()));
+        assertTrue("Element not expanded when serializing", 
element.isExpanded());
+    }
+    
+    /**
+     * Test serialization of OMSourcedElementImpl to a Stream
+     * @throws Exception
+     */
+    public void testSerializeAndConsumeToStream() throws Exception {
+        ByteArrayOutputStream bos = new ByteArrayOutputStream();
+        element.serializeAndConsume(bos);
         assertEquals("Serialized text error", testDocument,
             new String(bos.toByteArray()));
         assertFalse("Element expansion when serializing", 
element.isExpanded());
@@ -134,6 +154,24 @@
         element.serialize(writer);
         String result = writer.toString();
         assertEquals("Serialized text error", testDocument, result);
+        assertTrue("Element not expanded when serializing", 
element.isExpanded());
+        
+        writer = new StringWriter();
+        element.serialize(writer);
+        result = writer.toString();
+        assertEquals("Serialized text error", testDocument, result);
+        assertTrue("Element not expanded when serializing", 
element.isExpanded());
+    }
+    
+    /**
+     * Test serialization of OMSourcedElementImpl to a Writer
+     * @throws Exception
+     */
+    public void testSerializeAndConsumeToWriter() throws Exception {
+        StringWriter writer = new StringWriter();
+        element.serializeAndConsume(writer);
+        String result = writer.toString();
+        assertEquals("Serialized text error", testDocument, result);
         assertFalse("Element expansion when serializing", 
element.isExpanded());
     }
     
@@ -147,6 +185,26 @@
         element.serialize(writer);
         xmlwriter.flush();
         assertEquals("Serialized text error", testDocument, writer.toString());
+        assertTrue("Element not expanded when serializing", 
element.isExpanded());
+        
+        writer = new StringWriter();
+        xmlwriter = 
XMLOutputFactory.newInstance().createXMLStreamWriter(writer);
+        element.serialize(writer);
+        xmlwriter.flush();
+        assertEquals("Serialized text error", testDocument, writer.toString());
+        assertTrue("Element not expanded when serializing", 
element.isExpanded());
+    }
+    
+    /**
+     * Test serialization of OMSourcedElementImpl to an XMLWriter
+     * @throws Exception
+     */
+    public void testSerializeAndConsumeToXMLWriter() throws Exception {
+        StringWriter writer = new StringWriter();
+        XMLStreamWriter xmlwriter = 
XMLOutputFactory.newInstance().createXMLStreamWriter(writer);
+        element.serializeAndConsume(writer);
+        xmlwriter.flush();
+        assertEquals("Serialized text error", testDocument, writer.toString());
         assertFalse("Element expansion when serializing", 
element.isExpanded());
     }
     
@@ -158,6 +216,32 @@
         StringWriter writer = new StringWriter();
         XMLStreamWriter xmlwriter = 
XMLOutputFactory.newInstance().createXMLStreamWriter(writer);
         root.serialize(writer);
+        xmlwriter.flush();
+        String result = writer.toString();
+        // We can't test for equivalence because the underlying 
OMSourceElement is 
+        // streamed as it is serialized.  So I am testing for an internal 
value.
+        assertTrue("Serialized text error" + result, 
result.indexOf("1930110111") > 0);
+        assertTrue("Element not expanded when serializing", 
element.isExpanded());
+        
+        writer = new StringWriter();
+        xmlwriter = 
XMLOutputFactory.newInstance().createXMLStreamWriter(writer);
+        root.serialize(writer);
+        xmlwriter.flush();
+        result = writer.toString();
+        // We can't test for equivalence because the underlying 
OMSourceElement is 
+        // streamed as it is serialized.  So I am testing for an internal 
value.
+        assertTrue("Serialized text error" + result, 
result.indexOf("1930110111") > 0);
+        assertTrue("Element not expanded when serializing", 
element.isExpanded());
+    }
+    
+    /**
+     * Tests OMSourcedElement serialization when the root (parent) is 
serialized.
+     * @throws Exception
+     */
+    public void testSerializeAndConsumeToXMLWriterEmbedded() throws Exception {
+        StringWriter writer = new StringWriter();
+        XMLStreamWriter xmlwriter = 
XMLOutputFactory.newInstance().createXMLStreamWriter(writer);
+        root.serializeAndConsume(writer);
         xmlwriter.flush();
         String result = writer.toString();
         // We can't test for equivalence because the underlying 
OMSourceElement is 



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to