Code example for Properties

Methods: setProperty

0
import org.hivedb.HiveRuntimeException; 
 
public class Templater { 
	public static String render(String templateFile, Context context) {
		try { 
			Velocity.init(getDefaultVelocityProperties()); 
		} catch (Exception e) {
			throw new HiveRuntimeException("Failed to initialize Velocity templatng engine."); 
		} 
		 
		Template template = null;
		try { 
			template = Velocity.getTemplate(templateFile);
		} catch (ResourceNotFoundException e) {
			throw new HiveRuntimeException("Unable to locate template: " + templateFile);
		} catch (ParseErrorException e) {
			throw new HiveRuntimeException("Error parsing template: " + templateFile);
		} catch (Exception e) {
			throw new RuntimeException(e);
		} 
		 
		StringWriter writer = new StringWriter();
		try { 
			template.merge(context, writer);
		} catch (Exception e) {
			throw new HiveRuntimeException(e);
		} 
		 
		return writer.toString();
	} 
	 
	public static Properties getDefaultVelocityProperties() {
		Properties p = new Properties();
		p.setProperty( "resource.loader", "class" );
		p.setProperty( "class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader" );
		return p;
	} 
}