You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@velocity.apache.org by jv...@locus.apache.org on 2000/12/15 07:55:37 UTC

cvs commit: jakarta-velocity/src/java/org/apache/velocity/runtime/parser/node ASTElseIfStatement.java ASTElseStatement.java ASTIfStatement.java

jvanzyl     00/12/14 22:55:35

  Modified:    src/java/org/apache/velocity/runtime/parser/node
                        ASTElseIfStatement.java ASTElseStatement.java
                        ASTIfStatement.java
  Log:
  - The ASTIfStatement has been cleaned up and once again uses
    polymorphism to determine which node gets rendered. ASTElseStatement
    and ASTElseIfStatement once again know how to evaluate themselves
    and render themselves so the case statement has been removed.
  
    Also when the correct node is rendered the render() method
    exits instead of passing over every subsequent node. When
    an #if(true) occurred, every #elseif(expression) was still
    passed over. This has been corrected.
  
  Revision  Changes    Path
  1.4       +15 -1     jakarta-velocity/src/java/org/apache/velocity/runtime/parser/node/ASTElseIfStatement.java
  
  Index: ASTElseIfStatement.java
  ===================================================================
  RCS file: /home/cvs/jakarta-velocity/src/java/org/apache/velocity/runtime/parser/node/ASTElseIfStatement.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- ASTElseIfStatement.java	2000/11/11 22:40:04	1.3
  +++ ASTElseIfStatement.java	2000/12/15 06:55:33	1.4
  @@ -61,8 +61,9 @@
    * Please look at the Parser.jjt file which is
    * what controls the generation of this class.
    *
  + * @author <a href="mailto:jvanzyl@periapt.com">Jason van Zyl</a>
    * @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
  - * @version $Id: ASTElseIfStatement.java,v 1.3 2000/11/11 22:40:04 geirm Exp $ 
  + * @version $Id: ASTElseIfStatement.java,v 1.4 2000/12/15 06:55:33 jvanzyl Exp $ 
   */
   
   package org.apache.velocity.runtime.parser.node;
  @@ -89,5 +90,18 @@
       public Object jjtAccept(ParserVisitor visitor, Object data)
       {
           return visitor.visit(this, data);
  +    }
  +
  +    /**
  +     * An ASTElseStatement is true if the expression
  +     * it contains evaluates to true. Expressions know
  +     * how to evaluate themselves, so we do that
  +     * here and return the value back to ASTIfStatement
  +     * where this node was originally asked to evaluate
  +     * itself.
  +     */
  +    public boolean evaluate (Context context)
  +    {
  +        return jjtGetChild(0).evaluate(context);
       }
   }
  
  
  
  1.4       +12 -2     jakarta-velocity/src/java/org/apache/velocity/runtime/parser/node/ASTElseStatement.java
  
  Index: ASTElseStatement.java
  ===================================================================
  RCS file: /home/cvs/jakarta-velocity/src/java/org/apache/velocity/runtime/parser/node/ASTElseStatement.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- ASTElseStatement.java	2000/11/11 22:40:04	1.3
  +++ ASTElseStatement.java	2000/12/15 06:55:33	1.4
  @@ -61,9 +61,10 @@
    * Please look at the Parser.jjt file which is
    * what controls the generation of this class.
    *
  + * @author <a href="mailto:jvanzyl@periapt.com">Jason van Zyl</a>
    * @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
  - * @version $Id: ASTElseStatement.java,v 1.3 2000/11/11 22:40:04 geirm Exp $ 
  -*/
  + * @version $Id: ASTElseStatement.java,v 1.4 2000/12/15 06:55:33 jvanzyl Exp $ 
  + */
   
   package org.apache.velocity.runtime.parser.node;
   
  @@ -90,5 +91,14 @@
       {
           return visitor.visit(this, data);
       }
  +    
  +    /**
  +     * An ASTElseStatement always evaluates to
  +     * true. Basically behaves like an #if(true).
  +     */
  +    public boolean evaluate(Context context)
  +    {
  +        return true;
  +    }        
   }
   
  
  
  
  1.5       +33 -54    jakarta-velocity/src/java/org/apache/velocity/runtime/parser/node/ASTIfStatement.java
  
  Index: ASTIfStatement.java
  ===================================================================
  RCS file: /home/cvs/jakarta-velocity/src/java/org/apache/velocity/runtime/parser/node/ASTIfStatement.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- ASTIfStatement.java	2000/11/11 22:40:04	1.4
  +++ ASTIfStatement.java	2000/12/15 06:55:33	1.5
  @@ -60,7 +60,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: ASTIfStatement.java,v 1.4 2000/11/11 22:40:04 geirm Exp $ 
  + * @version $Id: ASTIfStatement.java,v 1.5 2000/12/15 06:55:33 jvanzyl Exp $ 
   */
   
   package org.apache.velocity.runtime.parser.node;
  @@ -89,66 +89,45 @@
           return visitor.visit(this, data);
       }
       
  -  
       public boolean render(Context context, Writer writer)
           throws IOException
       {
  -       
  -        Object data = null;
  -        Node expression;
  +        /*
  +         * Check if the #if(expression) construct evaluates to true:
  +         * if so render and leave immediately because there
  +         * is nothing left to do!
  +         */
  +        if (jjtGetChild(0).evaluate(context))
  +        {
  +            jjtGetChild(1).render(context, writer);
  +            return true;
  +        }
       
  +        int totalNodes = jjtGetNumChildren();
  +        
           /*
  -         *  iterate through the children and eval the first one that we come across
  +         * Now check the remaining nodes left in the
  +         * if construct. The nodes are either elseif
  +         *  nodes or else nodes. Each of these node
  +         * types knows how to evaluate themselves. If
  +         * a node evaluates to true then the node will
  +         * render itself and this method will return
  +         * as there is nothing left to do.
            */
  -
  -        boolean bEval = false;
  -
  -        for( int i = 0; i < jjtGetNumChildren(); i++)
  +        for (int i = 2; i < totalNodes; i++)
           {
  -            Node child = jjtGetChild(i);
  -
  -            switch( child.getType() ) {
  -
  -            case ParserTreeConstants.JJTEXPRESSION :
  -                {
  -                    /* if the expression evaluates */
  -                    if (child.evaluate(context))
  -                     {                  
  -                         /* render the block following the if */
  -                         jjtGetChild(i+1).render(context, writer);
  -                         bEval = true;
  -                     }
  -                    break;
  -                }
  -
  -            case ParserTreeConstants.JJTELSESTATEMENT :
  -                {
  -                    /* render the block if the if() was false*/
  -
  -                    if (!bEval)
  -                    {
  -                        child.jjtGetChild(0).render( context, writer );
  -                        bEval = true;
  -                    }
  -                    break;
  -                }
  -            case ParserTreeConstants.JJTELSEIFSTATEMENT :
  -                {
  -                    /* render the block if the if() was false*/
  -                    if (!bEval)
  -                    {
  -                        if ( child.jjtGetChild(0).evaluate( context ) )
  -                        {
  -                            child.jjtGetChild(1).render(context, writer);
  -                            bEval = true;
  -                        }
  -                    }
  -                    break;
  -                }
  -
  -            }  /* end switch */
  -        } /* end for */                
  -     
  +            if (jjtGetChild(i).evaluate(context))
  +            {
  +                jjtGetChild(i).render(context, writer);
  +                return true;
  +            }
  +        }
  +    
  +        /*
  +         * This is reached when an ASTIfStatement
  +         * consists of an if/elseif sequence where
  +         * none of the nodes evaluate to true.
  +         */
           return true;
       }