1 22 package org.enhydra.kelp.common.bridge; 23 24 import org.enhydra.xml.xmlc.compiler.Parse; 26 import org.enhydra.xml.xmlc.dom.XMLCDocument; 27 import org.enhydra.xml.xmlc.XMLCException; 28 29 import java.io.PrintWriter ; 31 import java.lang.reflect.Constructor ; 32 import java.lang.reflect.Method ; 33 34 public class ParserV1 implements Parser { 36 37 private final String ERROR_REPORTER_CLASS = 39 "org.enhydra.xml.xmlc.compiler.ErrorReporter"; private final String PARSE_CLASS = 41 "org.enhydra.xml.xmlc.compiler.Parse"; private final String PARSE_METHOD = "parse"; 44 private Parse parse = null; 46 private PrintWriter traceWriter = null; 47 private MetaDataHandler handler = null; 48 49 public ParserV1(PrintWriter trace, MetaDataHandler metaData) { 50 traceWriter = trace; 51 handler = metaData; 52 Class parseClass = null; 53 Constructor [] cons = new Constructor [0]; 54 Object [] values = new Object [2]; 55 56 try { 57 parseClass = Class.forName(PARSE_CLASS); 58 cons = parseClass.getConstructors(); 59 for (int i = 0; i < cons.length; i++) { 60 if (cons[i].getParameterTypes().length == 2) { 61 values[0] = createErrorReporter(traceWriter); 62 if (handler.getPrintParseInfo()) { 63 values[1] = traceWriter; 64 } else { 65 values[1] = null; 66 } 67 parse = (Parse) cons[i].newInstance(values); 68 break; 69 } 70 } 71 } catch (Exception e) { 72 e.printStackTrace(); 73 } 74 } 75 76 public MetaDataHandler getMetaDataHandler() { 77 return handler; 78 } 79 80 public PrintWriter getTraceWriter() { 81 return traceWriter; 82 } 83 84 public XMLCDocument parse() throws XMLCException { 85 XMLCDocument doc = null; 86 87 doc = (XMLCDocument) callParse(); 88 return doc; 89 } 90 91 private Object createErrorReporter(PrintWriter writer) { 92 Object reporter = null; 93 Constructor con = null; 94 Class reportClass = null; 95 Class [] types = new Class [1]; 96 Object [] values = new Object [1]; 97 98 types[0] = writer.getClass(); 99 try { 100 reportClass = Class.forName(ERROR_REPORTER_CLASS); 101 con = reportClass.getConstructor(types); 102 values[0] = writer; 103 reporter = con.newInstance(values); 104 } catch (Exception e) { 105 e.printStackTrace(); 106 } 107 return reporter; 108 } 109 110 private Object callParse() throws XMLCException { 111 Object doc = null; 112 Method meths[] = parse.getClass().getDeclaredMethods(); 113 Method parseMethod = null; 114 115 for (int i = 0; i < meths.length; i++) { 116 if (meths[i].getName().equals(PARSE_METHOD)) { 117 parseMethod = meths[i]; 118 break; 119 } 120 } 121 if (parseMethod != null) { 122 Object params[] = new Object [1]; 123 124 params[0] = handler.getMetaData(); 125 try { 126 doc = parseMethod.invoke(parse, params); 127 } catch (java.lang.IllegalAccessException e) { 128 e.printStackTrace(); 129 } catch (java.lang.reflect.InvocationTargetException e) { 130 if (e.getTargetException() instanceof XMLCException) { 131 throw (XMLCException) e.getTargetException(); 132 } else { 133 XMLCException xe = 134 new XMLCException(e.getTargetException().toString()); 135 136 e.getTargetException().printStackTrace(); 137 throw xe; 138 } 139 } 140 } 141 return doc; 142 } 143 144 } 145 | Popular Tags |