Anonymous avatar Anonymous committed 4629a84

Fixing type conversion

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

Comments (0)

Files changed (6)

java/com/opensymphony/xwork/XWorkBasicConverter.java

+/*
+ * Copyright (c) 2002-2003 by OpenSymphony
+ * All rights reserved.
+ */
 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 $Author$
  * @version $Revision$
  */
-
 public class XWorkBasicConverter extends DefaultTypeConverter {
-    public Object convertValue(Map context, Object value, Member member, String s, Object o1, Class toType) {
+    //~ Methods ////////////////////////////////////////////////////////////////
+
+    public Object convertValue(Map context, Object o, Member member, String s, Object value, Class toType) {
         Object result = null;
 
         if (toType == String.class) {
             result = doConvertToClass(context, value);
         } else {
             if ((toType == String[].class) && (value instanceof String)) {
-                result = new String[]{(String) value};
+                result = new String[] {(String) value};
             }
         }
 
 
         return result;
     }
-
 }

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

 
 import java.io.IOException;
 import java.io.InputStream;
+
 import java.lang.reflect.Member;
+
 import java.util.*;
 
 
 
     //~ Instance fields ////////////////////////////////////////////////////////
 
+    HashMap defaultMappings = new HashMap();
     HashMap mappings = new HashMap();
     HashSet noMapping = new HashSet();
-    HashMap defaultMappings = new HashMap();
 
     //~ Constructors ///////////////////////////////////////////////////////////
 
         }
     }
 
-    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 ////////////////////////////////////////////////////////////////
 
     public static XWorkConverter getInstance() {
         instance = null;
     }
 
-    public Object convertValue(Map context, Object value, Member member, String s, Object o1, Class aClass) {
+    public Object convertValue(Map context, Object o, Member member, String s, Object value, Class aClass) {
         if (value == null) {
             return null;
         }
 
         Class clazz = null;
+
         try {
             OgnlContext ognlContext = (OgnlContext) context;
             Evaluation eval = ognlContext.getCurrentEvaluation();
                     mapping.putAll(props);
 
                     for (Iterator iterator = mapping.entrySet().iterator();
-                         iterator.hasNext();) {
+                            iterator.hasNext();) {
                         Map.Entry entry = (Map.Entry) iterator.next();
                         entry.setValue(createTypeConverter((String) entry.getValue()));
                     }
                 TypeConverter converter = (TypeConverter) mapping.get(property);
 
                 if (converter != null) {
-                    return converter.convertValue(context, value, member, s, o1, aClass);
+                    return converter.convertValue(context, o, member, s, value, aClass);
                 }
             }
         } catch (Throwable t) {
         if (defaultMappings.containsKey(aClass.getName())) {
             try {
                 TypeConverter tc = (TypeConverter) defaultMappings.get(aClass.getName());
-                return tc.convertValue(context, value, member, s, o1, aClass);
+
+                return tc.convertValue(context, o, member, s, value, aClass);
             } catch (Exception e) {
                 e.printStackTrace();
             }
         }
 
-
-        return super.convertValue(context, value, member, s, o1, aClass);
+        return super.convertValue(context, o, member, s, value, aClass);
     }
 
     private TypeConverter createTypeConverter(String className) throws Exception, InstantiationException {
         return (TypeConverter) conversionClass.newInstance();
     }
 
+    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()));
+        }
+    }
 }

test/com/opensymphony/xwork/config/providers/XmlConfigurationProviderResultsTest.java

         results.put("mockTypedResult", new ResultConfig("mockTypedResult", MockResult.class, new HashMap()));
 
         Map resultParams = new HashMap();
-        resultParams.put("location", "bar.vm");
+        resultParams.put("actionName", "bar.vm");
         results.put("specificLocationResult", new ResultConfig("specificLocationResult", ActionChainResult.class, resultParams));
 
         resultParams = new HashMap();
-        resultParams.put("location", "foo.vm");
+        resultParams.put("actionName", "foo.vm");
         results.put("defaultLocationResult", new ResultConfig("defaultLocationResult", ActionChainResult.class, resultParams));
 
         resultParams = new HashMap();

test/com/opensymphony/xwork/config/providers/xwork-test-results.xml

         </result-types>
 
         <global-results>
-            <result name="login"> <!-- should be chain type since it is the default -->
-                <param name="actionName">login</param>
-            </result>
+            <!-- should be chain type since it is the default -->
+            <result name="login">login</result>
         </global-results>
 
         <action name="Bar" class="com.opensymphony.xwork.SimpleAction">
             <result name="chainDefaultTypedResult" />
             <result name="mockTypedResult" type="mock" />
             <result name="specificLocationResult">
-                <param name="location">bar.vm</param>
+                <param name="actionName">bar.vm</param>
             </result>
             <result name="defaultLocationResult">foo.vm</result>
             <result name="noDefaultLocationResult"> <!-- this will have only one param, named foo -->

test/com/opensymphony/xwork/test/SimpleAction2-validationAlias-validation.xml

             <message>You must enter a value for baz.</message>
         </field-validator>
         <field-validator type="int">
-            <param name="min">2"</param>
-            <param name="max">4"</param>
+            <param name="min">2</param>
+            <param name="max">4</param>
             <message>baz out of range.</message>
         </field-validator>
     </field>

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

         list.add("foo");
         list.add("bar");
         list.add("baz");
-        assertEquals(list, converter.convertValue(context, new String[]{
-            "foo", "bar", "baz"
-        }, null, null, null, Collection.class));
+        assertEquals(list, converter.convertValue(context, null, null, null, new String[] {
+                    "foo", "bar", "baz"
+                }, Collection.class));
     }
 
     public void testStringArrayToList() {
         list.add("foo");
         list.add("bar");
         list.add("baz");
-        assertEquals(list, converter.convertValue(context, new String[]{
-            "foo", "bar", "baz"
-        }, null, null, null, List.class));
+        assertEquals(list, converter.convertValue(context, null, null, null, new String[] {
+                    "foo", "bar", "baz"
+                }, List.class));
     }
 
     public void testStringArrayToPrimitiveWrappers() {
-        Long[] longs = (Long[]) converter.convertValue(context, new String[]{
-            "123", "456"
-        }, null, null, null, Long[].class);
+        Long[] longs = (Long[]) converter.convertValue(context, null, null, null, new String[] {
+                "123", "456"
+            }, 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.convertValue(context, new String[]{
-            "123", "456"
-        }, null, null, null, Integer[].class);
+        Integer[] ints = (Integer[]) converter.convertValue(context, null, null, null, new String[] {
+                "123", "456"
+            }, 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.convertValue(context, new String[]{
-            "123", "456"
-        }, null, null, null, Double[].class);
+        Double[] doubles = (Double[]) converter.convertValue(context, null, null, null, new String[] {
+                "123", "456"
+            }, 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() throws OgnlException {
-        long[] longs = (long[]) converter.convertValue(context, new String[]{
-            "123", "456"
-        }, null, null, null, long[].class);
+        long[] longs = (long[]) converter.convertValue(context, null, null, null, new String[] {
+                "123", "456"
+            }, long[].class);
         assertNotNull(longs);
-        assertTrue(Arrays.equals(new long[]{123, 456}, longs));
+        assertTrue(Arrays.equals(new long[] {123, 456}, longs));
 
-        int[] ints = (int[]) converter.convertValue(context, new String[]{
-            "123", "456"
-        }, null, null, null, int[].class);
+        int[] ints = (int[]) converter.convertValue(context, null, null, null, new String[] {
+                "123", "456"
+            }, int[].class);
         assertNotNull(ints);
-        assertTrue(Arrays.equals(new int[]{123, 456}, ints));
+        assertTrue(Arrays.equals(new int[] {123, 456}, ints));
 
-        double[] doubles = (double[]) converter.convertValue(context, new String[]{
-            "123", "456"
-        }, null, null, null, double[].class);
+        double[] doubles = (double[]) converter.convertValue(context, null, null, null, new String[] {
+                "123", "456"
+            }, 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.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));
+        assertEquals(new Long(123), converter.convertValue(context, null, null, null, "123", Long.class));
+        assertEquals(new Integer(123), converter.convertValue(context, null, null, null, "123", Integer.class));
+        assertEquals(new Double(123.5), converter.convertValue(context, null, null, null, "123.5", Double.class));
     }
 
     public void testStringToPrimitives() {
-        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));
+        assertEquals(new Long(123), converter.convertValue(context, null, null, null, "123", long.class));
+        assertEquals(new Integer(123), converter.convertValue(context, null, null, null, "123", int.class));
+        assertEquals(new Double(123.5), converter.convertValue(context, null, null, null, "123.5", double.class));
     }
 
     protected void setUp() throws Exception {
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.