Commits

Anonymous committed c693505

allows support for type conversion to be specified on a per-class basis in xwork-default-conversion.properties and xwork-conversion.properties
Issue number: XW-65

git-svn-id: http://svn.opensymphony.com/svn/xwork/trunk@18e221344d-f017-0410-9bd5-d282ab1896d7

  • Participants
  • Parent commits bd55478

Comments (0)

Files changed (4)

File src/java/com/opensymphony/xwork/XWorkBasicConverter.java

+package com.opensymphony.xwork;
+
+import com.opensymphony.util.TextUtils;
+import ognl.DefaultTypeConverter;
+
+import java.lang.reflect.Member;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.*;
+
+/**
+ *
+ *
+ * @author <a href="mailto:plightbo@cisco.com">Pat Lightbody</a>
+ * @author $Author$
+ * @version $Revision$
+ */
+
+public class XWorkBasicConverter extends DefaultTypeConverter {
+    public Object convertValue(Map context, Object value, Member member, String s, Object o1, Class toType) {
+        Object result = null;
+
+        if (toType == String.class) {
+            result = doConvertToString(context, value);
+        } else if (toType == int[].class) {
+            result = doConvertToIntArray(context, value);
+        } else if (toType == Integer[].class) {
+            result = doConvertToIntegerArray(context, value);
+        } else if (toType == long[].class) {
+            result = doConvertToLongArray(context, value);
+        } else if (toType == Long[].class) {
+            result = doConvertToLongArray2(context, value);
+        } else if (toType == double[].class) {
+            result = doConvertToDoubleArray(context, value);
+        } else if (toType == Double[].class) {
+            result = doConvertToDoubleArray2(context, value);
+        } else if (toType == Date.class) {
+            result = doConvertToDate(context, value);
+        } else if (toType == List.class) {
+            result = doConvertToList(context, value);
+        } else if (toType == Collection.class) {
+            result = doConvertToList(context, value);
+        } else if (toType == Class.class) {
+            result = doConvertToClass(context, value);
+        } else {
+            if ((toType == String[].class) && (value instanceof String)) {
+                result = new String[]{(String) value};
+            }
+        }
+
+        if (result == null) {
+            if (value instanceof String[]) {
+                String[] array = (String[]) value;
+
+                if ((array != null) && (array.length == 1)) {
+                    value = array[0];
+                }
+
+                // let's try to convert the first element only
+                result = convertValue(context, value, toType);
+            } else {
+                result = super.convertValue(context, value, toType);
+            }
+        }
+
+        return result;
+    }
+
+    private Class doConvertToClass(Map context, Object value) {
+        Class clazz = null;
+
+        if (value instanceof String) {
+            try {
+                clazz = Class.forName((String) value);
+            } catch (ClassNotFoundException e) {
+            }
+        }
+
+        return clazz;
+    }
+
+    private Date doConvertToDate(Map context, Object value) {
+        Date result = null;
+
+        if (value instanceof String) {
+            String sa = (String) value;
+            SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
+
+            try {
+                result = sdf.parse(sa);
+            } catch (ParseException e) {
+            }
+        }
+
+        return result;
+    }
+
+    private double[] doConvertToDoubleArray(Map context, Object value) {
+        double[] result = null;
+
+        if (value instanceof String[]) {
+            String[] sa = (String[]) value;
+            double[] da = new double[sa.length];
+
+            for (int i = 0; i < sa.length; i++) {
+                da[i] = ((Double) super.convertValue(context, sa[i], Double.class)).doubleValue();
+            }
+
+            result = da;
+        }
+
+        return result;
+    }
+
+    private Double[] doConvertToDoubleArray2(Map context, Object value) {
+        double[] primitives = doConvertToDoubleArray(context, value);
+        Double[] result = null;
+
+        if (primitives != null) {
+            result = new Double[primitives.length];
+
+            for (int i = 0; i < primitives.length; i++) {
+                double primitive = primitives[i];
+                result[i] = new Double(primitive);
+            }
+        }
+
+        return result;
+    }
+
+    private int[] doConvertToIntArray(Map context, Object value) {
+        int[] result = null;
+
+        if (value instanceof String[]) {
+            String[] sa = (String[]) value;
+            int[] ia = new int[sa.length];
+
+            for (int i = 0; i < sa.length; i++) {
+                ia[i] = ((Integer) super.convertValue(context, sa[i], Integer.class)).intValue();
+            }
+
+            result = ia;
+        }
+
+        return result;
+    }
+
+    private Integer[] doConvertToIntegerArray(Map context, Object value) {
+        int[] primitives = doConvertToIntArray(context, value);
+        Integer[] result = null;
+
+        if (primitives != null) {
+            result = new Integer[primitives.length];
+
+            for (int i = 0; i < primitives.length; i++) {
+                int primitive = primitives[i];
+                result[i] = new Integer(primitive);
+            }
+        }
+
+        return result;
+    }
+
+    private List doConvertToList(Map context, Object value) {
+        List result = null;
+
+        if (value instanceof String[]) {
+            String[] sa = (String[]) value;
+            result = new ArrayList(sa.length);
+
+            for (int i = 0; i < sa.length; i++) {
+                String s = sa[i];
+                result.add(s);
+            }
+        }
+
+        return result;
+    }
+
+    private long[] doConvertToLongArray(Map context, Object value) {
+        long[] result = null;
+
+        if (value instanceof String[]) {
+            String[] sa = (String[]) value;
+            long[] la = new long[sa.length];
+
+            for (int i = 0; i < sa.length; i++) {
+                la[i] = ((Long) super.convertValue(context, sa[i], Long.class)).longValue();
+            }
+
+            result = la;
+        }
+
+        return result;
+    }
+
+    private Long[] doConvertToLongArray2(Map context, Object value) {
+        long[] primitives = doConvertToLongArray(context, value);
+        Long[] result = null;
+
+        if (primitives != null) {
+            result = new Long[primitives.length];
+
+            for (int i = 0; i < primitives.length; i++) {
+                long primitive = primitives[i];
+                result[i] = new Long(primitive);
+            }
+        }
+
+        return result;
+    }
+
+    private String doConvertToString(Map context, Object value) {
+        String result = null;
+
+        if (value instanceof int[]) {
+            int[] x = (int[]) value;
+            List intArray = new ArrayList(x.length);
+
+            for (int i = 0; i < x.length; i++) {
+                intArray.add(new Integer(x[i]));
+            }
+
+            result = TextUtils.join(", ", intArray);
+        } else if (value instanceof long[]) {
+            long[] x = (long[]) value;
+            List intArray = new ArrayList(x.length);
+
+            for (int i = 0; i < x.length; i++) {
+                intArray.add(new Long(x[i]));
+            }
+
+            result = TextUtils.join(", ", intArray);
+        } else if (value instanceof Date) {
+            SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
+            result = sdf.format(value);
+        } else if (value instanceof String[]) {
+            result = TextUtils.join(", ", (String[]) value);
+        }
+
+        return result;
+    }
+
+}

File src/java/com/opensymphony/xwork/util/XWorkConverter.java

  */
 package com.opensymphony.xwork.util;
 
-import com.opensymphony.util.TextUtils;
-
 import ognl.DefaultTypeConverter;
 import ognl.Evaluation;
 import ognl.OgnlContext;
 import ognl.TypeConverter;
 
+import java.io.IOException;
 import java.io.InputStream;
-
 import java.lang.reflect.Member;
-
-import java.text.ParseException;
-import java.text.SimpleDateFormat;
-
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Date;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
-import java.util.Properties;
+import java.util.*;
 
 
 /**
 
     HashMap mappings = new HashMap();
     HashSet noMapping = new HashSet();
+    HashMap defaultMappings = new HashMap();
 
     //~ Constructors ///////////////////////////////////////////////////////////
 
     private XWorkConverter() {
+        try {
+            loadConversionProps("xwork-default-conversion.properties");
+        } catch (Exception e) {
+        }
+
+        try {
+            loadConversionProps("xwork-conversion.properties");
+        } catch (Exception e) {
+        }
+    }
+
+    private void loadConversionProps(String propsName) throws Exception {
+        InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(propsName);
+        Properties props = new Properties();
+        props.load(is);
+
+        for (Iterator iterator = props.entrySet().iterator(); iterator.hasNext();) {
+            Map.Entry entry = (Map.Entry) iterator.next();
+            String key = (String) entry.getKey();
+
+            defaultMappings.put(key, createTypeConverter((String) entry.getValue()));
+        }
     }
 
     //~ Methods ////////////////////////////////////////////////////////////////
             return null;
         }
 
+        Class clazz = null;
         try {
             OgnlContext ognlContext = (OgnlContext) context;
             Evaluation eval = ognlContext.getCurrentEvaluation();
-            Class clazz;
             String property;
 
             if (eval == null) {
                 property = (String) eval.getFirstChild().getResult();
             }
 
-            Map mapping = (Map) mappings.get(clazz);
+            if (!noMapping.contains(clazz)) {
+                Map mapping = (Map) mappings.get(clazz);
 
-            if (mapping == null) {
-                mapping = new HashMap();
-                mappings.put(clazz, mapping);
+                if (mapping == null) {
+                    mapping = new HashMap();
+                    mappings.put(clazz, mapping);
 
-                String className = clazz.getName();
-                String resource = className.replace('.', '/') + "-conversion.properties";
-                InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(resource);
+                    String className = clazz.getName();
+                    String resource = className.replace('.', '/') + "-conversion.properties";
+                    InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(resource);
 
-                Properties props = new Properties();
-                props.load(is);
-                mapping.putAll(props);
+                    Properties props = new Properties();
+                    props.load(is);
+                    mapping.putAll(props);
 
-                for (Iterator iterator = mapping.entrySet().iterator();
-                        iterator.hasNext();) {
-                    Map.Entry entry = (Map.Entry) iterator.next();
-                    entry.setValue(createTypeConverter((String) entry.getValue()));
+                    for (Iterator iterator = mapping.entrySet().iterator();
+                         iterator.hasNext();) {
+                        Map.Entry entry = (Map.Entry) iterator.next();
+                        entry.setValue(createTypeConverter((String) entry.getValue()));
+                    }
                 }
-            }
 
-            TypeConverter converter = (TypeConverter) mapping.get(property);
+                TypeConverter converter = (TypeConverter) mapping.get(property);
 
-            if (converter != null) {
-                return converter.convertValue(context, value, member, s, o1, aClass);
+                if (converter != null) {
+                    return converter.convertValue(context, value, member, s, o1, aClass);
+                }
             }
         } catch (Throwable t) {
-        }
-
-        return super.convertValue(context, value, member, s, o1, aClass);
-    }
-
-    public Object convertValue(Map context, Object value, Class toType) {
-        return doConvert(context, value, toType);
-    }
-
-    Object doConvert(Map context, Object value, Class toType) {
-        Object result = null;
-
-        if (toType == String.class) {
-            result = doConvertToString(context, value);
-        } else if (toType == int[].class) {
-            result = doConvertToIntArray(context, value);
-        } else if (toType == Integer[].class) {
-            result = doConvertToIntegerArray(context, value);
-        } else if (toType == long[].class) {
-            result = doConvertToLongArray(context, value);
-        } else if (toType == Long[].class) {
-            result = doConvertToLongArray2(context, value);
-        } else if (toType == double[].class) {
-            result = doConvertToDoubleArray(context, value);
-        } else if (toType == Double[].class) {
-            result = doConvertToDoubleArray2(context, value);
-        } else if (toType == Date.class) {
-            result = doConvertToDate(context, value);
-        } else if (toType == List.class) {
-            result = doConvertToList(context, value);
-        } else if (toType == Collection.class) {
-            result = doConvertToList(context, value);
-        } else if (toType == Class.class) {
-            result = doConvertToClass(context, value);
-        } else {
-            if ((toType == String[].class) && (value instanceof String)) {
-                result = new String[] {(String) value};
+            if (clazz != null) {
+                noMapping.add(clazz);
             }
         }
 
-        if (result == null) {
-            if (value instanceof String[]) {
-                String[] array = (String[]) value;
-
-                if ((array != null) && (array.length == 1)) {
-                    value = array[0];
-                }
-
-                // let's try to convert the first element only
-                result = convertValue(context, value, toType);
-            } else {
-                result = super.convertValue(context, value, toType);
+        if (defaultMappings.containsKey(aClass.getName())) {
+            try {
+                TypeConverter tc = (TypeConverter) defaultMappings.get(aClass.getName());
+                return tc.convertValue(context, value, member, s, o1, aClass);
+            } catch (Exception e) {
+                e.printStackTrace();
             }
         }
 
-        return result;
+
+        return super.convertValue(context, value, member, s, o1, aClass);
     }
 
     private TypeConverter createTypeConverter(String className) throws Exception, InstantiationException {
         return (TypeConverter) conversionClass.newInstance();
     }
 
-    private Class doConvertToClass(Map context, Object value) {
-        Class clazz = null;
-
-        if (value instanceof String) {
-            try {
-                clazz = Class.forName((String) value);
-            } catch (ClassNotFoundException e) {
-            }
-        }
-
-        return clazz;
-    }
-
-    private Date doConvertToDate(Map context, Object value) {
-        Date result = null;
-
-        if (value instanceof String) {
-            String sa = (String) value;
-            SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
-
-            try {
-                result = sdf.parse(sa);
-            } catch (ParseException e) {
-            }
-        }
-
-        return result;
-    }
-
-    private double[] doConvertToDoubleArray(Map context, Object value) {
-        double[] result = null;
-
-        if (value instanceof String[]) {
-            String[] sa = (String[]) value;
-            double[] da = new double[sa.length];
-
-            for (int i = 0; i < sa.length; i++) {
-                da[i] = ((Double) super.convertValue(context, sa[i], Double.class)).doubleValue();
-            }
-
-            result = da;
-        }
-
-        return result;
-    }
-
-    private Double[] doConvertToDoubleArray2(Map context, Object value) {
-        double[] primitives = doConvertToDoubleArray(context, value);
-        Double[] result = null;
-
-        if (primitives != null) {
-            result = new Double[primitives.length];
-
-            for (int i = 0; i < primitives.length; i++) {
-                double primitive = primitives[i];
-                result[i] = new Double(primitive);
-            }
-        }
-
-        return result;
-    }
-
-    private int[] doConvertToIntArray(Map context, Object value) {
-        int[] result = null;
-
-        if (value instanceof String[]) {
-            String[] sa = (String[]) value;
-            int[] ia = new int[sa.length];
-
-            for (int i = 0; i < sa.length; i++) {
-                ia[i] = ((Integer) super.convertValue(context, sa[i], Integer.class)).intValue();
-            }
-
-            result = ia;
-        }
-
-        return result;
-    }
-
-    private Integer[] doConvertToIntegerArray(Map context, Object value) {
-        int[] primitives = doConvertToIntArray(context, value);
-        Integer[] result = null;
-
-        if (primitives != null) {
-            result = new Integer[primitives.length];
-
-            for (int i = 0; i < primitives.length; i++) {
-                int primitive = primitives[i];
-                result[i] = new Integer(primitive);
-            }
-        }
-
-        return result;
-    }
-
-    private List doConvertToList(Map context, Object value) {
-        List result = null;
-
-        if (value instanceof String[]) {
-            String[] sa = (String[]) value;
-            result = new ArrayList(sa.length);
-
-            for (int i = 0; i < sa.length; i++) {
-                String s = sa[i];
-                result.add(s);
-            }
-        }
-
-        return result;
-    }
-
-    private long[] doConvertToLongArray(Map context, Object value) {
-        long[] result = null;
-
-        if (value instanceof String[]) {
-            String[] sa = (String[]) value;
-            long[] la = new long[sa.length];
-
-            for (int i = 0; i < sa.length; i++) {
-                la[i] = ((Long) super.convertValue(context, sa[i], Long.class)).longValue();
-            }
-
-            result = la;
-        }
-
-        return result;
-    }
-
-    private Long[] doConvertToLongArray2(Map context, Object value) {
-        long[] primitives = doConvertToLongArray(context, value);
-        Long[] result = null;
-
-        if (primitives != null) {
-            result = new Long[primitives.length];
-
-            for (int i = 0; i < primitives.length; i++) {
-                long primitive = primitives[i];
-                result[i] = new Long(primitive);
-            }
-        }
-
-        return result;
-    }
-
-    private String doConvertToString(Map context, Object value) {
-        String result = null;
-
-        if (value instanceof int[]) {
-            int[] x = (int[]) value;
-            List intArray = new ArrayList(x.length);
-
-            for (int i = 0; i < x.length; i++) {
-                intArray.add(new Integer(x[i]));
-            }
-
-            result = TextUtils.join(", ", intArray);
-        } else if (value instanceof long[]) {
-            long[] x = (long[]) value;
-            List intArray = new ArrayList(x.length);
-
-            for (int i = 0; i < x.length; i++) {
-                intArray.add(new Long(x[i]));
-            }
-
-            result = TextUtils.join(", ", intArray);
-        } else if (value instanceof Date) {
-            SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
-            result = sdf.format(value);
-        } else if (value instanceof String[]) {
-            result = TextUtils.join(", ", (String[]) value);
-        }
-
-        return result;
-    }
 }

File src/java/xwork-default-conversion.properties

+java.lang.String     =com.opensymphony.xwork.XWorkBasicConverter
+[D                   =com.opensymphony.xwork.XWorkBasicConverter
+double               =com.opensymphony.xwork.XWorkBasicConverter
+java.lang.Double     =com.opensymphony.xwork.XWorkBasicConverter
+[Ljava.lang.Double;  =com.opensymphony.xwork.XWorkBasicConverter
+[I                   =com.opensymphony.xwork.XWorkBasicConverter
+int                  =com.opensymphony.xwork.XWorkBasicConverter
+java.lang.Integer    =com.opensymphony.xwork.XWorkBasicConverter
+[Ljava.lang.Integer; =com.opensymphony.xwork.XWorkBasicConverter
+[J                   =com.opensymphony.xwork.XWorkBasicConverter
+long                 =com.opensymphony.xwork.XWorkBasicConverter
+java.lang.Long       =com.opensymphony.xwork.XWorkBasicConverter
+[Ljava.lang.Long;    =com.opensymphony.xwork.XWorkBasicConverter
+java.util.Date       =com.opensymphony.xwork.XWorkBasicConverter
+java.util.Collection =com.opensymphony.xwork.XWorkBasicConverter
+java.util.List       =com.opensymphony.xwork.XWorkBasicConverter
+java.lang.Class      =com.opensymphony.xwork.XWorkBasicConverter
+[Ljava.lang.String;  =com.opensymphony.xwork.XWorkBasicConverter
+

File src/test/com/opensymphony/xwork/util/XWorkConverterTest.java

 import junit.framework.TestCase;
 
 import ognl.Ognl;
+import ognl.OgnlException;
 
 import java.util.ArrayList;
 import java.util.Arrays;
         list.add("foo");
         list.add("bar");
         list.add("baz");
-        assertEquals(list, converter.doConvert(context, new String[] {
-                    "foo", "bar", "baz"
-                }, Collection.class));
+        assertEquals(list, converter.convertValue(context, new String[]{
+            "foo", "bar", "baz"
+        }, null, null, null, Collection.class));
     }
 
     public void testStringArrayToList() {
         list.add("foo");
         list.add("bar");
         list.add("baz");
-        assertEquals(list, converter.doConvert(context, new String[] {
-                    "foo", "bar", "baz"
-                }, List.class));
+        assertEquals(list, converter.convertValue(context, new String[]{
+            "foo", "bar", "baz"
+        }, null, null, null, List.class));
     }
 
     public void testStringArrayToPrimitiveWrappers() {
-        Long[] longs = (Long[]) converter.doConvert(context, new String[] {
-                "123", "456"
-            }, Long[].class);
+        Long[] longs = (Long[]) converter.convertValue(context, new String[]{
+            "123", "456"
+        }, null, null, null, Long[].class);
         assertNotNull(longs);
-        assertTrue(Arrays.equals(new Long[] {new Long(123), new Long(456)}, longs));
+        assertTrue(Arrays.equals(new Long[]{new Long(123), new Long(456)}, longs));
 
-        Integer[] ints = (Integer[]) converter.doConvert(context, new String[] {
-                "123", "456"
-            }, Integer[].class);
+        Integer[] ints = (Integer[]) converter.convertValue(context, new String[]{
+            "123", "456"
+        }, null, null, null, Integer[].class);
         assertNotNull(ints);
-        assertTrue(Arrays.equals(new Integer[] {
-                    new Integer(123), new Integer(456)
-                }, ints));
+        assertTrue(Arrays.equals(new Integer[]{
+            new Integer(123), new Integer(456)
+        }, ints));
 
-        Double[] doubles = (Double[]) converter.doConvert(context, new String[] {
-                "123", "456"
-            }, Double[].class);
+        Double[] doubles = (Double[]) converter.convertValue(context, new String[]{
+            "123", "456"
+        }, null, null, null, Double[].class);
         assertNotNull(doubles);
-        assertTrue(Arrays.equals(new Double[] {new Double(123), new Double(456)}, doubles));
+        assertTrue(Arrays.equals(new Double[]{new Double(123), new Double(456)}, doubles));
     }
 
-    public void testStringArrayToPrimitives() {
-        long[] longs = (long[]) converter.doConvert(context, new String[] {
-                "123", "456"
-            }, long[].class);
+    public void testStringArrayToPrimitives() throws OgnlException {
+        long[] longs = (long[]) converter.convertValue(context, new String[]{
+            "123", "456"
+        }, null, null, null, long[].class);
         assertNotNull(longs);
-        assertTrue(Arrays.equals(new long[] {123, 456}, longs));
+        assertTrue(Arrays.equals(new long[]{123, 456}, longs));
 
-        int[] ints = (int[]) converter.doConvert(context, new String[] {
-                "123", "456"
-            }, int[].class);
+        int[] ints = (int[]) converter.convertValue(context, new String[]{
+            "123", "456"
+        }, null, null, null, int[].class);
         assertNotNull(ints);
-        assertTrue(Arrays.equals(new int[] {123, 456}, ints));
+        assertTrue(Arrays.equals(new int[]{123, 456}, ints));
 
-        double[] doubles = (double[]) converter.doConvert(context, new String[] {
-                "123", "456"
-            }, double[].class);
+        double[] doubles = (double[]) converter.convertValue(context, new String[]{
+            "123", "456"
+        }, null, null, null, double[].class);
         assertNotNull(doubles);
-        assertTrue(Arrays.equals(new double[] {123, 456}, doubles));
+        assertTrue(Arrays.equals(new double[]{123, 456}, doubles));
     }
 
     public void testStringToPrimitiveWrappers() {
-        assertEquals(new Long(123), converter.doConvert(context, "123", Long.class));
-        assertEquals(new Integer(123), converter.doConvert(context, "123", Integer.class));
-        assertEquals(new Double(123.5), converter.doConvert(context, "123.5", Double.class));
+        assertEquals(new Long(123), converter.convertValue(context, "123", null, null, null, Long.class));
+        assertEquals(new Integer(123), converter.convertValue(context, "123", null, null, null, Integer.class));
+        assertEquals(new Double(123.5), converter.convertValue(context, "123.5", null, null, null, Double.class));
     }
 
     public void testStringToPrimitives() {
-        assertEquals(new Long(123), converter.doConvert(context, "123", long.class));
-        assertEquals(new Integer(123), converter.doConvert(context, "123", int.class));
-        assertEquals(new Double(123.5), converter.doConvert(context, "123.5", double.class));
+        assertEquals(new Long(123), converter.convertValue(context, "123", null, null, null, long.class));
+        assertEquals(new Integer(123), converter.convertValue(context, "123", null, null, null, int.class));
+        assertEquals(new Double(123.5), converter.convertValue(context, "123.5", null, null, null, double.class));
     }
 
     protected void setUp() throws Exception {