Author: shameera Date: Sun Mar 16 16:43:06 2014 New Revision: 1578111 URL: http://svn.apache.org/r1578111 Log: Improved XML Stream based JSON to generate intermediate structure when schema has ref elements and added a new test case too
Added: axis/axis2/java/core/trunk/modules/json/test-resources/custom_schema/testSchema_3.xsd Modified: axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/gson/GsonXMLStreamReader.java axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/gson/GsonXMLStreamWriter.java axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/gson/factory/XmlNodeGenerator.java axis/axis2/java/core/trunk/modules/json/test-resources/custom_schema/testSchema_2.xsd axis/axis2/java/core/trunk/modules/json/test/org/apache/axis2/json/gson/factory/XmlNodeGeneratorTest.java Modified: axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/gson/GsonXMLStreamReader.java URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/gson/GsonXMLStreamReader.java?rev=1578111&r1=1578110&r2=1578111&view=diff ============================================================================== --- axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/gson/GsonXMLStreamReader.java (original) +++ axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/gson/GsonXMLStreamReader.java Sun Mar 16 16:43:06 2014 @@ -21,6 +21,7 @@ package org.apache.axis2.json.gson; import com.google.gson.stream.JsonReader; import com.google.gson.stream.JsonToken; +import org.apache.axis2.AxisFault; import org.apache.axis2.context.ConfigurationContext; import org.apache.axis2.json.gson.factory.JSONType; import org.apache.axis2.json.gson.factory.JsonConstant; @@ -87,7 +88,7 @@ public class GsonXMLStreamReader impleme } public GsonXMLStreamReader(JsonReader jsonReader, QName elementQname, List<XmlSchema> xmlSchemaList, - ConfigurationContext configContext) { + ConfigurationContext configContext) throws AxisFault { this.jsonReader = jsonReader; initXmlStreamReader(elementQname, xmlSchemaList, configContext); } @@ -96,16 +97,20 @@ public class GsonXMLStreamReader impleme return jsonReader; } - public void initXmlStreamReader(QName elementQname, List<XmlSchema> xmlSchemaList, ConfigurationContext configContext) { + public void initXmlStreamReader(QName elementQname, List<XmlSchema> xmlSchemaList, ConfigurationContext configContext) throws AxisFault { this.elementQname = elementQname; this.xmlSchemaList = xmlSchemaList; this.configContext = configContext; - process(); + try { + process(); + } catch (AxisFault axisFault) { + throw new AxisFault("Error while initializing XMLStreamReader ", axisFault); + } isProcessed = true; } - private void process() { + private void process() throws AxisFault { Object ob = configContext.getProperty(JsonConstant.XMLNODES); if (ob != null) { Map<QName, XmlNode> nodeMap = (Map<QName, XmlNode>) ob; Modified: axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/gson/GsonXMLStreamWriter.java URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/gson/GsonXMLStreamWriter.java?rev=1578111&r1=1578110&r2=1578111&view=diff ============================================================================== --- axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/gson/GsonXMLStreamWriter.java (original) +++ axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/gson/GsonXMLStreamWriter.java Sun Mar 16 16:43:06 2014 @@ -653,9 +653,11 @@ public class GsonXMLStreamWriter impleme public void writeStartDocument(String encoding, String version) throws XMLStreamException { if (!isProcessed) { - xmlNodeGenerator.getMainXmlNode(); - queue = xmlNodeGenerator.getQueue(mainXmlNode); - isProcessed = true; + try { + process(); + } catch (IOException e) { + throw new XMLStreamException("Error occur while trying to write start document element", e); + } } } @@ -672,7 +674,7 @@ public class GsonXMLStreamWriter impleme try { process(); } catch (IOException e) { - throw new XMLStreamException("Error occur while trying to write first begin object "); + throw new XMLStreamException("Error occur while trying to write first begin object ", e); } } try { Modified: axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/gson/factory/XmlNodeGenerator.java URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/gson/factory/XmlNodeGenerator.java?rev=1578111&r1=1578110&r2=1578111&view=diff ============================================================================== --- axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/gson/factory/XmlNodeGenerator.java (original) +++ axis/axis2/java/core/trunk/modules/json/src/org/apache/axis2/json/gson/factory/XmlNodeGenerator.java Sun Mar 16 16:43:06 2014 @@ -19,6 +19,8 @@ package org.apache.axis2.json.gson.factory; +import org.apache.axis2.AxisFault; +import org.apache.ws.commons.schema.utils.XmlSchemaRef; import org.apache.ws.commons.schema.XmlSchema; import org.apache.ws.commons.schema.XmlSchemaComplexType; @@ -52,7 +54,7 @@ public class XmlNodeGenerator { public XmlNodeGenerator() { } - private void processSchemaList() { + private void processSchemaList() throws AxisFault { // get the operation schema and process. XmlSchema operationSchema = getXmlSchema(elementQname); XmlSchemaElement messageElement = operationSchema.getElementByName(elementQname.getLocalPart()); @@ -81,7 +83,7 @@ public class XmlNodeGenerator { } } - private void processElement(XmlSchemaElement element, XmlNode parentNode , XmlSchema schema) { + private void processElement(XmlSchemaElement element, XmlNode parentNode , XmlSchema schema) throws AxisFault { String targetNamespace = schema.getTargetNamespace(); XmlNode xmlNode; QName schemaTypeName = element.getSchemaTypeName(); @@ -106,11 +108,34 @@ public class XmlNodeGenerator { xmlNode = new XmlNode(element.getName(), targetNamespace, false, (element.getMaxOccurs() == 1 ? false : true), schemaType.getQName().getLocalPart()); parentNode.addChildToList(xmlNode); processSchemaType(schemaType, xmlNode, schema); + }else if (element.getRef() != null) { + // Handle ref element + XmlSchemaRef xmlSchemaRef = element.getRef(); + QName targetQname = xmlSchemaRef.getTargetQName(); + if (targetQname == null) { + throw new AxisFault("target QName is null while processing ref:" + element.getName()); + } + getXmlSchema(targetQname); + xmlNode = new XmlNode(targetQname.getLocalPart(), targetNamespace, false, (element.getMaxOccurs() != 1), targetQname.getLocalPart()); + parentNode.addChildToList(xmlNode); + if (("http://www.w3.org/2001/XMLSchema").equals(targetQname.getNamespaceURI())) { + } else { + XmlSchema schemaOfType; + // see whether Schema type is in the same schema + XmlSchemaType childSchemaType = schema.getTypeByName(targetQname.getLocalPart()); + if (childSchemaType == null) { + schemaOfType = getXmlSchema(targetQname); + childSchemaType = schemaOfType.getTypeByName(targetQname.getLocalPart()); + } else { + schemaOfType = schema; + } + processSchemaType(childSchemaType, xmlNode, schemaOfType); + } } } - private void processSchemaType(XmlSchemaType xmlSchemaType , XmlNode parentNode , XmlSchema schema) { + private void processSchemaType(XmlSchemaType xmlSchemaType , XmlNode parentNode , XmlSchema schema) throws AxisFault { if (xmlSchemaType instanceof XmlSchemaComplexType) { XmlSchemaComplexType complexType = (XmlSchemaComplexType)xmlSchemaType; XmlSchemaParticle particle = complexType.getParticle(); @@ -162,9 +187,13 @@ public class XmlNodeGenerator { } - public XmlNode getMainXmlNode() { + public XmlNode getMainXmlNode() throws AxisFault { if (mainXmlNode == null) { - processSchemaList(); + try { + processSchemaList(); + } catch (AxisFault axisFault) { + throw new AxisFault("Error while creating intermeidate xml structure ", axisFault); + } } return mainXmlNode; } Modified: axis/axis2/java/core/trunk/modules/json/test-resources/custom_schema/testSchema_2.xsd URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/json/test-resources/custom_schema/testSchema_2.xsd?rev=1578111&r1=1578110&r2=1578111&view=diff ============================================================================== --- axis/axis2/java/core/trunk/modules/json/test-resources/custom_schema/testSchema_2.xsd (original) +++ axis/axis2/java/core/trunk/modules/json/test-resources/custom_schema/testSchema_2.xsd Sun Mar 16 16:43:06 2014 @@ -20,10 +20,10 @@ --> -<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" -targetNamespace="http://test.json.axis2.apache.org" -xmlns="http://test.json.axis2.apache.org" -elementFormDefault="qualified"> +<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://test.json.axis2.apache.org" +xmlns="http://test.json.axis2.apache.org" xmlns:ns1="http://test.json.axis2.apache.org/employee" +xmlns:tns="http://test.json.axis2.apache.org" elementFormDefault="qualified"> + <xs:import namespace="http://test.json.axis2.apache.org/employee" schemaLocation="test-resources/custom_schema/testSchema_3.xsd"></xs:import> <xs:element name="echoPerson"> <xs:complexType> @@ -49,4 +49,23 @@ elementFormDefault="qualified"> </xs:sequence> </xs:complexType> + <xs:element name="Offices" type="tns:Offices"></xs:element> + + <xs:complexType name="Offices"> + <xs:sequence> + <xs:element maxOccurs="unbounded" minOccurs="0" name="Office" type="tns:Office"></xs:element> + </xs:sequence> + </xs:complexType> + + <xs:complexType name="Office"> + <xs:sequence> + <xs:element name="phone" nillable="true" type="xs:string"></xs:element> + <xs:element name="officeCode" nillable="true" type="xs:integer"></xs:element> + <xs:element ref="ns1:Employees"></xs:element> + <xs:element name="country" nillable="true" type="xs:string"></xs:element> + <xs:element name="city" nillable="true" type="xs:string"></xs:element> + </xs:sequence> + </xs:complexType> + + </xs:schema> \ No newline at end of file Added: axis/axis2/java/core/trunk/modules/json/test-resources/custom_schema/testSchema_3.xsd URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/json/test-resources/custom_schema/testSchema_3.xsd?rev=1578111&view=auto ============================================================================== --- axis/axis2/java/core/trunk/modules/json/test-resources/custom_schema/testSchema_3.xsd (added) +++ axis/axis2/java/core/trunk/modules/json/test-resources/custom_schema/testSchema_3.xsd Sun Mar 16 16:43:06 2014 @@ -0,0 +1,16 @@ +<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://test.json.axis2.apache.org/employee" + xmlns:ns1="http://test.json.axis2.apache.org/employee" attributeFormDefault="unqualified" elementFormDefault="qualified" > + <xs:element name="Employees" type="ns1:Employees"></xs:element> + <xs:complexType name="Employees"> + <xs:sequence> + <xs:element maxOccurs="unbounded" minOccurs="0" name="Employee" type="ns1:Employee"></xs:element> + </xs:sequence> + </xs:complexType> + <xs:complexType name="Employee"> + <xs:sequence> + <xs:element name="email" nillable="true" type="xs:string"></xs:element> + <xs:element name="firstName" nillable="true" type="xs:string"></xs:element> + <xs:element name="employeeNumber" nillable="true" type="xs:integer"></xs:element> + </xs:sequence> + </xs:complexType> +</xs:schema> Modified: axis/axis2/java/core/trunk/modules/json/test/org/apache/axis2/json/gson/factory/XmlNodeGeneratorTest.java URL: http://svn.apache.org/viewvc/axis/axis2/java/core/trunk/modules/json/test/org/apache/axis2/json/gson/factory/XmlNodeGeneratorTest.java?rev=1578111&r1=1578110&r2=1578111&view=diff ============================================================================== --- axis/axis2/java/core/trunk/modules/json/test/org/apache/axis2/json/gson/factory/XmlNodeGeneratorTest.java (original) +++ axis/axis2/java/core/trunk/modules/json/test/org/apache/axis2/json/gson/factory/XmlNodeGeneratorTest.java Sun Mar 16 16:43:06 2014 @@ -22,6 +22,7 @@ package org.apache.axis2.json.gson.facto import org.apache.ws.commons.schema.XmlSchema; import org.apache.ws.commons.schema.XmlSchemaCollection; import org.junit.Assert; +import org.junit.BeforeClass; import org.junit.Test; import javax.xml.namespace.QName; @@ -34,21 +35,11 @@ import java.util.List; public class XmlNodeGeneratorTest { + static List<XmlSchema> schemaList = null; @Test public void testMainXMLNode() throws Exception { - QName elementQName = new QName("http://test.json.axis2.apache.org" ,"echoPerson"); - - String fileName = "test-resources/custom_schema/testSchema_2.xsd"; - InputStream is = new FileInputStream(fileName); - XmlSchemaCollection schemaCol = new XmlSchemaCollection(); - XmlSchema schema = schemaCol.read(new StreamSource(is)); - - List<XmlSchema> schemaList = new ArrayList<XmlSchema>(); - schemaList.add(schema); - XmlNodeGenerator xmlNodeGenerator = new XmlNodeGenerator(schemaList, elementQName); - XmlNode mainXmlNode = xmlNodeGenerator.getMainXmlNode(); Assert.assertNotNull(mainXmlNode); @@ -67,7 +58,50 @@ public class XmlNodeGeneratorTest { Assert.assertEquals("gender", mainXmlNode.getChildrenList().get(0).getChildrenList().get(2).getName()); Assert.assertEquals(0, mainXmlNode.getChildrenList().get(0).getChildrenList().get(2).getChildrenList().size()); + } + + @Test + public void testXMLNodeGenWithRefElement() throws Exception { + QName eleQName = new QName("http://test.json.axis2.apache.org", "Offices"); + XmlNodeGenerator xmlNodeGenerator = new XmlNodeGenerator(schemaList, eleQName); + XmlNode mainXmlNode = xmlNodeGenerator.getMainXmlNode(); + Assert.assertNotNull(mainXmlNode); + Assert.assertEquals(true, mainXmlNode.getChildrenList().get(0).isArray()); + Assert.assertEquals(5, mainXmlNode.getChildrenList().get(0).getChildrenList().size()); + Assert.assertEquals("Employees", mainXmlNode.getChildrenList().get(0).getChildrenList().get(2).getName()); + Assert.assertEquals(false, mainXmlNode.getChildrenList().get(0).getChildrenList().get(2).isArray()); + Assert.assertEquals("Employee", mainXmlNode.getChildrenList().get(0).getChildrenList().get(2).getChildrenList().get(0).getName()); + Assert.assertEquals(true, mainXmlNode.getChildrenList().get(0).getChildrenList().get(2).getChildrenList().get(0).isArray()); + Assert.assertEquals(3, mainXmlNode.getChildrenList().get(0).getChildrenList().get(2).getChildrenList().get(0).getChildrenList().size()); } + + @BeforeClass + public static void setUp() throws Exception { + InputStream is2 = null; + InputStream is3 = null; + try { + String testSchema2 = "test-resources/custom_schema/testSchema_2.xsd"; + String testSchema3 = "test-resources/custom_schema/testSchema_3.xsd"; + is2 = new FileInputStream(testSchema2); + is3 = new FileInputStream(testSchema3); + XmlSchemaCollection schemaCol = new XmlSchemaCollection(); + XmlSchema schema2 = schemaCol.read(new StreamSource(is2)); + XmlSchema schema3 = schemaCol.read(new StreamSource(is3)); + + schemaList = new ArrayList<XmlSchema>(); + schemaList.add(schema2); + schemaList.add(schema3); + } finally { + if (is2 != null) { + is2.close(); + } + if (is3 != null) { + is3.close(); + } + } + + } + }