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...@apache.org on 2002/03/25 01:45:36 UTC

cvs commit: jakarta-velocity/src/java/org/apache/velocity/runtime/directive MacroParseException.java Macro.java

geirm       02/03/24 16:45:36

  Modified:    src/java/org/apache/velocity/runtime/directive Macro.java
  Added:       src/java/org/apache/velocity/runtime/directive
                        MacroParseException.java
  Log:
  For #7384 - now #macro() won't take anything but a word for first arg
  
  Revision  Changes    Path
  1.15      +60 -41    jakarta-velocity/src/java/org/apache/velocity/runtime/directive/Macro.java
  
  Index: Macro.java
  ===================================================================
  RCS file: /home/cvs/jakarta-velocity/src/java/org/apache/velocity/runtime/directive/Macro.java,v
  retrieving revision 1.14
  retrieving revision 1.15
  diff -u -r1.14 -r1.15
  --- Macro.java	7 Nov 2001 12:59:50 -0000	1.14
  +++ Macro.java	25 Mar 2002 00:45:36 -0000	1.15
  @@ -3,7 +3,7 @@
   /*
    * The Apache Software License, Version 1.1
    *
  - * Copyright (c) 2000-2001 The Apache Software Foundation.  All rights
  + * Copyright (c) 2000-2002 The Apache Software Foundation.  All rights
    * reserved.
    *
    * Redistribution and use in source and binary forms, with or without
  @@ -66,6 +66,8 @@
   import org.apache.velocity.runtime.parser.node.SimpleNode;
   import org.apache.velocity.runtime.parser.node.NodeUtils;
   import org.apache.velocity.runtime.parser.Token;
  +import org.apache.velocity.runtime.parser.ParseException;
  +import org.apache.velocity.runtime.parser.ParserTreeConstants;
   import org.apache.velocity.runtime.RuntimeServices;
   
   /**
  @@ -85,7 +87,7 @@
    *  macro.  It is used inline in the parser when processing a directive.
    *
    * @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
  - * @version $Id: Macro.java,v 1.14 2001/11/07 12:59:50 geirm Exp $
  + * @version $Id: Macro.java,v 1.15 2002/03/25 00:45:36 geirm Exp $
    */
   public class Macro extends Directive
   {
  @@ -111,7 +113,7 @@
        *   render() doesn't do anything in the final output rendering.
        *   There is no output from a #macro() directive.
        */
  -    public boolean render( InternalContextAdapter context, 
  +    public boolean render(InternalContextAdapter context,
                              Writer writer, Node node)
           throws IOException 
       {
  @@ -122,10 +124,11 @@
           return true;
       }
    
  -    public void init( RuntimeServices rs, InternalContextAdapter context, Node node) 
  +    public void init(RuntimeServices rs, InternalContextAdapter context,
  +                     Node node)
          throws Exception
       {
  -        super.init( rs, context, node );
  +        super.init(rs, context, node);
   
           /*
            * again, don't do squat.  We want the AST of the macro 
  @@ -144,8 +147,9 @@
        *  VelocimacroProxy objects, and if not currently used, adds it
        *  to the macro Factory
        */ 
  -    public static  void processAndRegister( RuntimeServices rs,  Node node, String sourceTemplate )
  -        throws IOException
  +    public static void processAndRegister(RuntimeServices rs,  Node node,
  +                                          String sourceTemplate)
  +        throws IOException, ParseException
       {
           /*
            *  There must be at least one arg to  #macro,
  @@ -169,23 +173,41 @@
                */
               
               rs.error("#macro error : Velocimacro must have name as 1st " + 
  -                "argument to #macro()");
  -            
  -            return;
  +                "argument to #macro(). #args = " + numArgs);
  +
  +            throw new MacroParseException("First argument to #macro() must be " +
  +                    " macro name.");
  +        }
  +
  +        /*
  +         *  lets make sure that the first arg is an ASTWord
  +         */
  +
  +        int firstType = node.jjtGetChild(0).getType();
  +
  +        if(firstType != ParserTreeConstants.JJTWORD)
  +        {
  +            Token t = node.jjtGetChild(0).getFirstToken();
  +
  +            throw new MacroParseException("First argument to #macro() must be a"
  +                    + " token without surrounding \' or \", which specifies"
  +                    + " the macro name.  Currently it is a "
  +                    + ParserTreeConstants.jjtNodeName[firstType]);
  +
           }
   
           /*
            *  get the arguments to the use of the VM
            */
   
  -        String argArray[] = getArgArray( node );
  +        String argArray[] = getArgArray(node);
   	 
           /*
            *   now, try and eat the code block. Pass the root.
            */
           
           List macroArray = 
  -            getASTAsStringArray( node.jjtGetChild( numArgs - 1) );
  +            getASTAsStringArray(node.jjtGetChild(numArgs - 1));
     
           /*
            *  make a big string out of our macro
  @@ -193,18 +215,20 @@
     
           StringBuffer temp  = new StringBuffer();
   
  -        for( int i=0; i < macroArray.size(); i++)
  -            temp.append( macroArray.get(i) );
  +        for (int i=0; i < macroArray.size(); i++)
  +        {
  +            temp.append(macroArray.get(i));
  +        }
   
  -        String macroBody = temp.toString();    
  +        String macroBody = temp.toString();
      
           /*
            * now, try to add it.  The Factory controls permissions, 
            * so just give it a whack...
            */
   
  -        boolean bRet = rs.addVelocimacro( argArray[0], macroBody,  
  -                        argArray, sourceTemplate );
  +        boolean bRet = rs.addVelocimacro(argArray[0], macroBody,
  +                        argArray, sourceTemplate);
   
           return;
       }
  @@ -214,7 +238,7 @@
        *  creates an array containing the literal
        *  strings in the macro arguement
        */
  -    private static String[] getArgArray( Node node )
  +    private static String[] getArgArray(Node node)
       {
           /*
            *  remember : this includes the block tree
  @@ -224,7 +248,7 @@
   	
           numArgs--;  // avoid the block tree...
   	
  -        String argArray[] = new String[ numArgs ];
  +        String argArray[] = new String[numArgs];
   	
           int i = 0;
   	
  @@ -232,7 +256,7 @@
            *  eat the args
            */
   	
  -        while( i <  numArgs ) 
  +        while (i < numArgs)
           {
               argArray[i] = node.jjtGetChild(i).getFirstToken().image;
   
  @@ -241,24 +265,28 @@
                *  saves everyone else from having to do it
                */
   
  -            if ( i > 0)
  +            if (i > 0)
               {
  -                if ( argArray[i].startsWith("$"))
  +                if (argArray[i].startsWith("$"))
  +                {
                       argArray[i] = argArray[i]
                           .substring(1, argArray[i].length());
  +                }
               }
   
               i++;
           }
   	
  -        if ( debugMode ) 
  +        if (debugMode)
           {
  -            System.out.println("Macro.getArgArray() : #args = " + numArgs );
  -            System.out.print( argArray[0] + "(" );
  -	    
  -            for (  i = 1; i < numArgs; i++) 
  -                System.out.print(" " + argArray[i] );
  +            System.out.println("Macro.getArgArray() : #args = " + numArgs);
  +            System.out.print(argArray[0] + "(");
   	    
  +            for (i = 1; i < numArgs; i++)
  +            {
  +                System.out.print(" " + argArray[i]);
  +            }
  +
               System.out.println(" )");
           }
   	
  @@ -268,7 +296,7 @@
       /**
        *  Returns an array of the literal rep of the AST
        */
  -    private static List getASTAsStringArray( Node rootNode )
  +    private static List getASTAsStringArray(Node rootNode)
       {
           /*
            *  this assumes that we are passed in the root 
  @@ -287,9 +315,9 @@
   
           t = rootNode.getFirstToken();
   
  -        while( t != tLast ) 
  +        while (t != tLast)
           {
  -            list.add( NodeUtils.tokenLiteral( t ) );
  +            list.add(NodeUtils.tokenLiteral(t));
               t = t.next;
           }
   
  @@ -297,17 +325,8 @@
            *  make sure we get the last one...
            */
   
  -        list.add( NodeUtils.tokenLiteral( t ) );
  +        list.add(NodeUtils.tokenLiteral(t));
   
           return list;
       }
   }
  -
  -
  -
  -
  -
  -
  -
  -
  -
  
  
  
  1.1                  jakarta-velocity/src/java/org/apache/velocity/runtime/directive/MacroParseException.java
  
  Index: MacroParseException.java
  ===================================================================
  package org.apache.velocity.runtime.directive;
  
  /*
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2002 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Velocity", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Group.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  import org.apache.velocity.runtime.parser.ParseException;
  
  /**
   *  Exception to indicate problem happened while constructing #macro()
   *
   *  For internal use in parser - not to be passed to app level
   *
   * @author <a href="mailto:geirm@apache.org">Geir Magnusson Jr.</a>
   * @version $Id: MacroParseException.java,v 1.1 2002/03/25 00:45:36 geirm Exp $
   */
  public class MacroParseException extends ParseException
  {
      public MacroParseException(String msg)
      {
          super(msg);
      }
  }
  
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>