You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@velocity.apache.org by Christoph Reck <Ch...@dlr.de> on 2001/02/01 11:26:51 UTC

[PATCH] parseDepth fix

The VTL code below shows the problem when parse_directive.maxdepth
is set to 10 or less. Please add this to the test cases.
----------------------------------------------------------------
#macro( recurse $count )
#set( $depth = $depth + 1 )
#parse('parse.vm')
#set( $depth = $depth - 1 )
#end
##
## start at depth=1 horizontal repetitions,
#if( !$count )
  #set( $depth = 1 )
  entering recursion
  #set( $count = 5 )#parse('parse.vm')
  entering recursion
  #set( $count = 4 )#parse('parse.vm')
  entering recursion
  #set( $count = 3 )#parse('parse.vm')
  entering recursion
  #set( $count = 2 )#parse('parse.vm')
  entering recursion
  #set( $count = 1 )#parse('parse.vm')
  Done.
#else
  at: $depth/$count
#set( $count = $count - 1 )
  #if($count > 0)#recurse($count)#else#**#exitting recursion
#end
#end
-------------- should produce the following output -------------
  entering recursion
    at: 1/5
    at: 2/4
    at: 3/3
    at: 4/2
    at: 5/1
  exitting recursion
  entering recursion
    at: 1/4
    at: 2/3
    at: 3/2
    at: 4/1
  exitting recursion
  entering recursion
    at: 1/3
    at: 2/2
    at: 3/1
  exitting recursion
  entering recursion
    at: 1/2
    at: 2/1
  exitting recursion
  entering recursion
    at: 1/1
  exitting recursion
  Done.
----------------------------------------------------------------
If you indent the "#set( $count = $count - 1 )" statement you get
undesired whitespaces, if you glue it to the text above it, it
exposes a *new* parser bug.

I fixed the InternalContextAdapter interface and implementations 
to provide a stack of the templates being parsed. Removed the
special handling of #parse in ASTDirective and updated Parse
to use the new stack.

patched files:
  Template.java
  InternalContextAdapterImpl.java 
  InternalContextBase.java
  InternalHousekeepingContext.java
  VMContext.java
  Parse.java
  ASTDirective.java

Next are the patched file diffs:
----------------------------------------------------------------
--- Apache-Velocity-20010131/src/java/org/apache/velocity/Template.java Sat Jan 13 17:53:10 2001
+++ Apache-Velocity/src/java/org/apache/velocity/Template.java  Wed Jan 31 15:38:15 2001
@@ -151,7 +151,7 @@
         
         InternalContextAdapterImpl ica = new InternalContextAdapterImpl(  new VelocityContext() );
 
-        ica.setCurrentTemplateName( name );
+        ica.pushCurrentTemplateName( name );
         
         ((SimpleNode)data).init( ica, null);
     }
@@ -178,7 +178,7 @@
 
             InternalContextAdapterImpl ica = new InternalContextAdapterImpl( context );
 
-            ica.setCurrentTemplateName( name );
+            ica.pushCurrentTemplateName( name );
 
             ( (SimpleNode) data ).render( ica, writer);
         }
@@ -187,4 +187,3 @@
                           "most likely due to parsing error.");
     }
 }
-
----------------------------------------------------------------
--- Apache-Velocity-20010131/src/java/org/apache/velocity/context/InternalContextAdapterImpl.java       Sat Jan 13 17:33:57 2001
+++ Apache-Velocity/src/java/org/apache/velocity/context/InternalContextAdapterImpl.java        Wed Jan 31 16:50:20 2001
@@ -130,9 +130,14 @@
 
     /* --- InternalContext interface methods --- */
 
-    public void setCurrentTemplateName( String s )
+    public void pushCurrentTemplateName( String s )
     {
-        icb.setCurrentTemplateName( s );
+        icb.pushCurrentTemplateName( s );
+    }
+
+    public void popCurrentTemplateName()
+    {
+        icb.popCurrentTemplateName();
     }
   
     public String getCurrentTemplateName()
@@ -140,6 +145,11 @@
         return icb.getCurrentTemplateName();
     }
 
+    public Object[] getTemplateNameStack()
+    {
+        return icb.getTemplateNameStack();
+    }
+
     public IntrospectionCacheData icacheGet( Object key )
     {
         return icb.icacheGet( key );
----------------------------------------------------------------
--- Apache-Velocity-20010131/src/java/org/apache/velocity/context/InternalContextBase.java      Sat Jan 13 17:36:19 2001
+++ Apache-Velocity/src/java/org/apache/velocity/context/InternalContextBase.java       Wed Jan 31 15:50:35 2001
@@ -55,6 +55,7 @@
  */
 
 import java.util.HashMap;
+import java.util.Stack;
 import java.io.Serializable;
 
 import org.apache.velocity.util.introspection.IntrospectionCacheData;
@@ -79,21 +80,30 @@
     /**
      *  cache for node/context specific introspection information
      */
-    private HashMap introspectionCache = new HashMap();
+    private HashMap introspectionCache = new HashMap(33);
     
     /**
-     *  Current template name.
+     *  Template name stack. The stack top contains the current template name.
      */
-    private String strCurrentTemplate = "<undef>";
+    private Stack templateNameStack = new Stack();
 
     /**
-     *  set the current template name
+     *  set the current template name on top of stack
      *
-     *  @param s current template name to set
+     *  @param s current template name
      */
-    public void setCurrentTemplateName( String s )
+    public void pushCurrentTemplateName( String s )
     {
-        strCurrentTemplate = s;
+        templateNameStack.push(s);
+        return;
+    }
+
+    /**
+     *  remove the current template name from stack
+     */
+    public void popCurrentTemplateName()
+    {
+        templateNameStack.pop();
         return;
     }
      
@@ -104,7 +114,20 @@
      */
     public String getCurrentTemplateName()
     {
-        return strCurrentTemplate;
+        if ( templateNameStack.empty() )
+            return "<undef>";
+        else
+            return (String) templateNameStack.peek();
+    }
+
+    /**
+     *  get the current template name stack
+     *
+     *  @return Object[] with the template name stack contents.
+     */
+    public Object[] getTemplateNameStack()
+    {
+        return templateNameStack.toArray();
     }
 
     /**
----------------------------------------------------------------
--- Apache-Velocity-20010131/src/java/org/apache/velocity/context/InternalHousekeepingContext.java      Sat Jan 13 17:24:41 2001
+++ Apache-Velocity/src/java/org/apache/velocity/context/InternalHousekeepingContext.java       Wed Jan 31 15:42:53 2001
@@ -71,11 +71,16 @@
 interface InternalHousekeepingContext
 {
     /**
-     *  set the current template name
+     *  set the current template name on top of stack
      *
-     *  @param s current template name to set
+     *  @param s current template name
      */
-    void setCurrentTemplateName( String s );
+    void pushCurrentTemplateName( String s );
+
+    /**
+     *  remove the current template name from stack
+     */
+    void popCurrentTemplateName();
     
     /**
      *  get the current template name
@@ -85,6 +90,13 @@
     String getCurrentTemplateName();
 
     /**
+     *  Returns the template name stack in form of an array.
+     *
+     *  @return Object[] with the template name stack contents.
+     */
+    Object[] getTemplateNameStack();
+
+    /**
      *  returns an IntrospectionCache Data (@see IntrospectionCacheData)
      *  object if exists for the key
      *
----------------------------------------------------------------
--- Apache-Velocity-20010131/src/java/org/apache/velocity/context/VMContext.java        Sun Jan 14 15:53:14 2001
+++ Apache-Velocity/src/java/org/apache/velocity/context/VMContext.java Wed Jan 31 15:52:40 2001
@@ -268,9 +268,14 @@
         return vmproxyhash.remove( key );
     }
 
-    public void setCurrentTemplateName( String s )
+    public void pushCurrentTemplateName( String s )
     {
-        innerContext.setCurrentTemplateName( s );
+        innerContext.pushCurrentTemplateName( s );
+    }
+
+    public void popCurrentTemplateName()
+    {
+        innerContext.popCurrentTemplateName();
     }
    
     public String getCurrentTemplateName()
@@ -278,6 +283,11 @@
         return innerContext.getCurrentTemplateName();
     }
 
+    public Object[] getTemplateNameStack()
+    {
+        return innerContext.getTemplateNameStack();
+    }
+
     public IntrospectionCacheData icacheGet( Object key )
     {
         return innerContext.icacheGet( key );
----------------------------------------------------------------
--- Apache-Velocity-20010131/src/java/org/apache/velocity/runtime/directive/Parse.java  Wed Jan  3 06:28:33 2001
+++ Apache-Velocity/src/java/org/apache/velocity/runtime/directive/Parse.java   Wed Jan 31 17:02:39 2001
@@ -64,7 +64,6 @@
 import org.apache.velocity.runtime.parser.ParserTreeConstants;
 import org.apache.velocity.runtime.parser.node.Node;
 import org.apache.velocity.runtime.parser.node.SimpleNode;
-import org.apache.velocity.runtime.visitor.ParseDirectiveVisitor;
 import org.apache.velocity.util.StringUtils;
 
 /**
@@ -88,8 +87,6 @@
  */
 public class Parse extends Directive
 {
-    private SimpleNode nodeTree = null;
-    private int parseDepth = 1;
     private boolean ready = false;
 
     /**
@@ -140,6 +137,25 @@
          */
         String arg = value.toString();
              
+        /*
+         * see if we have exceeded the configured depth.
+         * If it isn't configured, put a stop at 20 just in case.
+         */
+        Object[] templateStack = context.getTemplateNameStack();
+        if ( templateStack.length >=
+             Runtime.getInt(Runtime.PARSE_DIRECTIVE_MAXDEPTH, 20) )
+        {
+            StringBuffer path = new StringBuffer();
+            for( int i = 0; i < templateStack.length; ++i)
+            {
+                path.append( " > " + templateStack[i] );
+            }
+
+            Runtime.error( "Max recursion depth reached (" + templateStack.length + ")"
+              + " File stack:" + path );
+            return false;
+        }
+
         Template t = null;
         try 
         {
@@ -155,49 +171,19 @@
 
         try
         {        
-            //nodeTree = t.getDocument();
-            nodeTree = (SimpleNode) t.getData();
-            
-            ParseDirectiveVisitor v = new ParseDirectiveVisitor();
-            v.setDepth( parseDepth );
-            v.setContext( null );
-            v.setWriter( null );
-            nodeTree.jjtAccept( v, null );
-            nodeTree.init( context, null );
-            
-            ready = true;
-        }
-        catch ( ParseDirectiveException pde )
-        {
-            pde.addFile( arg );
-            Runtime.error( "Parse.render() : " + pde );
+            context.pushCurrentTemplateName(arg);
+            ((SimpleNode) t.getData()).render( context, writer );
         }
         catch ( Exception e )
         {        
             Runtime.error( "Parse.render() : " + e );
+            return false;
         }
-
-        if ( ready )
-            nodeTree.render( context, writer );
-
-        return true;
-    }
-
-    /**
-     *  Sets the depth of recursive parsing
-     */
-    public void setParseDepth( int i )
-        throws Exception
+        finally
     {
-        parseDepth = i;
-
-        /*
-         *  see if we have exceeded the configured depth.  If it isn't configured, put a stop at 20 just in case.
-         */
-        if ( parseDepth >= Runtime.getInt(Runtime.PARSE_DIRECTIVE_MAXDEPTH, 20))
-                throw new ParseDirectiveException("Max recursion depth reached.", parseDepth);
+            context.popCurrentTemplateName();
+        }
 
-        return;
+        return true;
     }
 }
-
----------------------------------------------------------------
--- Apache-Velocity-20010131/src/java/org/apache/velocity/runtime/parser/node/ASTDirective.java Wed Jan  3 06:24:59 2001
+++ Apache-Velocity/src/java/org/apache/velocity/runtime/parser/node/ASTDirective.java  Wed Jan 31 15:17:31 2001
@@ -84,8 +84,6 @@
     private String directiveName = "";
     private boolean isDirective;
 
-    int parseDepth = 0;
-
     public ASTDirective(int id)
     {
         super(id);
@@ -117,12 +115,6 @@
             directive = (Directive) parser.getDirective( directiveName )
                 .getClass().newInstance();
             
-            /*
-             *  we need to treat #parse differently, alas
-             */
-            if ( directiveName.equals("parse"))
-                ( (Parse) directive).setParseDepth( parseDepth );
-            
             directive.init(context,this);
         }          
         else if (Runtime.isVelocimacro( directiveName, context.getCurrentTemplateName()  )) 
@@ -180,16 +172,4 @@
         return directiveName;
     }
 
-    /**
-     *  Sets the parse depth for recursion limitataion
-     */
-    public void setParserDepth( int i)
-    {
-        parseDepth = i;
     }
-}
-
-
-
-
-
+        if ( right.getFirstToken().image.startsWith("$!") )
+        {
+            quietReference = true;
+        }
         blather = Runtime.getBoolean(Runtime.RUNTIME_LOG_REFERENCE_LOG_INVALID, true);
  
         return data;
@@ -129,15 +134,13 @@
         /*
          * it's an error if we don't have a value of some sort
          */
-
-        if ( value  == null)
+        if ( (value == null) && !quietReference )
         {
             if(blather)
-                Runtime.error("RHS of #set statement is null. Context will not be modified. " 
-                          + context.getCurrentTemplateName() + " [line " + getLine() 
+                Runtime.error("RHS of #set statement is null. "
+                          + context.getCurrentTemplateName() +
+                          " [line " + getLine()
                           + ", column " + getColumn() + "]");
-
-            return false;
         }                
 
         /*
----------------------------------------------------------------

If you need the original files as an attachment, I can send these
to you.

:) Chirstoph