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/03/19 18:13:07 UTC

cvs commit: jakarta-velocity/src/java/org/apache/velocity/runtime/directive Directive.java Foreach.java Include.java Parse.java VMProxyArg.java

geirm       01/03/19 09:13:07

  Modified:    src/java/org/apache/velocity/runtime/directive
                        Directive.java Foreach.java Include.java Parse.java
                        VMProxyArg.java
  Log:
  Changes to support propogation of MethodInvocationException propogation.
  
  Allows correct catching at the app level of exceptions thrown by objects
  in the context during rendering.
  
  Revision  Changes    Path
  1.13      +7 -4      jakarta-velocity/src/java/org/apache/velocity/runtime/directive/Directive.java
  
  Index: Directive.java
  ===================================================================
  RCS file: /home/cvs/jakarta-velocity/src/java/org/apache/velocity/runtime/directive/Directive.java,v
  retrieving revision 1.12
  retrieving revision 1.13
  diff -u -r1.12 -r1.13
  --- Directive.java	2001/03/05 11:45:37	1.12
  +++ Directive.java	2001/03/19 17:12:57	1.13
  @@ -3,7 +3,7 @@
   /*
    * The Apache Software License, Version 1.1
    *
  - * Copyright (c) 2001 The Apache Software Foundation.  All rights
  + * Copyright (c) 2000-2001 The Apache Software Foundation.  All rights
    * reserved.
    *
    * Redistribution and use in source and binary forms, with or without
  @@ -25,7 +25,7 @@
    *    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
  + * 4. The names "The Jakarta Project", "Velocity", 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 apache@apache.org.
  @@ -60,6 +60,9 @@
   import org.apache.velocity.context.InternalContextAdapter;
   import org.apache.velocity.runtime.parser.node.Node;
   
  +import org.apache.velocity.exception.MethodInvocationException;
  +
  +
   /**
    * Base class for all directives used in Velocity.
    */
  @@ -108,5 +111,5 @@
        * How this directive is to be rendered 
        */
       public abstract boolean render( InternalContextAdapter context, Writer writer, Node node)
  -        throws IOException;
  -}        
  +        throws IOException, MethodInvocationException;
  +}   
  
  
  
  1.33      +6 -6      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.32
  retrieving revision 1.33
  diff -u -r1.32 -r1.33
  --- Foreach.java	2001/03/05 11:45:41	1.32
  +++ Foreach.java	2001/03/19 17:12:58	1.33
  @@ -3,7 +3,7 @@
   /*
    * The Apache Software License, Version 1.1
    *
  - * Copyright (c) 2001 The Apache Software Foundation.  All rights
  + * Copyright (c) 2000-2001 The Apache Software Foundation.  All rights
    * reserved.
    *
    * Redistribution and use in source and binary forms, with or without
  @@ -25,7 +25,7 @@
    *    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
  + * 4. The names "The Jakarta Project", "Velocity", 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 apache@apache.org.
  @@ -73,8 +73,7 @@
   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.exception.MethodInvocationException;
   
   import org.apache.velocity.util.introspection.Introspector;
   import org.apache.velocity.util.introspection.IntrospectionCacheData;
  @@ -85,7 +84,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.32 2001/03/05 11:45:41 jvanzyl Exp $
  + * @version $Id: Foreach.java,v 1.33 2001/03/19 17:12:58 geirm Exp $
    */
   public class Foreach extends Directive
   {
  @@ -182,6 +181,7 @@
        *  @return Iterator to do the dataset
        */
       private Iterator getIterator( InternalContextAdapter context, Node node )
  +        throws MethodInvocationException
       {
           /*
            *  get our list object, and punt if it's null.
  @@ -284,7 +284,7 @@
        *  renders the #foreach() block
        */
       public boolean render( InternalContextAdapter context, Writer writer, Node node)
  -        throws IOException
  +        throws IOException,  MethodInvocationException
       {        
           /*
            *  do our introspection to see what our collection is
  
  
  
  1.16      +5 -3      jakarta-velocity/src/java/org/apache/velocity/runtime/directive/Include.java
  
  Index: Include.java
  ===================================================================
  RCS file: /home/cvs/jakarta-velocity/src/java/org/apache/velocity/runtime/directive/Include.java,v
  retrieving revision 1.15
  retrieving revision 1.16
  diff -u -r1.15 -r1.16
  --- Include.java	2001/03/05 11:45:43	1.15
  +++ Include.java	2001/03/19 17:12:59	1.16
  @@ -64,6 +64,8 @@
   import org.apache.velocity.runtime.resource.Resource;
   import org.apache.velocity.util.StringUtils;
   
  +import org.apache.velocity.exception.MethodInvocationException;
  +
   /**
    * Pluggable directive that handles the #include() statement in VTL. 
    * This #include() can take multiple arguments of either StringLiteral or Reference.
  @@ -95,7 +97,7 @@
    *
    * @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
    * @author <a href="mailto:jvanzyl@periapt.com">Jason van Zyl</a>
  - * @version $Id: Include.java,v 1.15 2001/03/05 11:45:43 jvanzyl Exp $
  + * @version $Id: Include.java,v 1.16 2001/03/19 17:12:59 geirm Exp $
    */
   public class Include extends Directive
   {
  @@ -121,7 +123,7 @@
        *  arguments are logged, but render() continues.
        */
       public boolean render( InternalContextAdapter context, Writer writer, Node node)
  -        throws IOException
  +        throws IOException, MethodInvocationException
       {
           /*
            *  get our arguments and check them
  @@ -162,7 +164,7 @@
        *  @return boolean success or failure.  failures are logged
        */
       private boolean renderOutput( Node node, InternalContextAdapter context, Writer writer )
  -       throws IOException
  +        throws IOException, MethodInvocationException
       {
           String arg = "";
           
  
  
  
  1.16      +4 -2      jakarta-velocity/src/java/org/apache/velocity/runtime/directive/Parse.java
  
  Index: Parse.java
  ===================================================================
  RCS file: /home/cvs/jakarta-velocity/src/java/org/apache/velocity/runtime/directive/Parse.java,v
  retrieving revision 1.15
  retrieving revision 1.16
  diff -u -r1.15 -r1.16
  --- Parse.java	2001/03/05 11:45:48	1.15
  +++ Parse.java	2001/03/19 17:13:00	1.16
  @@ -66,6 +66,8 @@
   import org.apache.velocity.runtime.parser.node.SimpleNode;
   import org.apache.velocity.util.StringUtils;
   
  +import org.apache.velocity.exception.MethodInvocationException;
  +
   /**
    * Pluggable directive that handles the #parse() statement in VTL. 
    *
  @@ -84,7 +86,7 @@
    * @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
    * @author <a href="mailto:jvanzyl@periapt.com">Jason van Zyl</a>
    * @author <a href="mailto:Christoph.Reck@dlr.de">Christoph Reck</a>
  - * @version $Id: Parse.java,v 1.15 2001/03/05 11:45:48 jvanzyl Exp $
  + * @version $Id: Parse.java,v 1.16 2001/03/19 17:13:00 geirm Exp $
    */
   public class Parse extends Directive
   {
  @@ -112,7 +114,7 @@
        *  arguments are logged, but render() continues.
        */
       public boolean render( InternalContextAdapter context, Writer writer, Node node)
  -        throws IOException
  +        throws IOException, MethodInvocationException
       {
           /*
            *  did we get an argument?
  
  
  
  1.5       +117 -104  jakarta-velocity/src/java/org/apache/velocity/runtime/directive/VMProxyArg.java
  
  Index: VMProxyArg.java
  ===================================================================
  RCS file: /home/cvs/jakarta-velocity/src/java/org/apache/velocity/runtime/directive/VMProxyArg.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- VMProxyArg.java	2001/03/05 11:45:50	1.4
  +++ VMProxyArg.java	2001/03/19 17:13:01	1.5
  @@ -3,7 +3,7 @@
   /*
    * The Apache Software License, Version 1.1
    *
  - * Copyright (c) 2001 The Apache Software Foundation.  All rights
  + * Copyright (c) 2000-2001 The Apache Software Foundation.  All rights
    * reserved.
    *
    * Redistribution and use in source and binary forms, with or without
  @@ -25,7 +25,7 @@
    *    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
  + * 4. The names "The Jakarta Project", "Velocity", 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 apache@apache.org.
  @@ -67,6 +67,8 @@
   import org.apache.velocity.runtime.parser.node.SimpleNode;
   import org.apache.velocity.util.StringUtils;
   
  +import org.apache.velocity.exception.MethodInvocationException;
  +
   /**
    *  The function of this class is to proxy for the calling parameter to the VM.
    *
  @@ -109,7 +111,7 @@
    *  into a local context.
    *  
    *  @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
  - *  @version $Id: VMProxyArg.java,v 1.4 2001/03/05 11:45:50 jvanzyl Exp $ 
  + *  @version $Id: VMProxyArg.java,v 1.5 2001/03/19 17:13:01 geirm Exp $ 
    */
   public class VMProxyArg
   {
  @@ -203,8 +205,6 @@
        */
       public Object setObject(  InternalContextAdapter context,  Object o )
       {  
  -        //System.out.println("VMProxyArg.setObject() : " + contextReference + " / " + callerReference );
  -
           /*
            *  if we are a reference, we could be updating a property
            */
  @@ -218,10 +218,14 @@
                    *  #foo( $bar.BangStart) 
                    */
   
  -                ( (ASTReference) nodeTree).setValue( context, o );
  -
  -               // alternate impl ( (ASTReference) nodeTree).setValue(usercontext, o );
  -  
  +                try
  +                {
  +                    ( (ASTReference) nodeTree).setValue( context, o );
  +                }
  +                catch( MethodInvocationException mie )
  +                {
  +                    Runtime.error("VMProxyArg.getObject() : method invocation error setting value : " + mie );                    
  +                }
              }
               else
               {
  @@ -266,123 +270,132 @@
        */
       public Object getObject( InternalContextAdapter context )
       {        
  -        /*
  -         *  we need to output based on our type
  -         */
  -
  -        Object retObject = null;
  -        
  -        if ( type == ParserTreeConstants.JJTREFERENCE ) 
  +        try
           {
  +
               /*
  -             *  two cases :  scalar reference ($foo) or multi-level ($foo.bar....)
  +             *  we need to output based on our type
                */
   
  -            if ( numTreeChildren == 0)
  -            {      
  +            Object retObject = null;
  +            
  +            if ( type == ParserTreeConstants.JJTREFERENCE ) 
  +            {
                   /*
  -                 *  if I am a single-level reference, can I not get get it out of my context?
  +                 *  two cases :  scalar reference ($foo) or multi-level ($foo.bar....)
                    */
  +                
  +                if ( numTreeChildren == 0)
  +                {      
  +                    /*
  +                     *  if I am a single-level reference, can I not get get it out of my context?
  +                     */
  +                    
  +                    retObject = context.get( singleLevelRef );
  +                }
  +                else
  +                {
  +                    /*
  +                     *  I need to let the AST produce it for me.
  +                     */
   
  -                retObject = context.get( singleLevelRef );
  +                    retObject = nodeTree.execute( null, context);
  +                }
   
  -                //  alternate impl : retObject = usercontext.get( singleLevelRef );
  -            }
  -            else
  -            {
                   /*
  -                 *  I need to let the AST produce it for me.
  +                 *  If this resolves to null, we need just the literal representation of it
  +                 *  to support the 'cut-and-paste' features of VMs
  +                 *
  +                 *  There are reasons why people do this, although why they can't solve it
  +                 *  with some other method...
  +                 *
  +                 *  Note that we lose a current feature of VMs, rendering the literal of the 
  +                 *  original calling arg.  This needs to be fixed or we live with it.
  +                 *
  +                 *  below, what we do is technically wrong.  Will revisit.
                    */
  -
  -                retObject = nodeTree.execute( null, context);
                   
  -                // atlernate impl :retObject = nodeTree.execute( null, usercontext);
  +                if( retObject == null && numTreeChildren > 0)
  +                {
  +                    try
  +                    {
  +                        StringWriter writer =  new StringWriter() ;
  +                        nodeTree.render( context, writer );
  +                        
  +                        retObject = writer;
  +                    }
  +                    catch (Exception e ) 
  +                    {
  +                        Runtime.error("VMProxyArg.getObject() : error rendering reference : " + e );
  +                    }
  +                }         
               }
  -
  -           /*
  -            *  If this resolves to null, we need just the literal representation of it
  -            *  to support the 'cut-and-paste' features of VMs
  -            *
  -            *  There are reasons why people do this, although why they can't solve it
  -            *  with some other method...
  -            *
  -            *  Note that we lose a current feature of VMs, rendering the literal of the 
  -            *  original calling arg.  This needs to be fixed or we live with it.
  -            *
  -            *  below, what we do is technically wrong.  Will revisit.
  -            */
  -
  -            if( retObject == null && numTreeChildren > 0)
  +            else if( type == ParserTreeConstants.JJTOBJECTARRAY )
               {
  +                retObject = nodeTree.value( context );
  +            }
  +            else if ( type == ParserTreeConstants.JJTINTEGERRANGE)
  +            {
  +                retObject = nodeTree.value( context );    
  +            }
  +            else if( type == ParserTreeConstants.JJTTRUE )
  +            {
  +                retObject = staticObject;
  +            }
  +            else if ( type == ParserTreeConstants.JJTFALSE )
  +            {
  +                retObject = staticObject;
  +            }
  +            else if ( type == ParserTreeConstants.JJTSTRINGLITERAL )
  +            {
  +                retObject =  nodeTree.value( context );
  +            }
  +            else if ( type == ParserTreeConstants.JJTNUMBERLITERAL )
  +            {
  +                retObject = staticObject;
  +            }
  +            else if ( type == ParserTreeConstants.JJTTEXT )
  +            {
  +                /*
  +                 *  this really shouldn't happen.  text is just a thowaway arg for #foreach()
  +                 */
  +                           
                   try 
                   {
  -                    //System.out.println("StringWriter!");
  -                    StringWriter writer =  new StringWriter() ;
  +                    StringWriter writer =new StringWriter();
                       nodeTree.render( context, writer );
                       
                       retObject = writer;
                   }
  -                catch (Exception e ) 
  +                catch (Exception e )
                   {
                       Runtime.error("VMProxyArg.getObject() : error rendering reference : " + e );
                   }
  -                   
  -            }         
  -       }
  -       else if( type == ParserTreeConstants.JJTOBJECTARRAY )
  -       {
  -           retObject = nodeTree.value( context );
  -       }
  -       else if ( type == ParserTreeConstants.JJTINTEGERRANGE)
  -       {
  -           retObject = nodeTree.value( context );    
  -       }
  -       else if( type == ParserTreeConstants.JJTTRUE )
  -       {
  -           retObject = staticObject;
  -       }
  -       else if ( type == ParserTreeConstants.JJTFALSE )
  -       {
  -           retObject = staticObject;
  -       }
  -       else if ( type == ParserTreeConstants.JJTSTRINGLITERAL )
  -       {
  -           retObject =  nodeTree.value( context );
  -       }
  -       else if ( type == ParserTreeConstants.JJTNUMBERLITERAL )
  -       {
  -           retObject = staticObject;
  -       }
  -       else if ( type == ParserTreeConstants.JJTTEXT )
  -       {
  -           /*
  -            *  this really shouldn't happen.  text is just a thowaway arg for #foreach()
  -            */
  -           
  -           System.out.println(" VMProxyArg.getObject() : asking for TEXT ");
  -           
  -           try 
  -           {
  -               StringWriter writer =new StringWriter();
  -               nodeTree.render( context, writer );
  -                   
  -               retObject = writer;
  -           }
  -           catch (Exception e )
  -           {
  -               Runtime.error("VMProxyArg.getObject() : error rendering reference : " + e );
  -           }
  -       }
  -       else if( type ==  GENERALSTATIC )
  -       {
  -           retObject = staticObject;
  -       }
  -       else
  -       {
  -           Runtime.error("Unsupported VM arg type : VM arg = " + callerReference +" type = " + type + "( VMProxyArg.getObject() )");
  -       }
  -        
  -       return retObject;
  +            }
  +            else if( type ==  GENERALSTATIC )
  +            {
  +                retObject = staticObject;
  +            }
  +            else
  +            {
  +                Runtime.error("Unsupported VM arg type : VM arg = " + callerReference +" type = " + type + "( VMProxyArg.getObject() )");
  +            }
  +            
  +            return retObject;
  +        }
  +        catch( MethodInvocationException mie )
  +        {
  +            /*
  +             *  not ideal, but otherwise we propogate out to the 
  +             *  VMContext, and the Context interface's put/get 
  +             *  don't throw. So this is a the best compromise
  +             *  I can think of
  +             */
  +            
  +            Runtime.error("VMProxyArg.getObject() : method invocation error getting value : " + mie );
  +            
  +            return null;
  +        }
       }
   
       /**