geirm       00/12/20 21:51:39

  Modified:    src/java/org/apache/velocity/runtime/parser/node
                        ASTStringLiteral.java
  Log:
  Reworked for neatness, efficiency, and support for single-quoted string literals :
  '$foo'
  This won't be interpolated, regardless of the interpolation setting.
  
  Revision  Changes    Path
  1.6       +66 -27    
jakarta-velocity/src/java/org/apache/velocity/runtime/parser/node/ASTStringLiteral.java
  
  Index: ASTStringLiteral.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-velocity/src/java/org/apache/velocity/runtime/parser/node/ASTStringLiteral.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- ASTStringLiteral.java     2000/12/11 04:36:03     1.5
  +++ ASTStringLiteral.java     2000/12/21 05:51:38     1.6
  @@ -59,7 +59,7 @@
    *
    * @author <a href="mailto:[EMAIL PROTECTED]">Geir Magnusson Jr.</a>
    * @author <a href="mailto:[EMAIL PROTECTED]">Jason van Zyl</a>
  - * @version $Id: ASTStringLiteral.java,v 1.5 2000/12/11 04:36:03 geirm Exp $
  + * @version $Id: ASTStringLiteral.java,v 1.6 2000/12/21 05:51:38 geirm Exp $
    */
   
   package org.apache.velocity.runtime.parser.node;
  @@ -70,10 +70,14 @@
   import java.io.StringWriter;
   import java.io.ByteArrayInputStream;
   import org.apache.velocity.runtime.Runtime;
  -import org.apache.velocity.runtime.RuntimeConstants;
   
   public class ASTStringLiteral extends SimpleNode
   {
  +    /* cache the value of the interpolation switch */
  +    private boolean interpolate = true;
  +    private SimpleNode nodeTree = null;
  +    private String image = "";
  +
       public ASTStringLiteral(int id)
       {
           super(id);
  @@ -83,7 +87,42 @@
       {
           super(p, id);
       }
  +    
  +    /**
  +     *  init : we don't have to do much.  Init the tree (there shouldn't be
  +     *  one ) and then see if interpolation is turned on.
  +     */
  +    public Object init(Context context, Object data) 
  +        throws Exception
  +    {
  +        /*
  +         *  simple habit...  we prollie don't have an AST beneath us
  +         */
  +
  +        super.init( context, data );
  +
  +        /*
  +         *  the stringlit is set at template parse time, so we can do this here for 
now.
  +         *  if things change and we can somehow create stringlits at runtime, this 
must
  +         *  move to the runtime execution path
  +         *
  +         *  so, only if interpolation is turned on AND it starts with a " AND it 
has a 
  +         *  directive or reference, then we can  interpolate.  Otherwise, don't 
bother.
  +         */
  +
  +        interpolate = Runtime.getBoolean(Runtime.INTERPOLATE_STRINGLITERALS , true)
  +            && getFirstToken().image.startsWith("\"")
  +            && ( (getFirstToken().image.indexOf("$") != -1 ) || ( 
getFirstToken().image.indexOf("#") != -1 ));
  +
  +        /*
  +         *  get the contents of the string, minus the '/" at each end
  +         */
  +        
  +        image = getFirstToken().image.substring(1, getFirstToken().image.length() - 
1);
   
  +        return data;
  +    }
  +
       /** Accept the visitor. **/
       public Object jjtAccept(ParserVisitor visitor, Object data)
       {
  @@ -98,42 +137,43 @@
        */
       public Object value(Context context)
       {
  -        /*
  -         *  check to see if there are anything that might be a reference or 
directive in the string
  -         */
  -
  -        if (  Runtime.getBoolean(RuntimeConstants.INTERPOLATE_STRINGLITERALS , 
true) && 
  -              ( getFirstToken().image.indexOf("$") != -1  || 
getFirstToken().image.indexOf("#") != -1 ) )
  -        {
  -            /*
  -             *  if so, and allowed, parse() and render()
  -             *  In the future, we will get stringlits parsed into the AST at 
template parse time
  -             */
  -            
  -            ByteArrayInputStream  inStream = new ByteArrayInputStream( 
getFirstToken().image.substring(1, getFirstToken().image.length() - 1).getBytes() );
  -            
  +        if (interpolate )
  +        {          
               try
  -            {    
  -                /*
  -                 *  parse the stringlit
  -                 */
  -                SimpleNode nodeTree = Runtime.parse( inStream, 
context.getCurrentTemplateName() );        
  -                
  +            {   
                   /*
  -                 *  init with a cloned context
  +                 *  only parse the first time
                    */
  -                Context ctxt = (Context) context.clone();
  -                nodeTree.init( ctxt, null );
  +
  +                if (nodeTree == null)
  +                {
  +                   
  +                    ByteArrayInputStream inStream = new ByteArrayInputStream( 
image.getBytes() );
  +  
  +                    /*
  +                     *  parse the stringlit
  +                     */
  +                    
  +                    nodeTree = Runtime.parse( inStream, 
context.getCurrentTemplateName() );        
                   
  +                    /*
  +                     *  init with context. It won't modify anything
  +                     */
  +
  +                    nodeTree.init( context, null );
  +                }
  +
                   /*
                    *  now render against the real context
                    */
  +
                   StringWriter writer = new StringWriter();
                   nodeTree.render(context, writer );
                   
                   /*
                    * and return the result as a String
                    */
  +
                   return writer.toString();
               }
               catch( Exception e )
  @@ -147,8 +187,7 @@
            *  ok, either not allowed to interpolate, there wasn't a ref or directive, 
or we failed, so
            *  just output the literal
            */
  -
  -        return getFirstToken().image.substring(1, getFirstToken().image.length() - 
1);
   
  +        return image;
       }
   }
  
  
  

Reply via email to