jvanzyl     01/01/06 14:08:35

  Modified:    src/java/org/apache/velocity/runtime/parser/node
                        ASTIdentifier.java AbstractExecutor.java
                        PropertyExecutor.java
  Added:       src/java/org/apache/velocity/runtime/parser/node
                        GetExecutor.java
  Removed:     src/java/org/apache/velocity/runtime/parser/node
                        MapExecutor.java
  Log:
  - Velocity can now operate with a get(key) method in an object
    that doesn't specifically implement the Map interface. Just
    makes things a little more flexible.
  
  Revision  Changes    Path
  1.6       +17 -16    
jakarta-velocity/src/java/org/apache/velocity/runtime/parser/node/ASTIdentifier.java
  
  Index: ASTIdentifier.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-velocity/src/java/org/apache/velocity/runtime/parser/node/ASTIdentifier.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- ASTIdentifier.java        2001/01/03 05:25:50     1.5
  +++ ASTIdentifier.java        2001/01/06 22:08:33     1.6
  @@ -71,16 +71,18 @@
    *
    * @author <a href="mailto:[EMAIL PROTECTED]">Jason van Zyl</a>
    * @author <a href="mailto:[EMAIL PROTECTED]">Geir Magnusson Jr.</a>
  - * @version $Id: ASTIdentifier.java,v 1.5 2001/01/03 05:25:50 geirm Exp $ 
  + * @version $Id: ASTIdentifier.java,v 1.6 2001/01/06 22:08:33 jvanzyl Exp $ 
    */
   package org.apache.velocity.runtime.parser.node;
   
   import java.util.Map;
  +
   import java.lang.reflect.Method;
   
   import org.apache.velocity.context.InternalContextAdapter;
   import org.apache.velocity.runtime.parser.*;
   import org.apache.velocity.util.introspection.IntrospectionCacheData;
  +import org.apache.velocity.util.introspection.Introspector;
   
   public class ASTIdentifier extends SimpleNode
   {
  @@ -130,23 +132,21 @@
           throws Exception
       {
           /*
  -         *  Now there might just be an error here.  If there is a typo in the 
property
  -         *  then a MapExecutor is created and this needs to be prevented.
  +         * We are now going to allow a get(key) method
  +         * to be used on objects that don't implement
  +         * the Map interface. That was a silly restriction,
  +         * I just ran into it so it's silly ;-)
  +         *
            */
  +        
  +        AbstractExecutor executor;
   
  -        try
  -        {
  -            AbstractExecutor executor = new PropertyExecutor();
  -            Method m = data.getMethod(method,null);
  -            executor.setData(m);
  -            return executor;
  -        }
  -        catch (NoSuchMethodException nsme)
  -        {
  -            AbstractExecutor executor = new MapExecutor();
  -            executor.setData(identifier);
  -            return executor;
  -        }
  +        executor = new PropertyExecutor(data, identifier);
  +
  +        if (executor.isAlive() == false)
  +            executor = new GetExecutor(data, identifier);
  +        
  +        return executor;
       }
   
       /**
  @@ -197,6 +197,7 @@
           }
           catch( Exception e)
           {
  +            e.printStackTrace();
               System.out.println("ASTIdentifier.execute() : identifier = " + 
identifier + " : " + e );
           }
   
  
  
  
  1.3       +11 -6     
jakarta-velocity/src/java/org/apache/velocity/runtime/parser/node/AbstractExecutor.java
  
  Index: AbstractExecutor.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-velocity/src/java/org/apache/velocity/runtime/parser/node/AbstractExecutor.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- AbstractExecutor.java     2001/01/03 05:27:38     1.2
  +++ AbstractExecutor.java     2001/01/06 22:08:34     1.3
  @@ -54,17 +54,22 @@
   
   package org.apache.velocity.runtime.parser.node;
   
  -import java.util.Map;
  +import java.lang.reflect.Method;
  +
   import org.apache.velocity.context.InternalContextAdapter;
   
   public abstract class AbstractExecutor
   {
  -    protected Object data;
  -
  +    protected Method method = null;
  +    
       public abstract Object execute(Object o, InternalContextAdapter context);
   
  -    public void setData(Object data)
  +    public boolean isAlive()
       {
  -        this.data = data;
  -    }        
  +        if (method != null)
  +            return true;
  +        else
  +            return false;
  +    }            
  +
   }
  
  
  
  1.3       +28 -8     
jakarta-velocity/src/java/org/apache/velocity/runtime/parser/node/PropertyExecutor.java
  
  Index: PropertyExecutor.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-velocity/src/java/org/apache/velocity/runtime/parser/node/PropertyExecutor.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- PropertyExecutor.java     2001/01/03 05:27:38     1.2
  +++ PropertyExecutor.java     2001/01/06 22:08:34     1.3
  @@ -1,4 +1,3 @@
  -package org.apache.velocity.runtime.parser.node;
   /*
    * The Apache Software License, Version 1.1
    *
  @@ -53,28 +52,49 @@
    * <http://www.apache.org/>.
    */
   
  +package org.apache.velocity.runtime.parser.node;
  +
   import java.lang.reflect.Method;
   
   import org.apache.velocity.context.InternalContextAdapter;
   
  +/**
  + * Returned the value of object property when executed.
  + */
   public class PropertyExecutor extends AbstractExecutor
   {
  -    protected Method method;
  -    
  -    public void setData(Object data)
  +    public PropertyExecutor(Class c, String property)
       {
  -        this.method = (Method) data;
  -    }        
  +        /*
  +         * Not using the Introspector here because
  +         * it can't deal with methods that have
  +         * no arguments! That needs to be fixed.
  +         */
  +        
  +        try
  +        {
  +            method = c.getMethod("get" + property, null);
  +        }
  +        catch (NoSuchMethodException nsme)
  +        {
  +        }
  +    }
       
  +    /**
  +     * Get the value of the specified property.
  +     */
       public Object execute(Object o, InternalContextAdapter context)
       {
           try
           {
  +            if (method == null)
  +                return null;
  +            
               return method.invoke(o, null);
           }
           catch (Exception e)
           {
               return null;
  -        }            
  -    }    
  +        }
  +    }
   }
  
  
  
  1.1                  
jakarta-velocity/src/java/org/apache/velocity/runtime/parser/node/GetExecutor.java
  
  Index: GetExecutor.java
  ===================================================================
  /*
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2000 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact [EMAIL PROTECTED]
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  package org.apache.velocity.runtime.parser.node;
  
  import java.lang.reflect.Method;
  
  import org.apache.velocity.context.InternalContextAdapter;
  import org.apache.velocity.util.introspection.Introspector;
  
  public class GetExecutor extends AbstractExecutor
  {
      private Object[] args = new Object[1];
      
      public GetExecutor(Class c, String key)
          throws Exception
      {
          args[0] = key;
          method = Introspector.getMethod(c, "get", args);
      }
      
      /**
       * Try and execute the get(key) method for this
       * object. There is no longer a requirement that
       * this object implement the Map interface.
       */
      public Object execute(Object o, InternalContextAdapter context)
      {
          try
          {
              if (method == null)
                  return null;
              
              return method.invoke(o, args);
          }
          catch (Exception e)
          {
              return null;
          }
      }
  }
  
  
  

Reply via email to