You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-commits@axis.apache.org by ve...@apache.org on 2012/05/01 11:18:53 UTC

svn commit: r1332584 - /axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/

Author: veithen
Date: Tue May  1 09:18:52 2012
New Revision: 1332584

URL: http://svn.apache.org/viewvc?rev=1332584&view=rev
Log:
Use the proper Axiom APIs to access the original JSON string in the OMDataSource.

Modified:
    axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/AbstractJSONDataSource.java
    axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/AbstractJSONMessageFormatter.java
    axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/JSONBadgerfishDataSource.java
    axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/JSONBadgerfishMessageFormatter.java
    axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/JSONDataSource.java
    axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/JSONMessageFormatter.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=1332584&r1=1332583&r2=1332584&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 Tue May  1 09:18:52 2012
@@ -21,6 +21,8 @@ package org.apache.axis2.json;
 
 import org.apache.axiom.om.OMException;
 import org.apache.axiom.om.ds.AbstractPullOMDataSource;
+import org.codehaus.jettison.AbstractXMLInputFactory;
+import org.codehaus.jettison.json.JSONTokener;
 
 import javax.xml.stream.XMLStreamReader;
 import javax.xml.stream.XMLStreamException;
@@ -44,23 +46,24 @@ public abstract class AbstractJSONDataSo
         this.jsonReader = jsonReader;
     }
 
-    /**
-     * Gives the StAX reader using the "Mapped" formatted input JSON String.
-     *
-     * @return The XMLStreamReader according to the JSON String.
-     * @throws javax.xml.stream.XMLStreamException
-     *          if there is an error while making the StAX reader.
-     */
-
-    public abstract XMLStreamReader getReader() throws XMLStreamException;
+    public final XMLStreamReader getReader() throws XMLStreamException {
+        return getXMLInputFactory().createXMLStreamReader(new JSONTokener(getJSONString()));
+    }
 
+    protected abstract AbstractXMLInputFactory getXMLInputFactory();
+    
     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;
     }
 
+    @Override
+    public Object getObject() {
+        return getJSONString();
+    }
+
     //returns the json string by consuming the JSON input stream.
-    protected String getJSONString() {
+    private String getJSONString() {
         if (isRead) {
             return jsonString;
         } else {

Modified: axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/AbstractJSONMessageFormatter.java
URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/AbstractJSONMessageFormatter.java?rev=1332584&r1=1332583&r2=1332584&view=diff
==============================================================================
--- axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/AbstractJSONMessageFormatter.java (original)
+++ axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/AbstractJSONMessageFormatter.java Tue May  1 09:18:52 2012
@@ -50,6 +50,18 @@ import java.net.URL;
 
 
 public abstract class AbstractJSONMessageFormatter implements MessageFormatter {
+    private final Class<? extends AbstractJSONDataSource> dataSourceClass;
+
+    /**
+     * Constructor.
+     * 
+     * @param dataSourceClass
+     *            the {@link OMDataSource} class corresponding to the JSON format used by this
+     *            message formatter; this information is used for optimization (pass through)
+     */
+    public AbstractJSONMessageFormatter(Class<? extends AbstractJSONDataSource> dataSourceClass) {
+        this.dataSourceClass = dataSourceClass;
+    }
 
     public String getContentType(MessageContext msgCtxt, OMOutputFormat format,
                                  String soapActionString) {
@@ -83,9 +95,8 @@ public abstract class AbstractJSONMessag
         //if the element is an OMSourcedElement and it contains a JSONDataSource with
         //correct convention, directly get the JSON string.
 
-        if (element instanceof OMSourcedElement &&
-                getStringToWrite(((OMSourcedElement)element).getDataSource()) != null) {
-            String jsonToWrite = getStringToWrite(((OMSourcedElement)element).getDataSource());
+        String jsonToWrite = getStringToWrite(element);
+        if (jsonToWrite != null) {
             return jsonToWrite.getBytes();
             //otherwise serialize the OM by expanding the tree
         } else {
@@ -128,13 +139,20 @@ public abstract class AbstractJSONMessag
     protected abstract XMLStreamWriter getJSONWriter(Writer writer);
 
     /**
-     * If the data source is a "Mapped" formatted data source, gives the JSON string by directly
-     * taking from the data source.
-     *
-     * @param dataSource data source to be checked
-     * @return the JSON string to write
+     * Get the original JSON string from the given element if it is available and if the element has
+     * not been modified.
+     * 
+     * @param element
+     *            the element
+     * @return the JSON string to write, or <code>null</code> if it is not available
      */
-    protected abstract String getStringToWrite(OMDataSource dataSource);
+    private String getStringToWrite(OMElement element) {
+        if (element instanceof OMSourcedElement) {
+            return (String)((OMSourcedElement)element).getObject(dataSourceClass);
+        } else {
+            return null;
+        }
+    }
 
     /**
      * Writes the JSON message to the output stream with the correct convention. If the payload is
@@ -162,11 +180,8 @@ public abstract class AbstractJSONMessag
                 element2.setText(fault.toString());
                 element = element2;
             }
-            if (element instanceof OMSourcedElement &&
-                    getStringToWrite(((OMSourcedElement)element).getDataSource()) != null) {
-                String jsonToWrite =
-                        getStringToWrite(((OMSourcedElement)element).getDataSource());
-
+            String jsonToWrite = getStringToWrite(element);
+            if (jsonToWrite != null) {
                 out.write(jsonToWrite.getBytes());
             } else {
                 XMLStreamWriter jsonWriter = getJSONWriter(out, format);
@@ -198,12 +213,8 @@ public abstract class AbstractJSONMessag
         if (dataOut != null && (httpMethod != null)
                 && Constants.Configuration.HTTP_METHOD_GET.equalsIgnoreCase(httpMethod)) {
             try {
-                String jsonString;
-                if (dataOut instanceof OMSourcedElement && getStringToWrite(
-                        ((OMSourcedElement) dataOut).getDataSource()) != null) {
-                    jsonString = getStringToWrite(((OMSourcedElement)
-                            dataOut).getDataSource());
-                } else {
+                String jsonString = getStringToWrite(dataOut);
+                if (jsonString == null) {
                     StringWriter out = new StringWriter();
                     XMLStreamWriter jsonWriter = getJSONWriter(out);
                     // Jettison v1.2+ relies on writeStartDocument being called (AXIS2-5044)

Modified: axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/JSONBadgerfishDataSource.java
URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/JSONBadgerfishDataSource.java?rev=1332584&r1=1332583&r2=1332584&view=diff
==============================================================================
--- axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/JSONBadgerfishDataSource.java (original)
+++ axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/JSONBadgerfishDataSource.java Tue May  1 09:18:52 2012
@@ -19,9 +19,8 @@
 
 package org.apache.axis2.json;
 
-
+import org.codehaus.jettison.AbstractXMLInputFactory;
 import org.codehaus.jettison.badgerfish.BadgerFishXMLInputFactory;
-import org.codehaus.jettison.json.JSONTokener;
 
 import java.io.Reader;
 
@@ -35,20 +34,8 @@ public class JSONBadgerfishDataSource ex
         super(jsonReader);
     }
 
-    /**
-     * Gives the StAX reader using the "Badgerfish" formatted input JSON String.
-     *
-     * @return The XMLStreamReader according to the JSON String.
-     * @throws javax.xml.stream.XMLStreamException
-     *          if there is an error while making the StAX reader.
-     */
     @Override
-    public javax.xml.stream.XMLStreamReader getReader() throws javax.xml.stream.XMLStreamException {
-
-        //input factory for "Badgerfish"
-        BadgerFishXMLInputFactory inputFactory = new BadgerFishXMLInputFactory();
-        return inputFactory.createXMLStreamReader(
-                new JSONTokener(getJSONString()));
-
+    protected AbstractXMLInputFactory getXMLInputFactory() {
+        return new BadgerFishXMLInputFactory();
     }
 }

Modified: axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/JSONBadgerfishMessageFormatter.java
URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/JSONBadgerfishMessageFormatter.java?rev=1332584&r1=1332583&r2=1332584&view=diff
==============================================================================
--- axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/JSONBadgerfishMessageFormatter.java (original)
+++ axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/JSONBadgerfishMessageFormatter.java Tue May  1 09:18:52 2012
@@ -19,7 +19,6 @@
 
 package org.apache.axis2.json;
 
-import org.apache.axiom.om.OMDataSource;
 import org.codehaus.jettison.badgerfish.BadgerFishXMLStreamWriter;
 
 import javax.xml.stream.XMLStreamWriter;
@@ -33,26 +32,13 @@ import java.io.Writer;
  */
 
 public class JSONBadgerfishMessageFormatter extends AbstractJSONMessageFormatter {
-
+    public JSONBadgerfishMessageFormatter() {
+        super(JSONBadgerfishDataSource.class);
+    }
+    
     //returns the writer for the badgerfish format
     @Override
     protected XMLStreamWriter getJSONWriter(Writer writer) {
         return new BadgerFishXMLStreamWriter(writer);
     }
-
-    /**
-     * If the data source is a "Badgerfish" formatted data source, gives the JSON string by directly
-     * taking from the data source.
-     *
-     * @param dataSource data source to be checked
-     * @return the JSON string to write
-     */
-    @Override
-    protected String getStringToWrite(OMDataSource dataSource) {
-        if (dataSource instanceof JSONBadgerfishDataSource) {
-            return ((JSONBadgerfishDataSource)dataSource).getJSONString();
-        } else {
-            return null;
-        }
-    }
 }

Modified: axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/JSONDataSource.java
URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/JSONDataSource.java?rev=1332584&r1=1332583&r2=1332584&view=diff
==============================================================================
--- axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/JSONDataSource.java (original)
+++ axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/JSONDataSource.java Tue May  1 09:18:52 2012
@@ -19,11 +19,9 @@
 
 package org.apache.axis2.json;
 
-import org.codehaus.jettison.json.JSONTokener;
+import org.codehaus.jettison.AbstractXMLInputFactory;
 import org.codehaus.jettison.mapped.MappedXMLInputFactory;
 
-import javax.xml.stream.XMLStreamReader;
-import javax.xml.stream.XMLStreamException;
 import java.io.Reader;
 import java.util.HashMap;
 
@@ -37,21 +35,13 @@ public class JSONDataSource extends Abst
         super(jsonReader);
     }
 
-    /**
-     * Gives the StAX reader using the "Mapped" formatted input JSON String.
-     *
-     * @return The XMLStreamReader according to the JSON String.
-     * @throws javax.xml.stream.XMLStreamException
-     *          if there is an error while making the StAX reader.
-     */
     @Override
-    public XMLStreamReader getReader() throws XMLStreamException {
+    protected AbstractXMLInputFactory getXMLInputFactory() {
 
         HashMap XMLToJSNNamespaceMap = new HashMap();
         XMLToJSNNamespaceMap.put("", "");
 
         //input factory for "Mapped" convention
-        MappedXMLInputFactory inputFactory = new MappedXMLInputFactory(XMLToJSNNamespaceMap);
-        return inputFactory.createXMLStreamReader(new JSONTokener(getJSONString()));
+        return new MappedXMLInputFactory(XMLToJSNNamespaceMap);
     }
 }

Modified: axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/JSONMessageFormatter.java
URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/JSONMessageFormatter.java?rev=1332584&r1=1332583&r2=1332584&view=diff
==============================================================================
--- axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/JSONMessageFormatter.java (original)
+++ axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/JSONMessageFormatter.java Tue May  1 09:18:52 2012
@@ -19,7 +19,6 @@
 
 package org.apache.axis2.json;
 
-import org.apache.axiom.om.OMDataSource;
 import org.codehaus.jettison.mapped.MappedNamespaceConvention;
 import org.codehaus.jettison.mapped.MappedXMLStreamWriter;
 
@@ -39,28 +38,14 @@ import java.io.Writer;
 
 
 public class JSONMessageFormatter extends AbstractJSONMessageFormatter {
-
+    public JSONMessageFormatter() {
+        super(JSONDataSource.class);
+    }
+    
     //returns the "Mapped" JSON writer
     @Override
     protected XMLStreamWriter getJSONWriter(Writer writer) {
         MappedNamespaceConvention mnc = new MappedNamespaceConvention();
         return new MappedXMLStreamWriter(mnc, writer);
     }
-
-    /**
-     * If the data source is a "Mapped" formatted data source, gives the JSON string by directly
-     * taking from the data source.
-     *
-     * @param dataSource data source to be checked
-     * @return the JSON string to write
-     */
-    @Override
-    protected String getStringToWrite(OMDataSource dataSource) {
-        if (dataSource instanceof JSONDataSource) {
-            return ((JSONDataSource)dataSource).getJSONString();
-        } else {
-            return null;
-        }
-    }
-
 }