/*
* did we get an argument?
*/
if ( node.jjtGetNumChildren() == 0 )
{
throw new VelocityException("#parse(): argument missing at " +
Log.formatFileString(this));
}
/*
* does it have a value? If you have a null reference, then no.
*/
Object value = node.jjtGetChild(0).value( context );
if (value == null && rsvc.getLog().isDebugEnabled())
{
rsvc.getLog().debug("#parse(): null argument at " +
Log.formatFileString(this));
}
/*
* get the path
*/
String sourcearg = value == null ? null : value.toString();
/*
* check to see if the argument will be changed by the event cartridge
*/
String arg = EventHandlerUtil.includeEvent( rsvc, context, sourcearg, context.getCurrentTemplateName(), getName());
/*
* a null return value from the event cartridge indicates we should not
* input a resource.
*/
if (arg == null)
{
// abort early, but still consider it a successful rendering
return true;
}
if (maxDepth > 0)
{
/*
* see if we have exceeded the configured depth.
*/
Object[] templateStack = context.getTemplateNameStack();
if (templateStack.length >= maxDepth)
{
StringBuffer path = new StringBuffer();
for( int i = 0; i < templateStack.length; ++i)
{
path.append( " > " + templateStack[i] );
}
rsvc.getLog().error("Max recursion depth reached (" +
templateStack.length + ')' + " File stack:" +
path);
return false;
}
}
/*
* now use the Runtime resource loader to get the template
*/
Template t = null;
try
{
t = rsvc.getTemplate( arg, getInputEncoding(context) );
}
catch ( ResourceNotFoundException rnfe )
{
/*
* the arg wasn't found. Note it and throw
*/
rsvc.getLog().error("#parse(): cannot find template '" + arg +
"', called at " + Log.formatFileString(this));
throw rnfe;
}
catch ( ParseErrorException pee )
{
/*
* the arg was found, but didn't parse - syntax error
* note it and throw
*/
rsvc.getLog().error("#parse(): syntax error in #parse()-ed template '"
+ arg + "', called at " + Log.formatFileString(this));
throw pee;
}
/**
* pass through application level runtime exceptions
*/
catch( RuntimeException e )
{
rsvc.getLog().error("Exception rendering #parse(" + arg + ") at " +
Log.formatFileString(this));
throw e;
}
catch ( Exception e)
{
String msg = "Exception rendering #parse(" + arg + ") at " +
Log.formatFileString(this);
rsvc.getLog().error(msg, e);
throw new VelocityException(msg, e);
}
/**
* Add the template name to the macro libraries list
*/
List macroLibraries = context.getMacroLibraries();
/**
* if macroLibraries are not set create a new one
*/
if (macroLibraries == null)
{
macroLibraries = new ArrayList();
}
context.setMacroLibraries(macroLibraries);
macroLibraries.add(arg);
/*
* and render it
*/
try
{
preRender(context);
context.pushCurrentTemplateName(arg);
((SimpleNode) t.getData()).render(context, writer);
}
catch( StopCommand stop )
{
if (!stop.isFor(this))
{
throw stop;
}
}
/**
* pass through application level runtime exceptions
*/
catch( RuntimeException e )
{
/**
* Log #parse errors so the user can track which file called which.
*/
rsvc.getLog().error("Exception rendering #parse(" + arg + ") at " +
Log.formatFileString(this));
throw e;
}
catch ( Exception e )
{
String msg = "Exception rendering #parse(" + arg + ") at " +
Log.formatFileString(this);
rsvc.getLog().error(msg, e);
throw new VelocityException(msg, e);
}
finally
{
context.popCurrentTemplateName();
postRender(context);