Source code: org/enhydra/kelp/common/bridge/GeneratorV1.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.dom.XMLCDocument;
26 import org.enhydra.xml.xmlc.XMLCException;
27
28 // Standard imports
29 import java.io.PrintWriter;
30 import java.lang.reflect.Constructor;
31 import java.lang.reflect.Method;
32
33 //
34 public class GeneratorV1 implements Generator {
35
36 // strings not to be resourced
37 private final String CODE_GENERATOR_CLASS =
38 "org.enhydra.xml.xmlc.compiler.CodeGenerator"; // nores
39 private final String GENERATE_METHOD = "generateJavaSource"; // nores
40
41 //
42 private Object generator = null;
43 private Class genClass = null;
44 private Object metaData = null;
45
46 public GeneratorV1(MetaDataHandler handler, XMLCDocument xmlcDoc,
47 PrintWriter verboseOutput) throws XMLCException {
48 Constructor[] cons = new Constructor[0];
49 Constructor constructor = null;
50 Class[] params = null;
51 XMLCException xe = null;
52 Object compileParams = handler.getMetaData();
53
54 metaData = compileParams;
55 try {
56 genClass = Class.forName(CODE_GENERATOR_CLASS);
57 cons = genClass.getConstructors();
58 for (int i = 0; i < cons.length; i++) {
59 params = cons[i].getParameterTypes();
60
61 // stop on non-default constructors.
62 if (params.length == 3) {
63 constructor = cons[i];
64 break;
65 }
66 }
67 if (constructor == null) {
68 throw new XMLCException("Constructor not found");
69 } else {
70 Object[] values = new Object[3];
71
72 values[0] = metaData;
73 values[1] = xmlcDoc;
74 values[2] = verboseOutput;
75 generator = constructor.newInstance(values);
76 }
77 } catch (java.lang.reflect.InvocationTargetException e) {
78 if (e.getTargetException() instanceof XMLCException) {
79 throw (XMLCException) e.getTargetException();
80 } else {
81 xe = new XMLCException(e.getTargetException().toString());
82 e.getTargetException().printStackTrace();
83 throw xe;
84 }
85 } catch (Exception e) {
86 xe = new XMLCException(e.toString());
87 e.printStackTrace();
88 throw xe;
89 }
90 }
91
92 public void generateJavaSource(PrintWriter writer) throws XMLCException {
93
94 // codeGen.generateJavaSource(node.getOptions(), null);
95 Method[] meths = genClass.getMethods();
96 Method generate = null;
97 Class[] paramTypes = null;
98 XMLCException xe = null;
99
100 for (int i = 0; i < meths.length; i++) {
101 if (meths[i].getName().equals(GENERATE_METHOD)) {
102 generate = meths[i];
103 break;
104 }
105 }
106 if (generate != null) {
107 paramTypes = generate.getParameterTypes();
108 Object paramValues[] = new Object[paramTypes.length];
109
110 paramValues[0] = metaData;
111 paramValues[1] = writer;
112 try {
113 generate.invoke(generator, paramValues);
114 } catch (java.lang.reflect.InvocationTargetException e) {
115 e.printStackTrace();
116 if (e.getTargetException() instanceof XMLCException) {
117 throw (XMLCException) e.getTargetException();
118 } else {
119 xe = new XMLCException(e.getTargetException().toString());
120 throw xe;
121 }
122 } catch (Exception e) {
123 xe = new XMLCException(e.toString());
124 e.printStackTrace();
125 throw xe;
126 }
127 }
128 }
129
130 }