replaced all StringBuffers with StringBuilder to improve performance
[Bob.git] / src / com / interrupt / bob / handler / GenHandler.java
blob94ab4640a7dfafe210e8db6649e7eba322d99d72
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.io.File;
17 import java.io.FileWriter;
18 import java.io.IOException;
19 import java.lang.reflect.Method;
20 import java.lang.reflect.InvocationTargetException;
22 import com.interrupt.bob.Balloon;
23 import com.interrupt.bob.generator.IGenerator;
24 import com.interrupt.bob.generator.ClassGenerator;
25 import com.interrupt.bob.generator.InterfaceGenerator;
26 import com.interrupt.bob.base.Bob;
27 import com.interrupt.bob.base.IBob;
28 import com.interrupt.bob.base.BobSystem;
29 import com.interrupt.bob.handler.DocumentHandler;
30 import com.interrupt.bob.util.Util;
31 import com.interrupt.bob.util.StringUtil;
32 import com.interrupt.callback.CallbackEvent;
34 /**
35 * build a bob tree hierarchy from definitions and xml files
37 public class GenHandler extends DocumentHandler {
40 private Logger logger = Logger.getLogger(GenHandler.class);
42 private Stack docStack = null;
43 private List tagList = null;
44 private Bob systemList = null;
47 public GenHandler() {
49 super();
50 systemList = new Bob();
51 docStack = new Stack();
53 public GenHandler(List tl) {
55 super();
56 systemList = new Bob();
57 docStack = new Stack();
58 tagList = tl;
61 public void startDocument() throws SAXException{
63 super.startDocument();
64 if(this.getMessage() instanceof List) {
65 tagList = (List)this.getMessage();
68 public void endDocument() throws SAXException{
70 super.endDocument();
71 CallbackEvent event = new DocumentEvent();
72 //event.setMessage(systemList);
73 //event.setName(BobSystem.SYSTEM);
74 notifyListeners(event);
77 public void ignorableWhitespace(char[] ch, int start, int length) {
78 //logger.debug( "ignorableWhitespace CALLED >> " + new String( ch, start, length ) );
80 public void characters(char[] ch, int start, int length) {
82 String content = new String( ch, start, length );
84 if( (content.trim()).length() > 0 ) {
85 IBob rr = (IBob)docStack.peek();
86 rr.setContent( content );
88 logger.debug( "characters CALLED >> " + new String( ch, start, length ) + " >> size ["+docStack.size() +"] >> ["+ rr.getTagName() +"]" );
91 //((IBob)docStack.peek()).setContent( new String( ch, start, length ) );
92 //logger.debug( "characters CALLED >> " + new String( ch, start, length ) + " >> size ["+docStack.size() +"] >> ["+ ((IBob)docStack.peek()).getTagName() +"]" );
96 public void startCDATA() {
97 logger.debug( "[[[ startCDATA CALLED" );
99 public void endCDATA() {
100 logger.debug( "]]] endCDATA CALLED" );
101 IBob rr = (IBob)docStack.peek();
102 String content = rr.getContent();
103 content = "<![CDATA[" + content + "]]>";
105 ((IBob)docStack.peek()).setContent( content );
109 public void startElement (String namespaceURI, String localName,
110 String qName, Attributes atts) throws SAXException {
112 super.startElement(namespaceURI, localName, qName, atts);
113 //logger.debug( "startElement CALLED >> ["+namespaceURI+"] > ["+localName+"]" );
115 // set the attributes
116 IBob created = Bob.make(namespaceURI,localName);
117 for(int i = 0; i < atts.getLength(); i++) {
119 String aname = atts.getLocalName(i);
120 Class createdClass = created.getClass();
121 Class params[] = { String.class };
122 String inputs[] = { atts.getValue(i) };
123 Method setMethod = null;
125 try {
126 setMethod = createdClass.getMethod( "set"+StringUtil.upperFirstLetter(aname), params);
127 setMethod.invoke( created, inputs );
129 catch(NoSuchMethodException e) {
130 e.printStackTrace();
132 catch(IllegalAccessException e) {
133 e.printStackTrace();
135 catch(InvocationTargetException e) {
136 e.printStackTrace();
139 docStack.push(created);
142 public void endElement(String namespaceURI, String localName, String qName) throws SAXException {
144 //logger.debug( "endElement CALLED >> ["+namespaceURI+"] > ["+localName+"]" );
146 // find the <tag/> in the list
147 Iterator iter = tagList.iterator();
148 IBob tagDef = null;
149 while(iter.hasNext()) {
150 tagDef = (IBob)iter.next();
151 String qnm = tagDef.getQName();
153 // repurposing 'qName' for my own format
154 if(namespaceURI == "" || namespaceURI == null) {
155 qName = localName;
157 else {
158 qName = namespaceURI +":"+ localName;
161 if(qnm.equalsIgnoreCase(qName)) {
162 break;
166 // set the CHILDREN
167 IBob created = (IBob)docStack.pop();
168 IBob parent = null;
169 if( docStack.size() > 0 ) {
170 parent = (IBob)docStack.peek();
172 else {
173 //root = created;
174 systemList.addChild( created );
175 return;
178 // get best subclass - default of 'GClazz' is 'Clazz'
179 String tname = created.getTagName();
180 String ns = created.getNamespace();
181 tname = StringUtil.upperFirstLetter(tname);
182 ns = StringUtil.namespaceToPackage(ns);
184 Class cclass = null;
185 try {
187 //logger.debug( ">> param ["+ ns +"] > ["+ "I" + tname +"]" );
188 if(ns.length() > 0 ) {
189 cclass = Class.forName( ns + "." + "I" + tname );
191 else {
192 cclass = Class.forName( "I" + tname );
195 catch(ClassNotFoundException e) {
197 e.printStackTrace();
200 //String tname = created.getTagName();
201 //Class defParams[] = { IBob.class };
202 Class defParams[] = { cclass };
203 IBob methParams[] = { created };
205 try {
206 Util.evalReflectedAdd(parent, tname, defParams, methParams);
208 catch(NoSuchMethodException e) {
209 e.printStackTrace();
211 catch(IllegalAccessException e) {
212 e.printStackTrace();
214 catch(InvocationTargetException e) {
215 e.printStackTrace();
217 catch(Throwable e) {
218 e.printStackTrace();
220 /* //parent.add{child}(created);
221 Class pclass = parent.getClass();
222 String methName = "add" + StringUtil.upperFirstLetter(tname);
223 Method setMeth = null;
225 try {
226 setMeth = pclass.getMethod( methName, defParams );
227 //setMeth = parentClass.getMethod( methName, defParams );
228 setMeth.invoke( parent, methParams );
230 catch(NoSuchMethodException e) {
231 e.printStackTrace();
233 catch(IllegalAccessException e) {
234 e.printStackTrace();
236 catch(InvocationTargetException e) {
237 e.printStackTrace();