public void init(RuntimeServices rs,
InternalContextAdapter context,
Node node) throws TemplateInitException {
super.init(rs, context, node);
/*
* again, don't do squat. We want the AST of the macro
* block to hang off of this but we don't want to
* init it... it's useless...
*/
}
|
public static void processAndRegister(RuntimeServices rs,
Token t,
Node node,
String sourceTemplate) throws IOException, ParseException {
/*
* There must be at least one arg to #macro,
* the name of the VM. Note that 0 following
* args is ok for naming blocks of HTML
*/
int numArgs = node.jjtGetNumChildren();
/*
* this number is the # of args + 1. The + 1
* is for the block tree
*/
if (numArgs < 2)
{
/*
* error - they didn't name the macro or
* define a block
*/
rs.getLog().error("#macro error : Velocimacro must have name as 1st " +
"argument to #macro(). #args = " + numArgs);
throw new MacroParseException("First argument to #macro() must be " +
" macro name.", sourceTemplate, t);
}
/*
* lets make sure that the first arg is an ASTWord
*/
int firstType = node.jjtGetChild(0).getType();
if(firstType != ParserTreeConstants.JJTWORD)
{
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], sourceTemplate, t);
}
/*
* get the arguments to the use of the VM
*/
String argArray[] = getArgArray(node, rs);
/*
* now, try and eat the code block. Pass the root.
*/
List macroArray =
getASTAsStringArray(node.jjtGetChild(numArgs - 1));
/*
* make a big string out of our macro
*/
StringBuffer macroBody = new StringBuffer();
for (int i=0; i < macroArray.size(); i++)
{
macroBody.append(macroArray.get(i));
}
/*
* now, try to add it. The Factory controls permissions,
* so just give it a whack...
*/
boolean macroAdded = rs.addVelocimacro(argArray[0],
macroBody.toString(),
argArray, sourceTemplate);
if (!macroAdded && rs.getLog().isWarnEnabled())
{
StringBuffer msg = new StringBuffer("Failed to add macro: ");
macroToString(msg, argArray);
msg.append(" : source = ").append(sourceTemplate);
rs.getLog().warn(msg);
}
}
Used by Parser.java to process VMs during the parsing process.
This method does not render the macro to the output stream,
but rather processes the macro body into the internal
representation used by {#link
org.apache.velocity.runtime.directive.VelocimacroProxy}
objects, and if not currently used, adds it to the macro
Factory. |