001: /*
002: * Enhydra Java Application Server Project
003: *
004: * The contents of this file are subject to the Enhydra Public License
005: * Version 1.1 (the "License"); you may not use this file except in
006: * compliance with the License. You may obtain a copy of the License on
007: * the Enhydra web site ( http://www.enhydra.org/ ).
008: *
009: * Software distributed under the License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
011: * the License for the specific terms governing rights and limitations
012: * under the License.
013: *
014: * The Initial Developer of the Enhydra Application Server is Lutris
015: * Technologies, Inc. The Enhydra Application Server and portions created
016: * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
017: * All Rights Reserved.
018: *
019: * Contributor(s):
020: *
021: */
022: package org.enhydra.kelp.common.bridge;
023:
024: // XMLC imports
025: import org.enhydra.xml.xmlc.compiler.Parse;
026: import org.enhydra.xml.xmlc.dom.XMLCDocument;
027: import org.enhydra.xml.xmlc.XMLCException;
028:
029: // Standard imports
030: import java.io.PrintWriter;
031: import java.lang.reflect.Constructor;
032: import java.lang.reflect.Method;
033:
034: //
035: public class ParserV1 implements Parser {
036:
037: // strings not to be resourced
038: private final String ERROR_REPORTER_CLASS = "org.enhydra.xml.xmlc.compiler.ErrorReporter"; // nores
039: private final String PARSE_CLASS = "org.enhydra.xml.xmlc.compiler.Parse"; // nores
040: private final String PARSE_METHOD = "parse"; // nores
041:
042: //
043: private Parse parse = null;
044: private PrintWriter traceWriter = null;
045: private MetaDataHandler handler = null;
046:
047: public ParserV1(PrintWriter trace, MetaDataHandler metaData) {
048: traceWriter = trace;
049: handler = metaData;
050: Class parseClass = null;
051: Constructor[] cons = new Constructor[0];
052: Object[] values = new Object[2];
053:
054: try {
055: parseClass = Class.forName(PARSE_CLASS);
056: cons = parseClass.getConstructors();
057: for (int i = 0; i < cons.length; i++) {
058: if (cons[i].getParameterTypes().length == 2) {
059: values[0] = createErrorReporter(traceWriter);
060: if (handler.getPrintParseInfo()) {
061: values[1] = traceWriter;
062: } else {
063: values[1] = null;
064: }
065: parse = (Parse) cons[i].newInstance(values);
066: break;
067: }
068: }
069: } catch (Exception e) {
070: e.printStackTrace();
071: }
072: }
073:
074: public MetaDataHandler getMetaDataHandler() {
075: return handler;
076: }
077:
078: public PrintWriter getTraceWriter() {
079: return traceWriter;
080: }
081:
082: public XMLCDocument parse() throws XMLCException {
083: XMLCDocument doc = null;
084:
085: doc = (XMLCDocument) callParse();
086: return doc;
087: }
088:
089: private Object createErrorReporter(PrintWriter writer) {
090: Object reporter = null;
091: Constructor con = null;
092: Class reportClass = null;
093: Class[] types = new Class[1];
094: Object[] values = new Object[1];
095:
096: types[0] = writer.getClass();
097: try {
098: reportClass = Class.forName(ERROR_REPORTER_CLASS);
099: con = reportClass.getConstructor(types);
100: values[0] = writer;
101: reporter = con.newInstance(values);
102: } catch (Exception e) {
103: e.printStackTrace();
104: }
105: return reporter;
106: }
107:
108: private Object callParse() throws XMLCException {
109: Object doc = null;
110: Method meths[] = parse.getClass().getDeclaredMethods();
111: Method parseMethod = null;
112:
113: for (int i = 0; i < meths.length; i++) {
114: if (meths[i].getName().equals(PARSE_METHOD)) {
115: parseMethod = meths[i];
116: break;
117: }
118: }
119: if (parseMethod != null) {
120: Object params[] = new Object[1];
121:
122: params[0] = handler.getMetaData();
123: try {
124: doc = parseMethod.invoke(parse, params);
125: } catch (java.lang.IllegalAccessException e) {
126: e.printStackTrace();
127: } catch (java.lang.reflect.InvocationTargetException e) {
128: if (e.getTargetException() instanceof XMLCException) {
129: throw (XMLCException) e.getTargetException();
130: } else {
131: XMLCException xe = new XMLCException(e
132: .getTargetException().toString());
133:
134: e.getTargetException().printStackTrace();
135: throw xe;
136: }
137: }
138: }
139: return doc;
140: }
141:
142: }
|