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.dom.XMLCDocument;
026: import org.enhydra.xml.xmlc.XMLCException;
027:
028: // Standard imports
029: import java.io.PrintWriter;
030: import java.lang.reflect.Constructor;
031: import java.lang.reflect.Method;
032:
033: //
034: public class GeneratorV1 implements Generator {
035:
036: // strings not to be resourced
037: private final String CODE_GENERATOR_CLASS = "org.enhydra.xml.xmlc.compiler.CodeGenerator"; // nores
038: private final String GENERATE_METHOD = "generateJavaSource"; // nores
039:
040: //
041: private Object generator = null;
042: private Class genClass = null;
043: private Object metaData = null;
044:
045: public GeneratorV1(MetaDataHandler handler, XMLCDocument xmlcDoc,
046: PrintWriter verboseOutput) throws XMLCException {
047: Constructor[] cons = new Constructor[0];
048: Constructor constructor = null;
049: Class[] params = null;
050: XMLCException xe = null;
051: Object compileParams = handler.getMetaData();
052:
053: metaData = compileParams;
054: try {
055: genClass = Class.forName(CODE_GENERATOR_CLASS);
056: cons = genClass.getConstructors();
057: for (int i = 0; i < cons.length; i++) {
058: params = cons[i].getParameterTypes();
059:
060: // stop on non-default constructors.
061: if (params.length == 3) {
062: constructor = cons[i];
063: break;
064: }
065: }
066: if (constructor == null) {
067: throw new XMLCException("Constructor not found");
068: } else {
069: Object[] values = new Object[3];
070:
071: values[0] = metaData;
072: values[1] = xmlcDoc;
073: values[2] = verboseOutput;
074: generator = constructor.newInstance(values);
075: }
076: } catch (java.lang.reflect.InvocationTargetException e) {
077: if (e.getTargetException() instanceof XMLCException) {
078: throw (XMLCException) e.getTargetException();
079: } else {
080: xe = new XMLCException(e.getTargetException()
081: .toString());
082: e.getTargetException().printStackTrace();
083: throw xe;
084: }
085: } catch (Exception e) {
086: xe = new XMLCException(e.toString());
087: e.printStackTrace();
088: throw xe;
089: }
090: }
091:
092: public void generateJavaSource(PrintWriter writer)
093: throws XMLCException {
094:
095: // codeGen.generateJavaSource(node.getOptions(), null);
096: Method[] meths = genClass.getMethods();
097: Method generate = null;
098: Class[] paramTypes = null;
099: XMLCException xe = null;
100:
101: for (int i = 0; i < meths.length; i++) {
102: if (meths[i].getName().equals(GENERATE_METHOD)) {
103: generate = meths[i];
104: break;
105: }
106: }
107: if (generate != null) {
108: paramTypes = generate.getParameterTypes();
109: Object paramValues[] = new Object[paramTypes.length];
110:
111: paramValues[0] = metaData;
112: paramValues[1] = writer;
113: try {
114: generate.invoke(generator, paramValues);
115: } catch (java.lang.reflect.InvocationTargetException e) {
116: e.printStackTrace();
117: if (e.getTargetException() instanceof XMLCException) {
118: throw (XMLCException) e.getTargetException();
119: } else {
120: xe = new XMLCException(e.getTargetException()
121: .toString());
122: throw xe;
123: }
124: } catch (Exception e) {
125: xe = new XMLCException(e.toString());
126: e.printStackTrace();
127: throw xe;
128: }
129: }
130: }
131:
132: }
|