/*
* create an InternalContextAdapter to carry the user Context down
* into the rendering engine. Set the template name and render()
*/
InternalContextAdapterImpl ica = new InternalContextAdapterImpl( context );
/**
* Set the macro libraries
*/
ica.setMacroLibraries(macroLibraries);
if (macroLibraries != null)
{
for (int i = 0; i < macroLibraries.size(); i++)
{
/**
* Build the macro library
*/
try
{
rsvc.getTemplate((String) macroLibraries.get(i));
}
catch (ResourceNotFoundException re)
{
/*
* the macro lib wasn't found. Note it and throw
*/
rsvc.getLog().error("template.merge(): " +
"cannot find template " +
(String) macroLibraries.get(i));
throw re;
}
catch (ParseErrorException pe)
{
/*
* the macro lib was found, but didn't parse - syntax error
* note it and throw
*/
rsvc.getLog().error("template.merge(): " +
"syntax error in template " +
(String) macroLibraries.get(i) + ".");
throw pe;
}
catch (Exception e)
{
throw new RuntimeException("Template.merge(): parse failed in template " +
(String) macroLibraries.get(i) + ".", e);
}
}
}
if (provideScope)
{
ica.put(scopeName, new Scope(this, ica.get(scopeName)));
}
try
{
ica.pushCurrentTemplateName( name );
ica.setCurrentResource( this );
( (SimpleNode) data ).render( ica, writer);
}
catch (StopCommand stop)
{
if (!stop.isFor(this))
{
throw stop;
}
else if (rsvc.getLog().isDebugEnabled())
{
rsvc.getLog().debug(stop.getMessage());
}
}
catch (IOException e)
{
throw new VelocityException("IO Error rendering template '"+ name + "'", e);
}
finally
{
/*
* lets make sure that we always clean up the context
*/
ica.popCurrentTemplateName();
ica.setCurrentResource( null );
if (provideScope)
{
Object obj = ica.get(scopeName);
if (obj instanceof Scope)
{
Scope scope = (Scope)obj;
if (scope.getParent() != null)
{
ica.put(scopeName, scope.getParent());
}
else if (scope.getReplaced() != null)
{
ica.put(scopeName, scope.getReplaced());
}
else
{
ica.remove(scopeName);
}
}
}
}
}