00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 package dochelper;
00011
00012 import dochelper.exceptions.DocumentParseException;
00013 import dochelper.exceptions.IllegalParamterTypeException;
00014 import dochelper.exceptions.InitializationException;
00015 import dochelper.exceptions.InvalidValueException;
00016 import dochelper.exceptions.MissingArgumentException;
00017 import dochelper.exceptions.ResourceUnavailableException;
00018 import java.io.File;
00019 import java.io.FileNotFoundException;
00020 import java.io.FileOutputStream;
00021 import java.io.IOException;
00022 import java.io.InputStream;
00023 import java.lang.reflect.Constructor;
00024 import java.lang.reflect.InvocationTargetException;
00025 import java.lang.reflect.Method;
00026 import java.net.URI;
00027 import java.net.URL;
00028 import java.net.URLClassLoader;
00029
00030 import java.util.ArrayList;
00031 import java.util.Hashtable;
00032 import java.util.List;
00033 import javax.xml.parsers.DocumentBuilderFactory;
00034 import javax.xml.parsers.DocumentBuilder;
00035
00036 import javax.xml.parsers.ParserConfigurationException;
00037 import org.w3c.dom.Document;
00038 import org.w3c.dom.Element;
00039 import org.w3c.dom.Node;
00040 import org.w3c.dom.NodeList;
00041 import org.xml.sax.SAXException;
00042
00043
00048 public class Main {
00049
00050 private static final String USAGE = "DocHelper v 1.0. Usage: \n "
00051 + " Usage: java -jar dochelper.jar [args...] \n "
00052 + " java -jar dochelper.jar [path to docdef] \n\n"
00053 + " where args are: \n"
00054 + " docdef: Document Definition file that follows the Dochelper standard. This \n"
00055 + " is a required argument.\n\n";
00056
00057
00058 private static final String ERROR = "There was an error parsing the document definition file.\n"
00059 + "Please check the file and try again. Error Info : ";
00060
00062 public Main() {
00063 }
00064
00065 static String docdefPath;
00066 static final String PKG = "dochelper.";
00067
00068
00069 static Hashtable<String, DocPattern> patterns = new Hashtable();
00070 static Hashtable<String, RecordSet> recordsets = new Hashtable();
00071 static Hashtable<String, Renderer> renderers = new Hashtable();
00072 static Hashtable<String, Processor> processors = new Hashtable();
00073 static ArrayList<DocumentDefinition> documents = new ArrayList();
00078 public static void main(String[] argv) throws ParserConfigurationException, SAXException, IOException {
00079
00080
00081
00082
00083 checkParams(argv);
00084
00085
00086 File xmlDefinition = new File(docdefPath);
00087 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
00088 DocumentBuilder db = dbf.newDocumentBuilder();
00089 Document doc = db.parse(xmlDefinition);
00090 doc.getDocumentElement().normalize();
00091
00092
00093 try {
00094 parseDocumentDefintion(doc);
00095 }
00096 catch(DocumentParseException dpe) {
00097 System.err.println(ERROR + dpe.getMessage());
00098 System.exit(1);
00099 }
00100 catch(InvalidValueException ive) {
00101 System.err.println(ERROR + ive.getMessage());
00102 System.exit(1);
00103 }
00104 catch(IllegalParamterTypeException ilpe) {
00105 System.err.println(ERROR + ilpe.getMessage());
00106 System.exit(1);
00107 }
00108 catch(MissingArgumentException mae) {
00109 System.err.println(ERROR + mae.getMessage());
00110 System.exit(1);
00111 }
00112
00113
00114
00115 for(DocumentDefinition document : documents) {
00116
00117
00118 document.execute();
00119
00120
00121 document.executeProcessors();
00122
00123
00124 document.render();
00125
00126 }
00127
00128 }
00129
00130 static void checkParams(String argv[]) {
00131
00132 try {
00133
00134 if(argv.length == 0 || argv[0] == null || argv[0].length() < 1) {
00135 System.out.println("Error: Document Definition File not specified\n");
00136 System.out.println(USAGE);
00137 System.exit(1);
00138 }
00139
00140 File f = new File(argv[0]);
00141
00142 if(!f.isFile()) {
00143 System.out.println("Error: Document Definition File not valid\n");
00144 System.out.println(USAGE);
00145 System.exit(1);
00146 }
00147
00148
00149 docdefPath = argv[0];
00150
00151 }
00152 catch(Exception e) {
00153 System.out.println(USAGE);
00154 e.printStackTrace();
00155 return;
00156 }
00157
00158 }
00159
00160
00161 static void parseDocumentDefintion(Document doc) throws DocumentParseException, InvalidValueException,
00162 IllegalParamterTypeException, MissingArgumentException, ResourceUnavailableException {
00163
00164
00165 NodeList nodeList = doc.getElementsByTagName("pattern");
00166
00167 for (int i = 0; i < nodeList.getLength(); i++) {
00168 Element node = (Element)nodeList.item(i);
00169 DocPattern p = parsePattern(node);
00170 p.setName(node.getAttribute("name"));
00171 patterns.put(node.getAttribute("name"), p);
00172 }
00173
00174
00175 nodeList = doc.getElementsByTagName("recordset");
00176
00177 for (int i = 0; i < nodeList.getLength(); i++) {
00178 Element node = (Element)nodeList.item(i);
00179 RecordSet r = parseRecordSet(node, node.getAttribute("name"));
00180 recordsets.put(node.getAttribute("name"), r);
00181 }
00182
00183
00184 nodeList = doc.getElementsByTagName("processor");
00185
00186 for (int i = 0; i < nodeList.getLength(); i++) {
00187 Element node = (Element)nodeList.item(i);
00188 Processor p = parseProcessor(node);
00189
00190 processors.put(node.getAttribute("name"), p);
00191
00192 }
00193
00194
00195 nodeList = doc.getElementsByTagName("renderer");
00196
00197 for (int i = 0; i < nodeList.getLength(); i++) {
00198 Element node = (Element)nodeList.item(i);
00199 Renderer r = parseRenderer(node);
00200 renderers.put(node.getAttribute("name"), r);
00201 }
00202
00203
00204 nodeList = doc.getElementsByTagName("documentdef");
00205
00206 for (int i = 0; i < nodeList.getLength(); i++) {
00207
00208 Element dElem = (Element)nodeList.item(i);
00209 DocumentDefinition document = new DocumentDefinition(dElem.getAttribute("name"));
00210
00211
00212 NodeList rss = dElem.getElementsByTagName("recordsetref");
00213
00214 for(int j=0;j<rss.getLength(); j++) {
00215
00216 Element rElem = (Element)rss.item(j);
00217 String rsName = rElem.getAttribute("name");
00218
00219
00220 RecordSet rs = recordsets.get(rsName);
00221
00222
00223 rs.clearPatterns();
00224
00225
00226 NodeList ps = rElem.getElementsByTagName("patternref");
00227
00228 for(int h = 0; h < ps.getLength(); h++) {
00229
00230 Element pElem = (Element)ps.item(h);
00231 String pName = pElem.getAttribute("name");
00232
00233
00234 DocPattern pattern = patterns.get(pName);
00235
00236
00237 rs.addPattern(pattern);
00238
00239 }
00240
00241
00242 document.addRecordSet(rs);
00243
00244 }
00245
00246
00247 NodeList processorrefs = dElem.getElementsByTagName("processorref");
00248 for(int j=0;j<processorrefs.getLength(); j++) {
00249
00250 Element rElem = (Element)processorrefs.item(j);
00251
00252
00253 String key = rElem.getAttribute("name") + "||" + ( (Element)rElem.getParentNode()).getAttribute("name");
00254
00255 Processor processor = processors.get(rElem.getAttribute("name"));
00256 document.addProcessor(key, processor);
00257
00258 }
00259
00260
00261
00262 NodeList renderrefs = dElem.getElementsByTagName("renderref");
00263 for(int j=0;j<renderrefs.getLength(); j++) {
00264
00265 Element rElem = (Element)renderrefs.item(j);
00266 String rName = rElem.getAttribute("name");
00267
00268
00269 Renderer renderref = renderers.get(rName);
00270 document.addRenderer(renderref);
00271
00272 }
00273
00274
00275 documents.add(document);
00276 }
00277
00278 }
00279
00280 private static DocPattern parsePattern(Element node) throws DocumentParseException {
00281
00282 String name = "";
00283 name = node.getAttribute("name");
00284
00285 try {
00286
00287 NodeList tList = node.getElementsByTagName("transform");
00288 Element tElement = (Element)tList.item(0);
00289
00290 String className = tElement.getAttribute("class");
00291 Class cons_param_types[] = {String.class};
00292 Class transformClass = Class.forName(PKG + className);
00293 String tValue = tElement.getTextContent();
00294
00295
00296 if(className.equals("FileTransformDefinition"))
00297 tValue = tElement.getAttribute("file");
00298
00299
00300 Object cons_args[] = {tValue};
00301 Constructor constructor = transformClass.getDeclaredConstructor(cons_param_types);
00302
00303
00304 TransformDefinition ndi = (TransformDefinition)constructor.newInstance(cons_args);
00305
00306
00307 tList = node.getElementsByTagName("expression");
00308 tElement = (Element)tList.item(0);
00309
00310
00311 DocPattern p = new DocPattern(tElement.getTextContent(), ndi);
00312
00313
00314 p.setName(node.getAttribute("name"));
00315
00316 return p;
00317 }
00318 catch(ClassNotFoundException cnfe ) {
00319 String error = "No class was found for the transform specified for pattern : " + name;
00320 throw new DocumentParseException(error + "\n" + cnfe.getMessage());
00321 }
00322 catch(NoSuchMethodException nsme) {
00323 String error = "The class for the transform specified for pattern : " + name;
00324 error += " does not contain an appropriate constructor.";
00325 throw new DocumentParseException(error + "\n" + nsme.getMessage());
00326 }
00327 catch(Exception ie) {
00328 String error = "Error instantiating transform specified for pattern : " + name;
00329 throw new DocumentParseException(error + "\n" + ie.getMessage());
00330 }
00331 }
00332
00333 private static Processor parseProcessor(Element node) throws DocumentParseException, InvalidValueException,
00334 IllegalParamterTypeException, MissingArgumentException, ResourceUnavailableException {
00335 return (Processor)parseConfigurable(node);
00336 }
00337
00338 private static RecordSet parseRecordSet(Element node, String name) throws DocumentParseException, InvalidValueException,
00339 IllegalParamterTypeException, MissingArgumentException, ResourceUnavailableException {
00340
00341
00342 String className = node.getAttribute("class");
00343 Class cons_param_types[] = {String.class};
00344 Class rsClass;
00345 Constructor constructor;
00346 RecordSet rs;
00347
00348 try {
00349 rsClass = Class.forName(PKG + className);
00350
00351
00352 constructor = rsClass.getDeclaredConstructor();
00353
00354
00355 rs = (RecordSet)constructor.newInstance();
00356 }
00357 catch(ClassNotFoundException cnfe ) {
00358 String error = "The class " + className + " was not found. Unable to create RecordSet. ";
00359 throw new DocumentParseException(error + "\n" + cnfe.getMessage());
00360 }
00361 catch(NoSuchMethodException nsme) {
00362 String error = "An appropriate constructor was not found in class " + className;
00363 error += " unable to create RecordSet.";
00364 throw new DocumentParseException(error + "\n" + nsme.getMessage());
00365 }
00366 catch(Exception ie) {
00367 String error = "Error instantiating RecordSet : " + className;
00368 throw new DocumentParseException(error + "\n" + ie.getMessage());
00369 }
00370
00371 rs.setName(name);
00372
00373
00374 NodeList params = node.getElementsByTagName("param");
00375 for(int i = 0; i<params.getLength(); i++) {
00376 Element param = (Element)params.item(i);
00377 rs.setParam(param.getAttribute("name"), param.getTextContent());
00378 }
00379
00380
00381 rs.checkParams();
00382
00383
00384 rs.init();
00385
00386 return rs;
00387
00388 }
00389
00390 private static Renderer parseRenderer(Element node) throws DocumentParseException, InvalidValueException,
00391 IllegalParamterTypeException, MissingArgumentException, ResourceUnavailableException {
00392
00393
00394 Renderer render = (Renderer)parseConfigurable(node);
00395
00396
00397 NodeList docsections = node.getElementsByTagName("docsection");
00398
00399 for(int i = 0; i< docsections.getLength(); i++) {
00400
00401 Element docsection = (Element)docsections.item(i);
00402 DocumentSection section = null;
00403
00404 String file = docsection.getAttribute("file");
00405 if(file!=null && file.length() > 1) {
00406 try {
00407 section = new DocumentSection(new File(file));
00408 }
00409 catch(FileNotFoundException fnfe) {
00410 String error = "Error creating document section from file : " + file;
00411 error += " File was not found";
00412 throw new DocumentParseException(error);
00413 }
00414 catch(IOException ioe) {
00415 String error = "Error creating document section from file : " + file;
00416 error += " Error reading file.";
00417 throw new DocumentParseException(error);
00418 }
00419 }
00420 else {
00421 String start = docsection.getFirstChild().getTextContent();
00422 String end = docsection.getLastChild().getTextContent();
00423 section = new DocumentSection(start, end);
00424 }
00425
00426 render.addDocumentSection(section, docsection.getAttribute("ref"));
00427 }
00428
00429 return render;
00430 }
00431
00432 private static Configurable parseConfigurable(Element node) throws DocumentParseException, InvalidValueException,
00433 IllegalParamterTypeException, MissingArgumentException, ResourceUnavailableException {
00434
00435
00436 String className = node.getAttribute("class");
00437 String name = node.getAttribute("name");
00438
00439 Class cons_param_types[] = {String.class};
00440 Class rsClass;
00441 Constructor constructor;
00442 Configurable rs;
00443
00444 try {
00445 rsClass = Class.forName(PKG + className);
00446
00447
00448 constructor = rsClass.getDeclaredConstructor();
00449
00450
00451 rs = (Configurable)constructor.newInstance();
00452 }
00453 catch(ClassNotFoundException cnfe ) {
00454 String error = "The class " + className + " was not found. Unable to create Configurable " + name;
00455 throw new DocumentParseException(error + "\n" + cnfe.getMessage());
00456 }
00457 catch(NoSuchMethodException nsme) {
00458 String error = "An appropriate constructor was not found in class " + className;
00459 error += " unable to create Configurable " + name + ". ";
00460 throw new DocumentParseException(error + "\n" + nsme.getMessage());
00461 }
00462 catch(Exception ie) {
00463 String error = "Error instantiating Configurable " + name + " : " + className;
00464 throw new DocumentParseException(error + "\n" + ie.getMessage());
00465 }
00466
00467
00468 NodeList params = node.getElementsByTagName("param");
00469 for(int i = 0; i<params.getLength(); i++) {
00470 Element param = (Element)params.item(i);
00471 rs.setParam(param.getAttribute("name"), param.getTextContent());
00472 }
00473
00474
00475 rs.checkParams();
00476
00477
00478 rs.init();
00479
00480 return rs;
00481
00482 }
00483
00484
00485 }