axis-java-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ami...@apache.org
Subject svn commit: r1376838 [2/2] - in /axis/axis2/java/core/trunk/modules/json: src/org/apache/axis2/json/impl/ src/org/apache/axis2/json/impl/rpc/ src/org/apache/axis2/json/impl/utils/ test-resources/custom_schema/ test/org/apache/axis2/json/impl/
Date Fri, 24 Aug 2012 06:34:33 GMT
Modified: axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/impl/utils/JsonUtils.java
URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/impl/utils/JsonUtils.java?rev=1376838&r1=1376837&r2=1376838&view=diff
==============================================================================
--- axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/impl/utils/JsonUtils.java
(original)
+++ axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/impl/utils/JsonUtils.java
Fri Aug 24 06:34:32 2012
@@ -31,19 +31,16 @@ import java.lang.reflect.Method;
 
 public class JsonUtils {
 
-    public static Object invokeServiceClass(InputStream inputStream ,
+    public static Object invokeServiceClass(JsonReader jsonReader,
                                             Object service,
                                             Method operation ,
                                             Class[] paramClasses ,
-                                            int paramCount , String charSetEncoding ) throws
InvocationTargetException,
+                                            int paramCount ) throws InvocationTargetException,
             IllegalAccessException, IOException  {
 
         Object[] methodParam = new Object[paramCount];
         Gson gson = new Gson();
         String[] argNames = new String[paramCount];
-        JsonReader jsonReader = null;
-
-        jsonReader = new JsonReader(new InputStreamReader(inputStream,charSetEncoding));
 
         if( ! jsonReader.isLenient()){
             jsonReader.setLenient(true);

Added: axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/impl/utils/XmlNode.java
URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/impl/utils/XmlNode.java?rev=1376838&view=auto
==============================================================================
--- axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/impl/utils/XmlNode.java
(added)
+++ axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/impl/utils/XmlNode.java
Fri Aug 24 06:34:32 2012
@@ -0,0 +1,71 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.axis2.json.impl.utils;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class XmlNode {
+
+    private String name;
+    private boolean isAttribute;
+    private boolean isArray;
+    private List<XmlNode> childrenList = new ArrayList<XmlNode>();
+    private String valueType;
+    private String namespaceUri;
+
+    public XmlNode(String name,String namespaceUri, boolean attribute, boolean array , String
valueType) {
+        this.name = name;
+        this.namespaceUri = namespaceUri;
+        isAttribute = attribute;
+        isArray = array;
+        this.valueType = valueType;
+    }
+
+
+    public void addChildtoList(XmlNode child) {
+        childrenList.add(child);
+    }
+
+
+    public String getName() {
+        return name;
+    }
+
+    public boolean isAttribute() {
+        return isAttribute;
+    }
+
+    public boolean isArray() {
+        return isArray;
+    }
+
+    public List<XmlNode> getChildrenList() {
+        return childrenList;
+    }
+
+    public String getValueType() {
+        return valueType;
+    }
+
+    public String getNamespaceUri() {
+        return namespaceUri;
+    }
+}

Added: axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/impl/utils/XmlNodeGenerator.java
URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/impl/utils/XmlNodeGenerator.java?rev=1376838&view=auto
==============================================================================
--- axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/impl/utils/XmlNodeGenerator.java
(added)
+++ axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/impl/utils/XmlNodeGenerator.java
Fri Aug 24 06:34:32 2012
@@ -0,0 +1,242 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.axis2.json.impl.utils;
+
+
+import org.apache.ws.commons.schema.XmlSchema;
+import org.apache.ws.commons.schema.XmlSchemaComplexType;
+import org.apache.ws.commons.schema.XmlSchemaElement;
+import org.apache.ws.commons.schema.XmlSchemaObjectCollection;
+import org.apache.ws.commons.schema.XmlSchemaParticle;
+import org.apache.ws.commons.schema.XmlSchemaSequence;
+import org.apache.ws.commons.schema.XmlSchemaSimpleType;
+import org.apache.ws.commons.schema.XmlSchemaType;
+
+import javax.xml.namespace.QName;
+import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Queue;
+
+public class XmlNodeGenerator {
+
+    List<XmlSchema> xmlSchemaList;
+
+    QName elementQname;
+
+    private XmlNode mainXmlNode;
+
+    Queue<JsonObject> queue = new LinkedList<JsonObject>();
+
+    public XmlNodeGenerator(List<XmlSchema> xmlSchemaList, QName elementQname) {
+        this.xmlSchemaList = xmlSchemaList;
+        this.elementQname = elementQname;
+    }
+
+    public XmlNodeGenerator() {
+    }
+
+    private void processSchemaList() {
+        // get the response schema and find the type then process.
+        XmlSchema responseSchema = getXmlSchema(elementQname);
+        XmlSchemaElement methodElement = responseSchema.getElementByName(elementQname.getLocalPart());
+        QName methodSchemaTypeName = methodElement.getSchemaTypeName();
+        mainXmlNode = new XmlNode(elementQname.getLocalPart(), elementQname.getNamespaceURI()
, false, (methodElement.getMaxOccurs() == 1 ? false : true) , "");
+        XmlSchemaParticle particle = ((XmlSchemaComplexType) methodElement.getSchemaType()).getParticle();
+        XmlSchemaSequence sequence = (XmlSchemaSequence) particle;
+        XmlSchemaObjectCollection xmlSchemaObjectCollection = sequence.getItems();
+
+        Iterator iterator = xmlSchemaObjectCollection.getIterator();
+        while (iterator.hasNext()) {
+            Object nextEle = iterator.next();
+            if (nextEle instanceof XmlSchemaElement) {
+                XmlSchemaElement innerElement = ((XmlSchemaElement) nextEle);   // todo add
to xml node
+                XmlSchemaType innerEleType = innerElement.getSchemaType();
+                if (innerEleType == null) {
+                    processSchemaTypeName(innerElement, mainXmlNode);
+                } else if (innerEleType instanceof XmlSchemaComplexType) {
+                    processComplexType(innerElement , mainXmlNode);
+                } else if (innerEleType instanceof XmlSchemaSimpleType) {
+                    processSimpleType(innerElement , mainXmlNode);
+                }
+            }
+        }
+
+/*        XmlSchemaElement argElement = (XmlSchemaElement) xmlSchemaObjectCollection.getItem(0);
+        XmlSchemaType schemaType = argElement.getSchemaType();
+        QName argQname = argElement.getQName();
+        QName schemaTypeName = argElement.getSchemaTypeName();
+        XmlNode temp;
+        if (argQname == null) {
+            temp = new XmlNode(argElement.getName(),elementQname.getNamespaceURI(), false,
(argElement.getMaxOccurs() == 1 ? false : true) ,schemaTypeName.getLocalPart());
+        } else {
+            temp = new XmlNode(argQname.getLocalPart(),argQname.getNamespaceURI(), false,
(argElement.getMaxOccurs() == 1 ? false : true) ,schemaTypeName.getLocalPart());
+
+        }
+        mainXmlNode.addChildtoList(temp);
+        String pref = schemaTypeName.getPrefix();
+        if (("xs").equals(pref)) {
+        } else {
+            XmlSchema tempXmlSchema = getXmlSchema(schemaTypeName);
+            processXmlSchema(tempXmlSchema, schemaTypeName,temp);
+        }*/
+    }
+
+    private XmlSchema getXmlSchema(QName qName) {
+        for (XmlSchema xmlSchema : xmlSchemaList) {
+            if (xmlSchema.getTargetNamespace().equals(qName.getNamespaceURI())) {
+                return xmlSchema;
+            }
+        }
+        return null;
+    }
+
+    private void processSchemaTypeName(XmlSchemaElement element, XmlNode parentNode) {
+        QName schemaTypeName = element.getSchemaTypeName();
+        QName qName = element.getQName();
+        String pref = schemaTypeName.getPrefix();
+        XmlNode temp;
+        if (qName == null) {
+            temp = new XmlNode(element.getName(), parentNode.getNamespaceUri(), false, (element.getMaxOccurs()
== 1 ? false : true), schemaTypeName.getLocalPart());
+
+        } else {
+            temp = new XmlNode(qName.getLocalPart(), qName.getNamespaceURI(), false, (element.getMaxOccurs()
== 1 ? false : true), schemaTypeName.getLocalPart());
+        }
+        parentNode.addChildtoList(temp);
+        if (("xs").equals(pref)) {
+        } else {
+            XmlSchema tempXmlSchema = getXmlSchema(schemaTypeName);
+            processXmlSchema(tempXmlSchema, schemaTypeName, temp);
+        }
+    }
+
+    private void processXmlSchema(XmlSchema schema, QName elementQName, XmlNode xmlNode)
{
+        XmlSchemaElement element = schema.getElementByName(elementQName);
+        if (element != null) {
+            XmlNode temp = new XmlNode(elementQName.getLocalPart(), elementQName.getNamespaceURI(),
false, (element.getMaxOccurs() == 1 ? false : true) , element.getSchemaTypeName().getLocalPart());
+            xmlNode.addChildtoList(temp);
+        } else {
+            XmlSchemaObjectCollection schemaObjectCollection = schema.getItems();
+            if (schemaObjectCollection.getCount() != 0) {
+                Iterator schemaObjIterator = schemaObjectCollection.getIterator();
+                while (schemaObjIterator.hasNext()) {
+                    Object next = schemaObjIterator.next();
+                    if (next instanceof XmlSchemaComplexType) {
+                        XmlSchemaComplexType comtype = (XmlSchemaComplexType) next;
+                        if (elementQName.getLocalPart().equals(comtype.getName())) {
+                            XmlSchemaParticle particle = comtype.getParticle();
+                            if (particle instanceof XmlSchemaSequence) {
+                                XmlSchemaSequence schemaSequence = (XmlSchemaSequence) particle;
+                                XmlSchemaObjectCollection InnerSchemaObjectCollection = schemaSequence.getItems();
+                                Iterator iterator = InnerSchemaObjectCollection.getIterator();
+                                while (iterator.hasNext()) {
+                                    Object nextEle = iterator.next();
+                                    if (nextEle instanceof XmlSchemaElement) {
+                                        XmlSchemaElement innerElement = ((XmlSchemaElement)
nextEle);   // todo add to xml node
+                                        XmlSchemaType innerEleType = innerElement.getSchemaType();
+                                        if (innerEleType == null) {
+                                            processSchemaTypeName(innerElement, xmlNode);
+                                        } else if (innerEleType instanceof XmlSchemaComplexType)
{
+                                            processComplexType(innerElement , xmlNode);
+                                        } else if (innerEleType instanceof XmlSchemaSimpleType)
{
+                                            processSimpleType(innerElement , xmlNode);
+                                        }
+                                    }
+                                }
+                            }
+                        }
+                    } else {
+                        // process simpletype
+                    }
+                }
+            }
+        }
+    }
+
+    private void generateQueue(XmlNode node) {
+        if (node.isArray()) {
+            if (node.getChildrenList().size() > 0) {
+                queue.add(new JsonObject(node.getName(), JSONType.NESTED_ARRAY, node.getValueType()
, node.getNamespaceUri()));
+                processXmlNodeChildren(node.getChildrenList());
+            } else {
+                queue.add(new JsonObject(node.getName(), JSONType.ARRAY , node.getValueType()
, node.getNamespaceUri()));
+            }
+        } else {
+            if (node.getChildrenList().size() > 0) {
+                queue.add(new JsonObject(node.getName(), JSONType.NESTED_OBJECT, node.getValueType()
, node.getNamespaceUri()));
+                processXmlNodeChildren(node.getChildrenList());
+            } else {
+                queue.add(new JsonObject(node.getName(), JSONType.OBJECT , node.getValueType()
, node.getNamespaceUri()));
+            }
+        }
+    }
+
+    private void processComplexType(XmlSchemaElement xmlSchemaElement , XmlNode parentNode)
{
+        QName schemaTypeName = xmlSchemaElement.getSchemaTypeName();
+        QName qName = xmlSchemaElement.getQName();
+        XmlNode temp = new XmlNode(qName.getLocalPart(), qName.getNamespaceURI(), false,
(xmlSchemaElement.getMaxOccurs() == 1 ? false : true) , schemaTypeName.getLocalPart());
+        parentNode.addChildtoList(temp);
+        XmlSchemaSequence schemaSequence;
+        XmlSchemaParticle particle = ((XmlSchemaComplexType)xmlSchemaElement.getSchemaType()).getParticle();
+        if (particle instanceof XmlSchemaSequence) {
+            schemaSequence = (XmlSchemaSequence) particle;
+            XmlSchemaObjectCollection schemaObjectCollection = schemaSequence.getItems();
+            Iterator iterator = schemaObjectCollection.getIterator();
+            while (iterator.hasNext()) {
+                Object element = iterator.next();
+                if (element instanceof XmlSchemaElement) {
+                    XmlSchemaElement innerElement = ((XmlSchemaElement) element);
+                    XmlSchemaType innerEleType = innerElement.getSchemaType();
+                    if (innerEleType instanceof XmlSchemaComplexType) {
+                        processComplexType(innerElement , temp);
+                    } else if(innerEleType instanceof XmlSchemaSimpleType){
+                        processSimpleType(innerElement , temp);
+                    }
+                }
+            }
+        }
+    }
+
+    private void processSimpleType(XmlSchemaElement xmlSchemaElement , XmlNode parentNode)
{
+        QName schemaTypeName= xmlSchemaElement.getSchemaTypeName();
+        QName qName = xmlSchemaElement.getQName();
+        XmlNode temp = new XmlNode(qName.getLocalPart(), qName.getNamespaceURI(), false,
(xmlSchemaElement.getMaxOccurs() == 1 ? false : true) , schemaTypeName.getLocalPart());
+        parentNode.addChildtoList(temp);
+    }
+
+    private void processXmlNodeChildren(List<XmlNode> childrenNodes) {
+        for (int i = 0; i < childrenNodes.size(); i++) {
+            generateQueue(childrenNodes.get(i));
+        }
+    }
+
+    public XmlNode getMainXmlNode() {
+        if (mainXmlNode == null) {
+            processSchemaList();
+        }
+        return mainXmlNode;
+    }
+
+    public Queue<JsonObject> getQueue(XmlNode node) {
+        generateQueue(node);
+        return queue;
+    }
+
+}

Added: axis/axis2/java/core/trunk/modules/json/test-resources/custom_schema/testSchema_1.xsd
URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/json/test-resources/custom_schema/testSchema_1.xsd?rev=1376838&view=auto
==============================================================================
--- axis/axis2/java/core/trunk/modules/json/test-resources/custom_schema/testSchema_1.xsd
(added)
+++ axis/axis2/java/core/trunk/modules/json/test-resources/custom_schema/testSchema_1.xsd
Fri Aug 24 06:34:32 2012
@@ -0,0 +1,44 @@
+<?xml version="1.0"?>
+
+<!--
+  ~ Licensed to the Apache Software Foundation (ASF) under one
+  ~ or more contributor license agreements. See the NOTICE file
+  ~ distributed with this work for additional information
+  ~ regarding copyright ownership. The ASF licenses this file
+  ~ to you 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.
+  -->
+
+
+<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
+targetNamespace="http://www.w3schools.com"
+xmlns="http://www.w3schools.com"
+elementFormDefault="qualified">
+
+    <xs:element name="response">
+        <xs:complexType>
+            <xs:sequence>
+                <xs:element name="return" type="Person"/>
+            </xs:sequence>
+        </xs:complexType>
+    </xs:element>
+
+    <xs:complexType name="Person">
+        <xs:sequence>
+            <xs:element name="name" type="xs:string"/>
+            <xs:element name="age" type="xs:string"/>
+            <xs:element name="gender" type="xs:string"/>
+        </xs:sequence>
+    </xs:complexType>
+
+</xs:schema>
\ No newline at end of file

Added: axis/axis2/java/core/trunk/modules/json/test/org/apache/axis2/json/impl/GsonXMLStreamReaderTest.java
URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/json/test/org/apache/axis2/json/impl/GsonXMLStreamReaderTest.java?rev=1376838&view=auto
==============================================================================
--- axis/axis2/java/core/trunk/modules/json/test/org/apache/axis2/json/impl/GsonXMLStreamReaderTest.java
(added)
+++ axis/axis2/java/core/trunk/modules/json/test/org/apache/axis2/json/impl/GsonXMLStreamReaderTest.java
Fri Aug 24 06:34:32 2012
@@ -0,0 +1,70 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.axis2.json.impl;
+
+import com.google.gson.stream.JsonReader;
+import org.apache.axiom.om.OMElement;
+import org.apache.axiom.om.impl.builder.StAXOMBuilder;
+import org.apache.axis2.context.ConfigurationContext;
+import org.apache.axis2.engine.AxisConfiguration;
+import org.apache.ws.commons.schema.XmlSchema;
+import org.apache.ws.commons.schema.XmlSchemaCollection;
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+import javax.xml.namespace.QName;
+import javax.xml.transform.stream.StreamSource;
+import java.io.ByteArrayInputStream;
+import java.io.FileInputStream;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.util.ArrayList;
+import java.util.List;
+
+public class GsonXMLStreamReaderTest {
+    
+    
+    @Test
+    public void testGsonXMLStreamReader() throws Exception {
+        String jsonString = "{\"response\":{\"return\":{\"name\":\"kate\",\"age\":\"35\",\"gender\":\"female\"}}}";
+        String xmlString = "<response xmlns=\"http://www.w3schools.com\"><return><name>kate</name><age>35</age><gender>female</gender></return></response>";
+        InputStream inputStream = new ByteArrayInputStream(jsonString.getBytes());
+        JsonReader jsonReader = new JsonReader(new InputStreamReader(inputStream , "UTF-8"));
+        String fileName = "test-resources/custom_schema/testSchema_1.xsd";
+        InputStream is = new FileInputStream(fileName);
+        XmlSchemaCollection schemaCol = new XmlSchemaCollection();
+        XmlSchema schema = schemaCol.read(new StreamSource(is), null);
+        List<XmlSchema> schemaList = new ArrayList<XmlSchema>();
+        schemaList.add(schema);
+        QName elementQName = new QName("http://www.w3schools.com", "response");
+        ConfigurationContext configCtxt = new ConfigurationContext(new AxisConfiguration());
+        GsonXMLStreamReader gsonXMLStreamReader = new GsonXMLStreamReader(jsonReader);
+        gsonXMLStreamReader.initXmlStreamReader(elementQName , schemaList , configCtxt);
+        StAXOMBuilder stAXOMBuilder = new StAXOMBuilder(gsonXMLStreamReader);
+        OMElement omElement = stAXOMBuilder.getDocumentElement();
+        String actual = omElement.toString();
+        inputStream.close();
+        is.close();
+        Assert.assertEquals(xmlString , actual);
+
+    }
+}

Added: axis/axis2/java/core/trunk/modules/json/test/org/apache/axis2/json/impl/GsonXMLStreamWriterTest.java
URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/json/test/org/apache/axis2/json/impl/GsonXMLStreamWriterTest.java?rev=1376838&view=auto
==============================================================================
--- axis/axis2/java/core/trunk/modules/json/test/org/apache/axis2/json/impl/GsonXMLStreamWriterTest.java
(added)
+++ axis/axis2/java/core/trunk/modules/json/test/org/apache/axis2/json/impl/GsonXMLStreamWriterTest.java
Fri Aug 24 06:34:32 2012
@@ -0,0 +1,94 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.axis2.json.impl;
+
+import com.google.gson.stream.JsonWriter;
+import org.apache.axiom.om.OMAbstractFactory;
+import org.apache.axiom.om.OMElement;
+import org.apache.axiom.om.OMFactory;
+import org.apache.axiom.om.OMNamespace;
+import org.apache.axis2.context.ConfigurationContext;
+import org.apache.axis2.engine.AxisConfiguration;
+import org.apache.ws.commons.schema.XmlSchema;
+import org.apache.ws.commons.schema.XmlSchemaCollection;
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+import javax.xml.namespace.QName;
+import javax.xml.transform.stream.StreamSource;
+import java.io.ByteArrayOutputStream;
+import java.io.FileInputStream;
+import java.io.InputStream;
+import java.io.OutputStreamWriter;
+import java.util.ArrayList;
+import java.util.List;
+
+
+public class GsonXMLStreamWriterTest {
+    private String jsonString;
+
+    @Test
+    public void testGsonXMLStreamWriter() throws Exception {
+        jsonString = "{\"response\":{\"return\":{\"name\":\"kate\",\"age\":\"35\",\"gender\":\"female\"}}}";
+        ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(baos, "UTF-8");
+        JsonWriter jsonWriter = new JsonWriter(outputStreamWriter);
+        String fileName = "test-resources/custom_schema/testSchema_1.xsd";
+        InputStream is = new FileInputStream(fileName);
+        XmlSchemaCollection schemaCol = new XmlSchemaCollection();
+        XmlSchema schema = schemaCol.read(new StreamSource(is), null);
+        List<XmlSchema> schemaList = new ArrayList<XmlSchema>();
+        schemaList.add(schema);
+        QName elementQName = new QName("http://www.w3schools.com", "response");
+        ConfigurationContext configCtxt = new ConfigurationContext(new AxisConfiguration());
+
+        GsonXMLStreamWriter gsonXMLStreamWriter = new GsonXMLStreamWriter(jsonWriter, elementQName,
schemaList, configCtxt);
+        OMElement omElement = getResponseOMElement();
+        gsonXMLStreamWriter.writeStartDocument();
+        omElement.serialize(gsonXMLStreamWriter);
+        gsonXMLStreamWriter.writeEndDocument();
+
+        String actualString = baos.toString();
+        outputStreamWriter.close();
+        Assert.assertEquals(jsonString, actualString);
+    }
+
+
+    private OMElement getResponseOMElement() {
+        OMFactory omFactory = OMAbstractFactory.getOMFactory();
+        OMNamespace ns = omFactory.createOMNamespace("", "");
+
+        OMElement response = omFactory.createOMElement("response", ns);
+        OMElement ret = omFactory.createOMElement("return", ns);
+        OMElement name = omFactory.createOMElement("name", ns);
+        name.setText("kate");
+        OMElement age = omFactory.createOMElement("age", ns);
+        age.setText("35");
+        OMElement gender = omFactory.createOMElement("gender", ns);
+        gender.setText("female");
+        ret.addChild(name);
+        ret.addChild(age);
+        ret.addChild(gender);
+        response.addChild(ret);
+        return response;
+    }
+}

Added: axis/axis2/java/core/trunk/modules/json/test/org/apache/axis2/json/impl/JsonBuilderTest.java
URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/json/test/org/apache/axis2/json/impl/JsonBuilderTest.java?rev=1376838&view=auto
==============================================================================
--- axis/axis2/java/core/trunk/modules/json/test/org/apache/axis2/json/impl/JsonBuilderTest.java
(added)
+++ axis/axis2/java/core/trunk/modules/json/test/org/apache/axis2/json/impl/JsonBuilderTest.java
Fri Aug 24 06:34:32 2012
@@ -0,0 +1,75 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.axis2.json.impl;
+
+import com.google.gson.stream.JsonReader;
+import org.apache.axis2.Constants;
+import org.apache.axis2.context.MessageContext;
+import org.apache.axis2.json.impl.utils.JsonConstant;
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+
+public class JsonBuilderTest {
+
+    @Test
+    public void testProcessDocument() throws Exception {
+        MessageContext messageContext = new MessageContext();
+        String contentType = "application/json-impl";
+        String jsonString = "{\"methodName\":{\"param\":\"value\"}}";
+        ByteArrayInputStream inputStream = new ByteArrayInputStream(jsonString.getBytes("UTF-8"));
+        messageContext.setProperty(Constants.Configuration.CHARACTER_SET_ENCODING, "UTF-8");
+
+        JsonBuilder jsonBuilder = new JsonBuilder();
+        jsonBuilder.processDocument(inputStream, contentType, messageContext);
+
+        Object isJson = messageContext.getProperty(JsonConstant.IS_JSON_STREAM);
+        Assert.assertNotNull(isJson);
+        isJson = Boolean.valueOf(isJson.toString());
+        Assert.assertEquals(true, isJson);
+        Object streamReader = messageContext.getProperty(JsonConstant.GSON_XML_STREAM_READER);
+        Assert.assertNotNull(streamReader);
+        GsonXMLStreamReader gsonXMLStreamReader = (GsonXMLStreamReader) streamReader;
+        JsonReader jsonReader = gsonXMLStreamReader.getJsonReader();
+        Assert.assertNotNull(jsonReader);
+        try {
+            String actualString = readJsonReader(jsonReader);
+            Assert.assertEquals("value", actualString);
+        } catch (IOException e) {
+            Assert.assertFalse(true);
+        }
+
+        inputStream.close();
+
+    }
+
+    private String readJsonReader(JsonReader jsonReader) throws IOException {
+        jsonReader.beginObject();
+        jsonReader.nextName();
+        jsonReader.beginObject();
+        jsonReader.nextName();
+        String name = jsonReader.nextString();
+        jsonReader.endObject();
+        jsonReader.endObject();
+        return name;
+    }
+}

Added: axis/axis2/java/core/trunk/modules/json/test/org/apache/axis2/json/impl/JsonFormatterTest.java
URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/json/test/org/apache/axis2/json/impl/JsonFormatterTest.java?rev=1376838&view=auto
==============================================================================
--- axis/axis2/java/core/trunk/modules/json/test/org/apache/axis2/json/impl/JsonFormatterTest.java
(added)
+++ axis/axis2/java/core/trunk/modules/json/test/org/apache/axis2/json/impl/JsonFormatterTest.java
Fri Aug 24 06:34:32 2012
@@ -0,0 +1,211 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.axis2.json.impl;
+
+import com.google.gson.Gson;
+import org.apache.axiom.om.OMAbstractFactory;
+import org.apache.axiom.om.OMElement;
+import org.apache.axiom.om.OMFactory;
+import org.apache.axiom.om.OMNamespace;
+import org.apache.axiom.om.OMOutputFormat;
+import org.apache.axiom.soap.SOAPEnvelope;
+import org.apache.axiom.soap.SOAPFactory;
+import org.apache.axis2.Constants;
+import org.apache.axis2.context.ConfigurationContext;
+import org.apache.axis2.context.MessageContext;
+import org.apache.axis2.description.AxisMessage;
+import org.apache.axis2.description.AxisOperation;
+import org.apache.axis2.description.AxisOperationFactory;
+import org.apache.axis2.description.AxisService;
+import org.apache.axis2.engine.AxisConfiguration;
+import org.apache.axis2.json.impl.utils.JsonConstant;
+import org.apache.axis2.wsdl.WSDLConstants;
+import org.apache.ws.commons.schema.XmlSchema;
+import org.apache.ws.commons.schema.XmlSchemaCollection;
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+import javax.xml.namespace.QName;
+import javax.xml.transform.stream.StreamSource;
+import java.io.ByteArrayOutputStream;
+import java.io.FileInputStream;
+import java.io.InputStream;
+import java.io.OutputStream;
+
+public class JsonFormatterTest {
+    MessageContext outMsgContext;
+    String contentType;
+    String jsonString;
+    SOAPEnvelope soapEnvelope;
+    OMOutputFormat outputFormat;
+    OutputStream outputStream;
+    @Before
+    public void setUp() throws Exception {
+        contentType = "application/json-impl";
+        SOAPFactory soapFactory = OMAbstractFactory.getSOAP11Factory();
+        soapEnvelope = soapFactory.getDefaultEnvelope();
+        outputFormat = new OMOutputFormat();
+        outputStream = new ByteArrayOutputStream();
+
+        outMsgContext = new MessageContext();
+        outMsgContext.setProperty(Constants.Configuration.CHARACTER_SET_ENCODING, "UTF-8");
+    }
+
+    @After
+    public void tearDown() throws Exception {
+        outputStream.close();
+    }
+
+    @Test
+    public void testWriteToFaultMessage() throws Exception {
+        jsonString = "{\"Fault\":{\"faultcode\":\"soapenv:Server\",\"faultstring\":\"javax.xml.stream.XMLStreamException\",\"detail\":\"testFaultMsg\"}}";
+        outMsgContext.setProcessingFault(true);
+        soapEnvelope.getBody().addChild(createFaultOMElement());
+        outMsgContext.setEnvelope(soapEnvelope);
+        JsonFormatter jsonFormatter = new JsonFormatter();
+        jsonFormatter.writeTo(outMsgContext, outputFormat, outputStream, false);
+        String faultMsg = outputStream.toString();
+        Assert.assertEquals(jsonString , faultMsg);
+    }
+
+
+    @Test
+    public void testWriteToXMLtoJSON() throws Exception {
+        jsonString = "{\"response\":{\"return\":{\"name\":\"kate\",\"age\":\"35\",\"gender\":\"female\"}}}";
+        String fileName = "test-resources/custom_schema/testSchema_1.xsd";
+        InputStream is = new FileInputStream(fileName);
+        XmlSchemaCollection schemaCol = new XmlSchemaCollection();
+        XmlSchema schema = schemaCol.read(new StreamSource(is), null);
+        QName elementQName = new QName("http://www.w3schools.com", "response");
+        ConfigurationContext configCtxt = new ConfigurationContext(new AxisConfiguration());
+        outMsgContext.setConfigurationContext(configCtxt);
+        AxisOperation axisOperation = AxisOperationFactory.getAxisOperation(AxisOperation.MEP_CONSTANT_IN_OUT);
+        AxisMessage message = new AxisMessage();
+        message.setElementQName(elementQName);
+        axisOperation.addMessage(message , WSDLConstants.MESSAGE_LABEL_OUT_VALUE);
+        outMsgContext.setAxisOperation(axisOperation);
+        AxisService axisService = new AxisService("testService");
+        axisService.addSchema(schema);
+        outMsgContext.setAxisService(axisService);
+        soapEnvelope.getBody().addChild(getResponseOMElement());
+        outMsgContext.setEnvelope(soapEnvelope);
+        JsonFormatter jsonFormatter = new JsonFormatter();
+        jsonFormatter.writeTo(outMsgContext, outputFormat , outputStream , false);
+        String response = outputStream.toString();
+        Assert.assertEquals(jsonString, response);
+    }
+
+
+    @Test
+    public void testWriteToJSON() throws Exception {
+        Person person = new Person();
+        person.setName("Leo");
+        person.setAge(27);
+        person.setGender("Male");
+        person.setSingle(true);
+        outMsgContext.setProperty(JsonConstant.RETURN_OBJECT, person);
+        outMsgContext.setProperty(JsonConstant.RETURN_TYPE, Person.class);
+        jsonString = "{\""+ JsonConstant.RESPONSE +"\":" + new Gson().toJson(person, Person.class)
+ "}";
+
+        JsonFormatter jsonFormatter = new JsonFormatter();
+        jsonFormatter.writeTo(outMsgContext, outputFormat, outputStream, false);
+        String personString = outputStream.toString();
+        Assert.assertEquals(jsonString, personString);
+
+    }
+
+
+    private OMElement createFaultOMElement() {
+        OMFactory omFactory = OMAbstractFactory.getOMFactory();
+        OMNamespace ns = omFactory.createOMNamespace("", "");
+        OMElement faultCode = omFactory.createOMElement("faultcode", ns);
+        faultCode.setText("soapenv:Server");
+        OMElement faultString = omFactory.createOMElement("faultstring", ns);
+        faultString.setText("javax.xml.stream.XMLStreamException");
+        OMElement detail = omFactory.createOMElement("detail", ns);
+        detail.setText("testFaultMsg");
+        OMElement fault = omFactory.createOMElement("Fault", ns);
+        fault.addChild(faultCode);
+        fault.addChild(faultString);
+        fault.addChild(detail);
+        return  fault;
+    }
+
+    private OMElement getResponseOMElement() {
+        OMFactory omFactory = OMAbstractFactory.getOMFactory();
+        OMNamespace ns = omFactory.createOMNamespace("", "");
+
+        OMElement response = omFactory.createOMElement("response", ns);
+        OMElement ret = omFactory.createOMElement("return", ns);
+        OMElement name = omFactory.createOMElement("name", ns);
+        name.setText("kate");
+        OMElement age = omFactory.createOMElement("age", ns);
+        age.setText("35");
+        OMElement gender = omFactory.createOMElement("gender", ns);
+        gender.setText("female");
+        ret.addChild(name);
+        ret.addChild(age);
+        ret.addChild(gender);
+        response.addChild(ret);
+        return response;
+    }
+
+    public static class Person {
+
+        private String name;
+        private int age;
+        private String gender;
+        private boolean single;
+
+        public String getName() {
+            return name;
+        }
+
+        public void setName(String name) {
+            this.name = name;
+        }
+
+        public int getAge() {
+            return age;
+        }
+
+        public void setAge(int age) {
+            this.age = age;
+        }
+
+        public String getGender() {
+            return gender;
+        }
+
+        public void setGender(String gender) {
+            this.gender = gender;
+        }
+
+        public boolean isSingle() {
+            return single;
+        }
+
+        public void setSingle(boolean single) {
+            this.single = single;
+        }
+    }
+}



Mime
View raw message