jon         00/11/24 15:59:48

  Modified:    src/java/org/apache/velocity/runtime/directive Foreach.java
  Log:
  better debugging
  moved introspection stuff to the introspector class
  
  Revision  Changes    Path
  1.21      +36 -40    
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.20
  retrieving revision 1.21
  diff -u -r1.20 -r1.21
  --- Foreach.java      2000/11/17 02:24:38     1.20
  +++ Foreach.java      2000/11/24 23:59:47     1.21
  @@ -74,15 +74,18 @@
   import org.apache.velocity.runtime.parser.ParserTreeConstants;
   import org.apache.velocity.runtime.parser.node.Node;
   
  +import org.apache.velocity.runtime.exception.NodeException;
   import org.apache.velocity.runtime.exception.ReferenceException;
   
  +import org.apache.velocity.util.introspection.Introspector;
  +
   /**
    * Foreach directive used for moving through arrays,
    * or objects that provide an Iterator.
    *
    * @author <a href="mailto:[EMAIL PROTECTED]">Jason van Zyl</a>
    * @author <a href="mailto:[EMAIL PROTECTED]">Geir Magnusson Jr.</a>
  - * @version $Id: Foreach.java,v 1.20 2000/11/17 02:24:38 daveb Exp $
  + * @version $Id: Foreach.java,v 1.21 2000/11/24 23:59:47 jon Exp $
    */
   public class Foreach extends Directive
   {
  @@ -142,33 +145,44 @@
           // element is so that we don't have to do it
           // everytime the node is traversed.
           //if (listObject instanceof Object[])
  -        if (listObject instanceof Object[])
  +        try
           {
  -            node.setInfo(ARRAY);
  -            Object[] arrayObject = ((Object[]) listObject);
  +            if (listObject instanceof Object[])
  +            {
  +                node.setInfo(ARRAY);
  +                Object[] arrayObject = ((Object[]) listObject);
               
  -            if (arrayObject.length == 0)
  +                if (arrayObject.length == 0)
  +                    node.setInvalid();
  +                else                
  +                    sampleElement = arrayObject[0];
  +            }            
  +            else if (Introspector.implementsMethod(listObject, "iterator"))
  +            {
  +                node.setInfo(ITERATOR);
  +                sampleElement = ((Collection) listObject).iterator().next();
  +            }
  +            else if (Introspector.implementsMethod(listObject, "values"))
  +            {
  +                node.setInfo(MAP);
  +                sampleElement = ((Map) listObject).values().iterator().next();
  +            }
  +            else
  +            {
  +                // If it's not an array or an object that provides
  +                // an iterator then the node is invalid and should
  +                // not be rendered.
                   node.setInvalid();
  -            else                
  -                sampleElement = arrayObject[0];
  -        }            
  -        else if (implementsMethod(listObject, "iterator"))
  -        {
  -            node.setInfo(ITERATOR);
  -            sampleElement = ((Collection) listObject).iterator().next();
  +                Runtime.warn ("Could not determine type of iterator for #foreach 
loop ");
  +                throw new NodeException ("Could not determine type of iterator for 
#foreach loop " , node);
  +            }            
           }
  -        else if (implementsMethod(listObject, "values"))
  +        catch (java.util.NoSuchElementException nsee)
           {
  -            node.setInfo(MAP);
  -            sampleElement = ((Map) listObject).values().iterator().next();
  -        }
  -        else
  -        {
  -            // If it's not an array or an object that provides
  -            // an iterator then the node is invalid and should
  -            // not be rendered.
               node.setInvalid();
  -        }            
  +            Runtime.warn ("Iterator for #foreach loop did not contain any values ");
  +            throw new NodeException ("Iterator for #foreach loop did not contain 
any values ", node);
  +        }
           
           // This is a little trick so that we can initialize
           // all the blocks in the foreach  properly given
  @@ -213,22 +227,4 @@
           return true;
       }
   
  -    /**
  -     * Checks whether the provided object implements a given method.
  -     *
  -     * @param object     The object to check.
  -     * @param methodName The method to check for.
  -     * @return           Whether the method is implemented.
  -     */
  -    public static boolean implementsMethod(Object object, String methodName)
  -    {
  -        int m;
  -        
  -        Method[] methods = object.getClass().getMethods();
  -        for (m = 0 ; m < methods.length ; ++m)
  -            if (methodName.equals(methods[m].getName()))
  -                break;
  -        
  -        return (m < methods.length);
  -    }
   }
  
  
  

Reply via email to