replaced all StringBuffers with StringBuilder to improve performance
[Bob.git] / src / com / interrupt / bob / handler / XMLHandler.java
blob2d4aba9b7aa3cf8ed3ed6ab184e5018251d1c124
1 package com.interrupt.bob.handler;
3 import org.apache.commons.cli.CommandLine;
4 import org.apache.log4j.Logger;
6 import org.xml.sax.Locator;
7 import org.xml.sax.SAXException;
8 import org.xml.sax.Attributes;
9 import org.xml.sax.helpers.AttributesImpl;
11 import java.util.Iterator;
12 import java.util.List;
13 import java.util.ArrayList;
14 import java.util.Stack;
15 import java.util.StringTokenizer;
16 import java.util.regex.Pattern;
17 import java.io.File;
18 import java.io.FileWriter;
19 import java.io.IOException;
21 import com.interrupt.bob.Balloon;
22 import com.interrupt.bob.generator.IGenerator;
23 import com.interrupt.bob.generator.ClassGenerator;
24 import com.interrupt.bob.generator.InterfaceGenerator;
25 import com.interrupt.bob.base.Bob;
26 import com.interrupt.bob.base.IBob;
27 import com.interrupt.bob.core.IMemory;
28 import com.interrupt.bob.core.Memory;
29 import com.interrupt.bob.core.MemoryField;
30 import com.interrupt.bob.core.Queue;
31 import com.interrupt.bob.handler.DocumentHandler;
32 import com.interrupt.bob.handler.GDocumentHandler;
33 import com.interrupt.bob.util.Util;
35 public class XMLHandler extends DocumentHandler {
39 private Logger logger = Logger.getLogger(XMLHandler.class);
41 private Stack docStack = null;
42 public List tagList = null;
45 public XMLHandler() {
47 super();
48 docStack = new Stack();
52 public XMLHandler(List tl) {
54 super();
55 docStack = new Stack();
56 tagList = tl;
60 public void startDocument() throws SAXException{
62 logger.debug("!!! START !!! XMLHandler.startDocument CALLED");
63 super.startDocument();
64 if(this.getMessage() instanceof List) {
65 tagList = (List)this.getMessage();
69 public void endDocument() throws SAXException{
71 logger.debug("!!! END !!! XMLHandler.endDocument CALLED");
72 super.endDocument();
74 if(this.getParent() != null) {
76 String memoryName = this.getOut();
77 MemoryField memoryField = new MemoryField();
78 memoryField.setName(memoryName);
79 memoryField.setValue(null);
81 Memory memory = (Memory)((Queue)this.getParent()).getMemory();
82 memory.addMemoryField(memoryField);
87 public void startElement (String namespaceURI, String localName,
88 String qName, Attributes atts) throws SAXException {
90 super.startElement(namespaceURI, localName, qName, atts);
92 logger.debug("XMLHandler.startElement >> " +
93 namespaceURI + " >> " +
94 localName + " >> " +
95 qName
96 /*atts.getURI(0) + " >> " +
97 atts.getLocalName(0) + " >> " +
98 atts.getQName(0) + " >> " +
99 atts.getType(0) + " >> " +
100 atts.getValue(0)
105 Balloon bigBalloon = new Balloon();
106 bigBalloon.setChildrenBalloon(new StringBuilder());
107 bigBalloon.setFindersBalloon(new StringBuilder());
108 docStack.push(bigBalloon);
112 public void endElement(String namespaceURI, String localName, String qName)
113 throws SAXException {
115 logger.debug("XMLHandler.endElement > tag["+localName+"]" +
116 "qname["+qName+"] "+ ">>> tagList["+tagList+"]");
118 // find the <tag/> in the list
119 Iterator iter = tagList.iterator();
120 IBob tagDef = null;
121 while(iter.hasNext()) {
123 tagDef = (IBob)iter.next();
124 String qnm = tagDef.getQName();
126 // repurposing 'qName' for my own format
127 if(namespaceURI == "" || namespaceURI == null) {
128 qName = localName;
130 else {
131 qName = namespaceURI +":"+ localName;
134 if(qnm.equalsIgnoreCase(qName)) {
135 break;
139 Balloon bigBalloon = (Balloon)docStack.pop();
142 this.makeInterface(tagDef, bigBalloon, namespaceURI);
143 bigBalloon.flush();
144 this.makeClass(tagDef, bigBalloon, namespaceURI);
148 public void makeClass(IBob tagDef, Balloon bigBalloon, String namespaceURI) {
150 String theName = genClassNameFromTag(tagDef.getQName());
152 // set the NAMESPACE
153 bigBalloon.setNamespace(tagDef.getNamespace());
155 // add the PACKAGENAME
156 StringBuilder bb = new StringBuilder();
157 (ClassGenerator.getInstance()).appendPackage(tagDef.getNamespace(), bb);
158 bigBalloon.setPackageBalloon(bb);
160 // add CLASSNAME based on tag name
161 StringBuilder cdb = new StringBuilder();
162 (ClassGenerator.getInstance()).appendClassName(tagDef, "public", "class", theName, cdb);
163 bigBalloon.setClassDeclBalloon(cdb);
165 // add FIELDS and ACCESSORS for the attributes
166 StringBuilder sb = new StringBuilder();
167 StringBuilder asb = new StringBuilder();
169 Attributes attrs = tagDef.getAttributes();
170 int attSize = attrs.getLength();
171 for(int i = 0; i < attSize; i++) {
173 /*(ClassGenerator.getInstance())
174 .appendAttribute(attrs.getLocalName(i), sb);
175 (ClassGenerator.getInstance())
176 .appendAttributeAccessors(attrs.getLocalName(i), asb);
179 /*(ClassGenerator.getInstance())
180 .appendAttribute(((com.interrupt.bob.Attributes)attrs).getLocalNameJ(i), sb);
182 (ClassGenerator.getInstance())
183 .appendAttributeAccessors(((com.interrupt.bob.Attributes)attrs).getLocalNameJ(i), asb);
185 bigBalloon.setAttributes(attrs);
186 bigBalloon.setAttributesBalloon(sb);
187 bigBalloon.setAccessorsBalloon(asb);
189 // add all CHILDREN & FINDERS
190 //List children = tagDef.allChildren();
191 List children = tagDef.allGenChildren();
192 StringBuilder cbuf = new StringBuilder();
194 StringBuilder findBuf = new StringBuilder();
195 StringBuilder listBuf = new StringBuilder();
196 StringBuilder addBuf = new StringBuilder();
197 StringBuilder removeBuf = new StringBuilder();
198 StringBuilder removeAllBuf = new StringBuilder();
199 StringBuilder makeBuf = new StringBuilder();
200 for(int i = 0; i < children.size() ; i++) {
202 IBob childDef = (IBob)children.get(i);
204 // the children
205 (ClassGenerator.getInstance()).appendChild(childDef.getTagName(), cbuf);
207 // the finders
208 (ClassGenerator.getInstance()).appendFinders(childDef, findBuf);
210 // the listers
211 (ClassGenerator.getInstance()).appendListers(childDef, listBuf);
213 // the adders
214 (ClassGenerator.getInstance()).appendAdders(childDef, addBuf);
216 // the removers
217 (ClassGenerator.getInstance()).appendRemovers(childDef, removeBuf);
219 // remove all
220 (ClassGenerator.getInstance()).appendRemoveAll(childDef, removeAllBuf);
224 bigBalloon.appendChildrenBalloon(cbuf);
225 bigBalloon.appendFindersBalloon(findBuf);
226 bigBalloon.appendListersBalloon(listBuf);
228 bigBalloon.appendAddersBalloon(addBuf);
229 bigBalloon.appendRemoversBalloon(removeBuf);
231 // REMOVE ALL
232 bigBalloon.appendRemoveAllBalloon(removeAllBuf);
234 // ** 'make()' for gen classes
235 (ClassGenerator.getInstance()).appendMake(tagDef, makeBuf);
236 bigBalloon.appendMakeBalloon(makeBuf);
238 // close off the class
239 StringBuilder buf = new StringBuilder();
240 (ClassGenerator.getInstance()).appendClose(buf);
241 bigBalloon.setClassCloseBalloon(buf);
243 //logger.debug("makeClass: appendMakeBalloon: " + bigBalloon.getMakeBalloon());
245 String classString = new String();
246 classString = bigBalloon.makeClass();
248 // make a java name
249 StringBuilder javaName = new StringBuilder();
250 javaName.append(theName.trim());
251 javaName.append(".java");
253 // convert URI to file name
254 String pathURI = new String();
255 if(namespaceURI.contains("/")) {
256 pathURI = namespaceURI.replace('/', File.separatorChar);
258 else {
259 pathURI = namespaceURI;
261 writeToFile(pathURI, javaName.toString(), classString);
265 public void makeInterface(IBob tagDef, Balloon bigBalloon, String namespaceURI) {
267 String theName = genInterfaceNameFromTag(tagDef.getQName());
269 // set the NAMESPACE
270 bigBalloon.setNamespace(tagDef.getNamespace());
272 // add the PACKAGENAME
273 StringBuilder bb = new StringBuilder();
274 (InterfaceGenerator.getInstance()).appendPackage(tagDef.getNamespace(), bb);
275 bigBalloon.setPackageBalloon(bb);
277 // add CLASSNAME based on tag name
278 StringBuilder cdb = new StringBuilder();
279 (InterfaceGenerator.getInstance()).appendClassName(tagDef, "public", "interface", theName, cdb);
280 bigBalloon.setClassDeclBalloon(cdb);
282 // add FIELDS and ACCESSORS for the attributes
283 StringBuilder sb = new StringBuilder();
284 StringBuilder asb = new StringBuilder();
286 Attributes attrs = tagDef.getAttributes();
287 int attSize = attrs.getLength();
288 for(int i = 0; i < attSize; i++) {
290 /*(InterfaceGenerator.getInstance())
291 .appendAttribute(attrs.getLocalName(i), sb);
292 (InterfaceGenerator.getInstance())
293 .appendAttributeAccessors(attrs.getLocalName(i), asb);
295 (InterfaceGenerator.getInstance())
296 .appendAttribute(((com.interrupt.bob.Attributes)attrs).getLocalNameJ(i), sb);
297 (InterfaceGenerator.getInstance())
298 .appendAttributeAccessors(((com.interrupt.bob.Attributes)attrs).getLocalNameJ(i), asb);
300 bigBalloon.setAttributes(attrs);
301 bigBalloon.setAttributesBalloon(sb);
302 bigBalloon.setAccessorsBalloon(asb);
304 // add all CHILDREN & FINDERS
305 //List children = tagDef.allChildren();
306 List children = tagDef.allGenChildren();
307 StringBuilder cbuf = new StringBuilder();
309 StringBuilder findBuf = new StringBuilder();
310 StringBuilder listBuf = new StringBuilder();
311 StringBuilder addBuf = new StringBuilder();
312 StringBuilder removeBuf = new StringBuilder();
313 StringBuilder removeAllBuf = new StringBuilder();
314 for(int i = 0; i < children.size() ; i++) {
316 IBob childDef = (IBob)children.get(i);
318 // the children
319 (InterfaceGenerator.getInstance()).appendChild(childDef.getTagName(), cbuf);
321 // the finders
322 (InterfaceGenerator.getInstance()).appendFinders(childDef, findBuf);
324 // the listers
325 (InterfaceGenerator.getInstance()).appendListers(childDef, listBuf);
327 // the adders
328 (InterfaceGenerator.getInstance()).appendAdders(childDef, addBuf);
330 // the removers
331 (InterfaceGenerator.getInstance()).appendRemovers(childDef, removeBuf);
333 // remove all
334 (InterfaceGenerator.getInstance()).appendRemoveAll(childDef, removeAllBuf);
337 bigBalloon.appendChildrenBalloon(cbuf);
338 bigBalloon.appendFindersBalloon(findBuf);
339 bigBalloon.appendFindersBalloon(listBuf);
340 bigBalloon.appendAddersBalloon(addBuf);
341 bigBalloon.appendRemoversBalloon(removeBuf);
343 // REMOVE ALL
344 bigBalloon.appendRemoveAllBalloon(removeAllBuf);
346 // close off the class
347 StringBuilder buf = new StringBuilder();
348 (InterfaceGenerator.getInstance()).appendClose(buf);
349 bigBalloon.setClassCloseBalloon(buf);
351 String classString = new String();
352 classString = bigBalloon.makeClass();
354 // make a java name
355 StringBuilder javaName = new StringBuilder();
356 javaName.append(theName.trim());
357 javaName.append(".java");
359 // convert URI to file name
360 String pathURI = new String();
361 if(namespaceURI.contains("/")) {
362 pathURI = namespaceURI.replace('/', File.separatorChar);
364 else {
365 pathURI = namespaceURI;
367 writeToFile(pathURI, javaName.toString(), classString);
371 public String genClassNameFromTag(String tagName) {
373 tagName = tagName.trim();
374 //logger.debug("GRRRR["+tagDef.getQName() == null +"]");
375 //logger.debug("GRRRR["+ tagName.length() +"]");
376 if(tagName == null || (tagName.length() < 1) ) {
377 //logger.debug("HERE");
378 return "";
381 String className = tagName.trim();
383 // if there is a namespace prefix, strip it off
384 if(className.indexOf(":") > 0) {
385 className = className.substring(className.indexOf(":") + 1);
387 String firstLetter = className.substring(0,1);
388 String restOf = className.substring(1);
390 StringBuilder sb = new StringBuilder("G");
391 sb.append(firstLetter.toUpperCase());
392 sb.append(restOf);
394 return sb.toString();
396 public String genInterfaceNameFromTag(String tagName) {
398 if(tagName == null || tagName == "") {
399 return "";
402 String className = tagName.trim();
404 // if there is a namespace prefix, strip it off
405 if(className.indexOf(":") > 0) {
406 className = className.substring(className.indexOf(":") + 1);
408 String firstLetter = className.substring(0,1);
409 String restOf = className.substring(1);
411 StringBuilder sb = new StringBuilder("I");
412 sb.append(firstLetter.toUpperCase());
413 sb.append(restOf);
415 return sb.toString();
418 public void writeToFile(String parentName, String javaName, String contents) {
420 String genDir = (System.getProperties()).getProperty(Util.GEN);
421 String baseDir = (System.getProperties()).getProperty(Util.BASE);
422 genDir = genDir.trim();
423 baseDir = baseDir.trim();
425 logger.debug("BASE dir > "+ baseDir);
426 logger.debug("GEN dir > "+ genDir);
427 logger.debug("parentName > "+ parentName);
429 // getting the 'base' dir
430 StringBuilder jfsb = new StringBuilder(baseDir);
431 if(jfsb.charAt(jfsb.length() - 1) != File.separator.charAt(0)) {
432 jfsb.append(File.separator);
435 // adding the 'gen' dir
436 jfsb.append(genDir);
437 if(jfsb.charAt(jfsb.length() - 1) != File.separator.charAt(0)) {
438 jfsb.append(File.separator);
441 //logger.debug("PARENT dir before expansion 1 > "+ jfsb);
443 // make directories
444 File parent = new File(jfsb.toString());
445 if(!parent.exists()) {
446 parent.mkdir();
449 //logger.debug("PARENT dir before expansion 2 > "+ parent.getAbsolutePath());
451 String next = null;
452 StringTokenizer st = new StringTokenizer(parentName, File.separator);
454 try {
456 while(st.hasMoreTokens()) {
457 next = st.nextToken();
458 jfsb.append(next);
459 jfsb.append(File.separator);
461 parent = new File(jfsb.toString());
462 //logger.debug("PARENT dir > "+ parent.getAbsolutePath() + " > exists["+parent.exists()+"]");
463 if(!parent.exists()) {
464 parent.mkdir();
465 //logger.debug("... creating parent dir > "+ parent.exists());
469 File javaFile = new File(jfsb.toString(), javaName);
470 logger.debug("writeToFile >> WRITING OUT ["+ javaFile.getAbsolutePath() +"]");
472 FileWriter fw = null;
473 javaFile.createNewFile();
474 fw = new FileWriter(javaFile);
475 fw.write(contents);
476 fw.close();
480 catch(IOException e) {
481 e.printStackTrace(System.out);