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...@locus.apache.org on 2000/11/11 23:40:05 UTC

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

geirm       00/11/11 14:40:05

  Modified:    src/java/org/apache/velocity/runtime/parser/node
                        ASTDirective.java ASTElseIfStatement.java
                        ASTElseStatement.java ASTIfStatement.java
                        ASTReference.java ASTSetDirective.java
  Log:
  Reworked AST nodes to remove their escape handling, as it is now in the parser and two supporting nodes, ASTEscape.java and ASTEscapedDirective.java.
  
  Revision  Changes    Path
  1.7       +15 -78    jakarta-velocity/src/java/org/apache/velocity/runtime/parser/node/ASTDirective.java
  
  Index: ASTDirective.java
  ===================================================================
  RCS file: /home/cvs/jakarta-velocity/src/java/org/apache/velocity/runtime/parser/node/ASTDirective.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- ASTDirective.java	2000/11/07 21:30:52	1.6
  +++ ASTDirective.java	2000/11/11 22:40:04	1.7
  @@ -64,7 +64,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: ASTDirective.java,v 1.6 2000/11/07 21:30:52 geirm Exp $ 
  + * @version $Id: ASTDirective.java,v 1.7 2000/11/11 22:40:04 geirm Exp $ 
   */
   
   package org.apache.velocity.runtime.parser.node;
  @@ -79,10 +79,8 @@
   public class ASTDirective extends SimpleNode
   {
       private Directive directive;
  -    private String directiveName;
  +    private String strDirectiveName_ = "";
       private boolean isDirective;
  -    private boolean bIsEscaped_ = false;
  -    private String strPrefix_ = "";
   
       public ASTDirective(int id)
       {
  @@ -103,48 +101,11 @@
       
       public Object init(Context context, Object data) throws Exception
       {
  -        /*
  -         *  first look for the 'escaped' case...
  -         */
  -
  -        bIsEscaped_ = false;
  -        isDirective = false;
  -
  -        directiveName = getFirstToken().image.substring(1);
  -
  -        if ( getFirstToken().image.startsWith("\\") )
  -        {
  -            /* 
  -             *  count the escapes : even # -> not escaped, odd -> escaped
  -             */
  -
  -            int i = 0;
  -            int iLen = getFirstToken().image.length();
  -
  -            while( i < iLen && getFirstToken().image.charAt(i) == '\\' )
  -                i++;
  -
  -            if ( (i % 2) != 0 )                
  -                bIsEscaped_ = true;
  -
  -            if (i > 0)
  -                strPrefix_ = getFirstToken().image.substring(0, i / 2 );
  -
  -            directiveName = getFirstToken().image.substring(i+1);
  - 
  -           if (bIsEscaped_)
  -                return data;
  -         }
  -
  -        /*
  -         *   normal processing 
  -         */
  -        
  -        if (parser.isDirective(directiveName))
  +        if (parser.isDirective( strDirectiveName_ ))
           {
               isDirective = true;
               
  -            directive = (Directive) parser.getDirective(directiveName)
  +            directive = (Directive) parser.getDirective( strDirectiveName_ )
                   .getClass().newInstance();
               
               directive.init(context,this);
  @@ -160,54 +121,30 @@
       public boolean render(Context context, Writer writer)
           throws IOException
       {
  -        
  +     
           /*
  -         *  handle the escaped case first
  -         */
  -
  -        if (bIsEscaped_)
  -        {
  -            /*
  -             *  if it is an escaped PD, write it as #foo else
  -             *  write it is \#foo
  -             */
  -
  -            if ( parser.isDirective( directiveName ) )
  -                writer.write( strPrefix_ + "#" + directiveName );
  -            else
  -            { 
  -                /*
  -                 *  do two strPrefix_, because if this is not a directive, it's schmoo, and therefore
  -                 *  the \ have no magic binding properties
  -                 */
  -                writer.write( strPrefix_ + strPrefix_ + "\\#" + directiveName );       
  -            }
  -
  -            return true;
  -        }
  -
  -        /*
            *  normal processing
            */
   
           if (isDirective)
  -        {
  -            if (strPrefix_.length() > 0)
  -                writer.write( strPrefix_ );
  +        {           
               directive.render(context, writer, this);
  -
           }
           else
           {
  -            /*
  -             *  do two strPrefix_, because if this is not a directive, it's schmoo, and therefore
  -             *  the \ have no magic binding properties
  -             */
  -            writer.write( strPrefix_ + strPrefix_ +  "#" + directiveName);
  +            writer.write( "#" +   strDirectiveName_);
           }
   
           return true;
       }
  +
  +    public void setDirectiveName( String str )
  +    {
  +        strDirectiveName_ = str;
  +        return;
  +    }
   }
  +
  +
   
   
  
  
  
  1.3       +1 -47     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.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- ASTElseIfStatement.java	2000/11/07 21:30:56	1.2
  +++ ASTElseIfStatement.java	2000/11/11 22:40:04	1.3
  @@ -62,7 +62,7 @@
    * what controls the generation of this class.
    *
    * @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
  - * @version $Id: ASTElseIfStatement.java,v 1.2 2000/11/07 21:30:56 geirm Exp $ 
  + * @version $Id: ASTElseIfStatement.java,v 1.3 2000/11/11 22:40:04 geirm Exp $ 
   */
   
   package org.apache.velocity.runtime.parser.node;
  @@ -75,8 +75,6 @@
   
   public class ASTElseIfStatement extends SimpleNode
   {
  -   public String strPrefix_ = "";
  -
       public ASTElseIfStatement(int id)
       {
           super(id);
  @@ -85,50 +83,6 @@
       public ASTElseIfStatement(Parser p, int id)
       {
           super(p, id);
  -    }
  -
  -    public Object init(Context context, Object data) throws Exception
  -    {
  -        /*
  -         * init our tree correctly
  -         */
  -        
  -        super.init( context, data );
  -        
  -        /*
  -         *  see if we have any escape shmoo attached...  
  -         */
  -        
  -        Token t = getFirstToken();
  -        
  -        strPrefix_ = "";
  -        
  -        if (t.image.startsWith("\\"))
  -        {
  -            int i = 0;
  -            int iLen = t.image.length();
  -            
  -            while( i < iLen && t.image.charAt(i) == '\\' )
  -                i++;
  -            
  -            if (i > 0)
  -                strPrefix_ = t.image.substring(0, i / 2 );
  -        }
  -        
  -        return data;
  -    }
  - 
  -   public boolean render(Context context, Writer writer)
  -        throws IOException
  -    {
  -        /*
  -         *  always write out the prefix. 
  -         */
  -
  -         if ( strPrefix_.length() > 0)
  -            writer.write( strPrefix_);
  -    
  -        return true;
       }
   
       /** Accept the visitor. **/
  
  
  
  1.3       +1 -47     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.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- ASTElseStatement.java	2000/11/07 21:30:58	1.2
  +++ ASTElseStatement.java	2000/11/11 22:40:04	1.3
  @@ -62,7 +62,7 @@
    * what controls the generation of this class.
    *
    * @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
  - * @version $Id: ASTElseStatement.java,v 1.2 2000/11/07 21:30:58 geirm Exp $ 
  + * @version $Id: ASTElseStatement.java,v 1.3 2000/11/11 22:40:04 geirm Exp $ 
   */
   
   package org.apache.velocity.runtime.parser.node;
  @@ -75,8 +75,6 @@
   
   public class ASTElseStatement extends SimpleNode
   {
  -    public String strPrefix_ = "";
  -
       public ASTElseStatement(int id)
       {
           super(id);
  @@ -85,50 +83,6 @@
       public ASTElseStatement(Parser p, int id)
       {
           super(p, id);
  -    }
  -
  -    public Object init(Context context, Object data) throws Exception
  -    {
  -        /*
  -         * init our tree correctly
  -         */
  -        
  -        super.init( context, data );
  -        
  -        /*
  -         *  see if we have any escape shmoo attached...  
  -         */
  -        
  -        Token t = getFirstToken();
  -        
  -        strPrefix_ = "";
  -        
  -        if (t.image.startsWith("\\"))
  -        {
  -            int i = 0;
  -            int iLen = t.image.length();
  -            
  -            while( i < iLen && t.image.charAt(i) == '\\' )
  -                i++;
  -            
  -            if (i > 0)
  -                strPrefix_ = t.image.substring(0, i / 2 );
  -        }
  -        
  -        return data;
  -    }
  - 
  -    public boolean render(Context context, Writer writer)
  -        throws IOException
  -    {
  -        /*
  -         *  always write out the prefix. 
  -         */
  -
  -        if ( strPrefix_.length() > 0)
  -            writer.write(strPrefix_);
  -    
  -        return true;
       }
   
       /** Accept the visitor. **/
  
  
  
  1.4       +46 -120   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.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- ASTIfStatement.java	2000/11/07 21:31:00	1.3
  +++ ASTIfStatement.java	2000/11/11 22:40:04	1.4
  @@ -55,15 +55,12 @@
   
   
   /**
  - * Handles the VTL EndStatements.  Right now, this is only needed for 
  - * proper handling of escape output
  - * 
    * 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: ASTIfStatement.java,v 1.3 2000/11/07 21:31:00 geirm Exp $ 
  + * @version $Id: ASTIfStatement.java,v 1.4 2000/11/11 22:40:04 geirm Exp $ 
   */
   
   package org.apache.velocity.runtime.parser.node;
  @@ -75,9 +72,7 @@
   import org.apache.velocity.runtime.parser.*;
   
   public class ASTIfStatement extends SimpleNode
  -{
  -    public String strPrefix_ = "";
  -    
  +{    
       public ASTIfStatement(int id)
       {
           super(id);
  @@ -94,135 +89,66 @@
           return visitor.visit(this, data);
       }
       
  -    public Object init(Context context, Object data) throws Exception
  -    {
  -        /*
  -         * init our tree correctly
  -         */
  -        
  -        super.init( context, data );
  -        
  -        /*
  -         *  see if we have any escape shmoo attached...  
  -         */
  -        
  -        Token t = getFirstToken();
  -        
  -        strPrefix_ = "";
  -        
  -        if (t.image.startsWith("\\"))
  -        {
  -            int i = 0;
  -            int iLen = t.image.length();
  -            
  -            while( i < iLen && t.image.charAt(i) == '\\' )
  -                i++;
  -            
  -            if (i > 0)
  -                strPrefix_ = t.image.substring(0, i / 2 );
  -        }
  -        
  -        return data;
  -    }
  -
  -
  +  
       public boolean render(Context context, Writer writer)
           throws IOException
       {
  -        /*
  -         *  always write out the prefix. 
  -         */
  -
  -        if ( strPrefix_.length() > 0)
  -            writer.write( strPrefix_ );
  -
  -        /*
  -         *  and the regular processing
  -         */
  -
  +       
           Object data = null;
           Node expression;
  -        
  -        // Only process the child nodes if the expression
  -        // evaluates to true. But we also need to look for
  -        // the presence of an else block and process that
  -        // if the expression doesn't evaluate to true.
  -        //if (evaluateExpression(expression))
  -
  +    
           /*
  -         *  $$$ gmj
  -         *  This is a mess (the logic so we are sure to render each control node...must revisit)
  +         *  iterate through the children and eval the first one that we come across
            */
   
  -        expression = jjtGetChild(0);
  +        boolean bEval = false;
   
  -        if (expression.evaluate(context))
  +        for( int i = 0; i < jjtGetNumChildren(); i++)
           {
  -            /*
  -             *  this is the block following the if
  -             */
  -
  -            jjtGetChild(1).render(context, writer);
  -
  -            /*
  -             *  if there is an #else or #elseif, we need to render that beause of the strange way escaping works
  -             */
  -            
  -            int iChildren = jjtGetNumChildren();
  -            
  -            for (int i = 2; i < iChildren; i++)
  -            {
  -                Node child = jjtGetChild(i);
  -                
  -                if (child.getType() == ParserTreeConstants.JJTELSESTATEMENT 
  -                    || child.getType() == ParserTreeConstants.JJTELSEIFSTATEMENT
  -                    || child.getType() == ParserTreeConstants.JJTENDSTATEMENT )
  -                {    
  -                        child.render(context, writer);
  +            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;
                   }
  -            }
  -            
  -        }
  -        else
  -        {
  -            /* The condition for the if statement above evaluated
  -             * to false so now we walk through the child nodes
  -             * looking for elseif/else children.
  -             */
  -            
  -            int children = jjtGetNumChildren();
  -            
  -            for (int i = 2; i < children; i++)
  -            {
  -                Node child = jjtGetChild(i);
  -                
  -                /*
  -                 * so we get the \\#else and \\#elseif and \\#end
  -                 */
  -                
  -                if (child.getType() == ParserTreeConstants.JJTELSESTATEMENT 
  -                    || child.getType() == ParserTreeConstants.JJTELSEIFSTATEMENT
  -                    || child.getType() == ParserTreeConstants.JJTENDSTATEMENT )
  -                {    
  -                        child.render(context, writer);
  +
  +            case ParserTreeConstants.JJTELSESTATEMENT :
  +                {
  +                    /* render the block if the if() was false*/
  +
  +                    if (!bEval)
  +                    {
  +                        child.jjtGetChild(0).render( context, writer );
  +                        bEval = true;
  +                    }
  +                    break;
                   }
  - 
  -                if (child.getType() == ParserTreeConstants.JJTELSEIFSTATEMENT)
  +            case ParserTreeConstants.JJTELSEIFSTATEMENT :
                   {
  -                    expression = child.jjtGetChild(0);
  -            
  -                    if (expression.evaluate(context))
  +                    /* render the block if the if() was false*/
  +                    if (!bEval)
                       {
  -                        child.jjtGetChild(1).render(context, writer);
  -                        //        break;
  +                        if ( child.jjtGetChild(0).evaluate( context ) )
  +                        {
  +                            child.jjtGetChild(1).render(context, writer);
  +                            bEval = true;
  +                        }
                       }
  +                    break;
                   }
  -                else
  -                    if (child.getType() != ParserTreeConstants.JJTENDSTATEMENT )
  -                        child.jjtGetChild(0).render(context, writer);
  -            }
  -        }
  -    
  +
  +            }  /* end switch */
  +        } /* end for */                
  +     
           return true;
       }
   
  
  
  
  1.10      +1 -3      jakarta-velocity/src/java/org/apache/velocity/runtime/parser/node/ASTReference.java
  
  Index: ASTReference.java
  ===================================================================
  RCS file: /home/cvs/jakarta-velocity/src/java/org/apache/velocity/runtime/parser/node/ASTReference.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- ASTReference.java	2000/11/07 21:31:01	1.9
  +++ ASTReference.java	2000/11/11 22:40:05	1.10
  @@ -64,7 +64,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: ASTReference.java,v 1.9 2000/11/07 21:31:01 geirm Exp $ 
  + * @version $Id: ASTReference.java,v 1.10 2000/11/11 22:40:05 geirm Exp $ 
   */
   
   package org.apache.velocity.runtime.parser.node;
  @@ -303,8 +303,6 @@
   
               if (i > 0)
                   strPrefix_ = t.image.substring(0, i / 2 );
  -
  -            //System.out.println( t.image + " " + i + " " + strPrefix_ );
   
               t.image = t.image.substring(i);
           }
  
  
  
  1.7       +1 -33     jakarta-velocity/src/java/org/apache/velocity/runtime/parser/node/ASTSetDirective.java
  
  Index: ASTSetDirective.java
  ===================================================================
  RCS file: /home/cvs/jakarta-velocity/src/java/org/apache/velocity/runtime/parser/node/ASTSetDirective.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- ASTSetDirective.java	2000/11/07 21:31:03	1.6
  +++ ASTSetDirective.java	2000/11/11 22:40:05	1.7
  @@ -59,7 +59,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: ASTSetDirective.java,v 1.6 2000/11/07 21:31:03 geirm Exp $
  + * @version $Id: ASTSetDirective.java,v 1.7 2000/11/11 22:40:05 geirm Exp $
    */
   
   package org.apache.velocity.runtime.parser.node;
  @@ -77,7 +77,6 @@
       private Node right;
       private ASTReference left;
       private Object value;
  -    public String strPrefix_ = "";
       
       public ASTSetDirective(int id)
       {
  @@ -118,26 +117,6 @@
   
           super.init( context, data );
   
  -        /*
  -         *  see if we have any escape shmoo attached...  
  -         */
  -
  -        Token t = getFirstToken();
  -
  -        strPrefix_ = "";
  -
  -        if (t.image.startsWith("\\"))
  -        {
  -            int i = 0;
  -            int iLen = t.image.length();
  -            
  -            while( i < iLen && t.image.charAt(i) == '\\' )
  -                i++;
  -                
  -            if (i > 0)
  -                strPrefix_ = t.image.substring(0, i / 2 );
  -        }
  -
           /**
            * We need to place all RHS objects into the context
            * so that subsequent introspection is performed
  @@ -184,17 +163,6 @@
       public boolean render(Context context, Writer writer)
           throws IOException
       {
  -        /*
  -         *  write any output we need to do (should be just escapes now)
  -         */
  -        
  -        if ( strPrefix_.length() > 0)
  -            writer.write( strPrefix_ );
  -           
  -        /*
  -         *  regular processing
  -         */
  -
           right = getRightHandSide();
   
           if (right.value(context) == null)