KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > carol > cmi > compiler > TemplateCompiler


1 /*
2  * Copyright (C) 2002-2003, Simon Nieuviarts
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17  * USA
18  */

19 package org.objectweb.carol.cmi.compiler;
20
21 import java.io.File JavaDoc;
22 import java.io.FileWriter JavaDoc;
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 JavaDoc clFullName;
35     private String JavaDoc clName;
36     private String JavaDoc genDirName;
37     private String JavaDoc pkgName;
38     private Compiler JavaDoc c;
39
40     public TemplateCompiler(Compiler JavaDoc 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 JavaDoc 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 JavaDoc genConfig() throws CompilerException {
67         Template tmpl;
68         try {
69             tmpl = ve.getTemplate("org/objectweb/carol/cmi/compiler/ClusterConfigTemplate.vm");
70         } catch (Exception JavaDoc e1) {
71             throw new CompilerException("Velocity engine error", e1);
72         }
73         VelocityContext vc = createVelocityContext();
74         String JavaDoc fileName = clName + "_ClusterConfig.java";
75         return generate(tmpl, vc, fileName);
76     }
77
78     public String JavaDoc genStub() throws CompilerException {
79         Template tmpl;
80         try {
81             tmpl = ve.getTemplate("org/objectweb/carol/cmi/compiler/ClusterStubTemplate.vm");
82         } catch (Exception JavaDoc e1) {
83             throw new CompilerException("Velocity engine error", e1);
84         }
85         VelocityContext vc = createVelocityContext();
86         String JavaDoc fileName = clName + "_Cluster.java";
87         return generate(tmpl, vc, fileName);
88     }
89
90     private String JavaDoc generate(Template tmpl, VelocityContext vc, String JavaDoc fileName) throws CompilerException {
91         FileWriter JavaDoc fw;
92         File JavaDoc file;
93         String JavaDoc fullFileName = genDirName + File.separator + fileName;
94         try {
95             File JavaDoc dir = new File JavaDoc(genDirName);
96             dir.mkdirs();
97         } catch (Exception JavaDoc e) {
98             throw new CompilerException("unable to create directory " + genDirName, e);
99         }
100         try {
101             file = new File JavaDoc(genDirName, fileName);
102             fw = new FileWriter JavaDoc(file);
103         } catch (Exception JavaDoc 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 JavaDoc 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 JavaDoc {
129     }
130
131     public void logVelocityMessage(int arg0, String JavaDoc arg1) {
132     }
133 }
134
Popular Tags