You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-dev@axis.apache.org by aj...@apache.org on 2005/10/22 09:06:47 UTC
svn commit: r327639 - in /webservices/axis2/trunk/java/modules/codegen:
other/ src/org/apache/axis2/databinding/schema/
src/org/apache/axis2/databinding/schema/template/
src/org/apache/axis2/databinding/schema/util/
Author: ajith
Date: Sat Oct 22 00:06:31 2005
New Revision: 327639
URL: http://svn.apache.org/viewcvs?rev=327639&view=rev
Log:
1. Completed the codegen for populating the objects given a pull parser
2. added the schema for the XML that the BeanTemplate.xsl takes
A decent test case is missing. Even though I've done some testing of my own , a maven runnable complete test suit is still missing!
Added:
webservices/axis2/trunk/java/modules/codegen/other/
webservices/axis2/trunk/java/modules/codegen/other/beanGenerationSchema.xsd
Modified:
webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/databinding/schema/BeanWriterMetaInfoHolder.java
webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/databinding/schema/JavaBeanWriter.java
webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/databinding/schema/SchemaCompiler.java
webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/databinding/schema/template/BeanTemplate.xsl
webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/databinding/schema/util/ConverterUtil.java
Added: webservices/axis2/trunk/java/modules/codegen/other/beanGenerationSchema.xsd
URL: http://svn.apache.org/viewcvs/webservices/axis2/trunk/java/modules/codegen/other/beanGenerationSchema.xsd?rev=327639&view=auto
==============================================================================
--- webservices/axis2/trunk/java/modules/codegen/other/beanGenerationSchema.xsd (added)
+++ webservices/axis2/trunk/java/modules/codegen/other/beanGenerationSchema.xsd Sat Oct 22 00:06:31 2005
@@ -0,0 +1,33 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!-- edited with XMLSpy v2005 rel. 3 U (http://www.altova.com) by Ajith Harshana Ranabahu (WSO2/Development) -->
+<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
+ <xs:element name="bean">
+ <xs:annotation>
+ <xs:documentation>This schema represents the XML docuement structure the ADBBean template accepts</xs:documentation>
+ </xs:annotation>
+ <xs:complexType>
+ <xs:sequence>
+ <xs:element name="property" type="propertyType" maxOccurs="unbounded"/>
+ </xs:sequence>
+ <xs:attribute name="name" type="xs:string" use="required"/>
+ <xs:attribute name="package" type="xs:string" use="required"/>
+ <xs:attribute name="nsuri" type="xs:string" use="required"/>
+ <xs:attribute name="nsprefix" type="xs:string" use="required"/>
+ <xs:attribute name="type" type="xs:string" use="optional" fixed="yes"/>
+ <xs:attribute name="extension" type="xs:string" use="optional"/>
+ </xs:complexType>
+ </xs:element>
+ <xs:complexType name="propertyType">
+ <xs:attribute name="name" type="xs:string" use="required"/>
+ <xs:attribute name="javaname" type="xs:string" use="required"/>
+ <xs:attribute name="type" type="xs:string" use="required"/>
+ <xs:attribute name="shorttypename" type="xs:string" use="required"/>
+ <xs:attribute name="ours" type="xs:string" use="optional" fixed="yes"/>
+ <xs:attribute name="any" type="xs:string" use="optional" fixed="yes"/>
+ <xs:attribute name="array" type="xs:string" use="optional" fixed="yes"/>
+ <xs:attribute name="anyAtt" type="xs:string" use="optional" fixed="yes"/>
+ <xs:attribute name="unbound" type="xs:string" use="optional" fixed="yes"/>
+ <xs:attribute name="minOccurs" type="xs:long" use="optional"/>
+ <xs:attribute name="maxOccurs" type="xs:long" use="optional"/>
+ </xs:complexType>
+</xs:schema>
Modified: webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/databinding/schema/BeanWriterMetaInfoHolder.java
URL: http://svn.apache.org/viewcvs/webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/databinding/schema/BeanWriterMetaInfoHolder.java?rev=327639&r1=327638&r2=327639&view=diff
==============================================================================
--- webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/databinding/schema/BeanWriterMetaInfoHolder.java (original)
+++ webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/databinding/schema/BeanWriterMetaInfoHolder.java Sat Oct 22 00:06:31 2005
@@ -96,6 +96,10 @@
return anyState != null && anyState.equals(SchemaConstants.ANY_ARRAY_TYPE);
}
+ public boolean getAnyAttributeStatusForQName(QName qName){
+ Integer anyState = (Integer) specialTypeFlagMap.get(qName);
+ return anyState != null && anyState.equals(SchemaConstants.ANY_ATTRIBUTE_TYPE);
+ }
/**
*
*/
@@ -125,7 +129,7 @@
}
/**
- *
+ *
* @param qName
* @return
*/
Modified: webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/databinding/schema/JavaBeanWriter.java
URL: http://svn.apache.org/viewcvs/webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/databinding/schema/JavaBeanWriter.java?rev=327639&r1=327638&r2=327639&view=diff
==============================================================================
--- webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/databinding/schema/JavaBeanWriter.java (original)
+++ webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/databinding/schema/JavaBeanWriter.java Sat Oct 22 00:06:31 2005
@@ -149,8 +149,18 @@
XSLTUtils.addAttribute(model,"any","yes",property);
}
+ if (metainf.getAnyAttributeStatusForQName(name)){
+ XSLTUtils.addAttribute(model,"anyAtt","yes",property);
+ }
if (metainf.getArrayStatusForQName(name)){
XSLTUtils.addAttribute(model,"array","yes",property);
+ XSLTUtils.addAttribute(
+ model,
+ "arrayBaseType",
+ javaClassNameForElement.substring(0,javaClassNameForElement.indexOf("[")),
+ property);
+
+
long minOccurs = metainf.getMinOccurs(name);
if (minOccurs >0){
Modified: webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/databinding/schema/SchemaCompiler.java
URL: http://svn.apache.org/viewcvs/webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/databinding/schema/SchemaCompiler.java?rev=327639&r1=327638&r2=327639&view=diff
==============================================================================
--- webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/databinding/schema/SchemaCompiler.java (original)
+++ webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/databinding/schema/SchemaCompiler.java Sat Oct 22 00:06:31 2005
@@ -38,6 +38,7 @@
private Map baseSchemaTypeMap = TypeMap.getTypeMap();
private static final String ANY_ELEMENT_FIELD_NAME = "extraElements";
+ private static final String EXTRA_ATTRIBUTE_FIELD_NAME = "extraAttributes";
public HashMap getProcessedElementMap() {
@@ -336,7 +337,7 @@
private void processAnyAttribute(BeanWriterMetaInfoHolder metainf) {
//The best thing we can do here is to add a set of OMAttributes
- metainf.registerMapping(new QName("extraAttributes"),
+ metainf.registerMapping(new QName(EXTRA_ATTRIBUTE_FIELD_NAME),
null,
OMElement[].class.getName(),
SchemaConstants.ANY_ATTRIBUTE_TYPE);
Modified: webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/databinding/schema/template/BeanTemplate.xsl
URL: http://svn.apache.org/viewcvs/webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/databinding/schema/template/BeanTemplate.xsl?rev=327639&r1=327638&r2=327639&view=diff
==============================================================================
--- webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/databinding/schema/template/BeanTemplate.xsl (original)
+++ webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/databinding/schema/template/BeanTemplate.xsl Sat Oct 22 00:06:31 2005
@@ -31,20 +31,21 @@
<xsl:variable name="propertyType"><xsl:value-of select="@type"></xsl:value-of></xsl:variable>
<xsl:variable name="propertyName"><xsl:value-of select="@name"></xsl:value-of></xsl:variable>
<xsl:variable name="javaName"><xsl:value-of select="@javaname"></xsl:value-of></xsl:variable>
+ <xsl:variable name="varName">local<xsl:value-of select="$javaName"/></xsl:variable>
/**
* field for <xsl:value-of select="$javaName"/>
<xsl:if test="@attribute">* This was an Attribute!</xsl:if>
<xsl:if test="@array">* This was an Array!</xsl:if>
*/
- private <xsl:value-of select="$propertyType"/> local<xsl:value-of select="$javaName"/>;
+ private <xsl:value-of select="$propertyType"/><xsl:text> </xsl:text><xsl:value-of select="$varName" /> ;
/**
* Auto generated getter method
* @return <xsl:value-of select="$propertyType"/>
*/
public <xsl:value-of select="$propertyType"/><xsl:text> </xsl:text>get<xsl:value-of select="$javaName"/>(){
- return local<xsl:value-of select="$javaName"/>;
+ return <xsl:value-of select="$varName"/>;
}
/**
@@ -65,7 +66,7 @@
}
</xsl:if>
</xsl:if>
- this.local<xsl:value-of select="$javaName"/>=param;
+ this.<xsl:value-of select="$varName"/>=param;
}
</xsl:for-each>
@@ -75,68 +76,151 @@
*/
public javax.xml.stream.XMLStreamReader getPullParser(javax.xml.namespace.QName qName){
- // Object[] elementList = new Object[]{
+ Object[] elementList = new Object[]{
<xsl:for-each select="property[not(@attribute)]">
<xsl:variable name="propertyName"><xsl:value-of select="@name"/></xsl:variable>
- //<xsl:if test="position()>1">,</xsl:if>
+ <xsl:variable name="varName">local<xsl:value-of select="@javaname"/></xsl:variable>
+
+ <xsl:if test="position()>1">,</xsl:if>
<xsl:choose>
<xsl:when test="@ours">
- // new javax.xml.namespace.QName("<xsl:value-of select="$propertyName"/>"),local<xsl:value-of select="@javaname"/>
+ new javax.xml.namespace.QName("<xsl:value-of select="$propertyName"/>"),<xsl:value-of select="$varName"/>
</xsl:when>
<xsl:when test="@any">
- // null,local<xsl:value-of select="@javaname"/>
+ new javax.xml.namespace.QName("<xsl:value-of select="$propertyName"/>"),<xsl:value-of select="$varName"/>
</xsl:when>
<xsl:otherwise>
- // "<xsl:value-of select="$propertyName"/>",org.apache.axis2.databinding.schema.util.ConverterUtil.convertToString(local<xsl:value-of select="@javaname"/>)
+ "<xsl:value-of select="$propertyName"/>",org.apache.axis2.databinding.schema.util.ConverterUtil.convertToString(<xsl:value-of select="$varName"/>)
</xsl:otherwise>
</xsl:choose>
- // </xsl:for-each>};
+ </xsl:for-each>};
- // Object[] attribList = new Object[]{
+ Object[] attribList = new Object[]{
<xsl:for-each select="property[@attribute]">
<xsl:variable name="propertyName"><xsl:value-of select="@name"/></xsl:variable>
- // <xsl:if test="position()>1">,</xsl:if>
- // "<xsl:value-of select="$propertyName"/>",org.apache.axis2.databinding.schema.util.ConverterUtil.convertToString(local<xsl:value-of select="@javaname"/>)
- // </xsl:for-each>
- // };
-
- // return org.apache.axis2.databinding.utils.ADBPullParser.createPullParser(qName, elementList, attribList);
+ <xsl:variable name="varName">local<xsl:value-of select="@javaname"/></xsl:variable>
+ <xsl:if test="position()>1">,</xsl:if>
+ <xsl:choose>
+ <xsl:when test="@anyAtt">
+ null,<xsl:value-of select="$varName"/>
+ </xsl:when>
+ <xsl:otherwise>
+ new javax.xml.namespace.QName("<xsl:value-of select="$propertyName"/>"),org.apache.axis2.databinding.schema.util.ConverterUtil.convertToString(<xsl:value-of select="$varName"/>)
+ </xsl:otherwise>
+ </xsl:choose>
+ </xsl:for-each>
+ };
- return null;
+ return org.apache.axis2.databinding.utils.ADBPullParser.createPullParser(qName, elementList, attribList);
}
/**
* static method to create the object
* Note - This is not complete
*/
- public static <xsl:value-of select="$name"/> parse(javax.xml.stream.XMLStreamReader reader){
+ public static <xsl:value-of select="$name"/> parse(javax.xml.stream.XMLStreamReader reader) throws java.lang.Exception{
<xsl:value-of select="$name"/> object = new <xsl:value-of select="$name"/>();
try {
- int event = reader.getEventType();
- int count = 0;
- int argumentCount = <xsl:value-of select="count(property)"/> ;
- boolean done =false;
- //event better be a START_ELEMENT. if not we should go up to the start element here
- while (!reader.isStartElement()){
- event = reader.next();
- }
-
+ int event = reader.getEventType();
+ int count = 0;
+ int argumentCount = <xsl:value-of select="count(property)"/> ;
+ boolean done =false;
+ //event better be a START_ELEMENT. if not we should go up to the start element here
+ while (!reader.isStartElement()){
+ event = reader.next();
+ }
+ <!-- First loop creates arrayLists for handling arrays -->
+ <xsl:for-each select="property">
+ <xsl:if test="@array">
+ java.util.ArrayList list<xsl:value-of select="position()"></xsl:value-of> = new java.util.ArrayList();
+ </xsl:if>
+ </xsl:for-each>
while(!done){
if (javax.xml.stream.XMLStreamConstants.START_ELEMENT==event){
+ <!-- Now reloop and populate the code -->
<xsl:for-each select="property">
<xsl:variable name="propertyName"><xsl:value-of select="@name"/></xsl:variable>
<xsl:variable name="propertyType"><xsl:value-of select="@type"/></xsl:variable>
<xsl:variable name="shortTypeName"><xsl:value-of select="@shorttypename"/></xsl:variable>
<xsl:variable name="javaName"><xsl:value-of select="@javaname"></xsl:value-of></xsl:variable>
+ <xsl:variable name="listName">list<xsl:value-of select="position()"/></xsl:variable>
+ <xsl:variable name="loopBoolName">loopDone<xsl:value-of select="position()"/></xsl:variable>
if ("<xsl:value-of select="$propertyName"/>".equals(reader.getLocalName())){
<xsl:choose>
<xsl:when test="@array">
- //do nothing yet for the array
+ <xsl:variable name="basePropertyType"><xsl:value-of select="@arrayBaseType"/></xsl:variable>
+ <xsl:choose>
+ <xsl:when test="@ours">
+ <xsl:value-of select="$listName"/>.add(<xsl:value-of select="$basePropertyType"/>.parse(reader));
+ //loop until we find a start element that is not part of this array
+ boolean <xsl:value-of select="$loopBoolName"/> = false;
+ while(!<xsl:value-of select="$loopBoolName"/>){
+ //loop to the end element
+ while (!reader.isEndElement()){
+ event = reader.next();
+ }
+ //step one event
+ event = reader.next();
+ if (reader.isEndElement()){
+ //two continuous end elements means we are exiting the xml structure
+ <xsl:value-of select="$loopBoolName"/> = true;
+ }else if (reader.isStartElement()){
+ if ("<xsl:value-of select="$propertyName"/>".equals(reader.getLocalName())){
+ <xsl:value-of select="$listName"/>.add(<xsl:value-of select="$basePropertyType"/>.parse(reader));
+ }else{
+ <xsl:value-of select="$loopBoolName"/> = true;
+ }
+ }
+ }
+
+
+ // call the converter utility to convert and set the array
+ object.set<xsl:value-of select="$javaName"/>(
+ (<xsl:value-of select="$propertyType"/>)
+ org.apache.axis2.databinding.schema.util.ConverterUtil.convertToArray(
+ <xsl:value-of select="$basePropertyType"/>.class,
+ <xsl:value-of select="$listName"/>));
+
+ count++;
+ </xsl:when>
+ <xsl:otherwise>
+ <xsl:value-of select="$listName"/>.add(reader.getElementText());
+ //loop until we find a start element that is not part of this array
+ boolean <xsl:value-of select="$loopBoolName"/> = false;
+ while(!<xsl:value-of select="$loopBoolName"/>){
+ //loop to the end element
+ while (!reader.isEndElement()){
+ event = reader.next();
+ }
+ //step one event
+ event = reader.next();
+ if (reader.isEndElement()){
+ //two continuous end elements means we are exiting the xml structure
+ <xsl:value-of select="$loopBoolName"/> = true;
+ }else if (reader.isStartElement()){
+ if ("<xsl:value-of select="$propertyName"/>".equals(reader.getLocalName())){
+ <xsl:value-of select="$listName"/>.add(reader.getElementText());
+ }else{
+ <xsl:value-of select="$loopBoolName"/> = true;
+ }
+ }
+ }
+
+ // call the converter utility to convert and set the array
+ object.set<xsl:value-of select="$javaName"/>(
+ (<xsl:value-of select="$propertyType"/>)
+ org.apache.axis2.databinding.schema.util.ConverterUtil.convertToArray(
+ <xsl:value-of select="$basePropertyType"/>.class,
+ <xsl:value-of select="$listName"/>));
+ count++;
+ </xsl:otherwise>
+ </xsl:choose>
</xsl:when>
<xsl:when test="@ours">
object.set<xsl:value-of select="$javaName"/>(
<xsl:value-of select="$propertyType"/>.parse(reader));
+ count++;
</xsl:when>
<xsl:when test="@any">
//do nothing yet!!!!
@@ -145,23 +229,27 @@
String content = reader.getElementText();
object.set<xsl:value-of select="$javaName"/>(
org.apache.axis2.databinding.schema.util.ConverterUtil.convertTo<xsl:value-of select="$shortTypeName"/>(content));
+ count++;
</xsl:otherwise>
</xsl:choose>
- count++;
+
}
+
</xsl:for-each>
- event = reader.next();
}
if (argumentCount==count){
- done=true;
+ done=true;
}
+ if (!done){
+ event = reader.next();
+ }
}
} catch (javax.xml.stream.XMLStreamException e) {
- e.printStackTrace();
+ throw new java.lang.Exception(e);
}
return object;
Modified: webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/databinding/schema/util/ConverterUtil.java
URL: http://svn.apache.org/viewcvs/webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/databinding/schema/util/ConverterUtil.java?rev=327639&r1=327638&r2=327639&view=diff
==============================================================================
--- webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/databinding/schema/util/ConverterUtil.java (original)
+++ webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/databinding/schema/util/ConverterUtil.java Sat Oct 22 00:06:31 2005
@@ -1,4 +1,7 @@
package org.apache.axis2.databinding.schema.util;
+
+import java.util.List;
+import java.lang.reflect.Array;
/*
* Copyright 2004,2005 The Apache Software Foundation.
*
@@ -17,6 +20,7 @@
public class ConverterUtil {
+ /* String conversion methods */
public static String convertToString(int i){
return i+"";
}
@@ -29,9 +33,33 @@
return i+"";
}
- // fill the other methods
+ public static String convertToString(double i){
+ return i+"";
+ }
+
+ public static String convertToString(byte i){
+ return new String(new byte[]{i});
+ }
+
+ public static String convertToString(char i){
+ return new String(new char[]{i});
+ }
+
+ public static String convertToString(short i){
+ return i+"";
+ }
+ public static String convertToString(boolean i){
+ return i+"";
+ }
+ public static String convertToString(Object o){
+ return o.toString();
+ }
+
+
+
+ /* String to primitive type conversions */
public static int convertToint(String s){
return Integer.parseInt(s);
}
@@ -40,27 +68,29 @@
return Float.parseFloat(s);
}
- public static String convertTostring(String s){
+ public static String convertTostring(String s){
return s;
}
+ public static long convertTolong(String s){
+ return Long.parseLong(s);
+ }
- //the pass through method
- public static String convertToString(String o){
- return o;
+ public static short convertToshort(String s){
+ return Short.parseShort(s);
}
- //the pass through method
- public static String convertToString(Object o){
- return o.toString();
+ public static boolean convertToboolean(String s){
+ return Boolean.getBoolean(s);
}
- //add the others here
+
+ /* primitive types to Object conversion methods */
public static Object convertToObject(String i){
return i;
}
- public static Object convertToObject(boolean i){
+ public static Object convertToObject(boolean i){
return Boolean.valueOf(i);
}
@@ -72,11 +102,47 @@
return new Byte(i);
}
- public static Object convertToObject(char i){
+ public static Object convertToObject(char i){
return new Character(i);
}
- public static Object convertToObject(short i){
+ public static Object convertToObject(short i){
return new Short(i);
}
+
+
+ /* list to array conversion methods */
+
+ /**
+ *
+ * @param baseArrayClass
+ * @param objectList -> for primitive type array conversion we assume the content to be
+ * strings!
+ * @return
+ */
+ public static Object convertToArray(Class baseArrayClass, List objectList){
+ int listSize = objectList.size();
+ Object returnArray = Array.newInstance(baseArrayClass,listSize);
+ for (int i = 0; i < listSize; i++) {
+ if (int.class.equals(baseArrayClass)){
+ Array.setInt(returnArray,i,Integer.parseInt(objectList.get(i).toString()));
+ }else if (float.class.equals(baseArrayClass)){
+ Array.setFloat(returnArray,i,Float.parseFloat(objectList.get(i).toString()));
+ }else if (short.class.equals(baseArrayClass)){
+ Array.setShort(returnArray,i,Short.parseShort(objectList.get(i).toString()));
+ }else if (long.class.equals(baseArrayClass)){
+ Array.setLong(returnArray,i,Long.parseLong(objectList.get(i).toString()));
+ }else if (boolean.class.equals(baseArrayClass)){
+ Array.setBoolean(returnArray,i,Boolean.getBoolean(objectList.get(i).toString()));
+ }else if (char.class.equals(baseArrayClass)){
+ Array.setChar(returnArray,i,objectList.get(i).toString().toCharArray()[0]);
+ }else if (double.class.equals(baseArrayClass)){
+ Array.setDouble(returnArray,i,Double.parseDouble(objectList.get(i).toString()));
+ }else{
+ Array.set(returnArray,i,objectList.get(i));
+ }
+ }
+ return returnArray;
+ }
+
}