package org.apache.velocity;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.util.List;
import org.apache.velocity.context.Context;
import org.apache.velocity.context.InternalContextAdapterImpl;
import org.apache.velocity.exception.MethodInvocationException;
import org.apache.velocity.exception.ParseErrorException;
import org.apache.velocity.exception.ResourceNotFoundException;
import org.apache.velocity.exception.TemplateInitException;
import org.apache.velocity.exception.VelocityException;
import org.apache.velocity.runtime.RuntimeConstants;
import org.apache.velocity.runtime.directive.Scope;
import org.apache.velocity.runtime.directive.StopCommand;
import org.apache.velocity.runtime.parser.ParseException;
import org.apache.velocity.runtime.parser.node.SimpleNode;
import org.apache.velocity.runtime.resource.Resource;
import org.apache.velocity.runtime.resource.ResourceManager;
public class Template extends Resource
{
private String scopeName = "template";
private boolean provideScope = false;
private VelocityException errorCondition = null;
public Template()
{
super();
setType(ResourceManager.RESOURCE_TEMPLATE);
}
public boolean process()
throws ResourceNotFoundException, ParseErrorException
{
data = null;
InputStream is = null;
errorCondition = null;
try
{
is = resourceLoader.getResourceStream(name);
}
catch( ResourceNotFoundException rnfe )
{
errorCondition = rnfe;
throw rnfe;
}
if (is != null)
{
try
{
BufferedReader br = new BufferedReader( new InputStreamReader( is, encoding ) );
data = rsvc.parse( br, name);
initDocument();
return true;
}
catch( UnsupportedEncodingException uce )
{
String msg = "Template.process : Unsupported input encoding : " + encoding
+ " for template " + name;
errorCondition = new ParseErrorException( msg );
throw errorCondition;
}
catch ( ParseException pex )
{
errorCondition = new ParseErrorException(pex, name);
throw errorCondition;
}
catch ( TemplateInitException pex )
{
errorCondition = new ParseErrorException( pex, name);
throw errorCondition;
}
catch( RuntimeException e )
{
errorCondition = new VelocityException("Exception thrown processing Template "
+getName(), e);
throw errorCondition;
}
finally
{
try
{
is.close();
}
catch(IOException e)
{
if (errorCondition == null)
{
throw new VelocityException(e);
}
}
}
}
else
{
errorCondition = new ResourceNotFoundException("Unknown resource error for resource " + name );
throw errorCondition;
}
}
public void initDocument()
throws TemplateInitException
{
InternalContextAdapterImpl ica = new InternalContextAdapterImpl( new VelocityContext() );
try
{
ica.pushCurrentTemplateName( name );
ica.setCurrentResource( this );
((SimpleNode)data).init( ica, rsvc);
String property = scopeName+'.'+RuntimeConstants.PROVIDE_SCOPE_CONTROL;
provideScope = rsvc.getBoolean(property, provideScope);
}
finally
{
ica.popCurrentTemplateName();
ica.setCurrentResource( null );
}
}
public void merge( Context context, Writer writer)
throws ResourceNotFoundException, ParseErrorException, MethodInvocationException
{
merge(context, writer, null);
}
public void merge( Context context, Writer writer, List macroLibraries)
throws ResourceNotFoundException, ParseErrorException, MethodInvocationException
{
if (errorCondition != null)
{
throw errorCondition;
}
if( data != null)
{
InternalContextAdapterImpl ica = new InternalContextAdapterImpl( context );
ica.setMacroLibraries(macroLibraries);
if (macroLibraries != null)
{
for (int i = 0; i < macroLibraries.size(); i++)
{
try
{
rsvc.getTemplate((String) macroLibraries.get(i));
}
catch (ResourceNotFoundException re)
{
rsvc.getLog().error("template.merge(): " +
"cannot find template " +
(String) macroLibraries.get(i));
throw re;
}
catch (ParseErrorException pe)
{
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
{
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);
}
}
}
}
}
else
{
String msg = "Template.merge() failure. The document is null, " +
"most likely due to parsing error.";
throw new RuntimeException(msg);
}
}
}