replaced all StringBuffers with StringBuilder to improve performance
[Bob.git] / src / com / interrupt / bob / generator / ClassGenerator.java
blob9d3f194d987f4b51240e48a23f0f41cc11518e5d
1 package com.interrupt.bob.generator;
3 import org.apache.velocity.app.Velocity;
4 import org.apache.velocity.VelocityContext;
5 import org.apache.velocity.Template;
6 import org.apache.velocity.exception.ParseErrorException;
7 import org.apache.velocity.exception.ResourceNotFoundException;
8 import org.xml.sax.Attributes;
9 import java.io.StringWriter;
10 import java.io.FileWriter;
11 import java.io.IOException;
12 import java.util.Iterator;
14 import com.interrupt.bob.base.IBob;
15 import com.interrupt.bob.util.StringUtil;
16 import com.interrupt.bob.util.Templates;
18 public class ClassGenerator implements IGenerator {
20 private static ClassGenerator instance = null;
22 private ClassGenerator() {
25 public static IGenerator getInstance() {
26 if(instance == null) {
27 instance = new ClassGenerator();
29 return instance;
32 public void mergeTemplate(VelocityContext context, String tplName, StringBuilder sb) {
34 Template tpl = null;
35 java.io.InputStream istream = null;
36 try {
37 //Velocity.init("velocity.properties");
38 ClassLoader cl = ClassLoader.getSystemClassLoader();
39 istream = cl.getResourceAsStream("velocity.properties");
41 java.util.Properties props = new java.util.Properties();
42 props.load(istream);
44 Velocity.init(props);
45 tpl = Velocity.getTemplate(tplName);
47 StringWriter swriter = new StringWriter();
49 if(tpl != null) {
50 tpl.merge(context, swriter);
52 sb.append(swriter.toString());
54 catch(ResourceNotFoundException e) {
55 e.printStackTrace();
57 catch(ParseErrorException e) {
58 e.printStackTrace();
60 catch(Exception e) {
61 e.printStackTrace();
63 finally {
65 try {
66 istream.close();
68 catch( IOException e ) {
69 e.printStackTrace();
77 //public void appendClassName(String visibility, String clazz,
78 // String clazzName, StringBuilder sb) {
79 public void appendClassName(IBob tagdef, String visibility, String clazz,
80 String clazzName, StringBuilder sb) {
82 String _new_gen = this._genNewChildren(tagdef);
83 //String _all_gen = this._genAllChildren(tagdef);
84 String _attArray = _genAttributeArray(tagdef);
86 VelocityContext context = new VelocityContext();
87 context.put("visibility", visibility);
88 context.put("clazz", clazz);
89 context.put("clazzName", clazzName);
90 context.put("_new_genchildren", _new_gen);
91 context.put("attArray", _attArray);
92 context.put("tag", tagdef.getTagName());
93 context.put("namespace", tagdef.getNamespace());
95 // ** complete HACK to get interface name
96 String iname = clazzName.trim();
97 iname = iname.substring(1);
98 iname = "I" + iname;
99 context.put("interface_", iname);
101 mergeTemplate(context, Templates.CLASSDECL, sb);
102 Template tpl = null;
105 public String _genNewChildren(IBob tagdef) {
107 IBob next = null;
108 Iterator iter = (tagdef.allChildren()).iterator();
109 StringBuilder sbuffer = new StringBuilder();
110 String tname = null;
111 VelocityContext context = new VelocityContext();
113 while(iter.hasNext()) {
115 next = (IBob)iter.next();
116 tname = next.getTagName();
118 context.put("tag",tname);
119 mergeTemplate(context, Templates.GENCHILD_NEW, sbuffer);
122 return sbuffer.toString();
124 public String _genAttributeArray(IBob tagDef) {
126 //{"elem1","elem2","elem3"}
127 StringBuilder sbuffer = new StringBuilder();
128 //sbuffer.append("{\"");
129 sbuffer.append("{");
130 Attributes attrs = tagDef.getAttributes();
131 for(int i=0; i < attrs.getLength(); i++) {
133 if( i == 0 ) {
134 sbuffer.append( "\"" );
137 sbuffer.append( attrs.getLocalName(i) );
138 if((i + 1) == attrs.getLength()) {
139 sbuffer.append("\"");
141 else {
142 sbuffer.append("\",\"");
145 //sbuffer.append("\"}");
146 sbuffer.append("}");
147 return sbuffer.toString();
149 /*public String _genAllChildren(IBob tagdef) {
151 IBob next = null;
152 Iterator iter = (tagdef.allChildren()).iterator();
153 StringBuilder sbuffer = new StringBuilder();
154 String tname = null;
155 VelocityContext context = new VelocityContext();
157 while(iter.hasNext()) {
159 next = (IBob)iter.next();
160 tname = next.getTagName();
162 context.put("tag",tname);
163 mergeTemplate(context, Templates.GENCHILD_ALL, sbuffer);
166 return sbuffer.toString();
170 public void appendAttribute(String attName, StringBuilder sb) {
171 VelocityContext context = new VelocityContext();
172 context.put("visibility", "private");
173 context.put("attName", attName);
174 mergeTemplate(context, Templates.ATTRDECL, sb);
177 public void appendAttributeAccessors(String attName, StringBuilder sb) {
179 VelocityContext context = new VelocityContext();
180 context.put("attrUpper", StringUtil.upperFirstLetter(attName));
181 context.put("attr", attName);
182 context.put("in", "_in");
183 mergeTemplate(context, Templates.GETTER, sb);
184 mergeTemplate(context, Templates.SETTER, sb);
187 public void appendChild(String tagName, StringBuilder sb) {
189 VelocityContext context = new VelocityContext();
190 context.put("tag", tagName);
191 mergeTemplate(context, Templates.CHILD, sb);
194 public void appendAdders(IBob td, StringBuilder sb) {
196 String tagName = td.getTagName();
197 String package_ = td.getNamespace();
198 package_ = StringUtil.namespaceToPackage(package_);
200 VelocityContext context = new VelocityContext();
201 context.put("package_", package_);
202 context.put("tag", tagName);
203 context.put("tagUpper", StringUtil.upperFirstLetter(tagName));
204 mergeTemplate(context, Templates.ADDER, sb);
207 public void appendFinders(IBob td, StringBuilder sb) {
209 Attributes attrs = td.getAttributes();
210 String tagName = td.getTagName();
211 String package_ = td.getNamespace();
212 package_ = StringUtil.namespaceToPackage(package_);
214 String currentAttr = null;
216 VelocityContext context = new VelocityContext();
217 for(int i = 0; i < attrs.getLength(); i++) {
219 currentAttr = attrs.getLocalName(i);
221 context = new VelocityContext();
222 context.put("package_", package_);
223 context.put("tag", tagName);
224 context.put("tagUpper", StringUtil.upperFirstLetter(tagName));
225 context.put("attributeUpper", StringUtil.upperFirstLetter(currentAttr));
227 mergeTemplate(context, Templates.FINDER, sb);
231 public void appendListers(IBob td, StringBuilder sb) {
233 Attributes attrs = td.getAttributes();
234 String tagName = td.getTagName();
235 String package_ = td.getNamespace();
236 package_ = StringUtil.namespaceToPackage(package_);
238 String currentAttr = null;
239 VelocityContext context = new VelocityContext();
240 for(int i = 0; i < attrs.getLength(); i++) {
242 currentAttr = attrs.getLocalName(i);
244 context = new VelocityContext();
245 context.put("package_", package_);
246 context.put("tag", tagName);
247 context.put("tagUpper", StringUtil.upperFirstLetter(tagName));
248 context.put("attributeUpper", StringUtil.upperFirstLetter(currentAttr));
250 mergeTemplate(context, Templates.LISTER, sb);
255 public void appendRemovers(IBob td, StringBuilder sb) {
257 Attributes attrs = td.getAttributes();
258 String tagName = td.getTagName();
259 String package_ = td.getNamespace();
260 package_ = StringUtil.namespaceToPackage(package_);
262 String currentAttr = null;
263 StringBuilder findersBuf = new StringBuilder();
265 VelocityContext context = new VelocityContext();
266 for(int i = 0; i < attrs.getLength(); i++) {
268 currentAttr = attrs.getLocalName(i);
270 context = new VelocityContext();
271 context.put("package_", package_);
272 context.put("tag", tagName);
273 context.put("tagUpper", StringUtil.upperFirstLetter(tagName));
274 context.put("attributeUpper", StringUtil.upperFirstLetter(currentAttr));
276 mergeTemplate(context, Templates.REMOVER, sb);
280 public void appendRemoveAll(IBob td, StringBuilder sb) {
282 String tagName = td.getTagName();
283 String package_ = td.getNamespace();
284 package_ = StringUtil.namespaceToPackage(package_);
286 VelocityContext context = new VelocityContext();
287 context.put("tagUpper", StringUtil.upperFirstLetter(tagName));
288 context.put("tag", tagName);
289 context.put("package_", package_);
291 mergeTemplate(context, Templates.REMOVEALL, sb);
294 public void appendMake(IBob td, StringBuilder sb) {
296 String tagName = td.getTagName();
297 VelocityContext context = new VelocityContext();
298 context.put("tagUpper", StringUtil.upperFirstLetter(tagName));
300 mergeTemplate(context, Templates.MAKE, sb);
303 /* close off the class' braces
305 public void appendClose(StringBuilder sb) {
306 sb.append("\n");
307 sb.append("}");
310 public void appendPackage(String nameSpace, StringBuilder sb) {
312 String package_ = StringUtil.namespaceToPackage(nameSpace);
313 VelocityContext context = new VelocityContext();
314 context.put("package_", package_);
316 mergeTemplate(context, Templates.PACKAGE, sb);
319 public static void main(String[ ] args) {
321 String classVisibility = "public";
322 String className = "MyClass";
325 StringBuilder sb = new StringBuilder();
327 //(ClassGenerator.getInstance()).appendClassName(classVisibility, "class", className, sb);
329 // print the closing brace that ends the class file
330 sb.append("}");
332 FileWriter fw = null;
334 try {
335 fw = new FileWriter("MyClass.java");
336 fw.write(sb.toString());
338 fw.close();
340 catch(IOException e) {
341 e.printStackTrace(System.out);