1 5 package xdoclet.modules.apache.velocity; 6 7 import java.io.StringWriter ; 8 import java.util.Properties ; 9 10 import org.apache.velocity.VelocityContext; 11 import org.apache.velocity.app.Velocity; 12 13 import xdoclet.XDocletException; 14 import xdoclet.modules.apache.SubTemplateEngine; 15 16 22 class VelocitySubTemplateEngine implements SubTemplateEngine 23 { 24 25 private VelocityContext context = new VelocityContext(); 27 28 32 public Object getVariable(String name) 33 { 34 return context.get(name); 35 } 36 37 38 42 public void setVariable(String name, Object value) 43 { 44 context.put(name, value); 45 } 46 47 51 public void clearVariables() 52 { 53 context = new VelocityContext(); 54 } 55 56 60 public String generate(String template, Properties attributes) throws XDocletException 61 { 62 67 try { 69 75 78 Velocity.init(); 79 80 82 87 89 97 110 115 StringWriter writer = new StringWriter (); 116 117 Velocity.evaluate(context, writer, "generate", template); 119 122 125 writer.flush(); 126 writer.close(); 127 128 return writer.getBuffer().toString(); 129 } 130 catch (Exception e) { 131 throw new XDocletException(e, "Exception when executing Velocity template!"); 132 } 133 } 137 } 138 139 | Popular Tags |