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