Author: veithen
Date: Mon Apr 30 20:12:04 2012
New Revision: 1332362

URL: http://svn.apache.org/viewvc?rev=1332362&view=rev
Log:
Improving the JSON code - Step 1 - Respect contracts: Axiom is about XML and 
nothing else. In particular OMDataSource#serialize(OutputStream, 
OMOutputFormat) and OMDataSource#serialize(Writer, OMOutputFormat) are expected 
to produce XML, not JSON nor anything else.

Also simplified the OMDataSource implementation by using an appropriate base 
class provided by Axiom.

Modified:
    
axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/AbstractJSONDataSource.java
    
axis/axis2/java/core/trunk/modules/json/test/org/apache/axis2/json/JSONDataSourceTest.java

Modified: 
axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/AbstractJSONDataSource.java
URL: 
http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/AbstractJSONDataSource.java?rev=1332362&r1=1332361&r2=1332362&view=diff
==============================================================================
--- 
axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/AbstractJSONDataSource.java
 (original)
+++ 
axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/AbstractJSONDataSource.java
 Mon Apr 30 20:12:04 2012
@@ -19,18 +19,13 @@
 
 package org.apache.axis2.json;
 
-import org.apache.axiom.om.OMDataSource;
 import org.apache.axiom.om.OMException;
-import org.apache.axiom.om.OMOutputFormat;
+import org.apache.axiom.om.ds.AbstractPullOMDataSource;
 
-import javax.xml.stream.XMLStreamConstants;
 import javax.xml.stream.XMLStreamReader;
 import javax.xml.stream.XMLStreamException;
-import javax.xml.stream.XMLStreamWriter;
 import java.io.IOException;
-import java.io.OutputStream;
 import java.io.Reader;
-import java.io.Writer;
 import java.io.BufferedReader;
 
 /**
@@ -39,7 +34,7 @@ import java.io.BufferedReader;
  * directly without expanding. This uses the "Mapped" JSON convention.
  */
 
-public abstract class AbstractJSONDataSource implements OMDataSource {
+public abstract class AbstractJSONDataSource extends AbstractPullOMDataSource {
 
     private Reader jsonReader;
     private String jsonString;
@@ -52,109 +47,6 @@ public abstract class AbstractJSONDataSo
     }
 
     /**
-     * Writes JSON into the output stream. As this should write JSON, it 
directly gets the JSON
-     * string and writes it without expanding the tree.
-     *
-     * @param outputStream   the stream to be written into
-     * @param omOutputFormat format of the message, this is ignored.
-     * @throws javax.xml.stream.XMLStreamException
-     *          if there is an error while writing the message in to the 
output stream.
-     */
-    public void serialize(OutputStream outputStream, OMOutputFormat 
omOutputFormat)
-            throws XMLStreamException {
-        try {
-            outputStream.write(getCompleteJOSNString().getBytes());
-        } catch (IOException e) {
-            throw new OMException();
-        }
-    }
-
-    /**
-     * Writes JSON through the writer. As this should write JSON, it directly 
gets the JSON string
-     * and writes it without expanding the tree.
-     *
-     * @param writer         Writer to be written into
-     * @param omOutputFormat format of the message, this is ignored.
-     * @throws javax.xml.stream.XMLStreamException
-     *          if there is an error while writing the message through the 
writer.
-     */
-    public void serialize(Writer writer, OMOutputFormat omOutputFormat)
-            throws XMLStreamException {
-        try {
-            writer.write(getCompleteJOSNString());
-        } catch (IOException e) {
-            throw new OMException();
-        }
-    }
-
-    /**
-     * Writes XML through the XMLStreamWriter. As the input data source is 
JSON, this method needs
-     * to get a StAX reader from that JSON String. Therefore this uses the 
getReader() method to get
-     * the StAX reader writes the events into the XMLStreamWriter.
-     *
-     * @param xmlStreamWriter StAX writer to be written into
-     * @throws javax.xml.stream.XMLStreamException
-     *          if there is an error while writing the message through the 
StAX writer.
-     */
-    public void serialize(XMLStreamWriter xmlStreamWriter) throws 
XMLStreamException {
-        XMLStreamReader reader = getReader();
-        xmlStreamWriter.writeStartDocument();
-        while (reader.hasNext()) {
-            int x = reader.next();
-            switch (x) {
-                case XMLStreamConstants.START_ELEMENT:
-                    xmlStreamWriter.writeStartElement(reader.getPrefix(), 
reader.getLocalName(),
-                                                      
reader.getNamespaceURI());
-                    int namespaceCount = reader.getNamespaceCount();
-                    for (int i = namespaceCount - 1; i >= 0; i--) {
-                        
xmlStreamWriter.writeNamespace(reader.getNamespacePrefix(i),
-                                                       
reader.getNamespaceURI(i));
-                    }
-                    int attributeCount = reader.getAttributeCount();
-                    for (int i = 0; i < attributeCount; i++) {
-                        
xmlStreamWriter.writeAttribute(reader.getAttributePrefix(i),
-                                                       
reader.getAttributeNamespace(i),
-                                                       
reader.getAttributeLocalName(i),
-                                                       
reader.getAttributeValue(i));
-                    }
-                    break;
-                case XMLStreamConstants.START_DOCUMENT:
-                    break;
-                case XMLStreamConstants.CHARACTERS:
-                    xmlStreamWriter.writeCharacters(reader.getText());
-                    break;
-                case XMLStreamConstants.CDATA:
-                    xmlStreamWriter.writeCData(reader.getText());
-                    break;
-                case XMLStreamConstants.END_ELEMENT:
-                    xmlStreamWriter.writeEndElement();
-                    break;
-                case XMLStreamConstants.END_DOCUMENT:
-                    xmlStreamWriter.writeEndDocument();
-                    break;
-                case XMLStreamConstants.SPACE:
-                    break;
-                case XMLStreamConstants.COMMENT:
-                    xmlStreamWriter.writeComment(reader.getText());
-                    break;
-                case XMLStreamConstants.DTD:
-                    xmlStreamWriter.writeDTD(reader.getText());
-                    break;
-                case XMLStreamConstants.PROCESSING_INSTRUCTION:
-                    xmlStreamWriter
-                            .writeProcessingInstruction(reader.getPITarget(), 
reader.getPIData());
-                    break;
-                case XMLStreamConstants.ENTITY_REFERENCE:
-                    xmlStreamWriter.writeEntityRef(reader.getLocalName());
-                    break;
-                default :
-                    throw new OMException();
-            }
-        }
-        xmlStreamWriter.writeEndDocument();
-    }
-
-    /**
      * Gives the StAX reader using the "Mapped" formatted input JSON String.
      *
      * @return The XMLStreamReader according to the JSON String.
@@ -164,6 +56,11 @@ public abstract class AbstractJSONDataSo
 
     public abstract XMLStreamReader getReader() throws XMLStreamException;
 
+    public boolean isDestructiveRead() {
+        // TODO: for the moment the data source in not destructive (because it 
reads the entire message into memory before processing it), but this will 
change...
+        return false;
+    }
+
     //returns the json string by consuming the JSON input stream.
     protected String getJSONString() {
         if (isRead) {

Modified: 
axis/axis2/java/core/trunk/modules/json/test/org/apache/axis2/json/JSONDataSourceTest.java
URL: 
http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/json/test/org/apache/axis2/json/JSONDataSourceTest.java?rev=1332362&r1=1332361&r2=1332362&view=diff
==============================================================================
--- 
axis/axis2/java/core/trunk/modules/json/test/org/apache/axis2/json/JSONDataSourceTest.java
 (original)
+++ 
axis/axis2/java/core/trunk/modules/json/test/org/apache/axis2/json/JSONDataSourceTest.java
 Mon Apr 30 20:12:04 2012
@@ -19,6 +19,7 @@
 
 package org.apache.axis2.json;
 
+import org.apache.axiom.om.OMOutputFormat;
 import org.apache.axiom.om.util.StAXUtils;
 import org.codehaus.jettison.json.JSONException;
 import org.custommonkey.xmlunit.XMLTestCase;
@@ -35,53 +36,55 @@ import java.io.StringReader;
 
 public class JSONDataSourceTest extends XMLTestCase {
 
-    public void testMappedSerialize1() throws XMLStreamException {
+    public void testMappedSerialize1() throws Exception {
         String jsonString = getMappedJSONString();
         ByteArrayOutputStream outStream = new ByteArrayOutputStream();
         JSONDataSource source = getMappedDataSource(jsonString);
-        source.serialize(outStream, null);
-        assertEquals(jsonString, new String(outStream.toByteArray()));
+        source.serialize(outStream, new OMOutputFormat());
+        assertXMLEqual("<mapping><inner><first>test string 
one</first></inner><inner>test string two</inner><name>foo</name></mapping>",
+                outStream.toString("utf-8"));
     }
 
-    public void testMappedSerialize2() throws XMLStreamException, IOException {
+    public void testMappedSerialize2() throws Exception {
         String jsonString = getMappedJSONString();
         ByteArrayOutputStream outStream = new ByteArrayOutputStream();
         OutputStreamWriter writer = new OutputStreamWriter(outStream);
         JSONDataSource source = getMappedDataSource(jsonString);
-        source.serialize(writer, null);
+        source.serialize(writer, new OMOutputFormat());
         writer.flush();
-        assertEquals(jsonString, new String(outStream.toByteArray()));
-
+        assertXMLEqual("<mapping><inner><first>test string 
one</first></inner><inner>test string two</inner><name>foo</name></mapping>",
+                outStream.toString("utf-8"));
     }
 
-    public void testMappedSerialize3() throws XMLStreamException {
+    public void testMappedSerialize3() throws Exception {
         String jsonString = getMappedJSONString();
         ByteArrayOutputStream outStream = new ByteArrayOutputStream();
         XMLStreamWriter writer = StAXUtils.createXMLStreamWriter(outStream);
         JSONDataSource source = getMappedDataSource(jsonString);
         source.serialize(writer);
         writer.flush();
-        assertEquals(
-                "<?xml version='1.0' 
encoding='UTF-8'?><mapping><inner><first>test string 
one</first></inner><inner>test string two</inner><name>foo</name></mapping>",
-                new String(outStream.toByteArray()));
+        assertXMLEqual("<mapping><inner><first>test string 
one</first></inner><inner>test string two</inner><name>foo</name></mapping>",
+                outStream.toString("utf-8"));
     }
 
-    public void testBadgerfishSerialize1() throws XMLStreamException {
+    public void testBadgerfishSerialize1() throws Exception {
         String jsonString = getBadgerfishJSONString();
         ByteArrayOutputStream outStream = new ByteArrayOutputStream();
         JSONBadgerfishDataSource source = getBadgerfishDataSource(jsonString);
-        source.serialize(outStream, null);
-        assertEquals(jsonString, new String(outStream.toByteArray()));
+        source.serialize(outStream, new OMOutputFormat());
+        assertXMLEqual("<p xmlns=\"http://def.ns\"; 
xmlns:bb=\"http://other.nsb\"; xmlns:aa=\"http://other.ns\";><sam 
att=\"lets\">555</sam></p>",
+                outStream.toString("utf-8"));
     }
 
-    public void testBadgerfishSerialize2() throws XMLStreamException, 
IOException {
+    public void testBadgerfishSerialize2() throws Exception {
         String jsonString = getBadgerfishJSONString();
         ByteArrayOutputStream outStream = new ByteArrayOutputStream();
         OutputStreamWriter writer = new OutputStreamWriter(outStream);
         JSONBadgerfishDataSource source = getBadgerfishDataSource(jsonString);
-        source.serialize(writer, null);
+        source.serialize(writer, new OMOutputFormat());
         writer.flush();
-        assertEquals(jsonString, new String(outStream.toByteArray()));
+        assertXMLEqual("<p xmlns=\"http://def.ns\"; 
xmlns:bb=\"http://other.nsb\"; xmlns:aa=\"http://other.ns\";><sam 
att=\"lets\">555</sam></p>",
+                outStream.toString("utf-8"));
     }
 
     public void testBadgerfishSerialize3() throws XMLStreamException, 
JSONException, IOException,
@@ -92,9 +95,8 @@ public class JSONDataSourceTest extends 
         JSONBadgerfishDataSource source = getBadgerfishDataSource(jsonString);
         source.serialize(writer);
         writer.flush();
-        assertXMLEqual(
-                "<?xml version='1.0' encoding='UTF-8'?><p 
xmlns=\"http://def.ns\"; xmlns:bb=\"http://other.nsb\"; 
xmlns:aa=\"http://other.ns\";><sam att=\"lets\">555</sam></p>",
-                new String(outStream.toByteArray()));
+        assertXMLEqual("<p xmlns=\"http://def.ns\"; 
xmlns:bb=\"http://other.nsb\"; xmlns:aa=\"http://other.ns\";><sam 
att=\"lets\">555</sam></p>",
+                outStream.toString("utf-8"));
     }
 
     private JSONBadgerfishDataSource getBadgerfishDataSource(String 
jsonString) {


Reply via email to