KickJava   Java API By Example, From Geeks To Geeks.

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


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
23 package org.enhydra.kelp.common.bridge;
24
25 // Kelp imports
26
import org.enhydra.kelp.common.node.OtterXMLCNode;
27
28 // XMLC imports
29
import org.enhydra.xml.xmlc.XMLCException;
30 import org.enhydra.xml.xmlc.commands.xmlc.XMLCOptions;
31 import org.enhydra.xml.xmlc.commands.options.OptionsParser;
32
33 // Standard imports
34
import java.io.File JavaDoc;
35 import java.io.IOException JavaDoc;
36 import java.io.PrintWriter JavaDoc;
37 import java.lang.reflect.Constructor JavaDoc;
38 import java.lang.reflect.Method JavaDoc;
39 import java.lang.reflect.InvocationTargetException JavaDoc;
40 import java.lang.reflect.Array JavaDoc;
41
42 //
43
public class MetaDataHandlerV1 implements MetaDataHandler {
44
45     // strings not to be resourced
46
private final String JavaDoc PARSER_CLASS =
47         "org.enhydra.xml.xmlc.commands.options.OptionsParser"; // nores
48
private final String JavaDoc SET_DOCUMENT_OUTPUT_METHOD = "setDocumentOutput"; // nores
49
private final String JavaDoc SET_FOR_RECOMP_METHOD = "setForRecomp"; // nores
50
private final String JavaDoc SET_PRINT_VERSION_METHOD = "setPrintVersion"; // nores
51
private final String JavaDoc GET_DELETE_ELEMENT_CLASSES_METHOD =
52         "getDeleteElementClasses"; // nores
53
private final String JavaDoc GET_URL_EDITS_METHOD = "getURLEdits"; // nores
54
private final String JavaDoc SAVE_METHOD = "save"; // nores
55
private final String JavaDoc PARSE_METHOD = "parse"; // nores
56
private final String JavaDoc BOOLEAN_TYPE = "boolean"; // nores
57

58     //
59
private LocalMetaData metaData = null;
60     private boolean printDocInfo = false;
61
62     public MetaDataHandlerV1() {
63         metaData = new LocalMetaData();
64     }
65
66     public String JavaDoc getDocumentOutput() {
67         String JavaDoc outPath = null;
68         File JavaDoc outFile = metaData.getDocumentOutput();
69
70         if (outFile != null) {
71             outPath = outFile.getAbsolutePath();
72         }
73         return outPath;
74     }
75
76     public void setDocumentOutput(String JavaDoc s) {
77         invokeSetter(SET_DOCUMENT_OUTPUT_METHOD, File JavaDoc.class.getName(),
78                      new File JavaDoc(s));
79     }
80
81     // Compiler Options
82
public boolean getCompileSource() {
83         return (!metaData.getNoCompile());
84     }
85
86     public void setCompileSource(boolean b) {
87         metaData.setNoCompile(!b);
88     }
89
90     public String JavaDoc getInputDocument() {
91         File JavaDoc doc = null;
92         String JavaDoc docPath = new String JavaDoc();
93
94         doc = metaData.getSourceDocument();
95         if (doc != null) {
96             docPath = doc.getAbsolutePath();
97         }
98         return docPath;
99     }
100
101     public void setInputDocument(String JavaDoc s) {
102         File JavaDoc f = new File JavaDoc(s);
103
104         metaData.setSourceDocument(f, true);
105     }
106
107     public boolean getKeepGeneratedSource() {
108         return metaData.getKeep();
109     }
110
111     public void setKeepGeneratedSource(boolean b) {
112         metaData.setKeep(b);
113     }
114
115     public boolean getPrintAccessorInfo() {
116         return metaData.getPrintAccessMethods();
117     }
118
119     public void setPrintAccessorInfo(boolean b) {
120         metaData.setPrintAccessMethods(b);
121     }
122
123     public boolean getPrintDocumentInfo() {
124         return printDocInfo;
125     }
126
127     public void setPrintDocumentInfo(boolean b) {
128         printDocInfo = b;
129     }
130
131     public boolean getPrintDOM() {
132         return metaData.getDump();
133     }
134
135     public void setPrintDOM(boolean b) {
136         metaData.setDump(b);
137     }
138
139     public boolean getPrintParseInfo() {
140         return metaData.getParseInfo();
141     }
142
143     public void setPrintParseInfo(boolean b) {
144         metaData.setParseInfo(b);
145     }
146
147     public boolean getPrintVersion() {
148         return metaData.getPrintVersion();
149     }
150
151     public boolean getVerbose() {
152         return metaData.getVerbose();
153     }
154
155     public void setVerbose(boolean b) {
156         metaData.setVerbose(b);
157     }
158
159     // Document Class
160
public String JavaDoc getClassName() {
161         return metaData.getClassName();
162     }
163
164     public void setClassName(String JavaDoc n) {
165         metaData.setClassName(n);
166     }
167
168     public File JavaDoc getJavaClassSource() {
169         return metaData.getJavaClassSource();
170     }
171
172     public void setJavaClassSource(File JavaDoc f, OtterXMLCNode node) {
173         metaData.setJavaClassSource(f);
174     }
175
176     public File JavaDoc getJavaInterfaceSource() {
177         return metaData.getJavaInterfaceSource();
178     }
179
180     public void setJavaInterfaceSource(File JavaDoc f, OtterXMLCNode node) {
181         metaData.setJavaInterfaceSource(f);
182     }
183
184     public String JavaDoc getPackageName() {
185         return metaData.getPackageName();
186     }
187
188     public boolean getRecompilation() {
189         return metaData.getForRecomp();
190     }
191
192     public void setRecompilation(boolean b) {
193         invokeSetter(SET_FOR_RECOMP_METHOD, BOOLEAN_TYPE, new Boolean JavaDoc(b));
194     }
195
196     public void setPrintVersion(boolean b) {
197         invokeSetter(SET_PRINT_VERSION_METHOD, BOOLEAN_TYPE, new Boolean JavaDoc(b));
198     }
199
200     // DOM Edits
201
public Object JavaDoc[] getDeleteElements() {
202         Object JavaDoc result = new Object JavaDoc();
203
204         result = invokeGetter(GET_DELETE_ELEMENT_CLASSES_METHOD);
205         return (Object JavaDoc[]) result;
206     }
207
208     public Object JavaDoc[] getURLMappings() {
209         Object JavaDoc result = new Object JavaDoc();
210
211         result = invokeGetter(GET_URL_EDITS_METHOD);
212         return (Object JavaDoc[]) result;
213     }
214
215     // Other
216
public Object JavaDoc getMetaData() {
217         return metaData;
218     }
219
220     public void parse(String JavaDoc[] files, String JavaDoc[] args, PrintWriter JavaDoc writer,
221                       OtterXMLCNode node) throws XMLCException, IOException JavaDoc {
222         OptionsParser op = null;
223
224         metaData.complete();
225         if ((files.length >= 1)) {
226             op = getNewOptionsParser();
227             callParse(op, files);
228         }
229         if ((args.length >= 1)) {
230             if (op == null) {
231                 op = getNewOptionsParser();
232             }
233             callParse(op, args);
234         }
235     }
236
237     public void save(File JavaDoc op) throws IOException JavaDoc {
238         if (!op.getParentFile().exists()) {
239             op.getParentFile().mkdirs();
240         }
241         invokeSetter(SAVE_METHOD, File JavaDoc.class.getName(), op);
242     }
243
244     // / private stuff
245
private Object JavaDoc invokeGetter(String JavaDoc methodName) {
246         Class JavaDoc[] types = new Class JavaDoc[0];
247         Object JavaDoc[] values = new Object JavaDoc[0];
248         Object JavaDoc result = new Object JavaDoc();
249         Method JavaDoc meth = null;
250
251         try {
252             meth = metaData.getClass().getMethod(methodName, types);
253             result = meth.invoke(metaData, values);
254         } catch (Exception JavaDoc e) {
255             e.printStackTrace();
256         }
257         return result;
258     }
259
260     private void invokeSetter(String JavaDoc methodName, String JavaDoc typeName,
261                               Object JavaDoc value) {
262         Class JavaDoc[] types = new Class JavaDoc[1];
263         Object JavaDoc[] values = new Object JavaDoc[1];
264         Method JavaDoc meth = null;
265
266         try {
267             types[0] = Class.forName(typeName);
268             values[0] = value;
269             meth = metaData.getClass().getMethod(methodName, types);
270             meth.invoke(metaData, values);
271         } catch (Exception JavaDoc e) {
272             e.printStackTrace();
273         }
274     }
275
276     private OptionsParser getNewOptionsParser() {
277         OptionsParser parser = null;
278
279         try {
280             Class JavaDoc opClass = Class.forName(PARSER_CLASS);
281             Constructor JavaDoc[] opCon = opClass.getConstructors();
282             Class JavaDoc[] params = opCon[0].getParameterTypes();
283             Object JavaDoc[] values = new Object JavaDoc[0];
284
285             if (params.length == 1) {
286                 values = new Object JavaDoc[1];
287                 values[0] = metaData;
288             }
289             parser = (OptionsParser) opCon[0].newInstance(values);
290         } catch (Exception JavaDoc e) {
291             parser = null;
292             e.printStackTrace();
293         }
294         return parser;
295     }
296
297     private void callParse(OptionsParser parser,
298                            String JavaDoc[] args) throws IOException JavaDoc, XMLCException {
299         Class JavaDoc opClass = parser.getClass();
300         Class JavaDoc[] params = new Class JavaDoc[1];
301         Object JavaDoc[] values = null;
302         Method JavaDoc method = null;
303
304         params[0] = args.getClass();
305         try {
306             method = opClass.getMethod(PARSE_METHOD, params);
307             values = new Object JavaDoc[1];
308             values[0] = args;
309             method.invoke(parser, values);
310         } catch (InvocationTargetException JavaDoc e) {
311             if (e.getTargetException() instanceof IOException JavaDoc) {
312                 throw (IOException JavaDoc) e.getTargetException();
313             } else if (e.getTargetException() instanceof XMLCException) {
314                 throw (XMLCException) e.getTargetException();
315             } else {
316                 e.printStackTrace();
317             }
318         } catch (Exception JavaDoc e) {
319             e.printStackTrace();
320         }
321
322         // op.parse(args); // 3.0
323
// op.parse(options, args); //2.3
324
}
325
326     /**
327      * Class declaration
328      */

329     public class LocalMetaData extends XMLCOptions {
330         private File JavaDoc javaClassSource = null;
331         private File JavaDoc javaInterfaceSource = null;
332         private File JavaDoc sourceDoc = null;
333
334         /**
335          * Method declaration
336          *
337          *
338          * @param f
339          */

340         public void setJavaClassSource(File JavaDoc f) {
341             javaClassSource = f;
342         }
343
344         public void setJavaInterfaceSource(File JavaDoc f) {
345             javaInterfaceSource = f;
346         }
347
348         /**
349          * Method declaration
350          *
351          *
352          * @return
353          */

354         public File JavaDoc getJavaClassSource() {
355             return javaClassSource;
356         }
357
358         public File JavaDoc getJavaInterfaceSource() {
359             return javaInterfaceSource;
360         }
361
362         /**
363          * Method declaration
364          *
365          *
366          * @param f
367          * @param real
368          */

369         public void setSourceDocument(File JavaDoc f, boolean real) {
370             if (real) {
371                 sourceDoc = f;
372                 setSourceDocument(sourceDoc);
373             }
374         }
375
376         /**
377          * Method declaration
378          *
379          *
380          * @param f
381          */

382         public void setSourceDocument(File JavaDoc f) {
383             super.setSourceDocument(sourceDoc);
384         }
385
386         /**
387          * Method declaration
388          *
389          *
390          * @return
391          */

392         public File JavaDoc getSourceDocument() {
393             return sourceDoc;
394         }
395
396     }
397 }
398
Popular Tags