You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@turbine.apache.org by tv...@apache.org on 2008/04/10 12:55:08 UTC

svn commit: r646751 - in /turbine/core/branches/TURBINE_2_3_BRANCH: src/java/org/apache/turbine/util/parser/BaseValueParser.java src/java/org/apache/turbine/util/parser/ValueParser.java xdocs/changes.xml

Author: tv
Date: Thu Apr 10 03:55:06 2008
New Revision: 646751

URL: http://svn.apache.org/viewvc?rev=646751&view=rev
Log:
Added getBooleans() and getBooleanObjects() to the ValueParser to provide a "more orthogonal" interface.

Modified:
    turbine/core/branches/TURBINE_2_3_BRANCH/src/java/org/apache/turbine/util/parser/BaseValueParser.java
    turbine/core/branches/TURBINE_2_3_BRANCH/src/java/org/apache/turbine/util/parser/ValueParser.java
    turbine/core/branches/TURBINE_2_3_BRANCH/xdocs/changes.xml

Modified: turbine/core/branches/TURBINE_2_3_BRANCH/src/java/org/apache/turbine/util/parser/BaseValueParser.java
URL: http://svn.apache.org/viewvc/turbine/core/branches/TURBINE_2_3_BRANCH/src/java/org/apache/turbine/util/parser/BaseValueParser.java?rev=646751&r1=646750&r2=646751&view=diff
==============================================================================
--- turbine/core/branches/TURBINE_2_3_BRANCH/src/java/org/apache/turbine/util/parser/BaseValueParser.java (original)
+++ turbine/core/branches/TURBINE_2_3_BRANCH/src/java/org/apache/turbine/util/parser/BaseValueParser.java Thu Apr 10 03:55:06 2008
@@ -90,6 +90,12 @@
     /** Logging */
     private static Log log = LogFactory.getLog(BaseValueParser.class);
 
+    /** String values which would evaluate to Boolean.TRUE */
+    private static String[] trueValues = {"TRUE","T","YES","Y","1","ON"};
+
+    /** String values which would evaluate to Boolean.FALSE */
+    private static String[] falseValues = {"FALSE","F","NO","N","0","OFF"};
+
     /**
      * Random access storage for parameter data.  The keys must always be
      * Strings.  The values will be arrays of Strings.
@@ -443,6 +449,57 @@
     {
         return keySet().toArray();
     }
+    
+    /**
+     * Returns a Boolean object for the given string. If the value
+     * can not be parsed as a boolean, null is returned.
+     * <p>
+     * Valid values for true: true, t, on, 1, yes, y<br>
+     * Valid values for false: false, f, off, 0, no, n<br>
+     * <p>
+     * The string is compared without reguard to case.
+     *
+     * @param string A String with the value.
+     * @return A Boolean.
+     */
+    private Boolean parseBoolean(String string)
+    {
+        Boolean result = null;
+        String value = StringUtils.trim(string);
+        
+        if (StringUtils.isNotEmpty(value))
+        {
+            for (int cnt = 0;
+            cnt < Math.max(trueValues.length, falseValues.length); cnt++)
+            {
+                // Short-cut evaluation or bust!
+                if ((cnt < trueValues.length) &&
+                   value.equalsIgnoreCase(trueValues[cnt]))
+                {
+                    result = Boolean.TRUE;
+                    break;
+                }
+
+                if ((cnt < falseValues.length) &&
+                   value.equalsIgnoreCase(falseValues[cnt]))
+                {
+                    result = Boolean.FALSE;
+                    break;
+                }
+            }
+            
+            if (result == null)
+            {
+                if (log.isWarnEnabled())
+                {
+                    log.warn("Parameter with value of ("
+                            + value + ") could not be converted to a Boolean");
+                }
+            }
+        }
+        
+        return result;
+    }
 
     /**
      * Return a boolean for the given name.  If the name does not
@@ -471,6 +528,29 @@
     }
 
     /**
+     * Return an array of booleans for the given name.  If the name does
+     * not exist, return null.
+     *
+     * @param name A String with the name.
+     * @return A boolean[].
+     */
+    public boolean[] getBooleans(String name)
+    {
+        boolean[] result = null;
+        String value[] = getParam(name);
+        if (value != null)
+        {
+            result = new boolean[value.length];
+            for (int i = 0; i < value.length; i++)
+            {
+                Boolean bool = parseBoolean(value[i]);
+                result[i] = (bool == null ? false : bool.booleanValue());
+            }
+        }
+        return result;
+    }
+    
+    /**
      * Returns a Boolean object for the given name.  If the parameter
      * does not exist or can not be parsed as a boolean, null is returned.
      * <p>
@@ -484,31 +564,7 @@
      */
     public Boolean getBooleanObject(String name)
     {
-        Boolean result = null;
-        String value = getString(name);
-
-        if (StringUtils.isNotEmpty(value))
-        {
-            if (value.equals("1") ||
-                    value.equalsIgnoreCase("true") ||
-                    value.equalsIgnoreCase("yes") ||
-                    value.equalsIgnoreCase("on"))
-            {
-                result = Boolean.TRUE;
-            }
-            else if (value.equals("0") ||
-                    value.equalsIgnoreCase("false") ||
-                    value.equalsIgnoreCase("no") ||
-                    value.equalsIgnoreCase("off"))
-            {
-                result = Boolean.FALSE;
-            }
-            else
-            {
-                logConvertionFailure(name, value, "Boolean");
-            }
-        }
-        return result;
+        return parseBoolean(getString(name));
     }
 
     /**
@@ -556,6 +612,28 @@
     public Boolean getBool(String name)
     {
         return getBooleanObject(name, Boolean.FALSE);
+    }
+
+    /**
+     * Return an array of Booleans for the given name.  If the name does
+     * not exist, return null.
+     *
+     * @param name A String with the name.
+     * @return A Boolean[].
+     */
+    public Boolean[] getBooleanObjects(String name)
+    {
+        Boolean[] result = null;
+        String value[] = getParam(name);
+        if (value != null)
+        {
+            result = new Boolean[value.length];
+            for (int i = 0; i < value.length; i++)
+            {
+                result[i] = parseBoolean(value[i]);
+            }
+        }
+        return result;
     }
 
     /**

Modified: turbine/core/branches/TURBINE_2_3_BRANCH/src/java/org/apache/turbine/util/parser/ValueParser.java
URL: http://svn.apache.org/viewvc/turbine/core/branches/TURBINE_2_3_BRANCH/src/java/org/apache/turbine/util/parser/ValueParser.java?rev=646751&r1=646750&r2=646751&view=diff
==============================================================================
--- turbine/core/branches/TURBINE_2_3_BRANCH/src/java/org/apache/turbine/util/parser/ValueParser.java (original)
+++ turbine/core/branches/TURBINE_2_3_BRANCH/src/java/org/apache/turbine/util/parser/ValueParser.java Thu Apr 10 03:55:06 2008
@@ -318,6 +318,24 @@
     Boolean getBooleanObject(String name, Boolean defaultValue);
 
     /**
+     * Return an array of booleans for the given name.  If the name does
+     * not exist, return null.
+     *
+     * @param name A String with the name.
+     * @return A boolean[].
+     */
+    boolean[] getBooleans(String name);
+
+    /**
+     * Return an array of Booleans for the given name.  If the name does
+     * not exist, return null.
+     *
+     * @param name A String with the name.
+     * @return A Boolean[].
+     */
+    Boolean[] getBooleanObjects(String name);
+
+    /**
      * Return a double for the given name.  If the name does not
      * exist, return defaultValue.
      *

Modified: turbine/core/branches/TURBINE_2_3_BRANCH/xdocs/changes.xml
URL: http://svn.apache.org/viewvc/turbine/core/branches/TURBINE_2_3_BRANCH/xdocs/changes.xml?rev=646751&r1=646750&r2=646751&view=diff
==============================================================================
--- turbine/core/branches/TURBINE_2_3_BRANCH/xdocs/changes.xml (original)
+++ turbine/core/branches/TURBINE_2_3_BRANCH/xdocs/changes.xml Thu Apr 10 03:55:06 2008
@@ -29,6 +29,13 @@
 <body>
   <release version="2.3.3-dev" date="in Subversion">
     <action type="update" dev="tv">
+      Further simplify the value assignment in Intake fields.
+    </action>
+    <action type="add" dev="tv">
+      Added getBooleans() and getBooleanObjects() to the ValueParser to provide
+      a "more orthogonal" interface.
+    </action>
+    <action type="update" dev="tv">
       Use the localize features of the parser to simplify the value assignment
       in Intake fields. Made handling of empty values consistent over all number
       field types.