KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > kelp > common > bridge > ParserV1


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 JavaDoc;
31 import java.lang.reflect.Constructor JavaDoc;
32 import java.lang.reflect.Method JavaDoc;
33
34 //
35
public class ParserV1 implements Parser {
36
37     // strings not to be resourced
38
private final String JavaDoc ERROR_REPORTER_CLASS =
39         "org.enhydra.xml.xmlc.compiler.ErrorReporter"; // nores
40
private final String JavaDoc PARSE_CLASS =
41         "org.enhydra.xml.xmlc.compiler.Parse"; // nores
42
private final String JavaDoc PARSE_METHOD = "parse"; // nores
43

44     //
45
private Parse parse = null;
46     private PrintWriter JavaDoc traceWriter = null;
47     private MetaDataHandler handler = null;
48
49     public ParserV1(PrintWriter JavaDoc trace, MetaDataHandler metaData) {
50         traceWriter = trace;
51         handler = metaData;
52         Class JavaDoc parseClass = null;
53         Constructor JavaDoc[] cons = new Constructor JavaDoc[0];
54         Object JavaDoc[] values = new Object JavaDoc[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 JavaDoc e) {
72             e.printStackTrace();
73         }
74     }
75
76     public MetaDataHandler getMetaDataHandler() {
77         return handler;
78     }
79
80     public PrintWriter JavaDoc 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 JavaDoc createErrorReporter(PrintWriter JavaDoc writer) {
92         Object JavaDoc reporter = null;
93         Constructor JavaDoc con = null;
94         Class JavaDoc reportClass = null;
95         Class JavaDoc[] types = new Class JavaDoc[1];
96         Object JavaDoc[] values = new Object JavaDoc[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 JavaDoc e) {
105             e.printStackTrace();
106         }
107         return reporter;
108     }
109
110     private Object JavaDoc callParse() throws XMLCException {
111         Object JavaDoc doc = null;
112         Method JavaDoc meths[] = parse.getClass().getDeclaredMethods();
113         Method JavaDoc 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 JavaDoc params[] = new Object JavaDoc[1];
123
124             params[0] = handler.getMetaData();
125             try {
126                 doc = parseMethod.invoke(parse, params);
127             } catch (java.lang.IllegalAccessException JavaDoc e) {
128                 e.printStackTrace();
129             } catch (java.lang.reflect.InvocationTargetException JavaDoc 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