1 package org.apache.beehive.controls.runtime.generator; 2 3 20 import java.io.File ; 21 import java.io.IOException ; 22 import java.io.Writer ; 23 import java.util.ArrayList ; 24 import java.util.HashMap ; 25 import java.util.List ; 26 import java.util.Properties ; 27 28 import org.apache.velocity.VelocityContext; 29 import org.apache.velocity.Template; 30 import org.apache.velocity.app.VelocityEngine; 31 import org.apache.velocity.exception.VelocityException; 32 import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader; 33 import org.apache.velocity.runtime.RuntimeConstants; 34 35 import com.sun.mirror.apt.AnnotationProcessorEnvironment; 36 37 41 public class VelocityGenerator extends CodeGenerator 42 { 43 public VelocityGenerator(AnnotationProcessorEnvironment env) throws Exception 44 { 45 super(); 46 47 _ve = new VelocityEngine(); 49 _ve.setProperty(VelocityEngine.RESOURCE_LOADER, "class"); 50 _ve.setProperty("class." + VelocityEngine.RESOURCE_LOADER + ".class", 51 ClasspathResourceLoader.class.getName()); 52 _ve.setProperty("velocimacro.library", 53 "org/apache/beehive/controls/runtime/generator/ControlMacros.vm"); 54 55 VelocityAptLogSystem logger = new VelocityAptLogSystem(env.getMessager()); 57 _ve.setProperty(VelocityEngine.RUNTIME_LOG_LOGSYSTEM, logger); 58 59 _ve.init(); 60 } 61 62 66 public void generate(GeneratorOutput genOut) throws CodeGenerationException 67 { 68 VelocityContext vc = new VelocityContext(); 72 73 HashMap <String ,Object > genContext = genOut.getContext(); 77 for(String key : genContext.keySet()) 78 vc.put(key, genContext.get(key)); 79 80 try 81 { 82 Writer genWriter = genOut.getWriter(); 83 Template template = getTemplate(genOut.getTemplateName()); 84 template.merge(vc, genWriter); 85 genWriter.close(); 86 } 87 catch (RuntimeException re) { throw re; } catch (Exception e) { throw new CodeGenerationException(e); } 89 } 90 91 public Template getTemplate(String templateName) throws Exception 96 { 97 if (_templateMap.containsKey(templateName)) 98 return _templateMap.get(templateName); 99 100 Template t = _ve.getTemplate(templateName); 101 _templateMap.put(templateName, t); 102 return t; 103 } 104 105 private HashMap <String , Template> _templateMap = new HashMap <String , Template>(); 106 private VelocityEngine _ve; 107 } 108 | Popular Tags |