replaced all StringBuffers with StringBuilder to improve performance
[Bob.git] / src / com / interrupt / bob / generator / InterfaceGenerator.java
blob99f9b1bf7b4aa31c840240d096a60a9da321d0c4
1 package com.interrupt.bob.generator;
3 import org.apache.log4j.Logger;
4 import org.apache.velocity.app.Velocity;
5 import org.apache.velocity.VelocityContext;
6 import org.apache.velocity.Template;
7 import org.apache.velocity.exception.ParseErrorException;
8 import org.apache.velocity.exception.ResourceNotFoundException;
9 import org.xml.sax.Attributes;
10 import java.io.StringWriter;
11 import java.io.FileWriter;
12 import java.io.IOException;
15 import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;
18 import com.interrupt.bob.base.Bob;
19 import com.interrupt.bob.base.IBob;
20 import com.interrupt.bob.util.StringUtil;
21 import com.interrupt.bob.util.Templates;
23 public class InterfaceGenerator implements IGenerator {
26 private Logger logger = Logger.getLogger(InterfaceGenerator.class);
28 private static InterfaceGenerator instance = null;
30 private InterfaceGenerator() {
33 public static IGenerator getInstance() {
34 if(instance == null) {
35 instance = new InterfaceGenerator();
37 return instance;
40 public void mergeTemplate(VelocityContext context, String tplName, StringBuilder sb) {
42 Template tpl = null;
43 java.io.InputStream istream = null;
44 try {
47 // Option 1
48 ClassLoader cl = ClassLoader.getSystemClassLoader();
49 istream = cl.getResourceAsStream("velocity.properties");
52 // inputstream for velocity.properties
53 //logger.debug( "InputStream ["+istream+"]");
56 java.util.Properties props = new java.util.Properties();
57 props.load(istream);
60 //props.list( System.out );
61 //System.exit(0);
63 ClasspathResourceLoader cpl = new ClasspathResourceLoader();
64 //logger.debug( "Initialised Class ["+ cpl +"]");
66 // System Classpath
67 //logger.debug( "System Classpath ["+System.getProperty("java.class.path")+"]");
68 //logger.debug( "Loaded Class ["+cl.loadClass( "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader" )+"]");
69 //System.exit(0);
72 Velocity.init(props);
73 tpl = Velocity.getTemplate(tplName);
75 StringWriter swriter = new StringWriter();
77 if(tpl != null) {
78 tpl.merge(context, swriter);
80 sb.append(swriter.toString());
82 catch(ResourceNotFoundException e) {
83 e.printStackTrace();
85 catch(ParseErrorException e) {
86 e.printStackTrace();
88 catch(Exception e) {
89 e.printStackTrace();
91 finally {
93 try {
94 istream.close();
96 catch( IOException e ) {
97 e.printStackTrace();
104 public void appendClassName(IBob tagdef, String visibility, String clazz,
105 String clazzName, StringBuilder sb) {
107 VelocityContext context = new VelocityContext();
108 context.put("visibility", visibility);
109 context.put("clazz", clazz);
110 context.put("clazzName", clazzName);
112 mergeTemplate(context, Templates.ICLASSDECL, sb);
113 Template tpl = null;
116 public void appendAttribute(String attName, StringBuilder sb) {
118 /*VelocityContext context = new VelocityContext();
119 context.put("visibility", "private");
120 context.put("attName", attName);
121 mergeTemplate(context, Templates.ATTRDECL, sb);
125 public void appendAttributeAccessors(String attName, StringBuilder sb) {
127 VelocityContext context = new VelocityContext();
128 context.put("attrUpper", StringUtil.upperFirstLetter(attName));
129 context.put("attr", attName);
130 context.put("in", "_in");
131 mergeTemplate(context, Templates.IGETTER, sb);
132 mergeTemplate(context, Templates.ISETTER, sb);
135 public void appendChild(String tagName, StringBuilder sb) {
137 /*VelocityContext context = new VelocityContext();
138 context.put("tag", tagName);
139 mergeTemplate(context, Templates.CHILD, sb);
143 public void appendAdders(IBob td, StringBuilder sb) {
145 String tagName = td.getTagName();
146 String package_ = td.getNamespace();
147 package_ = StringUtil.namespaceToPackage(package_);
149 VelocityContext context = new VelocityContext();
150 context.put("package_", package_);
151 context.put("tag", tagName);
152 context.put("tagUpper", StringUtil.upperFirstLetter(tagName));
153 mergeTemplate(context, Templates.IADDER, sb);
156 public void appendFinders(IBob td, StringBuilder sb) {
158 Attributes attrs = td.getAttributes();
159 String tagName = td.getTagName();
160 String package_ = td.getNamespace();
161 package_ = StringUtil.namespaceToPackage(package_);
163 String currentAttr = null;
164 VelocityContext context = new VelocityContext();
165 for(int i = 0; i < attrs.getLength(); i++) {
167 currentAttr = attrs.getLocalName(i);
169 context = new VelocityContext();
170 context.put("package_", package_);
171 context.put("tag", tagName);
172 context.put("tagUpper", StringUtil.upperFirstLetter(tagName));
173 context.put("attributeUpper", StringUtil.upperFirstLetter(currentAttr));
175 mergeTemplate(context, Templates.IFINDER, sb);
181 public void appendListers(IBob td, StringBuilder sb) {
183 Attributes attrs = td.getAttributes();
184 String tagName = td.getTagName();
185 String package_ = td.getNamespace();
186 package_ = StringUtil.namespaceToPackage(package_);
188 String currentAttr = null;
189 VelocityContext context = new VelocityContext();
190 for(int i = 0; i < attrs.getLength(); i++) {
192 currentAttr = attrs.getLocalName(i);
194 context = new VelocityContext();
195 context.put("package_", package_);
196 context.put("tag", tagName);
197 context.put("tagUpper", StringUtil.upperFirstLetter(tagName));
198 context.put("attributeUpper", StringUtil.upperFirstLetter(currentAttr));
200 mergeTemplate(context, Templates.ILISTER, sb);
207 public void appendRemovers(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;
215 StringBuilder findersBuf = new StringBuilder();
217 VelocityContext context = new VelocityContext();
218 for(int i = 0; i < attrs.getLength(); i++) {
220 currentAttr = attrs.getLocalName(i);
222 context = new VelocityContext();
223 context.put("package_", package_);
224 context.put("tag", tagName);
225 context.put("tagUpper", StringUtil.upperFirstLetter(tagName));
226 context.put("attributeUpper", StringUtil.upperFirstLetter(currentAttr));
228 mergeTemplate(context, Templates.IREMOVER, sb);
232 public void appendRemoveAll(IBob td, StringBuilder sb) {
234 String tagName = td.getTagName();
236 VelocityContext context = new VelocityContext();
237 context.put("tagUpper", StringUtil.upperFirstLetter(tagName));
239 mergeTemplate(context, Templates.IREMOVEALL, sb);
242 public void appendMake(IBob td, StringBuilder sb) {
246 /* close off the class' braces
248 public void appendClose(StringBuilder sb) {
249 sb.append("\n");
250 sb.append("}");
253 public void appendPackage(String nameSpace, StringBuilder sb) {
255 String package_ = StringUtil.namespaceToPackage(nameSpace);
256 VelocityContext context = new VelocityContext();
257 context.put("package_", package_);
259 mergeTemplate(context, Templates.PACKAGE, sb);
262 public static void main(String[ ] args) {
264 String classVisibility = "public";
265 String className = "MyClass";
268 StringBuilder sb = new StringBuilder();
270 //(InterfaceGenerator.getInstance()).appendClassName(classVisibility, "class", className, sb);
272 // print the closing brace that ends the class file
273 sb.append("}");
275 FileWriter fw = null;
277 try {
278 fw = new FileWriter("MyClass.java");
279 fw.write(sb.toString());
281 fw.close();
283 catch(IOException e) {
284 e.printStackTrace(System.out);