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/03/21 20:21:58 UTC

svn commit: r639770 - in /turbine/core/branches/TURBINE_2_3_BRANCH: src/java/org/apache/turbine/util/parser/ src/test/org/apache/turbine/util/parser/ xdocs/

Author: tv
Date: Fri Mar 21 12:21:55 2008
New Revision: 639770

URL: http://svn.apache.org/viewvc?rev=639770&view=rev
Log:
Made ValueParser and BaseValueParser locale-aware. Note that the default locale used is always the default value of the JVM which is different from the previous behaviour where the locale used was sometimes Locale.US (for float, double and BigDecimal) and sometimes the JVM-default (for dates).

TODO: Integration into RunData

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/src/test/org/apache/turbine/util/parser/BaseValueParserTest.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=639770&r1=639769&r2=639770&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 Fri Mar 21 12:21:55 2008
@@ -26,6 +26,7 @@
 import java.lang.reflect.Method;
 import java.math.BigDecimal;
 import java.text.DateFormat;
+import java.text.NumberFormat;
 import java.text.ParseException;
 import java.util.Calendar;
 import java.util.Collections;
@@ -34,6 +35,7 @@
 import java.util.GregorianCalendar;
 import java.util.HashMap;
 import java.util.Iterator;
+import java.util.Locale;
 import java.util.Map;
 import java.util.Set;
 
@@ -96,6 +98,15 @@
     /** The character encoding to use when converting to byte arrays */
     private String characterEncoding = TurbineConstants.PARAMETER_ENCODING_DEFAULT;
 
+    /** The locale to use when converting dates, floats and decimals */
+    private Locale locale = Locale.getDefault();
+    
+    /** The DateFormat to use for converting dates */
+    private DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.SHORT, locale);
+    
+    /** The NumberFormat to use when converting floats and decimals */
+    private NumberFormat numberFormat = NumberFormat.getNumberInstance(locale);
+    
     /**
      * A static version of the convert method, which
      * trims the string data and applies the conversion specified in
@@ -124,8 +135,17 @@
      */
     public BaseValueParser(String characterEncoding)
     {
+        this(characterEncoding, Locale.getDefault());
+    }
+
+    /**
+     * Constructor that takes a character encoding and a locale
+     */
+    public BaseValueParser(String characterEncoding, Locale locale)
+    {
         super();
         setCharacterEncoding(characterEncoding);
+        setLocale(locale);
     }
 
     /**
@@ -175,6 +195,56 @@
     }
 
     /**
+     * Set the locale that will be used by this ValueParser.
+     */
+    public void setLocale(Locale l)
+    {
+        locale = l;
+        setDateFormat(DateFormat.getDateInstance(DateFormat.SHORT, locale));
+        setNumberFormat(NumberFormat.getNumberInstance(locale));
+    }
+    
+    /**
+     * Get the locale that will be used by this ValueParser.
+     */
+    public Locale getLocale()
+    {
+        return locale;
+    }
+
+    /**
+     * Set the date format that will be used by this ValueParser.
+     */
+    public void setDateFormat(DateFormat df)
+    {
+        dateFormat = df;
+    }
+
+    /**
+     * Get the date format that will be used by this ValueParser.
+     */
+    public DateFormat getDateFormat()
+    {
+        return dateFormat;
+    }
+
+    /**
+     * Set the number format that will be used by this ValueParser.
+     */
+    public void setNumberFormat(NumberFormat nf)
+    {
+        numberFormat = nf;
+    }
+
+    /**
+     * Get the number format that will be used by this ValueParser.
+     */
+    public NumberFormat getNumberFormat()
+    {
+        return numberFormat;
+    }
+    
+    /**
      * Add a name/value pair into this object.
      *
      * @param name A String with the name.
@@ -182,7 +252,7 @@
      */
     public void add(String name, double value)
     {
-        add(name, Double.toString(value));
+        add(name, numberFormat.format(value));
     }
 
     /**
@@ -498,19 +568,20 @@
     public double getDouble(String name, double defaultValue)
     {
         double result = defaultValue;
-        String value = getString(name);
+        String value = StringUtils.trim(getString(name));
 
         if (StringUtils.isNotEmpty(value))
         {
             try
             {
-                result = Double.parseDouble(StringUtils.trim(value));
+                result = numberFormat.parse(value).doubleValue();
             }
-            catch (NumberFormatException e)
+            catch (ParseException e)
             {
                 logConvertionFailure(name, value, "Double");
             }
         }
+        
         return result;
     }
 
@@ -542,15 +613,17 @@
             result = new double[value.length];
             for (int i = 0; i < value.length; i++)
             {
-                if (StringUtils.isNotEmpty(value[i]))
+                String v = StringUtils.trim(value[i]);
+                
+                if (StringUtils.isNotEmpty(v))
                 {
                     try
                     {
-                        result[i] = Double.parseDouble(value[i]);
+                        result[i] = numberFormat.parse(v).doubleValue();
                     }
-                    catch (NumberFormatException e)
+                    catch (ParseException e)
                     {
-                        logConvertionFailure(name, value[i], "Double");
+                        logConvertionFailure(name, v, "Double");
                     }
                 }
             }
@@ -582,15 +655,15 @@
     public Double getDoubleObject(String name)
     {
         Double result = null;
-        String value = getString(name);
+        String value = StringUtils.trim(getString(name));
 
         if (StringUtils.isNotEmpty(value))
         {
             try
             {
-                result = new Double(StringUtils.trim(value));
+                result = new Double(numberFormat.parse(value).doubleValue());
             }
-            catch(NumberFormatException e)
+            catch(ParseException e)
             {
                 logConvertionFailure(name, value, "Double");
             }
@@ -614,15 +687,17 @@
             result = new Double[value.length];
             for (int i = 0; i < value.length; i++)
             {
-                if (StringUtils.isNotEmpty(value[i]))
+                String v = StringUtils.trim(value[i]);
+                
+                if (StringUtils.isNotEmpty(v))
                 {
                     try
                     {
-                        result[i] = Double.valueOf(value[i]);
+                        result[i] = new Double(numberFormat.parse(v).doubleValue());
                     }
-                    catch (NumberFormatException e)
+                    catch (ParseException e)
                     {
-                        logConvertionFailure(name, value[i], "Double");
+                        logConvertionFailure(name, v, "Double");
                     }
                 }
             }
@@ -641,15 +716,15 @@
     public float getFloat(String name, float defaultValue)
     {
         float result = defaultValue;
-        String value = getString(name);
+        String value = StringUtils.trim(getString(name));
 
         if (StringUtils.isNotEmpty(value))
         {
             try
             {
-                result = Float.parseFloat(StringUtils.trim(value));
+                result = numberFormat.parse(value).floatValue();
             }
-            catch (NumberFormatException e)
+            catch (ParseException e)
             {
                 logConvertionFailure(name, value, "Float");
             }
@@ -685,15 +760,17 @@
             result = new float[value.length];
             for (int i = 0; i < value.length; i++)
             {
-                if (StringUtils.isNotEmpty(value[i]))
+                String v = StringUtils.trim(value[i]);
+                
+                if (StringUtils.isNotEmpty(v))
                 {
                     try
                     {
-                        result[i] = Float.parseFloat(value[i]);
+                        result[i] = numberFormat.parse(v).floatValue();
                     }
-                    catch (NumberFormatException e)
+                    catch (ParseException e)
                     {
-                        logConvertionFailure(name, value[i], "Float");
+                        logConvertionFailure(name, v, "Float");
                     }
                 }
             }
@@ -725,15 +802,15 @@
     public Float getFloatObject(String name)
     {
         Float result = null;
-        String value = getString(name);
+        String value = StringUtils.trim(getString(name));
 
         if (StringUtils.isNotEmpty(value))
         {
             try
             {
-                result = new Float(StringUtils.trim(value));
+                result = new Float(numberFormat.parse(value).floatValue());
             }
-            catch(NumberFormatException e)
+            catch(ParseException e)
             {
                 logConvertionFailure(name, value, "Float");
             }
@@ -758,15 +835,17 @@
             result = new Float[value.length];
             for (int i = 0; i < value.length; i++)
             {
-                if (StringUtils.isNotEmpty(value[i]))
+                String v = StringUtils.trim(value[i]);
+                
+                if (StringUtils.isNotEmpty(v))
                 {
                     try
                     {
-                        result[i] = Float.valueOf(value[i]);
+                        result[i] = new Float(numberFormat.parse(v).floatValue());
                     }
-                    catch (NumberFormatException e)
+                    catch (ParseException e)
                     {
-                        logConvertionFailure(name, value[i], "Float");
+                        logConvertionFailure(name, v, "Float");
                     }
                 }
             }
@@ -785,15 +864,15 @@
     public BigDecimal getBigDecimal(String name, BigDecimal defaultValue)
     {
         BigDecimal result = defaultValue;
-        String value = getString(name);
+        String value = StringUtils.trim(getString(name));
 
         if (StringUtils.isNotEmpty(value))
         {
             try
             {
-                result = new BigDecimal(StringUtils.trim(value));
+                result = new BigDecimal(numberFormat.parse(value).doubleValue());
             }
-            catch (NumberFormatException e)
+            catch (ParseException e)
             {
                 logConvertionFailure(name, value, "BigDecimal");
             }
@@ -830,15 +909,17 @@
             result = new BigDecimal[value.length];
             for (int i = 0; i < value.length; i++)
             {
-                if (StringUtils.isNotEmpty(value[i]))
+                String v = StringUtils.trim(value[i]);
+                
+                if (StringUtils.isNotEmpty(v))
                 {
                     try
                     {
-                        result[i] = new BigDecimal(value[i]);
+                        result[i] = new BigDecimal(numberFormat.parse(v).doubleValue());
                     }
-                    catch (NumberFormatException e)
+                    catch (ParseException e)
                     {
-                        logConvertionFailure(name, value[i], "BigDecimal");
+                        logConvertionFailure(name, v, "BigDecimal");
                     }
                 }
             }
@@ -857,13 +938,13 @@
     public int getInt(String name, int defaultValue)
     {
         int result = defaultValue;
-        String value = getString(name);
+        String value = StringUtils.trim(getString(name));
 
         if (StringUtils.isNotEmpty(value))
         {
             try
             {
-                result = Integer.parseInt(StringUtils.trim(value));
+                result = Integer.parseInt(value);
             }
             catch (NumberFormatException e)
             {
@@ -944,15 +1025,17 @@
             result = new int[value.length];
             for (int i = 0; i < value.length; i++)
             {
-                if (StringUtils.isNotEmpty(value[i]))
+                String v = StringUtils.trim(value[i]);
+                
+                if (StringUtils.isNotEmpty(v))
                 {
                     try
                     {
-                        result[i] = Integer.parseInt(value[i]);
+                        result[i] = Integer.parseInt(v);
                     }
                     catch (NumberFormatException e)
                     {
-                        logConvertionFailure(name, value[i], "Integer");
+                        logConvertionFailure(name, v, "Integer");
                     }
                 }
             }
@@ -984,13 +1067,13 @@
     public Integer getIntObject(String name)
     {
         Integer result = null;
-        String value = getString(name);
+        String value = StringUtils.trim(getString(name));
 
         if (StringUtils.isNotEmpty(value))
         {
             try
             {
-                result = new Integer(StringUtils.trim(value));
+                result = new Integer(value);
             }
             catch(NumberFormatException e)
             {
@@ -1017,15 +1100,17 @@
             result = new Integer[value.length];
             for (int i = 0; i < value.length; i++)
             {
-                if (StringUtils.isNotEmpty(value[i]))
+                String v = StringUtils.trim(value[i]);
+                
+                if (StringUtils.isNotEmpty(v))
                 {
                     try
                     {
-                        result[i] = Integer.valueOf(value[i]);
+                        result[i] = Integer.valueOf(v);
                     }
                     catch (NumberFormatException e)
                     {
-                        logConvertionFailure(name, value[i], "Integer");
+                        logConvertionFailure(name, v, "Integer");
                     }
                 }
             }
@@ -1057,13 +1142,13 @@
     public long getLong(String name, long defaultValue)
     {
         long result = defaultValue;
-        String value = getString(name);
+        String value = StringUtils.trim(getString(name));
 
         if (StringUtils.isNotEmpty(value))
         {
             try
             {
-                result = Long.parseLong(StringUtils.trim(value));
+                result = Long.parseLong(value);
             }
             catch (NumberFormatException e)
             {
@@ -1102,15 +1187,17 @@
             result = new long[value.length];
             for (int i = 0; i < value.length; i++)
             {
-                if (StringUtils.isNotEmpty(value[i]))
+                String v = StringUtils.trim(value[i]);
+                
+                if (StringUtils.isNotEmpty(v))
                 {
                     try
                     {
-                        result[i] = Long.parseLong(value[i]);
+                        result[i] = Long.parseLong(v);
                     }
                     catch (NumberFormatException e)
                     {
-                        logConvertionFailure(name, value[i], "Long");
+                        logConvertionFailure(name, v, "Long");
                     }
                 }
             }
@@ -1134,15 +1221,17 @@
             result = new Long[value.length];
             for (int i = 0; i < value.length; i++)
             {
-                if (StringUtils.isNotEmpty(value[i]))
+                String v = StringUtils.trim(value[i]);
+                
+                if (StringUtils.isNotEmpty(v))
                 {
                     try
                     {
-                        result[i] = Long.valueOf(value[i]);
+                        result[i] = Long.valueOf(v);
                     }
                     catch (NumberFormatException e)
                     {
-                        logConvertionFailure(name, value[i], "Long");
+                        logConvertionFailure(name, v, "Long");
                     }
                 }
             }
@@ -1160,13 +1249,13 @@
     public Long getLongObject(String name)
     {
         Long result = null;
-        String value = getString(name);
+        String value = StringUtils.trim(getString(name));
 
         if (StringUtils.isNotEmpty(value))
         {
             try
             {
-                result = new Long(StringUtils.trim(value));
+                result = Long.valueOf(value);
             }
             catch(NumberFormatException e)
             {
@@ -1202,13 +1291,13 @@
     public byte getByte(String name, byte defaultValue)
     {
         byte result = defaultValue;
-        String value = getString(name);
+        String value = StringUtils.trim(getString(name));
 
         if (StringUtils.isNotEmpty(value))
         {
             try
             {
-                result = Byte.parseByte(StringUtils.trim(value));
+                result = Byte.parseByte(value);
             }
             catch (NumberFormatException e)
             {
@@ -1276,13 +1365,13 @@
     public Byte getByteObject(String name)
     {
         Byte result = null;
-        String value = getString(name);
+        String value = StringUtils.trim(getString(name));
 
         if (StringUtils.isNotEmpty(value))
         {
             try
             {
-                result = new Byte(StringUtils.trim(value));
+                result = Byte.valueOf(value);
             }
             catch(NumberFormatException e)
             {
@@ -1438,7 +1527,7 @@
     public Date getDate(String name, DateFormat df, Date defaultValue)
     {
         Date result = defaultValue;
-        String value = getString(name);
+        String value = StringUtils.trim(getString(name));
 
         if (StringUtils.isNotEmpty(value))
         {
@@ -1523,8 +1612,7 @@
         }
         else
         {
-            DateFormat df = DateFormat.getDateInstance();
-            date = getDate(name, df, null);
+            date = getDate(name, dateFormat, null);
         }
 
         return date;
@@ -1556,7 +1644,7 @@
         NumberKey result = null;
         try
         {
-            String value = getString(name);
+            String value = StringUtils.trim(getString(name));
             if (StringUtils.isNotEmpty(value))
             {
                 result = new NumberKey(value);
@@ -1583,7 +1671,7 @@
         StringKey result = null;
         try
         {
-            String value = getString(name);
+            String value = StringUtils.trim(getString(name));
             if (StringUtils.isNotEmpty(value))
             {
                 result = new StringKey(value);
@@ -1719,13 +1807,17 @@
         {
             args[0] = getString(prop.getName());
         }
+        else if (propclass == Byte.class || propclass == Byte.TYPE)
+        {
+            args[0] = getByteObject(prop.getName());
+        }
         else if (propclass == Integer.class || propclass == Integer.TYPE)
         {
             args[0] = getIntObject(prop.getName());
         }
         else if (propclass == Long.class || propclass == Long.TYPE)
         {
-            args[0] = new Long(getLong(prop.getName()));
+            args[0] = getLongObject(prop.getName());
         }
         else if (propclass == Boolean.class || propclass == Boolean.TYPE)
         {
@@ -1733,7 +1825,11 @@
         }
         else if (propclass == Double.class || propclass == Double.TYPE)
         {
-            args[0] = new Double(getDouble(prop.getName()));
+            args[0] = getDoubleObject(prop.getName());
+        }
+        else if (propclass == Float.class || propclass == Float.TYPE)
+        {
+            args[0] = getFloatObject(prop.getName());
         }
         else if (propclass == BigDecimal.class)
         {

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=639770&r1=639769&r2=639770&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 Fri Mar 21 12:21:55 2008
@@ -20,13 +20,12 @@
  */
 
 import java.io.UnsupportedEncodingException;
-
 import java.math.BigDecimal;
-
 import java.text.DateFormat;
-
+import java.text.NumberFormat;
 import java.util.Date;
 import java.util.Enumeration;
+import java.util.Locale;
 import java.util.Set;
 
 import org.apache.torque.om.NumberKey;
@@ -96,6 +95,36 @@
      */
     String getCharacterEncoding();
 
+    /**
+     * Set the locale that will be used by this ValueParser.
+     */
+    void setLocale(Locale l);
+
+    /**
+     * Get the locale that will be used by this ValueParser.
+     */
+    Locale getLocale();
+
+    /**
+     * Set the date format that will be used by this ValueParser.
+     */
+    void setDateFormat(DateFormat df);
+
+    /**
+     * Get the date format that will be used by this ValueParser.
+     */
+    DateFormat getDateFormat();
+
+    /**
+     * Set the number format that will be used by this ValueParser.
+     */
+    void setNumberFormat(NumberFormat nf);
+
+    /**
+     * Get the number format that will be used by this ValueParser.
+     */
+    NumberFormat getNumberFormat();
+    
     /**
      * Trims the string data and applies the conversion specified in
      * the property given by URL_CASE_FOLDING. It returns a new

Modified: turbine/core/branches/TURBINE_2_3_BRANCH/src/test/org/apache/turbine/util/parser/BaseValueParserTest.java
URL: http://svn.apache.org/viewvc/turbine/core/branches/TURBINE_2_3_BRANCH/src/test/org/apache/turbine/util/parser/BaseValueParserTest.java?rev=639770&r1=639769&r2=639770&view=diff
==============================================================================
--- turbine/core/branches/TURBINE_2_3_BRANCH/src/test/org/apache/turbine/util/parser/BaseValueParserTest.java (original)
+++ turbine/core/branches/TURBINE_2_3_BRANCH/src/test/org/apache/turbine/util/parser/BaseValueParserTest.java Fri Mar 21 12:21:55 2008
@@ -20,6 +20,9 @@
  */
 
 import java.math.BigDecimal;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.Locale;
 
 import junit.framework.TestSuite;
 
@@ -133,18 +136,19 @@
     public void testDoubleAdd()
     {
         ValueParser vp = new BaseValueParser();
+        vp.setLocale(Locale.US);
 
         assertEquals("Wrong number of keys", 0, vp.keySet().size());
 
-        double testValue = 2.0;
+        double testValue = 2.2;
 
         vp.add("foo", testValue);
 
         assertEquals("Wrong number of keys", 1, vp.keySet().size());
 
-        assertEquals("Wrong string value", "2.0", vp.getString("foo"));
-        assertEquals("Wrong double value", (double) testValue, vp.getDouble("foo"), 0.001);
-        assertEquals("Wrong Double value", (double) testValue, vp.getDoubleObject("foo").doubleValue(), 0.001);
+        assertEquals("Wrong string value", "2.2", vp.getString("foo"));
+        assertEquals("Wrong double value", testValue, vp.getDouble("foo"), 0.001);
+        assertEquals("Wrong Double value", testValue, vp.getDoubleObject("foo").doubleValue(), 0.001);
 
         double [] doubles = vp.getDoubles("foo");
         assertEquals("Wrong Array Size", 1, doubles.length);
@@ -155,6 +159,13 @@
         assertEquals("Wrong Array Size", 1, doubleObjs.length);
 
         assertEquals("Wrong Double array value", testValue, doubleObjs[0].doubleValue(), 0.001);
+        
+        vp.clear();
+        vp.setLocale(Locale.GERMANY);
+        
+        String testDouble = "2,3";
+        vp.add("foo", testDouble);
+        assertEquals("Wrong double value", 2.3, vp.getDouble("foo"), 0.001);
     }
 
     public void testIntAdd()
@@ -581,6 +592,33 @@
 
         assertEquals("Wrong number of keys", 3, vp.keySet().size());
         assertTrue(vp.containsTimeSelectorKeys("foo"));
+    }
+
+    public void testDate()
+    {
+        BaseValueParser vp = new BaseValueParser();
+        vp.setLocale(Locale.US);
+
+        assertEquals("Wrong number of keys", 0, vp.keySet().size());
+
+        vp.add("foo", "03/21/2008");
+        
+        Calendar cal = Calendar.getInstance(Locale.US);
+        cal.clear();
+        cal.set(2008, 2, 21, 0, 0, 0);
+
+        assertEquals("Wrong Date value (US)", cal.getTime(), vp.getDate("foo"));
+
+        vp.clear();
+        vp.setLocale(Locale.GERMANY);
+
+        vp.add("foo", "21.03.2008");
+        
+        cal = Calendar.getInstance(Locale.GERMANY);
+        cal.clear();
+        cal.set(2008, 2, 21, 0, 0, 0);
+
+        assertEquals("Wrong Date value (German)", cal.getTime(), vp.getDate("foo"));
     }
 
     public void testBooleanObject()

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=639770&r1=639769&r2=639770&view=diff
==============================================================================
--- turbine/core/branches/TURBINE_2_3_BRANCH/xdocs/changes.xml (original)
+++ turbine/core/branches/TURBINE_2_3_BRANCH/xdocs/changes.xml Fri Mar 21 12:21:55 2008
@@ -28,6 +28,12 @@
 
 <body>
   <release version="2.3.3-dev" date="in Subversion">
+    <action type="add" dev="tv">
+      Made ValueParser and BaseValueParser locale-aware. Note that the default
+      locale used is always the default value of the JVM which is different from 
+      the previous behaviour where the locale used was sometimes Locale.US (for
+      float, double and BigDecimal) and sometimes the JVM-default (for dates).
+    </action>
     <action type="update" dev="seade">
       Improved handling of null values for query parameter and path info values
       in TurbineURI (avoids a NPE, logs warning message).



Re: svn commit: r639770 - in /turbine/core/branches/TURBINE_2_3_BRANCH: src/java/org/apache/turbine/util/parser/ src/test/org/apache/turbine/util/parser/ xdocs/

Posted by Scott Eade <se...@backstagetech.com.au>.
Thomas Vandahl wrote:
> Scott Eade wrote:
>> I have had a look at this and have the following comments:
> Thank you for your review.
>>   1. I think NumberFormat.parse() will convert "12.3foo" to 12.3 where
>>      as the old Double.parseDouble() would throw a
>>      NumberFormatException. I don't think we should be blindly dropping
>>      anything after the number so I think we need to detect this and
>>      throw an exception.  This also applies to the corresponding Float
>>      and BigDecimal methods.
> That would involve parsing with ParsePosition, right?
That looks right.  Basically we need to make sure that all of the String 
was used to produce the number.
>>   2. Why doesn't getDoubleObject() just invoke getDouble()?      
>> getDoubleObjects() use getDoubles()?  Being pedantic, getDouble()
>>      and getDoubles() have a common block of code that could be in a
>>      private method.  This would apply to many of the other methods
>>      also.  Whether or not these should be addressed is a matter of
>>      style - clearly they were like this before your changes.
> No, they weren't. I just checked. Actually I changed very little in 
> the logic of the methods. But yes, you are right, that'd be less code 
> and less code is a Good Thing (TM). As numberFormat.parse() returns a 
> Number, it would be even possible to share the code between Float, 
> float, Double, and double. I'll try to fix this over the weekend.
Crossed wires I think - my comment was that the code was a bit sloppy 
_before_ you touched it.  You made it no worse than it already was, but 
tidying it up will be a good thing.
>> Try adding the following to BaseValueParserTest.testGetDouble():
>>        parser.add("unparsable2", "1a");
>>        result = parser.getDouble("unparsable2");
>>        assertEquals(result, 0, 0);
> Will do.
It should fail until the ParsePosition changes that confirm the entire 
String us used are made.
>> The TurbineRunDataService change looks okay, but I will try it out 
>> shortly to confirm that I can easily override the locale on a per 
>> user basis.
> That's exactly my problem, too. As I see it, the Turbine request 
> processing is missing a call to data.setLocale() at some (early) 
> place. I'd propose to add a method to the LocalizationService, 
> something like setRunDataLocale(RunData) which can be overridden in 
> the implementation.
Here is what I currently do (I require per user locales):

    * My User implementation has a getLocale() method.
    * When my custom LocalizationTool.init() is executed it pulls the
      User from RunData, grabs the locale and uses this (my
      LocalizationTool also implements a formatPrefixed() method that
      can handle an arbitrary number of parameters embedded in a single
      string, space separated following the key).  So in my templates I
      can use $l10n and the locale for the user will be used.
    * In Java code I do a number of things:
          o return just l10n keys and defer the lookups to the templates
            (best answer, but certainly need something akin to my
            formatPrefixed method)
          o use the Localization methods that take a locale argument
            that I first retrieve from the User which is obtained from
            RunData (better to leave this to the presentation layer, but
            sometimes it is necessary)
          o in some places it appears I have insanely initialized and
            used LocalizationTool (bad!)

As I see it, RunData is currently initialized (and the locale set to 
ParameterParser and CookieParser) before my user specific locale is 
available.  RunData doesn't see the user until populate() is invoked by 
the SessionValidator.  So the best I can do is to override 
RunData.populate() to then set the locale of the PP and CP.  I would see 
RunData.setLocale() updating the PP and CP locales if these are not null 
(TurbineRunDataService.getRunData() is currently going the other way and 
using RunData as the source of the locale for PP and CP - as you say, 
these need to get a default from somewhere).

So in what order do these things happen?  I won't have the true locale 
set in RunData until SessionValidator invokes RunData.populate().  If 
LocalizationTool.init() is not invoked until after this I would no 
longer have to set the locale explicitly - it could just pull the value 
from RunData.  In Java code I can if necessary pull the locale from 
RunData when retrieving formatted strings via Localization.  Are PP and 
CP used in any significant way prior to RunData.populate() being 
invoked?  Not likely or rather not in order to retrieve anything that 
requires localization.

So given your suggestion for providing 
LocalizationService.setRunDataLocale(RunData), which would just provide 
a default in my case until populate() can set the user specific value, 
where would *that* be invoked?  Thinking in the realm of removing rather 
than adding dependencies, how about RunData just duplicate the mechanism 
that LocalizationService uses to get it's default locale - otherwise 
there will be a messy optional dependency that will still need to come 
up with a fall back default if the LocalizationService is unavailable.

This is kind of a bunch of fairly unstructured thoughts and 
observations.  My apologies for the level of randomness and for not 
getting to this earlier in the week.

Scott

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@turbine.apache.org
For additional commands, e-mail: dev-help@turbine.apache.org


Re: svn commit: r639770 - in /turbine/core/branches/TURBINE_2_3_BRANCH: src/java/org/apache/turbine/util/parser/ src/test/org/apache/turbine/util/parser/ xdocs/

Posted by Thomas Vandahl <th...@tewisoft.de>.
Scott Eade wrote:

> I have had a look at this and have the following comments:

Thank you for your review.

>   1. I think NumberFormat.parse() will convert "12.3foo" to 12.3 where
>      as the old Double.parseDouble() would throw a
>      NumberFormatException. I don't think we should be blindly dropping
>      anything after the number so I think we need to detect this and
>      throw an exception.  This also applies to the corresponding Float
>      and BigDecimal methods.

That would involve parsing with ParsePosition, right?

>   2. Why doesn't getDoubleObject() just invoke getDouble()?      
> getDoubleObjects() use getDoubles()?  Being pedantic, getDouble()
>      and getDoubles() have a common block of code that could be in a
>      private method.  This would apply to many of the other methods
>      also.  Whether or not these should be addressed is a matter of
>      style - clearly they were like this before your changes.

No, they weren't. I just checked. Actually I changed very little in the 
logic of the methods. But yes, you are right, that'd be less code and 
less code is a Good Thing (TM). As numberFormat.parse() returns a 
Number, it would be even possible to share the code between Float, 
float, Double, and double. I'll try to fix this over the weekend.

> Try adding the following to BaseValueParserTest.testGetDouble():
>        parser.add("unparsable2", "1a");
>        result = parser.getDouble("unparsable2");
>        assertEquals(result, 0, 0);

Will do.

> The TurbineRunDataService change looks okay, but I will try it out 
> shortly to confirm that I can easily override the locale on a per user 
> basis.

That's exactly my problem, too. As I see it, the Turbine request 
processing is missing a call to data.setLocale() at some (early) place. 
I'd propose to add a method to the LocalizationService, something like 
setRunDataLocale(RunData) which can be overridden in the implementation.

Bye, Thomas.


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@turbine.apache.org
For additional commands, e-mail: dev-help@turbine.apache.org


Re: svn commit: r639770 - in /turbine/core/branches/TURBINE_2_3_BRANCH: src/java/org/apache/turbine/util/parser/ src/test/org/apache/turbine/util/parser/ xdocs/

Posted by Scott Eade <se...@backstagetech.com.au>.
tv@apache.org wrote:
> Author: tv
> Date: Fri Mar 21 12:21:55 2008
> New Revision: 639770
>
> URL: http://svn.apache.org/viewvc?rev=639770&view=rev
> Log:
> Made ValueParser and BaseValueParser locale-aware. Note that the default locale used is always the default value of the JVM which is different from the previous behaviour where the locale used was sometimes Locale.US (for float, double and BigDecimal) and sometimes the JVM-default (for dates).
>
> TODO: Integration into RunData
>
> 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/src/test/org/apache/turbine/util/parser/BaseValueParserTest.java
>     turbine/core/branches/TURBINE_2_3_BRANCH/xdocs/changes.xml
>   
<snip/>
I have had a look at this and have the following comments:

   1. I think NumberFormat.parse() will convert "12.3foo" to 12.3 where
      as the old Double.parseDouble() would throw a
      NumberFormatException. I don't think we should be blindly dropping
      anything after the number so I think we need to detect this and
      throw an exception.  This also applies to the corresponding Float
      and BigDecimal methods.
   2. Why doesn't getDoubleObject() just invoke getDouble()? 
      getDoubleObjects() use getDoubles()?  Being pedantic, getDouble()
      and getDoubles() have a common block of code that could be in a
      private method.  This would apply to many of the other methods
      also.  Whether or not these should be addressed is a matter of
      style - clearly they were like this before your changes.

Try adding the following to BaseValueParserTest.testGetDouble():
        parser.add("unparsable2", "1a");
        result = parser.getDouble("unparsable2");
        assertEquals(result, 0, 0);

The TurbineRunDataService change looks okay, but I will try it out 
shortly to confirm that I can easily override the locale on a per user 
basis.

Scott

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@turbine.apache.org
For additional commands, e-mail: dev-help@turbine.apache.org