replaced all StringBuffers with StringBuilder to improve performance
[Bob.git] / src / com / interrupt / bob / Main.java
blobcccd48e8c164248363a1ae57e101334962f43ada
2 package com.interrupt.bob;
4 import java.util.Iterator;
5 import java.util.Properties;
6 import java.util.Arrays;
7 import java.util.List;
8 import java.util.ArrayList;
9 import java.io.File;
10 import java.io.IOException;
12 import com.interrupt.bob.processor.DocumentProcessor;
13 import com.interrupt.bob.processor.ProcessorException;
14 import com.interrupt.bob.handler.DefinitionHandler;
15 import com.interrupt.bob.handler.GHandlerField;
16 import com.interrupt.bob.handler.HandlerField;
17 import com.interrupt.bob.handler.IHandlerField;
18 import com.interrupt.bob.handler.XMLHandler;
19 import com.interrupt.bob.util.Util;
21 import com.interrupt.bob.base.Bob;
22 import com.interrupt.bob.base.BobSystem;
24 import org.apache.commons.cli.Option;
25 import org.apache.commons.cli.Options;
26 import org.apache.commons.cli.OptionBuilder;
27 import org.apache.commons.cli.BasicParser;
28 import org.apache.commons.cli.CommandLine;
29 import org.apache.commons.cli.ParseException;
30 import org.apache.log4j.Logger;
32 import com.greenfabric.find.Find;
35 public class Main {
38 private Logger logger = Logger.getLogger(Main.class);
40 public static void main(String args[]) {
42 Options options = new Options();
44 Option genOption = new Option("gen", true, "directory for generated java files");
45 genOption.setArgs(1);
46 genOption.setArgName("gen directory");
48 Option baseOption = new Option("base", true, "base directory from which bob runs");
49 baseOption.setArgs(1);
50 baseOption.setArgName("base directory");
52 Option endOption = new Option("end", true, "process files with given suffix");
53 endOption.setArgs(1);
54 endOption.setArgName("file suffix");
56 Option defOption = new Option("def", true, "xml definitions that 'Bob' will use");
57 defOption.setArgs(1);
58 defOption.setArgName("xml definitions");
60 Option sysOption = new Option("sys", true, "xml files that 'Bob' will process");
61 sysOption.setArgs(1);
62 sysOption.setArgName("xml system files");
64 options.addOption(genOption);
65 options.addOption(baseOption);
66 options.addOption(endOption);
67 options.addOption(defOption);
68 options.addOption(sysOption);
71 BasicParser parser = new BasicParser();
73 // Parse command-line options
74 CommandLine cl = null;
75 try {
76 cl = parser.parse(options, args);
78 catch(ParseException e) {
79 e.printStackTrace(System.out);
82 // Set System properties
83 Properties properties = System.getProperties();
84 try {
86 properties.setProperty(Util.HOME, System.getProperty("user.dir"));
87 properties.setProperty(Util.BASE, cl.getOptionValue("base"));
88 properties.setProperty(Util.GEN, cl.getOptionValue("gen"));
89 properties.setProperty(Util.END, cl.getOptionValue("end"));
92 String def_s = cl.getOptionValue("def");
93 if( def_s == null || def_s.trim().length() < 1 ) {
94 //logger.debug("ERROR. -def <definition.files> not specified. Stopping.");
95 return;
98 String sys_s = cl.getOptionValue("sys");
99 if( sys_s == null || sys_s.trim().length() < 1 ) {
100 //logger.debug("ERROR. -sys <system.files> not specified. Stopping.");
101 return;
104 properties.setProperty(Util.DEF, def_s );
105 properties.setProperty(Util.SYS, sys_s );
108 catch(NullPointerException e) {
109 e.printStackTrace(System.out);
112 // process <xml/> and directory/ recursively
113 FindFiles ffiles = new FindFiles();
114 ffiles.setBase(properties.getProperty(Util.BASE));
115 ffiles.setSuffix(properties.getProperty(Util.END));
117 List fileList = ffiles.find(properties.getProperty(Util.DEF).split("\\s") );
119 //logger.debug("MAIN > cl >> " + java.util.Arrays.asList(properties.getProperty(Util.DEF)).toString());
120 //logger.debug("MAIN > files >> " + fileList.toString());
123 StringBuilder fileString_b = new StringBuilder();
124 Iterator fileIter = fileList.iterator();
125 while( fileIter.hasNext() ) {
127 fileString_b.append( " " );
128 fileString_b.append( (String)fileIter.next() );
131 String fileString_s = fileString_b.toString();
133 try {
135 DefinitionHandler dhandler = new DefinitionHandler();
136 dhandler.setFiles( fileString_s );
137 dhandler.setOut( Util.DEF );
139 IHandlerField hfield = new HandlerField();
140 hfield.setName("tagList");
141 hfield.setValue("${"+ Util.DEF +"}");
143 XMLHandler xhandler = new XMLHandler();
144 xhandler.setFiles( fileString_s );
145 xhandler.addHandlerField(hfield);
147 DocumentProcessor processor = new DocumentProcessor();
148 processor.addHandler(dhandler);
149 processor.addHandler(xhandler);
150 processor.process();
153 catch(ProcessorException e) {
154 e.printStackTrace(System.out);
157 // initialise 'bob' system
158 //(BobSystem.getInstance()).initialise();