jon         00/12/11 11:42:17

  Modified:    src/java/org/apache/velocity/runtime/directive Include.java
                        Parse.java
  Log:
  various cleanup
  added support for normalizing the path
  uhh...why aren't these files being cached?
  
  Revision  Changes    Path
  1.11      +18 -25    
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.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- Include.java      2000/12/11 03:58:20     1.10
  +++ Include.java      2000/12/11 19:42:12     1.11
  @@ -61,6 +61,7 @@
   import org.apache.velocity.runtime.configuration.*;
   import org.apache.velocity.runtime.Runtime;
   import org.apache.velocity.runtime.parser.node.Node;
  +import org.apache.velocity.util.StringUtils;
   
   /**
    * Pluggable directive that handles the #include() statement in VTL. 
  @@ -93,7 +94,7 @@
    *
    * @author <a href="mailto:[EMAIL PROTECTED]">Geir Magnusson Jr.</a>
    * @author <a href="mailto:[EMAIL PROTECTED]">Jason van Zyl</a>
  - * @version $Id: Include.java,v 1.10 2000/12/11 03:58:20 geirm Exp $
  + * @version $Id: Include.java,v 1.11 2000/12/11 19:42:12 jon Exp $
    */
   public class Include extends Directive
   {
  @@ -142,7 +143,7 @@
               }
               else
               {
  -                Runtime.error( new String("#include() error : invalid argument type 
: " + n.toString() ) );
  +                Runtime.error("#include() error : invalid argument type : " + 
n.toString());
                   outputErrorToStream( writer, "error with arg " + i + " please see 
log.");
               }
           }
  @@ -161,53 +162,50 @@
       private boolean renderOutput( Node node, Context context, Writer writer )
          throws IOException
       {
  -         String arg = "";
  +        String arg = "";
           
           if ( node == null )
           {
  -            Runtime.error( new String("#include() error :  null argument") );
  +            Runtime.error("#include() error :  null argument");
               return false;
           }
               
           /*
            *  does it have a value?  If you have a null reference, then no.
  -         */
  -        
  +         */        
           Object value = node.value( context );
  -        
           if ( value == null)
           {
  -            Runtime.error( new String("#include() error :  null argument") );
  +            Runtime.error("#include() error :  null argument");
               return false;
           }
   
           /*
            *  get the path
            */
  -        
           arg = value.toString();
  -            
  +
           /*
            *  everything must be under the template root TEMPLATE_PATH
  -         */
  -        
  +         */        
           String[] includePaths = Runtime.getIncludePaths();
           
           /*
            *  for security, we will not accept anything with .. in the path
            */
  -        
  -        if ( arg.indexOf("..") != -1)
  +        arg = StringUtils.normalizePath(arg);
  +        if ( arg == null || arg.length() == 0 )
           {
  -            Runtime.error( new String("#include() error : argument " + arg + " 
contains .. and may be trying to access content outside of template root.  Rejected.") 
);
  +            Runtime.error( "#include() error : argument " + arg + 
  +                " contains .. and may be trying to access " + 
  +                "content outside of template root.  Rejected." );
               return false;
           }
   
           /*
            *  if a / leads off, then just nip that :)
            */
  -        
  -        if ( arg.startsWith( "/") )
  +        if ( arg.startsWith("/") )
               arg = arg.substring(1);
           
           /*
  @@ -215,13 +213,10 @@
            * locations. If we can't even find a trace of existence
            * report the problem and leave.
            */
  -
           File file = null;
  -        
           for (int i = 0; i < includePaths.length; i++)
           {
  -            file = new File(includePaths[i], arg);
  -            
  +            file = new File(includePaths[i], arg);            
               if (file.exists())
                   break;
               else
  @@ -246,12 +241,11 @@
           }
           catch ( Exception e ) 
           {
  -            Runtime.error( new String("#include() : " + e ));
  +            Runtime.error("#include() : " + e.toString() );
               return false;
           }
  -    
           return true;
  -    }    
  +    }
   
       /**
        *  Puts a message to the render output stream if ERRORMSG_START / END
  @@ -268,7 +262,6 @@
           {
               writer.write( outputMsgStart + " " + msg + " " + outputMsgEnd );
           }
  -
           return;
       }
   }
  
  
  
  1.10      +11 -18    
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.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- Parse.java        2000/12/11 03:54:27     1.9
  +++ Parse.java        2000/12/11 19:42:13     1.10
  @@ -83,13 +83,13 @@
    *
    * @author <a href="mailto:[EMAIL PROTECTED]">Geir Magnusson Jr.</a>
    * @author <a href="mailto:[EMAIL PROTECTED]">Jason van Zyl</a>
  - * @version $Id: Parse.java,v 1.9 2000/12/11 03:54:27 geirm Exp $
  + * @version $Id: Parse.java,v 1.10 2000/12/11 19:42:13 jon Exp $
    */
   public class Parse extends Directive
   {
  -    SimpleNode nodeTree = null;
  -    int parseDepth = 1;
  -    boolean ready = false;
  +    private SimpleNode nodeTree = null;
  +    private int parseDepth = 1;
  +    private boolean ready = false;
   
       /**
        * Return name of this directive.
  @@ -118,55 +118,49 @@
           /*
            *  did we get an argument?
            */
  -        
           if ( node.jjtGetChild(0) == null)
           {
  -            Runtime.error( new String("#parse() error :  null argument") );
  +            Runtime.error( "#parse() error :  null argument" );
               return false;
           }
           
           /*
            *  does it have a value?  If you have a null reference, then no.
            */
  -        
           Object value =  node.jjtGetChild(0).value( context );
  -        
           if ( value == null)
           {
  -            Runtime.error( new String("#parse() error :  null argument") );
  +            Runtime.error( "#parse() error :  null argument" );
               return  false;
           }
   
           /*
            *  get the path
            */
  -        
           String arg = value.toString();
                
           /*
            *  for security, we will not accept anything with .. in the path
            */
  -        
  -        if ( arg.indexOf("..") != -1)
  +        arg = StringUtils.normalizePath(arg);
  +        if ( arg == null || arg.length() == 0 )
           {
  -            Runtime.error( new String("#parse() error : argument " 
  -                                      + arg + " contains .. and may be trying to 
access content outside of template root.  Rejected.") );
  +            Runtime.error( "#parse() error : argument " + arg + 
  +                " contains .. and may be trying to access" + 
  +                " content outside of template root.  Rejected." );
               return false;
           }
   
           /*
            *  if a / leads off, then just nip that :)
            */
  -        
           if ( arg.startsWith( "/") )
               arg = arg.substring(1);
   
           /*
            *  we will put caching here in the future...
            */
  -
           Template t = null;
  -
           try 
           {
               t = Runtime.getTemplate( arg );   
  @@ -219,7 +213,6 @@
           /*
            *  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);
   
  
  
  

Reply via email to