Location: Metabolic Component Library @ 2ac29dd97e92 / CellML-source / CellMLTools / src / componentlibrary / html / HTMLTemplateRenderer.java

Author:
Matthias K?nig <matthias.koenig@charite.de>
Date:
2013-05-17 20:14:36+02:00
Desc:
Further work on the HTML documentation.
Permanent Source URI:
https://models.cellml.org/w/matthiaskoenig/MetabolicComponentLibrary/rawfile/2ac29dd97e9204b8c5de02fa2ddf416afdd997e4/CellML-source/CellMLTools/src/componentlibrary/html/HTMLTemplateRenderer.java

package componentlibrary.html;

import java.io.FileWriter;
import java.io.IOException;
import java.io.StringWriter;
import java.util.TreeMap;

import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.exception.MethodInvocationException;
import org.apache.velocity.exception.ParseErrorException;
import org.apache.velocity.exception.ResourceNotFoundException;

import componentlibrary.RCellMLComponent;

/**
 * Generate the documentation for the Metabolic Component Library 
 * using velocity templates and information stored for the individual components.
 * Here the MCL component information is written in the HTML template (*.vm).
 * 
 * @author mkoenig
 *
 */
public class HTMLTemplateRenderer {

	private String f_template;
	private String f_html;
	
	public HTMLTemplateRenderer(String f_template, String f_html) throws IOException{
		this.f_template = f_template;
		this.f_html = f_html;
	}
	
	/* Render the template with the given components */
	public void render(TreeMap<String, RCellMLComponent> cMap) throws IOException{
		System.out.println("*** START TEMPLATE RENDERING ***");
		
		Velocity.init();

		// Generate the Context for the Template
		// put the components in the context
		VelocityContext context = new VelocityContext();
		//context.put( "name", new String("Velocity") );
		context.put( "cSet", cMap.values());
		
		
		// Generate the Template
		Template template = null;
		try
		{
		   template = Velocity.getTemplate(f_template);
		}
		catch( ResourceNotFoundException rnfe )
		{
		   // couldn't find the template
		   System.out.println("Template not found.");
		   System.out.println(f_template);
		}
		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(f_html);
		StringWriter sw = new StringWriter();
		template.merge( context, sw );
		
		fw.write(sw.toString());
		fw.close();
		System.out.println("HTML --> " + f_html);
		System.out.println("*** END TEMPLATE RENDERING ***");
	}
}