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:
023: package org.enhydra.kelp.common.bridge;
024:
025: // Kelp imports
026: import org.enhydra.kelp.common.node.OtterXMLCNode;
027:
028: // XMLC imports
029: import org.enhydra.xml.xmlc.XMLCException;
030: import org.enhydra.xml.xmlc.commands.options.OptionsParser;
031: import org.enhydra.xml.xmlc.commands.xmlc.XMLCOptions;
032:
033: // Standard imports
034: import java.io.File;
035: import java.io.IOException;
036: import java.io.PrintWriter;
037: import java.lang.reflect.Constructor;
038: import java.lang.reflect.Method;
039: import java.lang.reflect.InvocationTargetException;
040: import java.lang.reflect.Array;
041:
042: //
043: public class MetaDataHandlerV1 implements MetaDataHandler {
044:
045: // strings not to be resourced
046: private final String PARSER_CLASS = "org.enhydra.xml.xmlc.commands.options.OptionsParser"; // nores
047: private final String SET_DOCUMENT_OUTPUT_METHOD = "setDocumentOutput"; // nores
048: private final String SET_FOR_RECOMP_METHOD = "setForRecomp"; // nores
049: private final String SET_PRINT_VERSION_METHOD = "setPrintVersion"; // nores
050: private final String GET_DELETE_ELEMENT_CLASSES_METHOD = "getDeleteElementClasses"; // nores
051: private final String GET_URL_EDITS_METHOD = "getURLEdits"; // nores
052: private final String SAVE_METHOD = "save"; // nores
053: private final String PARSE_METHOD = "parse"; // nores
054: private final String BOOLEAN_TYPE = "boolean"; // nores
055:
056: //
057: private LocalMetaData metaData = null;
058: private boolean printDocInfo = false;
059:
060: public MetaDataHandlerV1() {
061: metaData = new LocalMetaData();
062: }
063:
064: public String getDocumentOutput() {
065: String outPath = null;
066: File outFile = metaData.getDocumentOutput();
067:
068: if (outFile != null) {
069: outPath = outFile.getAbsolutePath();
070: }
071: return outPath;
072: }
073:
074: public void setDocumentOutput(String s) {
075: invokeSetter(SET_DOCUMENT_OUTPUT_METHOD, File.class.getName(),
076: new File(s));
077: }
078:
079: // Compiler Options
080: public boolean getCompileSource() {
081: return (!metaData.getNoCompile());
082: }
083:
084: public void setCompileSource(boolean b) {
085: metaData.setNoCompile(!b);
086: }
087:
088: public String getInputDocument() {
089: File doc = null;
090: String docPath = new String();
091:
092: doc = metaData.getSourceDocument();
093: if (doc != null) {
094: docPath = doc.getAbsolutePath();
095: }
096: return docPath;
097: }
098:
099: public void setInputDocument(String s) {
100: File f = new File(s);
101:
102: metaData.setSourceDocument(f, true);
103: }
104:
105: public boolean getKeepGeneratedSource() {
106: return metaData.getKeep();
107: }
108:
109: public void setKeepGeneratedSource(boolean b) {
110: metaData.setKeep(b);
111: }
112:
113: public boolean getPrintAccessorInfo() {
114: return metaData.getPrintAccessMethods();
115: }
116:
117: public void setPrintAccessorInfo(boolean b) {
118: metaData.setPrintAccessMethods(b);
119: }
120:
121: public boolean getPrintDocumentInfo() {
122: return printDocInfo;
123: }
124:
125: public void setPrintDocumentInfo(boolean b) {
126: printDocInfo = b;
127: }
128:
129: public boolean getPrintDOM() {
130: return metaData.getDump();
131: }
132:
133: public void setPrintDOM(boolean b) {
134: metaData.setDump(b);
135: }
136:
137: public boolean getPrintParseInfo() {
138: return metaData.getParseInfo();
139: }
140:
141: public void setPrintParseInfo(boolean b) {
142: metaData.setParseInfo(b);
143: }
144:
145: public boolean getPrintVersion() {
146: return metaData.getPrintVersion();
147: }
148:
149: public boolean getVerbose() {
150: return metaData.getVerbose();
151: }
152:
153: public void setVerbose(boolean b) {
154: metaData.setVerbose(b);
155: }
156:
157: // Document Class
158: public String getClassName() {
159: return metaData.getClassName();
160: }
161:
162: public void setClassName(String n) {
163: metaData.setClassName(n);
164: }
165:
166: public File getJavaClassSource() {
167: return metaData.getJavaClassSource();
168: }
169:
170: public void setJavaClassSource(File f, OtterXMLCNode node) {
171: metaData.setJavaClassSource(f);
172: }
173:
174: public File getJavaInterfaceSource() {
175: return metaData.getJavaInterfaceSource();
176: }
177:
178: public void setJavaInterfaceSource(File f, OtterXMLCNode node) {
179: metaData.setJavaInterfaceSource(f);
180: }
181:
182: public String getPackageName() {
183: return metaData.getPackageName();
184: }
185:
186: public boolean getRecompilation() {
187: return metaData.getForRecomp();
188: }
189:
190: public void setRecompilation(boolean b) {
191: invokeSetter(SET_FOR_RECOMP_METHOD, BOOLEAN_TYPE,
192: new Boolean(b));
193: }
194:
195: public void setPrintVersion(boolean b) {
196: invokeSetter(SET_PRINT_VERSION_METHOD, BOOLEAN_TYPE,
197: new Boolean(b));
198: }
199:
200: // DOM Edits
201: public Object[] getDeleteElements() {
202: Object result = new Object();
203:
204: result = invokeGetter(GET_DELETE_ELEMENT_CLASSES_METHOD);
205: return (Object[]) result;
206: }
207:
208: public Object[] getURLMappings() {
209: Object result = new Object();
210:
211: result = invokeGetter(GET_URL_EDITS_METHOD);
212: return (Object[]) result;
213: }
214:
215: // Other
216: public Object getMetaData() {
217: return metaData;
218: }
219:
220: public void parse(String[] files, String[] args,
221: PrintWriter writer, OtterXMLCNode node)
222: throws XMLCException, IOException {
223: OptionsParser op = null;
224:
225: metaData.complete();
226: if ((files.length >= 1)) {
227: op = getNewOptionsParser();
228: callParse(op, files);
229: }
230: if ((args.length >= 1)) {
231: if (op == null) {
232: op = getNewOptionsParser();
233: }
234: callParse(op, args);
235: }
236: }
237:
238: public void save(File op) throws IOException {
239: if (!op.getParentFile().exists()) {
240: op.getParentFile().mkdirs();
241: }
242: invokeSetter(SAVE_METHOD, File.class.getName(), op);
243: }
244:
245: // / private stuff
246: private Object invokeGetter(String methodName) {
247: Class[] types = new Class[0];
248: Object[] values = new Object[0];
249: Object result = new Object();
250: Method meth = null;
251:
252: try {
253: meth = metaData.getClass().getMethod(methodName, types);
254: result = meth.invoke(metaData, values);
255: } catch (Exception e) {
256: e.printStackTrace();
257: }
258: return result;
259: }
260:
261: private void invokeSetter(String methodName, String typeName,
262: Object value) {
263: Class[] types = new Class[1];
264: Object[] values = new Object[1];
265: Method meth = null;
266:
267: try {
268: types[0] = Class.forName(typeName);
269: values[0] = value;
270: meth = metaData.getClass().getMethod(methodName, types);
271: meth.invoke(metaData, values);
272: } catch (Exception e) {
273: e.printStackTrace();
274: }
275: }
276:
277: private OptionsParser getNewOptionsParser() {
278: OptionsParser parser = null;
279:
280: try {
281: Class opClass = Class.forName(PARSER_CLASS);
282: Constructor[] opCon = opClass.getConstructors();
283: Class[] params = opCon[0].getParameterTypes();
284: Object[] values = new Object[0];
285:
286: if (params.length == 1) {
287: values = new Object[1];
288: values[0] = metaData;
289: }
290: parser = (OptionsParser) opCon[0].newInstance(values);
291: } catch (Exception e) {
292: parser = null;
293: e.printStackTrace();
294: }
295: return parser;
296: }
297:
298: private void callParse(OptionsParser parser, String[] args)
299: throws IOException, XMLCException {
300: Class opClass = parser.getClass();
301: Class[] params = new Class[1];
302: Object[] values = null;
303: Method method = null;
304:
305: params[0] = args.getClass();
306: try {
307: method = opClass.getMethod(PARSE_METHOD, params);
308: values = new Object[1];
309: values[0] = args;
310: method.invoke(parser, values);
311: } catch (InvocationTargetException e) {
312: if (e.getTargetException() instanceof IOException) {
313: throw (IOException) e.getTargetException();
314: } else if (e.getTargetException() instanceof XMLCException) {
315: throw (XMLCException) e.getTargetException();
316: } else {
317: e.printStackTrace();
318: }
319: } catch (Exception e) {
320: e.printStackTrace();
321: }
322:
323: // op.parse(args); // 3.0
324: // op.parse(options, args); //2.3
325: }
326:
327: /**
328: * Class declaration
329: */
330: public class LocalMetaData extends XMLCOptions {
331: private File javaClassSource = null;
332: private File javaInterfaceSource = null;
333: private File sourceDoc = null;
334:
335: /**
336: * Method declaration
337: *
338: *
339: * @param f
340: */
341: public void setJavaClassSource(File f) {
342: javaClassSource = f;
343: }
344:
345: public void setJavaInterfaceSource(File f) {
346: javaInterfaceSource = f;
347: }
348:
349: /**
350: * Method declaration
351: *
352: *
353: * @return
354: */
355: public File getJavaClassSource() {
356: return javaClassSource;
357: }
358:
359: public File getJavaInterfaceSource() {
360: return javaInterfaceSource;
361: }
362:
363: /**
364: * Method declaration
365: *
366: *
367: * @param f
368: * @param real
369: */
370: public void setSourceDocument(File f, boolean real) {
371: if (real) {
372: sourceDoc = f;
373: setSourceDocument(sourceDoc);
374: }
375: }
376:
377: /**
378: * Method declaration
379: *
380: *
381: * @param f
382: */
383: public void setSourceDocument(File f) {
384: super .setSourceDocument(sourceDoc);
385: }
386:
387: /**
388: * Method declaration
389: *
390: *
391: * @return
392: */
393: public File getSourceDocument() {
394: return sourceDoc;
395: }
396:
397: }
398: }
|