Source code: org/enhydra/kelp/common/bridge/ParserV1.java
1 /*
2 * Enhydra Java Application Server Project
3 *
4 * The contents of this file are subject to the Enhydra Public License
5 * Version 1.1 (the "License"); you may not use this file except in
6 * compliance with the License. You may obtain a copy of the License on
7 * the Enhydra web site ( http://www.enhydra.org/ ).
8 *
9 * Software distributed under the License is distributed on an "AS IS"
10 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11 * the License for the specific terms governing rights and limitations
12 * under the License.
13 *
14 * The Initial Developer of the Enhydra Application Server is Lutris
15 * Technologies, Inc. The Enhydra Application Server and portions created
16 * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17 * All Rights Reserved.
18 *
19 * Contributor(s):
20 *
21 */
22 package org.enhydra.kelp.common.bridge;
23
24 // XMLC imports
25 import org.enhydra.xml.xmlc.compiler.Parse;
26 import org.enhydra.xml.xmlc.dom.XMLCDocument;
27 import org.enhydra.xml.xmlc.XMLCException;
28
29 // Standard imports
30 import java.io.PrintWriter;
31 import java.lang.reflect.Constructor;
32 import java.lang.reflect.Method;
33
34 //
35 public class ParserV1 implements Parser {
36
37 // strings not to be resourced
38 private final String ERROR_REPORTER_CLASS =
39 "org.enhydra.xml.xmlc.compiler.ErrorReporter"; // nores
40 private final String PARSE_CLASS =
41 "org.enhydra.xml.xmlc.compiler.Parse"; // nores
42 private final String PARSE_METHOD = "parse"; // nores
43
44 //
45 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 }