You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@click.apache.org by bc...@apache.org on 2010/08/17 21:52:24 UTC

svn commit: r986467 - in /click/trunk/click/framework/src: ognl/ org/apache/click/util/ContainerUtils.java

Author: bckfnn
Date: Tue Aug 17 19:52:24 2010
New Revision: 986467

URL: http://svn.apache.org/viewvc?rev=986467&view=rev
Log:
remove OgnlOps, implement the double->bigdecimal fix in a typeconverter.

Removed:
    click/trunk/click/framework/src/ognl/
Modified:
    click/trunk/click/framework/src/org/apache/click/util/ContainerUtils.java

Modified: click/trunk/click/framework/src/org/apache/click/util/ContainerUtils.java
URL: http://svn.apache.org/viewvc/click/trunk/click/framework/src/org/apache/click/util/ContainerUtils.java?rev=986467&r1=986466&r2=986467&view=diff
==============================================================================
--- click/trunk/click/framework/src/org/apache/click/util/ContainerUtils.java (original)
+++ click/trunk/click/framework/src/org/apache/click/util/ContainerUtils.java Tue Aug 17 19:52:24 2010
@@ -20,6 +20,8 @@ package org.apache.click.util;
 
 import java.lang.reflect.Constructor;
 import java.lang.reflect.Method;
+import java.math.BigDecimal;
+import java.math.BigInteger;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
@@ -27,6 +29,10 @@ import java.util.Map;
 import java.util.Set;
 import java.util.TreeSet;
 
+import ognl.DefaultTypeConverter;
+import ognl.Ognl;
+import ognl.OgnlOps;
+
 import org.apache.click.Control;
 import org.apache.click.Page;
 import org.apache.click.control.Button;
@@ -129,7 +135,8 @@ public class ContainerUtils {
         LogService logService = ClickUtils.getLogService();
 
         Set<String> properties = getObjectPropertyNames(object);
-        Map<?, ?> ognlContext = new HashMap<Object, Object>();
+        Map<?, ?> ognlContext = Ognl.createDefaultContext(
+                object, null, new FixBigDecimalTypeConverter(), null);
 
         for (Field field : fieldList) {
 
@@ -170,7 +177,7 @@ public class ContainerUtils {
             }
         }
     }
-
+    
     /**
      * Populate the given object attributes from the Containers field values.
      *
@@ -1374,4 +1381,39 @@ public class ContainerUtils {
 
         ClickUtils.getLogService().warn(message);
     }
+    
+    /**
+     * This class fix an error in ognl's conversion of double->BigDecimal. The 
+     * default conversion uses BigDecimal(double), the fix is to use 
+     * BigDecimal.valueOf(double)
+     *
+     */
+    private static class FixBigDecimalTypeConverter extends DefaultTypeConverter {
+        @SuppressWarnings("unchecked")
+        @Override
+        public Object convertValue(Map context, Object value, Class toType) {
+            if (value != null && toType == BigDecimal.class) {
+                return bigDecValue(value);
+            }
+            return OgnlOps.convertValue(value, toType);
+        }
+        
+        private BigDecimal bigDecValue( Object value ) {
+            if (value == null)
+                return BigDecimal.valueOf(0L);
+            Class<?> c = value.getClass();
+            if ( c == BigDecimal.class )
+                return (BigDecimal)value;
+            if ( c == BigInteger.class )
+                return new BigDecimal( (BigInteger)value );
+            if ( c.getSuperclass() == Number.class )
+                //return new BigDecimal( ((Number)value).doubleValue() );
+                return BigDecimal.valueOf(((Number)value).doubleValue());
+            if ( c == Boolean.class )
+                return BigDecimal.valueOf(((Boolean)value).booleanValue()? 1 : 0 );
+            if ( c == Character.class )
+                return BigDecimal.valueOf(((Character)value).charValue() );
+            return new BigDecimal(value.toString().trim());
+        }
+    }
 }