You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@velocity.apache.org by nb...@apache.org on 2007/08/28 20:19:46 UTC

svn commit: r570515 - in /velocity/tools/branches/2.x/src: main/java/org/apache/velocity/tools/config/Data.java test/java/org/apache/velocity/tools/test/whitebox/ConfigTests.java

Author: nbubna
Date: Tue Aug 28 11:19:45 2007
New Revision: 570515

URL: http://svn.apache.org/viewvc?rev=570515&view=rev
Log:
make auto-conversion support numbers (double,integer&long), fields, and more booleans

Modified:
    velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/config/Data.java
    velocity/tools/branches/2.x/src/test/java/org/apache/velocity/tools/test/whitebox/ConfigTests.java

Modified: velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/config/Data.java
URL: http://svn.apache.org/viewvc/velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/config/Data.java?rev=570515&r1=570514&r2=570515&view=diff
==============================================================================
--- velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/config/Data.java (original)
+++ velocity/tools/branches/2.x/src/main/java/org/apache/velocity/tools/config/Data.java Tue Aug 28 11:19:45 2007
@@ -361,21 +361,38 @@
             // only bother with strings for now
             if (obj instanceof String)
             {
-                String value = (String)obj;
-                //TODO: start using regexps here
-                if (value.equalsIgnoreCase("true"))
+                try
                 {
-                    return Boolean.TRUE;
+                    return convert((String)obj);
+                    
                 }
-                else if (value.equalsIgnoreCase("false"))
+                catch (Exception e)
                 {
-                    return Boolean.FALSE;
+                    return obj;
                 }
-                //TODO: handle other types
-                return value;
             }
             return obj;
         }
+
+        public Object convert(String value)
+        {
+            // check if this looks like a typical boolean type
+            if (value.matches("true|false|yes|no|y|n|on|off"))
+            {
+                return new BooleanConverter().convert(Boolean.class, value);
+            }
+            // check if this looks like a typical number
+            else if (value.matches("-?[0-9]+(\\.[0-9]+)?"))
+            {
+                return new NumberConverter().convert(Number.class, value);
+            }
+            // check if this looks like a typical field
+            else if (value.matches("(\\w+\\.)+\\w+"))
+            {
+                return new FieldConverter().convert(Object.class, value);
+            }
+            return value;
+        }
     }
 
     protected static class NumberConverter implements Converter
@@ -395,7 +412,16 @@
                 // now, let's return integers for integer values
                 else if (value.indexOf('.') < 0)
                 {
-                    num = Integer.valueOf(num.intValue());
+                    // unless, of course, we need a long
+                    if (num.doubleValue() > Integer.MAX_VALUE ||
+                        num.doubleValue() < Integer.MIN_VALUE)
+                    {
+                        num = Long.valueOf(num.longValue());
+                    }
+                    else
+                    {
+                        num = Integer.valueOf(num.intValue());
+                    }
                 }
             }
             return num;

Modified: velocity/tools/branches/2.x/src/test/java/org/apache/velocity/tools/test/whitebox/ConfigTests.java
URL: http://svn.apache.org/viewvc/velocity/tools/branches/2.x/src/test/java/org/apache/velocity/tools/test/whitebox/ConfigTests.java?rev=570515&r1=570514&r2=570515&view=diff
==============================================================================
--- velocity/tools/branches/2.x/src/test/java/org/apache/velocity/tools/test/whitebox/ConfigTests.java (original)
+++ velocity/tools/branches/2.x/src/test/java/org/apache/velocity/tools/test/whitebox/ConfigTests.java Tue Aug 28 11:19:45 2007
@@ -219,10 +219,37 @@
         datum.setValue("true");
         assertValid(datum);
 
+        // check that the default type is set
+        assertSame(datum.getType(), Data.DEFAULT_TYPE);
+
+        // check that "true" auto-converted to boolean
+        assertSame(datum.getConvertedValue(), Boolean.TRUE);
+
+        // check that "1" auto-converts to integer
+        datum.setValue("1");
+        assertEquals(datum.getConvertedValue(), Integer.valueOf(1));
+
+        // check that a really big number converts to a Long
+        datum.setValue(""+Integer.MAX_VALUE+"0");
+        assertEquals(datum.getConvertedValue().getClass(), Long.class);
+
+        // check that "1.2" auto-converts to double
+        datum.setValue("1.2");
+        assertEquals(datum.getConvertedValue(), 1.2);
+        assertEquals(datum.getConvertedValue().getClass(), Double.class);
+
+        // check that java.lang.Integer.MIN_VALUE auto-converts to the field
+        datum.setValue("java.lang.Integer.MIN_VALUE");
+        assertEquals(datum.getConvertedValue(), Integer.MIN_VALUE);
+
+        // check that "yes" also auto-converts to boolean
+        datum.setValue("yes");
+        assertSame(datum.getConvertedValue(), Boolean.TRUE);
+
         // check boolean type
         datum.setType("boolean");
         assertValid(datum);
-        assertEquals(datum.getConvertedValue(), Boolean.TRUE);
+        assertSame(datum.getConvertedValue(), Boolean.TRUE);
 
         // check valid field type
         datum.setValue("java.lang.Boolean.TRUE");