- Author:
- Tommy Yu <tommy.yu@auckland.ac.nz>
- Date:
- 2015-02-12 19:28:32+13:00
- Desc:
- Merging to remove the extra head
- Since the title is changed and in the interest of maintaining the
current structure we keep it as is.
- Permanent Source URI:
- https://staging.physiomeproject.org/w/matthiaskoenig/MetabolicComponentLibrary/rawfile/e0e2c28e2dfdfe37ea2cae3390a6ae26c28a3393/CellML-source/CellMLTools/src/componentlibrary/html/VelocityTemplateRenderer.java
package componentlibrary.html;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringWriter;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.Template;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.exception.ResourceNotFoundException;
import org.apache.velocity.exception.ParseErrorException;
import org.apache.velocity.exception.MethodInvocationException;
/**
* Example how to use Apache Velocity to render an HTML template.
*
*/
public class VelocityTemplateRenderer {
public static void render() throws IOException{
Velocity.init();
// Generate the Context for the Template
VelocityContext context = new VelocityContext();
context.put( "name", new String("Velocity") );
// Generate the Template
Template template = null;
try
{
template = Velocity.getTemplate("template/mytemplate.vm");
}
catch( ResourceNotFoundException rnfe )
{
// couldn't find the template
System.out.println("Template not found.");
}
catch( ParseErrorException pee )
{
// syntax error: problem parsing the template
System.out.println("Template not parsable.");
}
catch( MethodInvocationException mie )
{
// something invoked in the template
// threw an exception
System.out.println("Template method invocation error.");
}
catch( Exception e )
{}
// Render the Template
FileWriter fw = new FileWriter("template/mytemplate.html");
StringWriter sw = new StringWriter();
template.merge( context, sw );
fw.write(sw.toString());
fw.close();
}
public static void main(String[] args) throws IOException{
render();
}
}