axis-java-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From veit...@apache.org
Subject svn commit: r791135 - in /webservices/axis2/trunk/java/modules: adb-codegen/src/org/apache/axis2/schema/template/ adb-codegen/test/org/apache/axis2/schema/ adb/src/org/apache/axis2/databinding/
Date Sat, 04 Jul 2009 14:50:16 GMT
Author: veithen
Date: Sat Jul  4 14:50:16 2009
New Revision: 791135

URL: http://svn.apache.org/viewvc?rev=791135&view=rev
Log:
Started to overhaul the ADB helper mode:
* Made the helpers singletons implementing a common interface.
* Let the helpers expose a serialize method directly.
* Modified the test suite so that the existing test cases also cover the helper mode.

Added:
    webservices/axis2/trunk/java/modules/adb-codegen/test/org/apache/axis2/schema/ADBBeanUtil.java
  (with props)
    webservices/axis2/trunk/java/modules/adb/src/org/apache/axis2/databinding/ADBHelper.java
  (with props)
Modified:
    webservices/axis2/trunk/java/modules/adb-codegen/src/org/apache/axis2/schema/template/ADBBeanTemplate-helpermode.xsl
    webservices/axis2/trunk/java/modules/adb-codegen/src/org/apache/axis2/schema/template/ADBBeanTemplate.xsl
    webservices/axis2/trunk/java/modules/adb-codegen/src/org/apache/axis2/schema/template/ADBDatabindingTemplate.xsl
    webservices/axis2/trunk/java/modules/adb-codegen/test/org/apache/axis2/schema/AbstractTestCase.java

Modified: webservices/axis2/trunk/java/modules/adb-codegen/src/org/apache/axis2/schema/template/ADBBeanTemplate-helpermode.xsl
URL: http://svn.apache.org/viewvc/webservices/axis2/trunk/java/modules/adb-codegen/src/org/apache/axis2/schema/template/ADBBeanTemplate-helpermode.xsl?rev=791135&r1=791134&r2=791135&view=diff
==============================================================================
--- webservices/axis2/trunk/java/modules/adb-codegen/src/org/apache/axis2/schema/template/ADBBeanTemplate-helpermode.xsl
(original)
+++ webservices/axis2/trunk/java/modules/adb-codegen/src/org/apache/axis2/schema/template/ADBBeanTemplate-helpermode.xsl
Sat Jul  4 14:50:16 2009
@@ -529,12 +529,21 @@
     </xsl:when>
     <xsl:otherwise>
     <!--  Start of helper generation part of the template-->
-public <xsl:if test="not(@unwrapped) or (@skip-write)">static</xsl:if> class
<xsl:value-of select="$helpername"/> {
+public <xsl:if test="not(@unwrapped) or (@skip-write)">static</xsl:if> class
<xsl:value-of select="$helpername"/>
+        implements org.apache.axis2.databinding.ADBHelper&lt;<xsl:value-of select="$name"/>&gt;
{
 
      <!-- get OMElement methods that allows direct writing. generated inside the helper
class-->
      <xsl:variable name="fullyQualifiedName"><xsl:value-of select="$package"/>.<xsl:value-of
select="$name"/></xsl:variable>
      <xsl:variable name="fullyQualifiedHelperName"><xsl:value-of select="$package"/>.<xsl:value-of
select="$helpername"/></xsl:variable>
 
+    public static final <xsl:value-of select="$helpername"/> INSTANCE = new <xsl:value-of
select="$helpername"/>();
+
+    private <xsl:value-of select="$helpername"/>() {}
+
+    public java.lang.Class&lt;<xsl:value-of select="$fullyQualifiedName"/>&gt;
getBeanClass() {
+        return <xsl:value-of select="$fullyQualifiedName"/>.class;
+    }
+    
         private static java.lang.String generatePrefix(java.lang.String namespace) {
             if(namespace.equals(<xsl:value-of select="$fullyQualifiedName"/>.MY_QNAME.getNamespaceURI())){
                 return <xsl:value-of select="$fullyQualifiedName"/>.MY_QNAME.getPrefix();
@@ -564,7 +573,7 @@
                * @param factory
                * @return org.apache.axiom.om.OMElement
                */
-              public static org.apache.axiom.om.OMElement getOMElement(
+              public org.apache.axiom.om.OMElement getOMElement(
                       final <xsl:value-of select="$fullyQualifiedName"/> bean,
                       final javax.xml.namespace.QName parentQName,
                       final org.apache.axiom.om.OMFactory factory) throws org.apache.axis2.databinding.ADBException{
@@ -597,13 +606,18 @@
 
         org.apache.axiom.om.OMDataSource dataSource =
                        new org.apache.axis2.databinding.ADBHelperDataSource(bean,parentQName,"<xsl:value-of
select="$fullyQualifiedHelperName"/>"){
+            public void serialize(javax.xml.stream.XMLStreamWriter xmlWriter) throws javax.xml.stream.XMLStreamException
{
+                INSTANCE.serialize((<xsl:value-of select="$fullyQualifiedName"/>)bean,
parentQName, factory, xmlWriter);
+            }
+        };
 
-         public void serialize(
+        return dataSource;
+    }
+            
+         public void serialize(<xsl:value-of select="$fullyQualifiedName"/> typedBean,
+                           javax.xml.namespace.QName parentQName, org.apache.axiom.om.OMFactory
factory,
                            javax.xml.stream.XMLStreamWriter xmlWriter) throws javax.xml.stream.XMLStreamException
{
 
-           <xsl:value-of select="$fullyQualifiedName"/> typedBean =
-                               (<xsl:value-of select="$fullyQualifiedName"/>)bean;
-
              <xsl:choose>
             <xsl:when test="@type or @anon">
                 <!-- For a type write the passed in QName first-->
@@ -1384,17 +1398,10 @@
                   private java.lang.String createPrefix() {
                         return "ns" + (int)Math.random();
                   }
-                };
-
-                return dataSource;
-            }
 
-     public static javax.xml.stream.XMLStreamReader getPullParser(java.lang.Object beanObject,
javax.xml.namespace.QName qName)
+     public javax.xml.stream.XMLStreamReader getPullParser(<xsl:value-of select="@name"/>
bean, javax.xml.namespace.QName qName)
         throws org.apache.axis2.databinding.ADBException{
 
-        <xsl:value-of select="@package"/>.<xsl:value-of select="@name"/> bean
=
-         (<xsl:value-of select="@package"/>.<xsl:value-of select="@name"/>)beanObject;
-
         <xsl:choose>
             <xsl:when test="@type or @anon">
                  java.util.ArrayList elementList = new java.util.ArrayList();
@@ -1632,10 +1639,10 @@
                                 if (<xsl:value-of select="$varName"/>==null){
                                    return new org.apache.axis2.databinding.utils.reader.NullXMLStreamReader(bean.MY_QNAME);
                                 }else{
-                                   return <xsl:value-of select="$propertyType"/>Helper.getPullParser(<xsl:value-of
select="$varName"/>,bean.MY_QNAME);
+                                   return <xsl:value-of select="$propertyType"/>Helper.INSTANCE.getPullParser(<xsl:value-of
select="$varName"/>,bean.MY_QNAME);
                                 }
                             </xsl:when>
-                            <xsl:otherwise>return <xsl:value-of select="$propertyType"/>Helper.getPullParser(<xsl:value-of
select="$varName"/>,bean.MY_QNAME);</xsl:otherwise>
+                            <xsl:otherwise>return <xsl:value-of select="$propertyType"/>Helper.INSTANCE.getPullParser(<xsl:value-of
select="$varName"/>,bean.MY_QNAME);</xsl:otherwise>
                         </xsl:choose>
                     </xsl:when>
                     <xsl:otherwise>
@@ -1673,7 +1680,7 @@
         * Postcondition: If this object is an element, the reader is positioned at its end
element
         *                If this object is a complex type, the reader is positioned at the
end element of its outer element
         */
-        public static <xsl:value-of select="$fullyQualifiedName"/> parse(javax.xml.stream.XMLStreamReader
reader) throws java.lang.Exception{
+        public <xsl:value-of select="$fullyQualifiedName"/> parse(javax.xml.stream.XMLStreamReader
reader) throws java.lang.Exception{
             <xsl:if test="not(property/enumFacet)"><xsl:value-of select="$fullyQualifiedName"/>
object = new <xsl:value-of select="$fullyQualifiedName"/>();</xsl:if>
             <xsl:if test="property/enumFacet"><xsl:value-of select="$name"/>
object = null;</xsl:if>
             int event;
@@ -1864,7 +1871,7 @@
                                                   reader.next();
                                               } else {
                                             </xsl:if>
-                                                <xsl:value-of select="$listName"/>.add(<xsl:value-of
select="$basePropertyType"/>Helper.parse(reader));
+                                                <xsl:value-of select="$listName"/>.add(<xsl:value-of
select="$basePropertyType"/>Helper.INSTANCE.parse(reader));
                                             <xsl:if test="@nillable">}</xsl:if>
                                             //loop until we find a start element that is
not part of this array
                                             boolean <xsl:value-of select="$loopBoolName"/>
= false;
@@ -1889,7 +1896,7 @@
                                                               reader.next();
                                                           } else {
                                                         </xsl:if>
-                                                        <xsl:value-of select="$listName"/>.add(<xsl:value-of
select="$basePropertyType"/>Helper.parse(reader));
+                                                        <xsl:value-of select="$listName"/>.add(<xsl:value-of
select="$basePropertyType"/>Helper.INSTANCE.parse(reader));
                                                         <xsl:if test="@nillable">}</xsl:if>
                                                     }else{
                                                         <xsl:value-of select="$loopBoolName"/>
= true;
@@ -2244,7 +2251,7 @@
                                           </xsl:if>
                                       }else{
                                     </xsl:if>
-                                        object.set<xsl:value-of select="$javaName"/>(<xsl:value-of
select="$propertyType"/>Helper.parse(reader));
+                                        object.set<xsl:value-of select="$javaName"/>(<xsl:value-of
select="$propertyType"/>Helper.INSTANCE.parse(reader));
                                     <xsl:if test="$isType or $anon">  <!-- This
is a subelement property to be consumed -->
                                         reader.next();
                                     </xsl:if>

Modified: webservices/axis2/trunk/java/modules/adb-codegen/src/org/apache/axis2/schema/template/ADBBeanTemplate.xsl
URL: http://svn.apache.org/viewvc/webservices/axis2/trunk/java/modules/adb-codegen/src/org/apache/axis2/schema/template/ADBBeanTemplate.xsl?rev=791135&r1=791134&r2=791135&view=diff
==============================================================================
--- webservices/axis2/trunk/java/modules/adb-codegen/src/org/apache/axis2/schema/template/ADBBeanTemplate.xsl
(original)
+++ webservices/axis2/trunk/java/modules/adb-codegen/src/org/apache/axis2/schema/template/ADBBeanTemplate.xsl
Sat Jul  4 14:50:16 2009
@@ -93,7 +93,7 @@
                   "<xsl:value-of select="@shortname"/>".equals(typeName)){
                    <xsl:choose>
                        <xsl:when test="$helperMode">
-                           return  <xsl:value-of select="@classname"/>Helper.parse(reader);
+                           return  <xsl:value-of select="@classname"/>Helper.INSTANCE.parse(reader);
                        </xsl:when>
                         <xsl:otherwise>
                             return  <xsl:value-of select="@classname"/>.Factory.parse(reader);

Modified: webservices/axis2/trunk/java/modules/adb-codegen/src/org/apache/axis2/schema/template/ADBDatabindingTemplate.xsl
URL: http://svn.apache.org/viewvc/webservices/axis2/trunk/java/modules/adb-codegen/src/org/apache/axis2/schema/template/ADBDatabindingTemplate.xsl?rev=791135&r1=791134&r2=791135&view=diff
==============================================================================
--- webservices/axis2/trunk/java/modules/adb-codegen/src/org/apache/axis2/schema/template/ADBDatabindingTemplate.xsl
(original)
+++ webservices/axis2/trunk/java/modules/adb-codegen/src/org/apache/axis2/schema/template/ADBDatabindingTemplate.xsl
Sat Jul  4 14:50:16 2009
@@ -42,7 +42,7 @@
             <xsl:choose>
                     <xsl:when test="$helpermode">
                          try{
-                            return <xsl:value-of select="@type"/>Helper.getOMElement(
+                            return <xsl:value-of select="@type"/>Helper.INSTANCE.getOMElement(
                                         param,
                                         <xsl:value-of select="@type"/>.MY_QNAME,
                                         org.apache.axiom.om.OMAbstractFactory.getOMFactory());
@@ -115,7 +115,7 @@
                                org.apache.axiom.soap.SOAPEnvelope emptyEnvelope = factory.getDefaultEnvelope();
                                   <xsl:choose>
                                     <xsl:when test="$helpermode">
-                                        emptyEnvelope.getBody().addChild(<xsl:value-of
select="$inputElementType"/>Helper.getOMElement(
+                                        emptyEnvelope.getBody().addChild(<xsl:value-of
select="$inputElementType"/>Helper.INSTANCE.getOMElement(
                                         wrappedType,
                                         <xsl:value-of select="$inputElementType"/>.MY_QNAME,factory));
                                     </xsl:when>
@@ -142,7 +142,7 @@
                                                 <xsl:when test="$helpermode">
                                                     try{
                                                         org.apache.axiom.soap.SOAPEnvelope
emptyEnvelope = factory.getDefaultEnvelope();
-                                                        emptyEnvelope.getBody().addChild(<xsl:value-of
select="$inputElementType"/>Helper.getOMElement(
+                                                        emptyEnvelope.getBody().addChild(<xsl:value-of
select="$inputElementType"/>Helper.INSTANCE.getOMElement(
                                                         param,
                                                         <xsl:value-of select="$inputElementType"/>.MY_QNAME,factory));
                                                         return emptyEnvelope;
@@ -260,7 +260,7 @@
                            <xsl:choose>
                                 <xsl:when test="$helpermode">
                                     emptyEnvelope.getBody().addChild(
-                                    <xsl:value-of select="../../param[@type!='' and @direction='out'
and @opname=$opname]/@type"/>Helper.getOMElement(
+                                    <xsl:value-of select="../../param[@type!='' and @direction='out'
and @opname=$opname]/@type"/>Helper.INSTANCE.getOMElement(
                                     param,
                                     <xsl:value-of select="../../param[@type!='' and @direction='out'
and @opname=$opname]/@type"/>.MY_QNAME,factory));
                                 </xsl:when>
@@ -405,7 +405,7 @@
                 if (<xsl:value-of select="@type"/>.class.equals(type)){
                 <xsl:choose>
                     <xsl:when test="$helpermode">
-                           return <xsl:value-of select="@type"/>Helper.parse(param.getXMLStreamReaderWithoutCaching());
+                           return <xsl:value-of select="@type"/>Helper.INSTANCE.parse(param.getXMLStreamReaderWithoutCaching());
                     </xsl:when>
                     <xsl:when test="@type = 'org.apache.axiom.om.OMElement'">
                            return param;

Added: webservices/axis2/trunk/java/modules/adb-codegen/test/org/apache/axis2/schema/ADBBeanUtil.java
URL: http://svn.apache.org/viewvc/webservices/axis2/trunk/java/modules/adb-codegen/test/org/apache/axis2/schema/ADBBeanUtil.java?rev=791135&view=auto
==============================================================================
--- webservices/axis2/trunk/java/modules/adb-codegen/test/org/apache/axis2/schema/ADBBeanUtil.java
(added)
+++ webservices/axis2/trunk/java/modules/adb-codegen/test/org/apache/axis2/schema/ADBBeanUtil.java
Sat Jul  4 14:50:16 2009
@@ -0,0 +1,105 @@
+/*
+ * 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.schema;
+
+import javax.xml.namespace.QName;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamReader;
+
+import org.apache.axiom.om.OMAbstractFactory;
+import org.apache.axiom.om.OMElement;
+import org.apache.axiom.om.OMFactory;
+import org.apache.axis2.databinding.ADBBean;
+import org.apache.axis2.databinding.ADBException;
+import org.apache.axis2.databinding.ADBHelper;
+import org.apache.axis2.databinding.utils.writer.MTOMAwareXMLStreamWriter;
+
+public class ADBBeanUtil {
+    private ADBBeanUtil() {}
+
+    private static ADBHelper<?> getHelper(Class<?> beanClass) throws Exception
{
+        return (ADBHelper<?>)Class.forName(beanClass.getName() + "Helper").getField("INSTANCE").get(null);
+    }
+    
+    public static QName getQName(Class<?> beanClass) throws Exception {
+        return (QName)beanClass.getField("MY_QNAME").get(null);
+    }
+    
+    public static <T> T parse(Class<T> beanClass, XMLStreamReader reader) throws
Exception {
+        if (ADBBean.class.isAssignableFrom(beanClass)) {
+            for (Class<?> clazz : beanClass.getDeclaredClasses()) {
+                if (clazz.getSimpleName().equals("Factory")) {
+                    return beanClass.cast(clazz.getMethod("parse", XMLStreamReader.class).invoke(null,
reader));
+                }
+            }
+            return null; // We should never get here
+        } else {
+            return beanClass.cast(getHelper(beanClass).parse(reader));
+        }
+    }
+    
+    public static OMElement getOMElement(Object bean, QName qname, OMFactory factory) throws
Exception {
+        if (bean instanceof ADBBean) {
+            return ((ADBBean)bean).getOMElement(qname, factory);
+        } else {
+            return getOMElement(bean, getHelper(bean.getClass()), qname, factory);
+        }
+    }
+    
+    private static <T> OMElement getOMElement(Object bean, ADBHelper<T> helper,
QName qname, OMFactory factory) throws ADBException {
+        return helper.getOMElement(helper.getBeanClass().cast(bean), qname, factory);
+    }
+    
+    public static OMElement getOMElement(Object bean) throws Exception {
+        return getOMElement(bean, getQName(bean.getClass()), OMAbstractFactory.getOMFactory());
+    }
+    
+    public static XMLStreamReader getPullParser(Object bean, QName qname) throws Exception
{
+        if (bean instanceof ADBBean) {
+            return ((ADBBean)bean).getPullParser(qname);
+        } else {
+            return getPullParser(bean, getHelper(bean.getClass()), qname);
+        }
+    }
+    
+    private static <T> XMLStreamReader getPullParser(Object bean, ADBHelper<T>
helper, QName qname) throws XMLStreamException {
+        return helper.getPullParser(helper.getBeanClass().cast(bean), qname);
+    }
+    
+    public static XMLStreamReader getPullParser(Object bean) throws Exception {
+        return getPullParser(bean, getQName(bean.getClass()));
+    }
+    
+    public static void serialize(Object bean, QName qname, MTOMAwareXMLStreamWriter writer)
throws Exception {
+        if (bean instanceof ADBBean) {
+            ((ADBBean)bean).serialize(qname, null, writer);
+        } else {
+            serialize(bean, getHelper(bean.getClass()), qname, writer);
+        }
+    }
+    
+    private static <T> void serialize(Object bean, ADBHelper<T> helper, QName
qname, MTOMAwareXMLStreamWriter writer) throws XMLStreamException {
+        helper.serialize(helper.getBeanClass().cast(bean), qname, null, writer);
+    }
+    
+    public static void serialize(Object bean, MTOMAwareXMLStreamWriter writer) throws Exception
{
+        serialize(bean, getQName(bean.getClass()), writer);
+    }
+}

Propchange: webservices/axis2/trunk/java/modules/adb-codegen/test/org/apache/axis2/schema/ADBBeanUtil.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: webservices/axis2/trunk/java/modules/adb-codegen/test/org/apache/axis2/schema/AbstractTestCase.java
URL: http://svn.apache.org/viewvc/webservices/axis2/trunk/java/modules/adb-codegen/test/org/apache/axis2/schema/AbstractTestCase.java?rev=791135&r1=791134&r2=791135&view=diff
==============================================================================
--- webservices/axis2/trunk/java/modules/adb-codegen/test/org/apache/axis2/schema/AbstractTestCase.java
(original)
+++ webservices/axis2/trunk/java/modules/adb-codegen/test/org/apache/axis2/schema/AbstractTestCase.java
Sat Jul  4 14:50:16 2009
@@ -29,12 +29,15 @@
 import java.io.StringReader;
 import java.io.StringWriter;
 import java.lang.reflect.Array;
+import java.lang.reflect.Field;
 import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
 import java.math.BigDecimal;
 import java.math.BigInteger;
 import java.util.Arrays;
 import java.util.Date;
 import java.util.HashSet;
+import java.util.Map;
 import java.util.Set;
 
 import javax.activation.DataHandler;
@@ -63,20 +66,11 @@
             URI.class, Language.class, HexBinary.class
     }));
     
-    private static QName getADBBeanQName(Class<? extends ADBBean> beanClass) throws
Exception {
-        return (QName)beanClass.getField("MY_QNAME").get(null);
+    private static boolean isADBBean(Class<?> beanClass) {
+        return ADBBean.class.isAssignableFrom(beanClass) || beanClass.getName().startsWith("helper.");
     }
     
-    private static <T extends ADBBean> T parse(Class<T> beanClass, XMLStreamReader
reader) throws Exception {
-        for (Class<?> clazz : beanClass.getDeclaredClasses()) {
-            if (clazz.getSimpleName().equals("Factory")) {
-                return beanClass.cast(clazz.getMethod("parse", XMLStreamReader.class).invoke(null,
reader));
-            }
-        }
-        return null; // We should never get here
-    }
-    
-    private static boolean isEnum(Class<? extends ADBBean> beanClass) {
+    private static boolean isEnum(Class<?> beanClass) {
         try {
             beanClass.getDeclaredField("_table_");
             return true;
@@ -85,6 +79,15 @@
         }
     }
     
+    private static BeanInfo getBeanInfo(Class<?> beanClass) {
+        try {
+            return Introspector.getBeanInfo(beanClass, Object.class);
+        } catch (IntrospectionException ex) {
+            fail("Failed to introspect " + beanClass);
+            return null; // Make compiler happy
+        }
+    }
+    
     /**
      * Assert that two ADB beans are equal. This method recursively compares properties
      * in the bean. It supports comparison of various property types, including arrays
@@ -93,21 +96,14 @@
      * @param expected
      * @param actual
      */
-    public static void assertBeanEquals(ADBBean expected, ADBBean actual) {
+    public static void assertBeanEquals(Object expected, Object actual) {
         if (expected == null) {
             assertNull(actual);
             return;
         }
         Class<?> beanClass = expected.getClass();
         assertEquals(beanClass, actual.getClass());
-        BeanInfo beanInfo;
-        try {
-            beanInfo = Introspector.getBeanInfo(beanClass, Object.class);
-        } catch (IntrospectionException ex) {
-            fail("Failed to introspect " + beanClass);
-            return; // Make compiler happy
-        }
-        for (PropertyDescriptor desc : beanInfo.getPropertyDescriptors()) {
+        for (PropertyDescriptor desc : getBeanInfo(beanClass).getPropertyDescriptors()) {
             String propertyName = desc.getName();
 //            System.out.println("Comparing property " + propertyName);
             Method readMethod = desc.getReadMethod();
@@ -141,11 +137,11 @@
                 assertEquals("value for " + message, expected, actual);
             } else if (DataHandler.class.isAssignableFrom(type)) {
                 assertDataHandlerEquals((DataHandler)expected, (DataHandler)actual);
-            } else if (ADBBean.class.isAssignableFrom(type)) {
-                if (isEnum(((ADBBean)expected).getClass())) {
+            } else if (isADBBean(type)) {
+                if (isEnum(type)) {
                     assertSame("enum value for " + message, expected, actual);
                 } else {
-                    assertBeanEquals((ADBBean)expected, (ADBBean)actual);
+                    assertBeanEquals(expected, actual);
                 }
             } else {
                 fail("Don't know how to compare values of type " + type.getName() + " for
" + message);
@@ -167,6 +163,53 @@
         }
     }
     
+    public static Object toHelperModeBean(ADBBean bean) throws Exception {
+        Class<?> beanClass = bean.getClass();
+        Object helperModeBean = null;
+        do {
+            Class<?> helperModeBeanClass = Class.forName("helper." + beanClass.getName());
+            if (helperModeBean == null) {
+                helperModeBean = helperModeBeanClass.newInstance();
+            }
+            for (Field field : beanClass.getDeclaredFields()) {
+                if (!Modifier.isStatic(field.getModifiers())) {
+                    field.setAccessible(true);
+                    Object value = field.get(bean);
+                    if (value instanceof ADBBean) {
+                        // Try to get the _table_ field if this is an enumeration
+                        Map<?,?> enumValues;
+                        try {
+                            Field tableField = value.getClass().getDeclaredField("_table_");
+                            tableField.setAccessible(true);
+                            enumValues = (Map<?,?>)tableField.get(null);
+                        } catch (NoSuchFieldException ex) {
+                            enumValues = null;
+                        }
+                        if (enumValues == null) {
+                            // Not an enumeration => translate is as a bean
+                            value = toHelperModeBean((ADBBean)value);
+                        } else {
+                            Field tableField = Class.forName("helper." + value.getClass().getName()).getDeclaredField("_table_");
+                            tableField.setAccessible(true);
+                            Map<?,?> destEnumValues = (Map<?,?>)tableField.get(null);
+                            for (Map.Entry<?,?> entry : enumValues.entrySet()) {
+                                if (entry.getValue() == value) {
+                                    value = destEnumValues.get(entry.getKey());
+                                    break;
+                                }
+                            }
+                        }
+                    }
+                    Field destField = helperModeBeanClass.getDeclaredField(field.getName());
+                    destField.setAccessible(true);
+                    destField.set(helperModeBean, value);
+                }
+            }
+            beanClass = beanClass.getSuperclass();
+        } while (!beanClass.equals(Object.class));
+        return helperModeBean;
+    }
+    
     /**
      * Serialize a bean to XML and then deserialize the XML.
      * 
@@ -176,11 +219,11 @@
      */
     public static ADBBean serializeDeserialize(ADBBean bean) throws Exception {
         Class<? extends ADBBean> beanClass = bean.getClass();
-        OMElement omElement = bean.getOMElement(getADBBeanQName(beanClass), OMAbstractFactory.getOMFactory());
+        OMElement omElement = bean.getOMElement(ADBBeanUtil.getQName(beanClass), OMAbstractFactory.getOMFactory());
         String omElementString = omElement.toStringWithConsume();
 //        System.out.println("om string ==> " + omElementString);
         XMLStreamReader xmlReader = StAXUtils.createXMLStreamReader(new ByteArrayInputStream(omElementString.getBytes()));
-        return parse(beanClass, xmlReader);
+        return ADBBeanUtil.parse(beanClass, xmlReader);
     }
     
     /**
@@ -213,54 +256,55 @@
         }
         
         testSerializeDeserialize4(bean, expectedResult);
+        
+        try {
+            Class.forName("helper." + bean.getClass().getName());
+        } catch (ClassNotFoundException ex) {
+            // Code has not been compiled in helper mode; skip the rest of the tests.
+            return;
+        }
+        
+        testSerializeDeserialize1(toHelperModeBean(bean), toHelperModeBean(expectedResult));
     }
     
     // Deserialization approach 1: use an XMLStreamReader produced by the StAX parser.
-    private static void testSerializeDeserialize1(ADBBean bean, ADBBean expectedResult) throws
Exception {
-        Class<? extends ADBBean> beanClass = bean.getClass();
-        QName qname = getADBBeanQName(beanClass);
-        OMElement omElement = bean.getOMElement(qname, OMAbstractFactory.getOMFactory());
+    private static void testSerializeDeserialize1(Object bean, Object expectedResult) throws
Exception {
+        OMElement omElement = ADBBeanUtil.getOMElement(bean);
         String omElementString = omElement.toStringWithConsume();
         System.out.println(omElementString);
-        assertBeanEquals(expectedResult, parse(beanClass,
+        assertBeanEquals(expectedResult, ADBBeanUtil.parse(bean.getClass(),
                 StAXUtils.createXMLStreamReader(new StringReader(omElementString))));
     }
     
     // Deserialization approach 2: use an Axiom tree with caching. In this case the
     // XMLStreamReader implementation is OMStAXWrapper and we test interoperability
     // between ADB and Axiom's OMStAXWrapper.
-    private static void testSerializeDeserialize2(ADBBean bean, ADBBean expectedResult) throws
Exception {
-        Class<? extends ADBBean> beanClass = bean.getClass();
-        QName qname = getADBBeanQName(beanClass);
-        OMElement omElement = bean.getOMElement(qname, OMAbstractFactory.getOMFactory());
+    private static void testSerializeDeserialize2(Object bean, Object expectedResult) throws
Exception {
+        OMElement omElement = ADBBeanUtil.getOMElement(bean);
         String omElementString = omElement.toStringWithConsume();
         OMElement omElement2 = new StAXOMBuilder(StAXUtils.createXMLStreamReader(
                 new StringReader(omElementString))).getDocumentElement();
-        assertBeanEquals(expectedResult, parse(beanClass, omElement2.getXMLStreamReader()));
+        assertBeanEquals(expectedResult, ADBBeanUtil.parse(bean.getClass(), omElement2.getXMLStreamReader()));
     }
     
     // Deserialization approach 3: use the pull parser produced by ADB.
-    private static void testSerializeDeserialize3(ADBBean bean, ADBBean expectedResult) throws
Exception {
-        Class<? extends ADBBean> beanClass = bean.getClass();
-        QName qname = getADBBeanQName(beanClass);
-        assertBeanEquals(expectedResult, parse(beanClass, bean.getPullParser(qname)));
+    private static void testSerializeDeserialize3(Object bean, Object expectedResult) throws
Exception {
+        assertBeanEquals(expectedResult, ADBBeanUtil.parse(bean.getClass(), ADBBeanUtil.getPullParser(bean)));
     }
     
     // Approach 4: Serialize the bean as the child of an element that declares a default
namespace.
     // If ADB behaves correctly, this should not have any impact. A failure here may be an
indication
     // of an incorrect usage of XMLStreamWriter#writeStartElement(String).
-    private static void testSerializeDeserialize4(ADBBean bean, ADBBean expectedResult) throws
Exception {
-        Class<? extends ADBBean> beanClass = bean.getClass();
-        QName qname = getADBBeanQName(beanClass);
+    private static void testSerializeDeserialize4(Object bean, Object expectedResult) throws
Exception {
         StringWriter sw = new StringWriter();
         MTOMAwareXMLStreamWriter writer = new MTOMAwareXMLSerializer(StAXUtils.createXMLStreamWriter(sw));
         writer.writeStartElement("", "root", "urn:test");
         writer.writeDefaultNamespace("urn:test");
-        bean.serialize(qname, null, writer);
+        ADBBeanUtil.serialize(bean, writer);
         writer.writeEndElement();
         writer.flush();
         OMElement omElement3 = new StAXOMBuilder(StAXUtils.createXMLStreamReader(new StringReader(sw.toString()))).getDocumentElement();
-        assertBeanEquals(expectedResult, parse(beanClass, omElement3.getFirstElement().getXMLStreamReader()));
+        assertBeanEquals(expectedResult, ADBBeanUtil.parse(bean.getClass(), omElement3.getFirstElement().getXMLStreamReader()));
     }
     
     /**
@@ -271,7 +315,7 @@
      */
     public static void assertSerializationFailure(ADBBean bean) throws Exception {
         try {
-            OMElement omElement = bean.getOMElement(getADBBeanQName(bean.getClass()), OMAbstractFactory.getOMFactory());
+            OMElement omElement = bean.getOMElement(ADBBeanUtil.getQName(bean.getClass()),
OMAbstractFactory.getOMFactory());
             omElement.toStringWithConsume();
             fail("Expected ADBException");
         } catch (ADBException ex) {

Added: webservices/axis2/trunk/java/modules/adb/src/org/apache/axis2/databinding/ADBHelper.java
URL: http://svn.apache.org/viewvc/webservices/axis2/trunk/java/modules/adb/src/org/apache/axis2/databinding/ADBHelper.java?rev=791135&view=auto
==============================================================================
--- webservices/axis2/trunk/java/modules/adb/src/org/apache/axis2/databinding/ADBHelper.java
(added)
+++ webservices/axis2/trunk/java/modules/adb/src/org/apache/axis2/databinding/ADBHelper.java
Sat Jul  4 14:50:16 2009
@@ -0,0 +1,46 @@
+/*
+ * 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.databinding;
+
+import javax.xml.namespace.QName;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamReader;
+import javax.xml.stream.XMLStreamWriter;
+
+import org.apache.axiom.om.OMElement;
+import org.apache.axiom.om.OMFactory;
+
+/**
+ * Interface implemented by all ADB helpers. Helpers are generated by ADB when helper mode
+ * is enabled. They contain the parsing and serialization logic.
+ *
+ * @param <T> the bean class that this helper is able to process
+ */
+public interface ADBHelper<T> {
+    Class<T> getBeanClass();
+    
+    XMLStreamReader getPullParser(T bean, QName adbBeanQName) throws XMLStreamException;
+
+    OMElement getOMElement(T bean, QName parentQName, OMFactory factory) throws ADBException;
+
+    void serialize(T bean, QName parentQName, OMFactory factory, XMLStreamWriter xmlWriter)
throws XMLStreamException;
+
+    T parse(XMLStreamReader reader) throws Exception;
+}

Propchange: webservices/axis2/trunk/java/modules/adb/src/org/apache/axis2/databinding/ADBHelper.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message