I've noticed that it's often the case that getString(String key) gets
called on a key whose associated value is a list (usually due to a
misconfigured properties file).  I was thinking that in such a case
behavior other than a ClassCastException may be desirable.  In ideal
code, such a situation might be caught and handled gracefully.
Unfortunately, ClassCastException is one of those Exceptions that can
be thrown almost anywhere, so it often is dealt with as well as it
could be.

Two reasonable behaviors that come to mind include either returning
the first item in the list, or returning the entire list as it was
originally specified in the properties file.  I've implemented the
second possiblity, but want feedback on this change from others.

Index: StringUtils.java
===================================================================
RCS file: 
/home/cvs/jakarta-velocity/src/java/org/apache/velocity/util/StringUtils.java,v
retrieving revision 1.9
diff -u -u -r1.9 StringUtils.java
--- StringUtils.java    2001/03/05 11:48:39     1.9
+++ StringUtils.java    2001/05/01 06:58:09
@@ -63,6 +63,7 @@
 
 import java.util.ArrayList;
 import java.util.Hashtable;
+import java.util.List;
 import java.util.StringTokenizer;
 import java.util.Vector;
 
@@ -171,6 +172,29 @@
             s[i] = (String) v.elementAt(i);
 
         return s;
+    }
+
+    /**
+     * Bridges the elements in the specified list into a single piece
+     * of text using the provided delimeter.
+     *
+     * @param list  The elements to remold.
+     * @param delim The glue.
+     * @return      The reformed bits.
+     */
+    public static String join(List list, String delim)
+    {
+        StringBuffer buf = new StringBuffer();
+        int size = list.size();
+        for (int i = 0; i < size; i++)
+        {
+            if (i > 0)
+            {
+                buf.append(delim);
+            }
+            buf.append(list.get(i));
+        }
+        return buf.toString();
     }
 
     /**
Index: Configuration.java
===================================================================
RCS file: 
/home/cvs/jakarta-velocity/src/java/org/apache/velocity/runtime/configuration/Configuration.java,v
retrieving revision 1.28
diff -u -u -r1.28 Configuration.java
--- Configuration.java  2001/05/01 07:05:39     1.28
+++ Configuration.java  2001/05/01 07:06:27
@@ -75,6 +75,8 @@
 import java.util.StringTokenizer;
 import java.util.Vector;
 
+import org.apache.velocity.util.StringUtils;
+
 /**
  * This class extends normal Java properties by adding the possibility
  * to use the same key many times concatenating the value strings
@@ -951,6 +953,11 @@
         if (value instanceof String)
         {
             return (String) value;
+        }
+        else if (value instanceof Vector)
+        {
+            return StringUtils.join((Vector) value,
+                                    PropertiesTokenizer.DELIMETER);
         }
         else if (value == null)
         {

Reply via email to