craigmcc    02/03/11 21:35:41

  Modified:    src/share/org/apache/struts/taglib/logic IterateTag.java
               web/exercise-taglib logic-iterate.jsp
  Log:
  Make it possible for <logic:iterate> to iterate over arrays of Java
  primitives, as well as all the other previous possibilities.  Add a
  test case to the exercise suite to prove that it works.
  
  PR: Bugzilla #6553
  Submitted by: Martin Rose <martin.rose at gsullivan.com>
  
  Revision  Changes    Path
  1.14      +15 -9     
jakarta-struts/src/share/org/apache/struts/taglib/logic/IterateTag.java
  
  Index: IterateTag.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-struts/src/share/org/apache/struts/taglib/logic/IterateTag.java,v
  retrieving revision 1.13
  retrieving revision 1.14
  diff -u -r1.13 -r1.14
  --- IterateTag.java   16 Jul 2001 00:44:57 -0000      1.13
  +++ IterateTag.java   12 Mar 2002 05:35:40 -0000      1.14
  @@ -1,7 +1,7 @@
   /*
  - * $Header: 
/home/cvs/jakarta-struts/src/share/org/apache/struts/taglib/logic/IterateTag.java,v 
1.13 2001/07/16 00:44:57 craigmcc Exp $
  - * $Revision: 1.13 $
  - * $Date: 2001/07/16 00:44:57 $
  + * $Header: 
/home/cvs/jakarta-struts/src/share/org/apache/struts/taglib/logic/IterateTag.java,v 
1.14 2002/03/12 05:35:40 craigmcc Exp $
  + * $Revision: 1.14 $
  + * $Date: 2002/03/12 05:35:40 $
    *
    * ====================================================================
    *
  @@ -63,7 +63,8 @@
   package org.apache.struts.taglib.logic;
   
   
  -import java.util.Arrays;
  +import java.lang.reflect.Array;
  +import java.util.ArrayList;
   import java.util.Collection;
   import java.util.Enumeration;
   import java.util.Iterator;
  @@ -87,7 +88,7 @@
    * or a Map (which includes Hashtables) whose elements will be iterated over.
    *
    * @author Craig R. McClanahan
  - * @version $Revision: 1.13 $ $Date: 2001/07/16 00:44:57 $
  + * @version $Revision: 1.14 $ $Date: 2002/03/12 05:35:40 $
    */
   
   public class IterateTag extends BodyTagSupport {
  @@ -309,15 +310,20 @@
   
   
        // Construct an iterator for this collection
  -     if (collection.getClass().isArray())
  -         collection = Arrays.asList((Object[]) collection);
  -     if (collection instanceof Collection)
  +     if (collection.getClass().isArray()) {
  +            int length = Array.getLength(collection);
  +            ArrayList c = new ArrayList(length);
  +            for (int i = 0; i < length; i++) {
  +                c.add(Array.get(collection, i));
  +            }
  +            iterator = c.iterator();
  +     } else if (collection instanceof Collection)
            iterator = ((Collection) collection).iterator();
        else if (collection instanceof Iterator)
            iterator = (Iterator) collection;
        else if (collection instanceof Map)
            iterator = ((Map) collection).entrySet().iterator();
  -    else if (collection instanceof Enumeration)
  +        else if (collection instanceof Enumeration)
            iterator = new IteratorAdapter((Enumeration)collection);
        else {
            JspException e = new JspException
  
  
  
  1.5       +31 -0     jakarta-struts/web/exercise-taglib/logic-iterate.jsp
  
  Index: logic-iterate.jsp
  ===================================================================
  RCS file: /home/cvs/jakarta-struts/web/exercise-taglib/logic-iterate.jsp,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- logic-iterate.jsp 15 Jun 2001 21:49:21 -0000      1.4
  +++ logic-iterate.jsp 12 Mar 2002 05:35:41 -0000      1.5
  @@ -16,6 +16,10 @@
       list.add("Fourth");
       list.add("Fifth");
       pageContext.setAttribute("list", list, PageContext.PAGE_SCOPE);
  +
  +    int intArray[] = new int[]
  +     { 0, 10, 20, 30, 40 };
  +    pageContext.setAttribute("intArray", intArray, PageContext.PAGE_SCOPE);
     }
   %>
   
  @@ -86,6 +90,33 @@
     <li><em><bean:write name="item"/></em>&nbsp;[<bean:write name="index"/>]</li>
   </logic:iterate>
   </ol>
  +
  +<h3>Test 8 - Iterate Over an int array</h3>
  +
  +<ol>
  +<logic:iterate id="item" name="intArray" indexId="index">
  +  <li><em><bean:write name="item"/></em>&nbsp;[<bean:write name="index"/>]</li>
  +</logic:iterate>
  +</ol>
  +
  +<h3>Test 9 - Iterate Over an int array [0..2]</h3>
  +
  +<ol>
  +<logic:iterate id="item" name="intArray" indexId="index"
  +           length="3">
  +  <li><em><bean:write name="item"/></em>&nbsp;[<bean:write name="index"/>]</li>
  +</logic:iterate>
  +</ol>
  +
  +<h3>Test 10 - Iterate Over an int array [2..4]</h3>
  +
  +<ol>
  +<logic:iterate id="item" name="intArray" indexId="index"
  +           offset="2" length="3">
  +  <li><em><bean:write name="item"/></em>&nbsp;[<bean:write name="index"/>]</li>
  +</logic:iterate>
  +</ol>
  +
   
   </body>
   </html>
  
  
  

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

Reply via email to