1 19 package org.objectweb.carol.cmi.compiler; 20 21 import java.io.File ; 22 import java.io.FileWriter ; 23 24 import org.apache.velocity.Template; 25 import org.apache.velocity.VelocityContext; 26 import org.apache.velocity.app.VelocityEngine; 27 import org.apache.velocity.runtime.RuntimeConstants; 28 import org.apache.velocity.runtime.RuntimeServices; 29 import org.apache.velocity.runtime.log.LogSystem; 30 31 public class TemplateCompiler implements LogSystem { 32 private ClassConf ccc; 33 private VelocityEngine ve; 34 private String clFullName; 35 private String clName; 36 private String genDirName; 37 private String pkgName; 38 private Compiler c; 39 40 public TemplateCompiler(Compiler c, ClassConf ccc) throws CompilerException { 41 this.c = c; 42 this.ccc = ccc; 43 ve = new VelocityEngine(); 44 ve.setProperty( RuntimeConstants.RUNTIME_LOG_LOGSYSTEM, this); 45 ve.setProperty(RuntimeConstants.VM_LIBRARY, ""); 46 ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "class"); 47 ve.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"); 48 try { 49 ve.init(); 50 } catch (Exception e1) { 51 throw new CompilerException("Velocity engine error", e1); 52 } 53 clFullName = ccc.getClassName(); 54 int n = clFullName.lastIndexOf("."); 55 if (n < 0) { 56 clName = ccc.getClassName(); 57 genDirName = c.getSrcDir(); 58 pkgName = ""; 59 } else { 60 pkgName = ccc.getClassName().substring(0, n); 61 clName = ccc.getClassName().substring(n + 1); 62 genDirName = c.getSrcDir() + File.separator + pkgName.replace('.', File.separatorChar); 63 } 64 } 65 66 public String genConfig() throws CompilerException { 67 Template tmpl; 68 try { 69 tmpl = ve.getTemplate("org/objectweb/carol/cmi/compiler/ClusterConfigTemplate.vm"); 70 } catch (Exception e1) { 71 throw new CompilerException("Velocity engine error", e1); 72 } 73 VelocityContext vc = createVelocityContext(); 74 String fileName = clName + "_ClusterConfig.java"; 75 return generate(tmpl, vc, fileName); 76 } 77 78 public String genStub() throws CompilerException { 79 Template tmpl; 80 try { 81 tmpl = ve.getTemplate("org/objectweb/carol/cmi/compiler/ClusterStubTemplate.vm"); 82 } catch (Exception e1) { 83 throw new CompilerException("Velocity engine error", e1); 84 } 85 VelocityContext vc = createVelocityContext(); 86 String fileName = clName + "_Cluster.java"; 87 return generate(tmpl, vc, fileName); 88 } 89 90 private String generate(Template tmpl, VelocityContext vc, String fileName) throws CompilerException { 91 FileWriter fw; 92 File file; 93 String fullFileName = genDirName + File.separator + fileName; 94 try { 95 File dir = new File (genDirName); 96 dir.mkdirs(); 97 } catch (Exception e) { 98 throw new CompilerException("unable to create directory " + genDirName, e); 99 } 100 try { 101 file = new File (genDirName, fileName); 102 fw = new FileWriter (file); 103 } catch (Exception e) { 104 throw new CompilerException("unable to create file " + fileName, e); 105 } 106 107 try { 108 tmpl.merge(vc, fw); 109 fw.flush(); 110 fw.close(); 111 } catch (CompilerException ce) { 112 throw ce; 113 } catch (Exception e2) { 114 throw new CompilerException(fullFileName, e2); 115 } 116 return fullFileName; 117 } 118 119 private VelocityContext createVelocityContext() throws CompilerException { 120 VelocityContext vc = new VelocityContext(); 121 vc.put("className", clName); 122 vc.put("pkgName", pkgName); 123 vc.put("classFullName", clFullName); 124 vc.put("classConf", ccc); 125 return vc; 126 } 127 128 public void init(RuntimeServices arg0) throws Exception { 129 } 130 131 public void logVelocityMessage(int arg0, String arg1) { 132 } 133 } 134 | Popular Tags |