Anonymous avatar Anonymous committed ea1d998

Fix for XW-172, use locale when converting numbers.

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

Comments (0)

Files changed (2)

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

 import java.lang.reflect.Member;
 
 import java.text.DateFormat;
+import java.text.NumberFormat;
 import java.text.ParseException;
 
 import java.util.*;
             result = doConvertToDate(context, value);
         } else if (Collection.class.isAssignableFrom(toType)) {
             result = doConvertToCollection(context, o, member, s, value, toType);
+        } else if (Number.class.isAssignableFrom(toType)) {
+            result = doConvertToNumber(context, value, toType);
         } else if (toType == Class.class) {
             result = doConvertToClass(value);
         }
         Class memberType = String.class;
 
         if (o != null) {
-            memberType = (Class) XWorkConverter.getInstance().getConverter(o.getClass(), "Collection_" + prop);
+            memberType = (Class) XWorkConverter.getInstance().getConverter(o.getClass(), XWorkConverter.CONVERSION_COLLECTION_PREFIX + prop);
 
             if (memberType == null) {
                 memberType = String.class;
         return result;
     }
 
+    private Object doConvertToNumber(Map context, Object value, Class toType) {
+        if (value instanceof String) {
+            NumberFormat numFormat = NumberFormat.getInstance(getLocale(context));
+
+            try {
+                // convert it to a Number
+                value = super.convertValue(context, numFormat.parse((String) value), toType);
+            } catch (ParseException ex) {
+                // ignore parse failure and hope default behavior works
+            }
+        }
+
+        // pass it through DefaultTypeConverter
+        return super.convertValue(context, value, toType);
+    }
+
     private String doConvertToString(Map context, Object value) {
         String result = null;
 
             result = df.format(value);
         } else if (value instanceof String[]) {
             result = TextUtils.join(", ", (String[]) value);
+        } else if (Number.class.isAssignableFrom(value.getClass())) {
+            NumberFormat numFormat = NumberFormat.getInstance(getLocale(context));
+            result = numFormat.format(value);
         }
 
         return result;

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

 import java.math.BigInteger;
 
 import java.text.DateFormat;
+import java.text.NumberFormat;
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
 
         assertEquals(dateString, converter.convertValue(context, null, null, null, date, String.class));
     }
 
+    public void testPrimitiveToString() {
+        Locale locale = Locale.GERMANY;
+        NumberFormat nf = NumberFormat.getInstance(locale);
+        context.put(ActionContext.LOCALE, locale);
+
+        String origValue = "123456789";
+        Long longValue = new Long(origValue);
+        String formattedValue = nf.format(new Long(origValue));
+        assertEquals(formattedValue, converter.convertValue(context, null, null, null, new Long(origValue), String.class));
+        assertEquals(longValue, converter.convertValue(context, null, null, null, formattedValue, Long.class));
+        assertEquals(longValue, converter.convertValue(context, null, null, null, origValue, Long.class));
+
+        origValue = "123456.789";
+        Float floatValue = new Float(origValue);
+        formattedValue = nf.format(new Float(origValue));
+        assertEquals(formattedValue, converter.convertValue(context, null, null, null, new Float(origValue), String.class));
+        assertEquals(floatValue, converter.convertValue(context, null, null, null, formattedValue, Float.class));
+    }
+
     public void testStringArrayToCollection() {
         ArrayList list = new ArrayList();
         list.add("foo");
         // because the Foo-conversion.properties file is only used when converting a property of Foo
         converter.registerConverter(Bar.class.getName(), new FooBarConverter());
 
-        Bar bar = null;
-
-        bar = (Bar) converter.convertValue(null, null, null, null, "blah:123", Bar.class);
+        Bar bar = (Bar) converter.convertValue(null, null, null, null, "blah:123", Bar.class);
         assertNotNull("conversion failed", bar);
         assertEquals(123, bar.getSomethingElse());
         assertEquals("blah", bar.getTitle());
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.