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 2006/07/19 11:03:40 UTC

svn commit: r423409 - in /webservices/axis2/trunk/java/modules: adb/src/org/apache/axis2/databinding/i18n/ adb/src/org/apache/axis2/databinding/utils/ adb/test/org/apache/axis2/databinding/utils/ codegen/src/org/apache/axis2/wsdl/template/java/

Author: ajith
Date: Wed Jul 19 02:03:38 2006
New Revision: 423409

URL: http://svn.apache.org/viewvc?rev=423409&view=rev
Log:
1. Modified the ConverterUtil.java to handle arrays for some of the classes. Specifically ones with a parse method taking the string or having a constructor that takes a string value.
2. Added a testcase to test the functionality of the ConverterUtil
3. Added the messages and the resources to give i18n support to the ADB module.
4. Removed some unwanted methods from the templates

Added:
    webservices/axis2/trunk/java/modules/adb/src/org/apache/axis2/databinding/i18n/
    webservices/axis2/trunk/java/modules/adb/src/org/apache/axis2/databinding/i18n/ADBMessages.java
    webservices/axis2/trunk/java/modules/adb/src/org/apache/axis2/databinding/i18n/resource.properties
    webservices/axis2/trunk/java/modules/adb/test/org/apache/axis2/databinding/utils/ConverterUtilTest.java
Modified:
    webservices/axis2/trunk/java/modules/adb/src/org/apache/axis2/databinding/utils/ConverterUtil.java
    webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/wsdl/template/java/MessageReceiverTemplate.xsl

Added: webservices/axis2/trunk/java/modules/adb/src/org/apache/axis2/databinding/i18n/ADBMessages.java
URL: http://svn.apache.org/viewvc/webservices/axis2/trunk/java/modules/adb/src/org/apache/axis2/databinding/i18n/ADBMessages.java?rev=423409&view=auto
==============================================================================
--- webservices/axis2/trunk/java/modules/adb/src/org/apache/axis2/databinding/i18n/ADBMessages.java (added)
+++ webservices/axis2/trunk/java/modules/adb/src/org/apache/axis2/databinding/i18n/ADBMessages.java Wed Jul 19 02:03:38 2006
@@ -0,0 +1,154 @@
+package org.apache.axis2.databinding.i18n;
+
+import org.apache.axis2.i18n.MessagesConstants;
+import org.apache.axis2.i18n.MessageBundle;
+
+import java.util.Locale;
+import java.util.ResourceBundle;
+import java.util.MissingResourceException;
+/*
+ * Copyright 2004,2005 The Apache Software Foundation.
+ *
+ * Licensed 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.
+ */
+
+public class ADBMessages {
+     private static Class thisClass = ADBMessages.class;
+
+    private static final String projectName = MessagesConstants.projectName;
+
+    private static final String resourceName = MessagesConstants.resourceName;
+    private static final Locale locale = MessagesConstants.locale;
+
+    private static final String packageName = getPackage(thisClass.getName());
+    private static final ClassLoader classLoader = thisClass.getClassLoader();
+
+    private static final ResourceBundle parent =
+            (MessagesConstants.rootPackageName.equals(packageName))
+            ? null
+            : MessagesConstants.rootBundle;
+
+
+    /**
+     * ** NO NEED TO CHANGE ANYTHING BELOW ****
+     */
+
+    private static final MessageBundle messageBundle =
+            new MessageBundle(projectName, packageName, resourceName,
+                    locale, classLoader, parent);
+
+    /**
+     * Gets a message from resource.properties from the package of the given object.
+     *
+     * @param key The resource key
+     * @return Returns the formatted message.
+     */
+    public static String getMessage(String key)
+            throws MissingResourceException {
+        return messageBundle.getMessage(key);
+    }
+
+    /**
+     * Get a message from resource.properties from the package of the given object.
+     *
+     * @param key  The resource key
+     * @param arg0 The argument to place in variable {0}
+     * @return Returns the formatted message.
+     */
+    public static String getMessage(String key, String arg0)
+            throws MissingResourceException {
+        return messageBundle.getMessage(key, arg0);
+    }
+
+    /**
+     * Gets a message from resource.properties from the package of the given object.
+     *
+     * @param key  The resource key
+     * @param arg0 The argument to place in variable {0}
+     * @param arg1 The argument to place in variable {1}
+     * @return Returns the formatted message.
+     */
+    public static String getMessage(String key, String arg0, String arg1)
+            throws MissingResourceException {
+        return messageBundle.getMessage(key, arg0, arg1);
+    }
+
+    /**
+     * Gets a message from resource.properties from the package of the given object.
+     *
+     * @param key  The resource key
+     * @param arg0 The argument to place in variable {0}
+     * @param arg1 The argument to place in variable {1}
+     * @param arg2 The argument to place in variable {2}
+     * @return Returns the formatted message.
+     */
+    public static String getMessage(String key, String arg0, String arg1, String arg2)
+            throws MissingResourceException {
+        return messageBundle.getMessage(key, arg0, arg1, arg2);
+    }
+
+    /**
+     * Get a message from resource.properties from the package of the given object.
+     *
+     * @param key  The resource key
+     * @param arg0 The argument to place in variable {0}
+     * @param arg1 The argument to place in variable {1}
+     * @param arg2 The argument to place in variable {2}
+     * @param arg3 The argument to place in variable {3}
+     * @return Returns the formatted message.
+     */
+    public static String getMessage(String key, String arg0, String arg1, String arg2, String arg3)
+            throws MissingResourceException {
+        return messageBundle.getMessage(key, arg0, arg1, arg2, arg3);
+    }
+
+    /**
+     * Gets a message from resource.properties from the package of the given object.
+     *
+     * @param key  The resource key
+     * @param arg0 The argument to place in variable {0}
+     * @param arg1 The argument to place in variable {1}
+     * @param arg2 The argument to place in variable {2}
+     * @param arg3 The argument to place in variable {3}
+     * @param arg4 The argument to place in variable {4}
+     * @return Returns the formatted message.
+     */
+    public static String getMessage(String key, String arg0, String arg1, String arg2, String arg3, String arg4)
+            throws MissingResourceException {
+        return messageBundle.getMessage(key, arg0, arg1, arg2, arg3, arg4);
+    }
+
+    /**
+     * Gets a message from resource.properties from the package of the given object.
+     *
+     * @param key  The resource key
+     * @param args An array of objects to place in corresponding variables
+     * @return Returns the formatted message.
+     */
+    public static String getMessage(String key, String[] args)
+            throws MissingResourceException {
+        return messageBundle.getMessage(key, args);
+    }
+
+    public static ResourceBundle getResourceBundle() {
+        return messageBundle.getResourceBundle();
+    }
+
+    public static MessageBundle getMessageBundle() {
+        return messageBundle;
+    }
+
+    private static String getPackage(String name) {
+        return name.substring(0, name.lastIndexOf('.')).intern();
+    }
+}

Added: webservices/axis2/trunk/java/modules/adb/src/org/apache/axis2/databinding/i18n/resource.properties
URL: http://svn.apache.org/viewvc/webservices/axis2/trunk/java/modules/adb/src/org/apache/axis2/databinding/i18n/resource.properties?rev=423409&view=auto
==============================================================================
--- webservices/axis2/trunk/java/modules/adb/src/org/apache/axis2/databinding/i18n/resource.properties (added)
+++ webservices/axis2/trunk/java/modules/adb/src/org/apache/axis2/databinding/i18n/resource.properties Wed Jul 19 02:03:38 2006
@@ -0,0 +1,17 @@
+# Translation instructions.
+# 1.  Each message line is of the form key=value.
+#     Translate the value, DO NOT translate the key.
+# 2.  The messages may contain arguments that will be filled in
+#     by the runtime.  These are of the form: {0}, {1}, etc.
+#     These must appear as is in the message, though the order
+#     may be changed to support proper language syntax.
+# 3.  If a single quote character is to appear in the resulting
+#     message, it must appear in this file as two consecutive
+#     single quote characters.
+# 4.  Lines beginning with "#" (like this one) are comment lines
+#     and may contain translation instructions.  They need not be
+#     translated unless your translated file, rather than this file,
+#     will serve as a base for other translators.
+################## Converter Util #################################################
+converter.cannotGenerate=Cannot generate object for {0}
+converter.cannotConvert=No way to convert {0}

Modified: webservices/axis2/trunk/java/modules/adb/src/org/apache/axis2/databinding/utils/ConverterUtil.java
URL: http://svn.apache.org/viewvc/webservices/axis2/trunk/java/modules/adb/src/org/apache/axis2/databinding/utils/ConverterUtil.java?rev=423409&r1=423408&r2=423409&view=diff
==============================================================================
--- webservices/axis2/trunk/java/modules/adb/src/org/apache/axis2/databinding/utils/ConverterUtil.java (original)
+++ webservices/axis2/trunk/java/modules/adb/src/org/apache/axis2/databinding/utils/ConverterUtil.java Wed Jul 19 02:03:38 2006
@@ -36,16 +36,18 @@
 import org.apache.axis2.databinding.types.UnsignedShort;
 import org.apache.axis2.databinding.types.Year;
 import org.apache.axis2.databinding.types.YearMonth;
+import org.apache.axis2.databinding.i18n.ADBMessages;
 import org.apache.axis2.util.Base64;
 
 import javax.xml.namespace.QName;
-import javax.xml.stream.XMLInputFactory;
 import javax.xml.stream.XMLStreamException;
 import javax.xml.stream.XMLStreamReader;
 import javax.activation.DataHandler;
 import java.io.ByteArrayInputStream;
 import java.io.InputStream;
 import java.lang.reflect.Array;
+import java.lang.reflect.Constructor;
+import java.lang.reflect.Method;
 import java.math.BigDecimal;
 import java.math.BigInteger;
 import java.text.SimpleDateFormat;
@@ -221,9 +223,9 @@
         return Base64.encode(bytes);
     }
 
-     public static String convertToString(javax.activation.DataHandler handler) {
+    public static String convertToString(javax.activation.DataHandler handler) {
         return getStringFromDatahandler(handler);
-     }
+    }
 
     /* ################################################################################ */
     /* String to java type conversions
@@ -254,7 +256,7 @@
     }
 
     public static float convertTofloat(String s) {
-         if (POSITIVE_INFINITY.equals(s)){
+        if (POSITIVE_INFINITY.equals(s)){
             return Float.POSITIVE_INFINITY;
         }else if (NEGATIVE_INFINITY.equals(s)){
             return Float.NEGATIVE_INFINITY;
@@ -649,7 +651,7 @@
     /* list to array conversion methods */
 
     public static Object convertToArray(Class baseArrayClass, String[] valueArray) {
-          //create a list using the string array
+        //create a list using the string array
         List valuesList = new ArrayList(valueArray.length);
         for (int i = 0; i < valueArray.length; i++) {
             valuesList.add(valueArray[i]);
@@ -697,14 +699,45 @@
             for (int i = 0; i < listSize; i++) {
                 Array.setDouble(returnArray, i, Double.parseDouble(objectList.get(i).toString()));
             }
-        } else {
-            objectList.toArray((Object[])returnArray);
+        } else{
+            ConvertToArbitraryObjectArray( returnArray, baseArrayClass, objectList);
         }
         return returnArray;
     }
 
     /**
      * 
+     * @param returnArray
+     * @param baseArrayClass
+     * @param objectList
+     */
+    private static void ConvertToArbitraryObjectArray(                                                     Object returnArray,
+                                                      Class baseArrayClass,
+                                                      List objectList) {
+        try {
+            for (int i = 0; i < objectList.size(); i++) {
+                Array.set(returnArray, i, getObjectForClass(
+                        baseArrayClass,
+                        objectList.get(i).toString()));
+            }
+            return;
+        } catch (Exception e) {
+            //oops! - this cannot be converted fall through and
+            //try the other alternative
+        }
+
+        try {
+            objectList.toArray((Object[])returnArray);
+        } catch (Exception e) {
+            //we are over with alternatives - throw the
+            //converison exception
+            throw new ObjectConversionException(e);
+        }
+    }
+
+    /**
+     * We could have used the Arraya.asList() method
+     * but that returns an *immutable* list !!!!!
      * @param array
      * @return
      */
@@ -733,5 +766,74 @@
         } catch (Exception e) {
             throw new RuntimeException(e);
         }
+    }
+
+    /**
+     * A reflection based method to generate an instance of
+     * a given class and populate it with a given value
+     * @param clazz
+     * @param value
+     * @return
+     */
+    public static Object getObjectForClass(Class clazz,String value){
+        //first see whether this class has a constructor that can
+        //take the string as an argument.
+        boolean continueFlag = false;
+        try {
+            Constructor stringConstructor = clazz.getConstructor(new Class[]{String.class});
+            return stringConstructor.newInstance(new Object[]{value});
+        } catch (NoSuchMethodException e) {
+            //oops - no such constructors - continue with the
+            //parse method
+            continueFlag = true;
+        } catch (Exception e) {
+            throw new ObjectConversionException(
+                    ADBMessages.getMessage("converter.cannotGenerate",
+                            clazz.getName()),
+                    e);
+        }
+
+        if (!continueFlag){
+            throw new ObjectConversionException(
+                    ADBMessages.getMessage("converter.cannotConvert",
+                            clazz.getName()));
+        }
+
+        try {
+            Method parseMethod =  clazz.getMethod("parse",new Class[]{String.class});
+            Object instance = clazz.newInstance();
+            return parseMethod.invoke(instance,new Object[]{value});
+        } catch (NoSuchMethodException e) {
+            throw new ObjectConversionException(
+                    ADBMessages.getMessage("converter.cannotConvert",
+                            clazz.getName()));
+        } catch (Exception e) {
+            throw new ObjectConversionException(
+                    ADBMessages.getMessage("converter.cannotGenerate",
+                            clazz.getName()),
+                    e);
+        }
+
+    }
+
+    /**
+     * A simple exception that is thrown when the conversion fails
+     */
+    public static class ObjectConversionException extends RuntimeException{
+        public ObjectConversionException() {
+        }
+
+        public ObjectConversionException(String message) {
+            super(message);
+        }
+
+        public ObjectConversionException(Throwable cause) {
+            super(cause);
+        }
+
+        public ObjectConversionException(String message, Throwable cause) {
+            super(message, cause);
+        }
+
     }
 }

Added: webservices/axis2/trunk/java/modules/adb/test/org/apache/axis2/databinding/utils/ConverterUtilTest.java
URL: http://svn.apache.org/viewvc/webservices/axis2/trunk/java/modules/adb/test/org/apache/axis2/databinding/utils/ConverterUtilTest.java?rev=423409&view=auto
==============================================================================
--- webservices/axis2/trunk/java/modules/adb/test/org/apache/axis2/databinding/utils/ConverterUtilTest.java (added)
+++ webservices/axis2/trunk/java/modules/adb/test/org/apache/axis2/databinding/utils/ConverterUtilTest.java Wed Jul 19 02:03:38 2006
@@ -0,0 +1,82 @@
+package org.apache.axis2.databinding.utils;
+
+import junit.framework.TestCase;
+
+import java.util.List;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.math.BigInteger;
+import java.lang.reflect.Array;
+/*
+ * Copyright 2004,2005 The Apache Software Foundation.
+ *
+ * Licensed 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.
+ */
+
+public class ConverterUtilTest extends TestCase {
+
+    /**
+     * Test conversion of Big Integer
+     */
+    public void testBigInteger(){
+        List l = new ArrayList();
+        l.add("23445");
+        l.add("23446");
+        l.add("23456646");
+        l.add("1113646");
+
+        Object convertedObj = ConverterUtil.convertToArray(
+                BigInteger.class,l);
+
+        assertTrue(convertedObj.getClass().isArray());
+        assertTrue(convertedObj.getClass().equals(BigInteger[].class));
+
+    }
+
+    /**
+     * integer arrays
+     */
+    public void testInt(){
+        List l = new ArrayList();
+        l.add("23445");
+        l.add("23446");
+        l.add("23456646");
+        l.add("1113646");
+
+        Object convertedObj = ConverterUtil.convertToArray(
+                int.class,l);
+
+        assertTrue(convertedObj.getClass().isArray());
+        assertTrue(convertedObj.getClass().equals(int[].class));
+
+    }
+
+     /**
+     * boolean arrays
+     */
+    public void testBool(){
+        List l = new ArrayList();
+        l.add("true");
+        l.add("false");
+        l.add("true");
+        l.add("false");
+
+        Object convertedObj = ConverterUtil.convertToArray(
+                boolean.class,l);
+
+        assertTrue(convertedObj.getClass().isArray());
+        assertTrue(convertedObj.getClass().equals(boolean[].class));
+
+    }
+
+}

Modified: webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/wsdl/template/java/MessageReceiverTemplate.xsl
URL: http://svn.apache.org/viewvc/webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/wsdl/template/java/MessageReceiverTemplate.xsl?rev=423409&r1=423408&r2=423409&view=diff
==============================================================================
--- webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/wsdl/template/java/MessageReceiverTemplate.xsl (original)
+++ webservices/axis2/trunk/java/modules/codegen/src/org/apache/axis2/wsdl/template/java/MessageReceiverTemplate.xsl Wed Jul 19 02:03:38 2006
@@ -177,22 +177,6 @@
         }
 
 
-        /**
-            *  Generate a method that can explode the OMElement and provide a list of inner
-            *  elements. the inner elements need to be ordered! This however need not be
-            *   specific to a databinding impl
-            */
-           private java.util.List explode(org.apache.axiom.om.OMElement element){
-               java.util.List listToReturn = new java.util.LinkedList();
-               for (java.util.Iterator children = element.getChildElements();
-                    children.hasNext();){
-                  listToReturn.add(children.next());
-               }
-
-               return listToReturn;
-           }
-
-
 
         }//end of class
     </xsl:template>
@@ -305,20 +289,6 @@
         return returnMap;
         }
 
-        /**
-            *  Generate a method that can explode the OMElement and provide a list of inner
-            *  elements. the inner elements need to be ordered! This however need not be
-            *   specific to a databinding impl
-            */
-           private java.util.List explode(org.apache.axiom.om.OMElement element){
-               java.util.List listToReturn = new java.util.LinkedList();
-               for (java.util.Iterator children = element.getChildElements();
-                    children.hasNext();){
-                  listToReturn.add(children.next());
-               }
-
-               return listToReturn;
-           }
 
 
         }//end of class



---------------------------------------------------------------------
To unsubscribe, e-mail: axis-cvs-unsubscribe@ws.apache.org
For additional commands, e-mail: axis-cvs-help@ws.apache.org