Date: 2004-11-07T03:03:02
   Editor: ShinobuKawai <[EMAIL PROTECTED]>
   Wiki: Jakarta-Velocity Wiki
   Page: ArrayTool
   URL: http://wiki.apache.org/jakarta-velocity/ArrayTool

   Add set support.

Change Log:

------------------------------------------------------------------------------
@@ -25,14 +25,16 @@
 /**
  * Tool for working with arrays in Velocity templates.
  * It provides a method to get the length and
- * specified elements of an array object.
+ * get and set specified elements of an array object.
  * Also provides a method to convert arrays into java.util.List's.
  *
  * <p><pre>
  * Example uses:
- *  $primes                -> new int[] {2, 3, 5, 7}
- *  $array.length($primes) -> 4
- *  $array.get($primes, 2) -> 5
+ *  $primes                   -> new int[] {2, 3, 5, 7}
+ *  $array.length($primes)    -> 4
+ *  $array.get($primes, 2)    -> 5
+ *  $array.set($primes, 2, 1) -> (primes[2] becomes 1)
+ *  $array.get($primes, 2)    -> 1
  *
  * Example toolbox.xml config (if you want to use this with VelocityView):
  * &lt;tool&gt;
@@ -121,18 +123,43 @@
         {
             return null;
         }
+        if (!array.getClass().isArray())
+        {
+            return null;
+        }
+
+        try
+        {
+            return Array.get(array, index);
+        }
+        catch (IndexOutOfBoundsException e)
+        {
+            // The index was wrong.
+            return null;
+        }
+    }
+
+    public Object set(Object array, int index, Object value)
+    {
+        if (array == null)
+        {
+            return null;
+        }
+        if (!array.getClass().isArray())
+        {
+            return null;
+        }
+
         try
         {
-            if (array.getClass().isArray())
-            {
-                return Array.get(array, index);
-            }
+            Array.set(array, index, value);
+            return "";
         }
         catch (IndexOutOfBoundsException e)
         {
             // The index was wrong.
+            return null;
         }
-        return null;
     }
 
     /**

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to