You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@velocity.apache.org by ge...@apache.org on 2001/02/14 23:31:50 UTC

cvs commit: jakarta-velocity/src/java/org/apache/velocity/runtime/directive Foreach.java

geirm       01/02/14 14:31:50

  Modified:    src/java/org/apache/velocity/runtime/directive Foreach.java
  Log:
  Incorporated suggestions from jeff <jl...@houseofdistraction.com> removing 'questionable assumptions'
  WRT discovery of iterator and added support for having a naked Iterator in the context. This
  is provisional wrt some of the concerns going back and forth on the dev list, but will help us
  try and work things out.  There is a large warning message squawked into the log if you use one.
  
  Passes all tests, but will review testbed to explicitly test every iterative
   type we claim to support. I think that we are covered, but want to make sure.
  
  Revision  Changes    Path
  1.31      +34 -28    jakarta-velocity/src/java/org/apache/velocity/runtime/directive/Foreach.java
  
  Index: Foreach.java
  ===================================================================
  RCS file: /home/cvs/jakarta-velocity/src/java/org/apache/velocity/runtime/directive/Foreach.java,v
  retrieving revision 1.30
  retrieving revision 1.31
  diff -u -r1.30 -r1.31
  --- Foreach.java	2001/01/03 05:28:33	1.30
  +++ Foreach.java	2001/02/14 22:31:50	1.31
  @@ -85,7 +85,7 @@
    *
    * @author <a href="mailto:jvanzyl@periapt.com">Jason van Zyl</a>
    * @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
  - * @version $Id: Foreach.java,v 1.30 2001/01/03 05:28:33 geirm Exp $
  + * @version $Id: Foreach.java,v 1.31 2001/02/14 22:31:50 geirm Exp $
    */
   public class Foreach extends Directive
   {
  @@ -124,6 +124,12 @@
        * is a Map.
        */
       private final static int INFO_MAP = 3;
  +
  +    /**
  +     * Flag to indicate that the list object being used
  +     * is a Collection.
  +     */
  +    private final static int INFO_COLLECTION = 4;
     
       /**
        * The name of the variable to use when placing
  @@ -217,10 +223,12 @@
           {
               if (listObject instanceof Object[])
                   type = INFO_ARRAY;
  -            else if (Introspector.implementsMethod(listObject, "iterator"))
  -                type = INFO_ITERATOR;
  -            else if (Introspector.implementsMethod(listObject, "values"))
  +            else if ( listObject instanceof Collection)
  +                type = INFO_COLLECTION;
  +            else if ( listObject instanceof Map )
                   type = INFO_MAP;
  +            else if ( listObject instanceof Iterator )
  +                type = INFO_ITERATOR;
   
               /*
                *  if we did figure it out, cache it
  @@ -240,36 +248,34 @@
            */
   
           switch( type ) {
  -            
  -        case INFO_ITERATOR :
  -            
  -            if (((Collection) listObject).size() == 0)
  -                return null;    
  -           
  -            return ((Collection) listObject).iterator();        
               
  +        case INFO_COLLECTION :        
  +            return ( (Collection) listObject).iterator();        
  +
  +        case INFO_ITERATOR :        
  +            Runtime.warn ("Warning! The reference " 
  +                          + node.jjtGetChild(2).getFirstToken().image
  +                          + " is an Iterator in the #foreach() loop at ["
  +                          + getLine() + "," + getColumn() + "]"
  +                          + " in template " + context.getCurrentTemplateName() 
  +                          + ". If used in more than once, this may lead to unexpected results.");
  +
  +            return ( (Iterator) listObject);       
  +
           case INFO_ARRAY:
  -            
  -            Object[] arrayObject = ((Object[]) listObject);
  -            
  -            if (arrayObject.length == 0)
  -                return null; 
  -            
  -            return new ArrayIterator( arrayObject );
  -           
  -        case INFO_MAP:          
  +            return new ArrayIterator( (Object [] )  listObject );
   
  -            if (((Map) listObject).size() == 0)
  -                return null;
  +        case INFO_MAP:          
  +            return ( (Map) listObject).values().iterator();
   
  -            return ((Map) listObject).values().iterator();
  -        
           default:
           
               /*  we have no clue what this is  */
  -            Runtime.warn ("Could not determine type of iterator for " + 
  -                "#foreach loop for " +  node.jjtGetChild(2).getFirstToken().image);
  -            
  +            Runtime.warn ("Could not determine type of iterator in " 
  +                          +  "#foreach loop for " +  node.jjtGetChild(2).getFirstToken().image 
  +                          + " at [" + getLine() + "," + getColumn() + "]"
  +                          + " in template " + context.getCurrentTemplateName() );            
  +
               return null;
           }
       }
  @@ -286,7 +292,7 @@
   
           Iterator i = getIterator( context, node );
      
  -        if ( i == null)
  +        if ( i == null )
               return false;
           
           int counter = COUNTER_INITIAL_VALUE;