View Javadoc

1   /*
2    * Created on Apr 19, 2006
3    */
4   package ca.uhn.hl7v2.sourcegen.util;
5   
6   import org.apache.velocity.Template;
7   import org.apache.velocity.app.VelocityEngine;
8   import org.apache.velocity.runtime.RuntimeConstants;
9   import org.springframework.ui.velocity.CommonsLoggingLogSystem;
10  
11  
12  /**
13   * Simple utility class to generate velocity engines.
14   * 
15   * Copied from UHN internal class under "commons-se"
16   * 
17   * @author <a href="mailto:jamesagnew@users.sourceforge.net">James Agnew </a>
18   * @version $Revision: 1.3 $ updated on $Date: 2011-04-01 13:24:45 $ by $Author:
19   *              jagnew $
20   */
21  public class VelocityFactory
22  {
23     /**
24      * non instantiable
25      */
26     private VelocityFactory() {
27        // nothing
28     }
29  
30  
31     /**
32      * Creates a velocity template
33      * 
34      * @param theTemplate
35      *                The template
36      * @return A template engine
37      * @throws Exception
38      *                If velocity fails to initialize
39      */
40     public static Template getClasspathTemplateInstance(String theTemplate) throws Exception {
41        VelocityEngine engine = getEngineInstance();
42        return engine.getTemplate(theTemplate);
43     }
44  
45  
46     /**
47      * @return Returns a velocity engine
48      * @throws Exception
49      *                If engine fails to initialize
50      */
51     public static VelocityEngine getEngineInstance() throws Exception {
52        VelocityEngine engine = new VelocityEngine();
53        engine.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
54        engine.setProperty("classpath." + RuntimeConstants.RESOURCE_LOADER + ".class", ResourceLoader.class
55              .getName());
56        engine.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS, CommonsLoggingLogSystem.class.getName());
57        engine.setProperty(RuntimeConstants.VM_LIBRARY, "");
58        engine.setProperty(RuntimeConstants.RUNTIME_REFERENCES_STRICT, Boolean.TRUE);
59        engine.init();
60        engine.setProperty(RuntimeConstants.RUNTIME_REFERENCES_STRICT, Boolean.TRUE);
61        return engine;
62     }
63  
64  }