Author: scheu
Date: Fri Nov 16 09:34:40 2007
New Revision: 595754

URL: http://svn.apache.org/viewvc?rev=595754&view=rev
Log:
Contributor:Rich Scheuerle
Refactored some of the DataSource implementation code into a new class
OMDataSourceExtBase.  This is an implementation change to reduce code and 
encourage reuse.

Added:
    
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/ds/OMDataSourceExtBase.java
Modified:
    
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/ds/ByteArrayDataSource.java
    
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/ds/CharArrayDataSource.java
    
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/ds/InputStreamDataSource.java

Modified: 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/ds/ByteArrayDataSource.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/ds/ByteArrayDataSource.java?rev=595754&r1=595753&r2=595754&view=diff
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/ds/ByteArrayDataSource.java
 (original)
+++ 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/ds/ByteArrayDataSource.java
 Fri Nov 16 09:34:40 2007
@@ -16,25 +16,13 @@
 package org.apache.axiom.om.ds;
 
 import org.apache.axiom.om.OMDataSourceExt;
-import org.apache.axiom.om.OMDocument;
-import org.apache.axiom.om.OMNode;
-import org.apache.axiom.om.OMOutputFormat;
-import org.apache.axiom.om.impl.MTOMXMLStreamWriter;
-import org.apache.axiom.om.impl.builder.StAXOMBuilder;
 import org.apache.axiom.om.util.StAXUtils;
 
 import javax.xml.stream.XMLStreamException;
 import javax.xml.stream.XMLStreamReader;
-import javax.xml.stream.XMLStreamWriter;
 
 import java.io.ByteArrayInputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
 import java.io.UnsupportedEncodingException;
-import java.io.Writer;
-import java.util.HashMap;
-import java.util.Iterator;
 
 /**
  * ByteArrayDataSource is an example implementation of OMDataSourceExt.
@@ -42,12 +30,10 @@
  * This data source is useful for placing bytes into an OM
  * tree, instead of having a deeply nested tree.
  */
-public class ByteArrayDataSource implements OMDataSourceExt {
+public class ByteArrayDataSource extends OMDataSourceExtBase {
 
     ByteArray byteArray = null;
     
-    HashMap map = null;  // Map of properties
-    
     /**
      * Constructor
      * @param bytes 
@@ -59,54 +45,11 @@
         byteArray.encoding = encoding;
     }
    
-    public void serialize(OutputStream output, OMOutputFormat format) throws 
XMLStreamException {
-        try {
-            // Write bytes to the output stream
-            output.write(getXMLBytes(format.getCharSetEncoding()));
-        } catch (IOException e) {
-            throw new XMLStreamException(e);
-        }
-    }
-
-    public void serialize(Writer writer, OMOutputFormat format) throws 
XMLStreamException {
-        try {
-            // Convert the bytes into a String and write it to the Writer
-            String text = new String(getXMLBytes(format.getCharSetEncoding()));
-            writer.write(text);
-        } catch (UnsupportedEncodingException e) {
-            throw new XMLStreamException(e);
-        } catch (IOException e) {
-            throw new XMLStreamException(e);
-        }
-    }
-
-    public void serialize(XMLStreamWriter xmlWriter) throws XMLStreamException 
{
-        // Some XMLStreamWriters (e.g. MTOMXMLStreamWriter) 
-        // provide direct access to the OutputStream.  
-        // This allows faster writing.
-        OutputStream os = getOutputStream(xmlWriter);
-        if (os != null) {
-            String encoding = getCharacterEncoding(xmlWriter);
-            OMOutputFormat format = new OMOutputFormat();
-            format.setCharSetEncoding(encoding);
-            serialize(os, format);
-        } else {
-            // Read the bytes into a reader and 
-            // write to the writer.
-            XMLStreamReader xmlReader = getReader();
-            reader2writer(xmlReader, xmlWriter);
-        }
-    }
-    
+ 
     public XMLStreamReader getReader() throws XMLStreamException {
         return StAXUtils.createXMLStreamReader(new 
ByteArrayInputStream(byteArray.bytes),
                                                byteArray.encoding);            
                                                           
     }
-    
-    public InputStream getXMLInputStream(String encoding)  throws 
-        UnsupportedEncodingException{
-        return new ByteArrayInputStream(getXMLBytes(encoding));
-    }
 
     public Object getObject() {
        return byteArray;
@@ -146,81 +89,10 @@
     }
     
     /**
-     * Some XMLStreamWriters expose an OutputStream that can be
-     * accessed directly.
-     * @return OutputStream or null
-     */
-    private OutputStream getOutputStream(XMLStreamWriter writer) 
-     throws XMLStreamException {
-        if (writer instanceof MTOMXMLStreamWriter) {
-            return ((MTOMXMLStreamWriter) writer).getOutputStream();
-        }
-        return null;
-    }
-    
-    /**
-     * Get the character set encoding of the XMLStreamWriter
-     * @return String or null
-     */
-    private String getCharacterEncoding(XMLStreamWriter writer) {
-        if (writer instanceof MTOMXMLStreamWriter) {
-            return ((MTOMXMLStreamWriter) writer).getCharSetEncoding();
-        }
-        return null;
-    }
-    
-    /**
-     * Simple utility that takes an XMLStreamReader and writes it
-     * to an XMLStreamWriter
-     * @param reader
-     * @param writer
-     * @throws XMLStreamException
-     */
-    private static void reader2writer(XMLStreamReader reader, 
-                                     XMLStreamWriter writer)
-    throws XMLStreamException {
-        StAXOMBuilder builder = new StAXOMBuilder(reader);
-        builder.releaseParserOnClose(true);
-        try {
-            OMDocument omDocument = builder.getDocument();
-            Iterator it = omDocument.getChildren();
-            while (it.hasNext()) {
-                OMNode omNode = (OMNode) it.next();
-                omNode.serializeAndConsume(writer);
-            }
-        } finally {
-            builder.close();
-        }
-    }
-     
-    /**
      * Object containing the byte[]/encoding pair
      */
     public class ByteArray {
         public byte[] bytes;
         public String encoding;
-    }
-
-    public Object getProperty(String key) {
-        if (map == null) {
-            return null;
-        }
-        return map.get(key);
-    }
-
-    public Object setProperty(String key, Object value) {
-        if (map == null) {
-            map = new HashMap();
-        }
-        return map.put(key, value);
-    }
-
-    public boolean hasProperty(String key) {
-        if (map == null) {
-            return false;
-        } 
-        return map.containsKey(key);
-    }
-
-    
+    }   
 }

Modified: 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/ds/CharArrayDataSource.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/ds/CharArrayDataSource.java?rev=595754&r1=595753&r2=595754&view=diff
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/ds/CharArrayDataSource.java
 (original)
+++ 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/ds/CharArrayDataSource.java
 Fri Nov 16 09:34:40 2007
@@ -16,26 +16,16 @@
 package org.apache.axiom.om.ds;
 
 import org.apache.axiom.om.OMDataSourceExt;
-import org.apache.axiom.om.OMDocument;
-import org.apache.axiom.om.OMNode;
 import org.apache.axiom.om.OMOutputFormat;
-import org.apache.axiom.om.impl.MTOMXMLStreamWriter;
-import org.apache.axiom.om.impl.builder.StAXOMBuilder;
 import org.apache.axiom.om.util.StAXUtils;
 
 import javax.xml.stream.XMLStreamException;
 import javax.xml.stream.XMLStreamReader;
-import javax.xml.stream.XMLStreamWriter;
 
-import java.io.ByteArrayInputStream;
 import java.io.CharArrayReader;
 import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
 import java.io.UnsupportedEncodingException;
 import java.io.Writer;
-import java.util.HashMap;
-import java.util.Iterator;
 
 /**
  * CharArrayDataSource is an example implementation of OMDataSourceExt.
@@ -43,11 +33,10 @@
  * This data source is useful for placing characters into an OM
  * tree, instead of having a deeply nested tree.
  */
-public class CharArrayDataSource implements OMDataSourceExt {
+public class CharArrayDataSource extends OMDataSourceExtBase {
 
     char[] chars = null;
-    
-    HashMap map = null;  // Map of properties
+   
     /**
      * Constructor
      * @param bytes 
@@ -56,15 +45,6 @@
     public CharArrayDataSource(char[] chars) {
         this.chars = chars;
     }
-   
-    public void serialize(OutputStream output, OMOutputFormat format) throws 
XMLStreamException {
-        try {
-            // Write bytes to the output stream
-            output.write(getXMLBytes(format.getCharSetEncoding()));
-        } catch (IOException e) {
-            throw new XMLStreamException(e);
-        }
-    }
 
     public void serialize(Writer writer, OMOutputFormat format) throws 
XMLStreamException {
         try {
@@ -75,24 +55,6 @@
             throw new XMLStreamException(e);
         }
     }
-
-    public void serialize(XMLStreamWriter xmlWriter) throws XMLStreamException 
{
-        // Some XMLStreamWriters (e.g. MTOMXMLStreamWriter) 
-        // provide direct access to the OutputStream.  
-        // This allows faster writing.
-        OutputStream os = getOutputStream(xmlWriter);
-        if (os != null) {
-            String encoding = getCharacterEncoding(xmlWriter);
-            OMOutputFormat format = new OMOutputFormat();
-            format.setCharSetEncoding(encoding);
-            serialize(os, format);
-        } else {
-            // Read the bytes into a reader and 
-            // write to the writer.
-            XMLStreamReader xmlReader = getReader();
-            reader2writer(xmlReader, xmlWriter);
-        }
-    }
     
     public XMLStreamReader getReader() throws XMLStreamException {
         CharArrayReader reader = new CharArrayReader(chars);
@@ -100,11 +62,7 @@
         return StAXUtils.createXMLStreamReader(reader);                        
                                           
     }
     
-    public InputStream getXMLInputStream(String encoding)  throws 
-        UnsupportedEncodingException{
-        return new ByteArrayInputStream(getXMLBytes(encoding));
-    }
-
+    
     public Object getObject() {
        return chars;
     }
@@ -132,73 +90,5 @@
     public OMDataSourceExt copy() {
         // Return shallow copy
         return new CharArrayDataSource(chars);
-    }
-    
-    /**
-     * Some XMLStreamWriters expose an OutputStream that can be
-     * accessed directly.
-     * @return OutputStream or null
-     */
-    private OutputStream getOutputStream(XMLStreamWriter writer) 
-     throws XMLStreamException {
-        if (writer instanceof MTOMXMLStreamWriter) {
-            return ((MTOMXMLStreamWriter) writer).getOutputStream();
-        }
-        return null;
-    }
-    
-    /**
-     * Get the character set encoding of the XMLStreamWriter
-     * @return String or null
-     */
-    private String getCharacterEncoding(XMLStreamWriter writer) {
-        if (writer instanceof MTOMXMLStreamWriter) {
-            return ((MTOMXMLStreamWriter) writer).getCharSetEncoding();
-        }
-        return null;
-    }
-    
-    /**
-     * Simple utility that takes an XMLStreamReader and writes it
-     * to an XMLStreamWriter
-     * @param reader
-     * @param writer
-     * @throws XMLStreamException
-     */
-    private static void reader2writer(XMLStreamReader reader, 
-                                     XMLStreamWriter writer)
-    throws XMLStreamException {
-        StAXOMBuilder builder = new StAXOMBuilder(reader);
-        builder.releaseParserOnClose(true);
-        try {
-            OMDocument omDocument = builder.getDocument();
-            Iterator it = omDocument.getChildren();
-            while (it.hasNext()) {
-                OMNode omNode = (OMNode) it.next();
-                omNode.serializeAndConsume(writer);
-            }
-        } finally {
-            builder.close();
-        }
-    }
-    public Object getProperty(String key) {
-        if (map == null) {
-            return null;
-        }
-        return map.get(key);
-    }
-
-    public Object setProperty(String key, Object value) {
-        if (map == null) {
-            map = new HashMap();
-        }
-        return map.put(key, value);
-    }
-    
-    public boolean hasProperty(String key) {
-        if (map == null) {
-            return false;
-        } 
-        return map.containsKey(key);
     }
 }

Modified: 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/ds/InputStreamDataSource.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/ds/InputStreamDataSource.java?rev=595754&r1=595753&r2=595754&view=diff
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/ds/InputStreamDataSource.java
 (original)
+++ 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/ds/InputStreamDataSource.java
 Fri Nov 16 09:34:40 2007
@@ -44,13 +44,11 @@
  * This data source is useful for placing an InputStream into an OM
  * tree, instead of having a deeply nested tree.
  */
-public class InputStreamDataSource implements OMDataSourceExt {
+public class InputStreamDataSource extends OMDataSourceExtBase {
 
     Data data = null;
     private static final int BUFFER_LEN = 4096;
     
-    HashMap map = null;  // Map of properties
-    
     /**
      * Constructor
      * @param bytes 
@@ -82,41 +80,11 @@
         }
     }
 
-    public void serialize(Writer writer, OMOutputFormat format) throws 
XMLStreamException {
-        if (data == null) {
-            throw new OMException("The InputStreamDataSource does not have a 
backing object");
-        }
-        try {
-            // Convert the bytes into a String and write it to the Writer
-            String text = new String(getXMLBytes(format.getCharSetEncoding()));
-            writer.write(text);
-        } catch (UnsupportedEncodingException e) {
-            throw new XMLStreamException(e);
-        } catch (IOException e) {
-            throw new XMLStreamException(e);
-        }
-    }
-
     public void serialize(XMLStreamWriter xmlWriter) throws XMLStreamException 
{
         if (data == null) {
             throw new OMException("The InputStreamDataSource does not have a 
backing object");
         }
-        
-        // Some XMLStreamWriters (e.g. MTOMXMLStreamWriter) 
-        // provide direct access to the OutputStream.  
-        // This allows faster writing.
-        OutputStream os = getOutputStream(xmlWriter);
-        if (os != null) {
-            String encoding = getCharacterEncoding(xmlWriter);
-            OMOutputFormat format = new OMOutputFormat();
-            format.setCharSetEncoding(encoding);
-            serialize(os, format);
-        } else {
-            // Read the bytes into a reader and 
-            // write to the writer.
-            XMLStreamReader xmlReader = getReader();
-            reader2writer(xmlReader, xmlWriter);
-        }
+        super.serialize(xmlWriter);
     }
     
     public XMLStreamReader getReader() throws XMLStreamException {
@@ -196,30 +164,6 @@
     }
     
     /**
-     * Some XMLStreamWriters expose an OutputStream that can be
-     * accessed directly.
-     * @return OutputStream or null
-     */
-    private OutputStream getOutputStream(XMLStreamWriter writer) 
-     throws XMLStreamException {
-        if (writer instanceof MTOMXMLStreamWriter) {
-            return ((MTOMXMLStreamWriter) writer).getOutputStream();
-        }
-        return null;
-    }
-    
-    /**
-     * Get the character set encoding of the XMLStreamWriter
-     * @return String or null
-     */
-    private String getCharacterEncoding(XMLStreamWriter writer) {
-        if (writer instanceof MTOMXMLStreamWriter) {
-            return ((MTOMXMLStreamWriter) writer).getCharSetEncoding();
-        }
-        return null;
-    }
-     
-    /**
      * Private utility to write the InputStream contents to the OutputStream.
      * @param is
      * @param os
@@ -266,26 +210,5 @@
     public class Data {
         public String encoding;
         public InputStream is;
-    }
-
-    public Object getProperty(String key) {
-        if (map == null) {
-            return null;
-        }
-        return map.get(key);
-    }
-
-    public Object setProperty(String key, Object value) {
-        if (map == null) {
-            map = new HashMap();
-        }
-        return map.put(key, value);
-    }
-    
-    public boolean hasProperty(String key) {
-        if (map == null) {
-            return false;
-        } 
-        return map.containsKey(key);
     }
 }

Added: 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/ds/OMDataSourceExtBase.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/ds/OMDataSourceExtBase.java?rev=595754&view=auto
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/ds/OMDataSourceExtBase.java
 (added)
+++ 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/ds/OMDataSourceExtBase.java
 Fri Nov 16 09:34:40 2007
@@ -0,0 +1,156 @@
+/*
+ * Copyright 2004,2007 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.
+ */
+package org.apache.axiom.om.ds;
+
+import org.apache.axiom.om.OMDataSourceExt;
+import org.apache.axiom.om.OMDocument;
+import org.apache.axiom.om.OMNode;
+import org.apache.axiom.om.OMOutputFormat;
+import org.apache.axiom.om.impl.MTOMXMLStreamWriter;
+import org.apache.axiom.om.impl.builder.StAXOMBuilder;
+
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamReader;
+import javax.xml.stream.XMLStreamWriter;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.UnsupportedEncodingException;
+import java.io.Writer;
+import java.util.HashMap;
+import java.util.Iterator;
+
+/**
+ * OMDataSourceExtBase is a convenient base class that can be extended
+ * by other OMDataSourceExt implementations.
+ */
+public abstract class OMDataSourceExtBase implements OMDataSourceExt {
+
+    HashMap map = null;  // Map of properties
+
+    public Object getProperty(String key) {
+        if (map == null) {
+            return null;
+        }
+        return map.get(key);
+    }
+
+    public Object setProperty(String key, Object value) {
+        if (map == null) {
+            map = new HashMap();
+        }
+        return map.put(key, value);
+    }
+    
+    public boolean hasProperty(String key) {
+        if (map == null) {
+            return false;
+        } 
+        return map.containsKey(key);
+    }
+   
+    public InputStream getXMLInputStream(String encoding)  throws 
+        UnsupportedEncodingException{
+        return new ByteArrayInputStream(getXMLBytes(encoding));
+    }
+
+    public void serialize(OutputStream output, OMOutputFormat format) throws 
XMLStreamException {
+        try {
+            // Write bytes to the output stream
+            output.write(getXMLBytes(format.getCharSetEncoding()));
+        } catch (IOException e) {
+            throw new XMLStreamException(e);
+        }
+    }
+
+    public void serialize(Writer writer, OMOutputFormat format) throws 
XMLStreamException {
+        try {
+            // Convert the bytes into a String and write it to the Writer
+            String text = new String(getXMLBytes(format.getCharSetEncoding()));
+            writer.write(text);
+        } catch (UnsupportedEncodingException e) {
+            throw new XMLStreamException(e);
+        } catch (IOException e) {
+            throw new XMLStreamException(e);
+        }
+    }
+
+    public void serialize(XMLStreamWriter xmlWriter) throws XMLStreamException 
{
+        // Some XMLStreamWriters (e.g. MTOMXMLStreamWriter) 
+        // provide direct access to the OutputStream.  
+        // This allows faster writing.
+        OutputStream os = getOutputStream(xmlWriter);
+        if (os != null) {
+            String encoding = getCharacterEncoding(xmlWriter);
+            OMOutputFormat format = new OMOutputFormat();
+            format.setCharSetEncoding(encoding);
+            serialize(os, format);
+        } else {
+            // Read the bytes into a reader and 
+            // write to the writer.
+            XMLStreamReader xmlReader = getReader();
+            reader2writer(xmlReader, xmlWriter);
+        }
+    }
+    /**
+     * Simple utility that takes an XMLStreamReader and writes it
+     * to an XMLStreamWriter
+     * @param reader
+     * @param writer
+     * @throws XMLStreamException
+     */
+    private static void reader2writer(XMLStreamReader reader, 
+                                     XMLStreamWriter writer) throws 
XMLStreamException {
+        StAXOMBuilder builder = new StAXOMBuilder(reader);
+        builder.releaseParserOnClose(true);
+        try {
+            OMDocument omDocument = builder.getDocument();
+            Iterator it = omDocument.getChildren();
+            while (it.hasNext()) {
+                OMNode omNode = (OMNode) it.next();
+                omNode.serializeAndConsume(writer);
+            }
+        } finally {
+            builder.close();
+        }
+    }
+    
+    /**
+     * Some XMLStreamWriters expose an OutputStream that can be
+     * accessed directly.
+     * @return OutputStream or null
+     */
+    private static OutputStream getOutputStream(XMLStreamWriter writer) 
+     throws XMLStreamException {
+        if (writer instanceof MTOMXMLStreamWriter) {
+            return ((MTOMXMLStreamWriter) writer).getOutputStream();
+        }
+        return null;
+    }
+    
+    /**
+     * Get the character set encoding of the XMLStreamWriter
+     * @return String or null
+     */
+    private static String getCharacterEncoding(XMLStreamWriter writer) {
+        if (writer instanceof MTOMXMLStreamWriter) {
+            return ((MTOMXMLStreamWriter) writer).getCharSetEncoding();
+        }
+        return null;
+    }
+}



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

Reply via email to