velocity-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nbu...@apache.org
Subject svn commit: r689980 - /velocity/tools/trunk/src/main/java/org/apache/velocity/tools/generic/ConversionTool.java
Date Thu, 28 Aug 2008 20:59:31 GMT
Author: nbubna
Date: Thu Aug 28 13:59:31 2008
New Revision: 689980

URL: http://svn.apache.org/viewvc?rev=689980&view=rev
Log:
make it easy to trim the members of string lists

Modified:
    velocity/tools/trunk/src/main/java/org/apache/velocity/tools/generic/ConversionTool.java

Modified: velocity/tools/trunk/src/main/java/org/apache/velocity/tools/generic/ConversionTool.java
URL: http://svn.apache.org/viewvc/velocity/tools/trunk/src/main/java/org/apache/velocity/tools/generic/ConversionTool.java?rev=689980&r1=689979&r2=689980&view=diff
==============================================================================
--- velocity/tools/trunk/src/main/java/org/apache/velocity/tools/generic/ConversionTool.java
(original)
+++ velocity/tools/trunk/src/main/java/org/apache/velocity/tools/generic/ConversionTool.java
Thu Aug 28 13:59:31 2008
@@ -56,14 +56,17 @@
 public class ConversionTool extends LocaleConfig
 {
     public static final String STRINGS_DELIMITER_FORMAT_KEY = "stringsDelimiter";
+    public static final String STRINGS_TRIM_KEY = "trimStrings";
     public static final String DATE_FORMAT_KEY = "dateFormat";
     public static final String NUMBER_FORMAT_KEY = "numberFormat";
 
     public static final String DEFAULT_STRINGS_DELIMITER = ",";
+    public static final boolean DEFAULT_STRINGS_TRIM = true;
     public static final String DEFAULT_NUMBER_FORMAT = "default";
     public static final String DEFAULT_DATE_FORMAT = "default";
 
     private String stringsDelimiter = DEFAULT_STRINGS_DELIMITER;
+    private boolean stringsTrim = DEFAULT_STRINGS_TRIM;
     private String numberFormat = DEFAULT_NUMBER_FORMAT;
     private String dateFormat = DEFAULT_DATE_FORMAT;
 
@@ -112,6 +115,23 @@
         return this.stringsDelimiter;
     }
 
+    /**
+     * Sets whether strings should be trimmed when separated from
+     * a delimited string value.
+     * The default is true.
+     *
+     * @see #parseStringList
+     */
+    protected final void setStringsTrim(boolean stringsTrim)
+    {
+        this.stringsTrim = stringsTrim;
+    }
+
+    public final boolean getStringsTrim()
+    {
+        return this.stringsTrim;
+    }
+
     protected final void setNumberFormat(String format)
     {
         this.numberFormat = format;
@@ -643,16 +663,28 @@
 
     /**
      * Converts a single String value into an array of Strings by splitting
-     * it on the tool's set stringsDelimiter.  The default stringsDelimiter is a comma.
-     *
+     * it on the tool's set stringsDelimiter.  The default stringsDelimiter is a comma,
+     * and by default, all strings parsed out are trimmed before returning.
      */
     protected String[] parseStringList(String value)
     {
+        String[] values;
         if (value.indexOf(this.stringsDelimiter) < 0)
         {
-            return new String[] { value };
+            values = new String[] { value };
+        }
+        else
+        {
+            values = value.split(this.stringsDelimiter);
+        }
+        if (this.stringsTrim)
+        {
+            for (int i=0,l=values.length; i < l; i++)
+            {
+                values[i] = values[i].trim();
+            }
         }
-        return value.split(this.stringsDelimiter);
+        return values;
     }
 
     /**



Mime
View raw message