001: ///////////////////////////////////////////////////////////////////////////////
002: //
003: // Copyright (C) 2003-@year@ by Thomas M. Hazel, MyOODB (www.myoodb.org)
004: //
005: // All Rights Reserved
006: //
007: // This program is free software; you can redistribute it and/or modify
008: // it under the terms of the GNU General Public License and GNU Library
009: // General Public License as published by the Free Software Foundation;
010: // either version 2, or (at your option) any later version.
011: //
012: // This program is distributed in the hope that it will be useful,
013: // but WITHOUT ANY WARRANTY; without even the implied warranty of
014: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: // GNU General Public License and GNU Library General Public License
016: // for more details.
017: //
018: // You should have received a copy of the GNU General Public License
019: // and GNU Library General Public License along with this program; if
020: // not, write to the Free Software Foundation, 675 Mass Ave, Cambridge,
021: // MA 02139, USA.
022: //
023: ///////////////////////////////////////////////////////////////////////////////
024: package org.myoodb.tools.generator;
025:
026: import java.io.*;
027: import java.util.*;
028: import java.lang.reflect.Method;
029: import java.lang.reflect.Modifier;
030:
031: import org.myoodb.*;
032: import org.myoodb.core.*;
033:
034: public class ScriptGenerator extends AbstractCodeGeneratorImpl
035: implements AbstractCodeGenerator {
036: public final static String IMPL_NAME_SUFFIX = "@implExtension@";
037:
038: private String m_file;
039: private PrintWriter m_out;
040: private Class m_myoodbImplClass;
041: private Method[] m_sortedMethods;
042: private ArrayList m_remoteInterfaces;
043:
044: public ScriptGenerator() {
045: m_out = null;
046: m_file = null;
047: m_sortedMethods = null;
048: m_myoodbImplClass = null;
049: m_remoteInterfaces = null;
050: }
051:
052: private int getMethodIndex(String methodName,
053: AbstractCodeGenerator.Parameter parameters[])
054: throws GeneratorException {
055: try {
056: Method method = m_myoodbImplClass.getMethod(methodName,
057: parametersToClasses(parameters));
058: return MethodHelper.getMethodIndex(m_sortedMethods, method);
059: } catch (ClassNotFoundException e) {
060: throw new GeneratorException(e);
061: } catch (NoSuchMethodException e) {
062: throw new GeneratorException(e);
063: }
064: }
065:
066: private void makeConstructor(String className) {
067: m_out.print("function "
068: + Helper.packageName(className).replaceAll("\\.", "_")
069: + "_" + Helper.simpleClassName(className)
070: + "(identifier,database)\n");
071: m_out.print("{\n");
072:
073: m_out.print(" this.__init__(identifier,database);\n\n");
074: }
075:
076: private void makePackage(String implementationClass) {
077: m_out
078: .print("// JavaScript class generated by MyOODB-@version@ (DO NOT EDIT!)\n");
079: m_out.print("\n");
080: if (Helper.packageName(implementationClass).equals("") == false) {
081: m_out.print(Helper.packageName(implementationClass)
082: .replaceAll("\\.", "_")
083: + "_");
084: }
085: }
086:
087: private void makeInhertance(String className) {
088: /*
089: String parentClassName = m_myoodbImplClass.getSuperclass().getName();
090: if (parentClassName.equals("org.myoodb.MyOodbObject") == false)
091: {
092: m_out.print("public class " + Helper.simpleClassName(className) + " extends " + parentClassName);
093: }
094: else
095: {
096: */
097: m_out.print(Helper.simpleClassName(className)
098: + ".inheritsFrom(org_myoodb_MyOodbProxy);");
099: m_out.print("\n");
100: m_out.print("\n");
101: /*
102: }
103: */
104: }
105:
106: public void beginClass(String sourcePath, String className)
107: throws GeneratorException {
108: m_file = Helper.classToFileName(className) + ".js";
109: System.out.println(" BEGIN - Generating Script for: "
110: + m_file);
111:
112: try {
113: String fileName = sourcePath + "/" + m_file;
114: m_out = new PrintWriter(fileName);
115: } catch (IOException e) {
116: throw new GeneratorException(e);
117: }
118:
119: try {
120: Class interfaceType = Class.forName(className);
121: m_myoodbImplClass = Class.forName(className
122: + IMPL_NAME_SUFFIX);
123: m_sortedMethods = MethodHelper.getMethods(
124: m_myoodbImplClass, interfaceType);
125: m_remoteInterfaces = Helper.findInterfaces(
126: m_myoodbImplClass, MyOodbRemote.class);
127: } catch (ClassNotFoundException e) {
128: throw new GeneratorException(e);
129: }
130:
131: makePackage(className);
132: makeInhertance(className);
133: makeConstructor(className);
134: }
135:
136: public void makeMethod(String name, int accessLevel)
137: throws GeneratorException {
138: ArrayList methods = new ArrayList();
139:
140: for (int methodIndex = 0; methodIndex < m_sortedMethods.length; methodIndex++) {
141: Method method = m_sortedMethods[methodIndex];
142:
143: if ((Modifier.isFinal(method.getModifiers()) == true)
144: || (Modifier.isStatic(method.getModifiers()) == true)
145: || (method.getName().equals(name) == false)) {
146: continue;
147: }
148:
149: methods.add(method);
150: }
151:
152: for (int i = 0; i < methods.size(); i++) {
153: String uniqueName = name;
154: Method method = (Method) methods.get(i);
155: AbstractCodeGenerator.Parameter parameters[] = getParameters(method
156: .getParameterTypes());
157:
158: if (methods.size() > 1) {
159: uniqueName += (parameters.length > 0 ? "By" : "");
160: for (int j = 0; j < parameters.length; j++) {
161: String type = Helper.nameForPrimitive(parameters[j]
162: .getType());
163: if (type == null) {
164: type = parameters[j].getType();
165: int index = type.lastIndexOf('.');
166: if (index != -1) {
167: type = type.substring(index + 1);
168: }
169: }
170: uniqueName += (j > 0 ? "And" : "");
171: uniqueName += type;
172: }
173: }
174:
175: // synchronous
176: {
177: StringBuilder signaturBuf = new StringBuilder();
178: signaturBuf.append(" this." + uniqueName
179: + " = function(");
180:
181: for (int j = 0; j < parameters.length; j++) {
182: signaturBuf.append(j > 0 ? "," : "");
183: signaturBuf.append(parameters[j].getName());
184: }
185: signaturBuf.append(")");
186: String signaturStr = signaturBuf.toString();
187:
188: m_out.print(signaturStr);
189:
190: m_out.print("\n");
191: m_out.print(" {\n");
192:
193: if (parameters.length != 0) {
194: m_out.print(" var args = new Array(");
195: for (int j = 0; j < parameters.length; j++) {
196: m_out.print(j > 0 ? "," : "");
197: String type = parameters[j].getType();
198: if (Helper.isPrimitive(type) == true) {
199: m_out.print("\"<" + type + ">\" + arg" + j
200: + " + \"</" + type + ">\"");
201: } else if (Helper.isBase(type) == true) {
202: type = Helper.getBase(type);
203:
204: m_out.print("\"<" + type + ">\" + arg" + j
205: + " + \"</" + type + ">\"");
206: } else if (type.equals("java.lang.String") == true) {
207: m_out.print("\"<string>\" + arg" + j
208: + " + \"</string>\"");
209: } else {
210: m_out.print("arg" + j);
211: }
212: }
213: m_out.print(");\n");
214:
215: m_out
216: .print(" return this.database.invokeMethod(this.identifier,"
217: + getMethodIndex(name, parameters)
218: + ",args," + accessLevel + ");\n");
219: } else {
220: m_out
221: .print(" return this.database.invokeMethod(this.identifier,"
222: + getMethodIndex(name, parameters)
223: + ",null," + accessLevel + ");\n");
224: }
225: }
226:
227: m_out.print(" }\n");
228:
229: // asynchronous
230: {
231: StringBuilder signaturBuf = new StringBuilder();
232: signaturBuf.append(" this." + uniqueName
233: + "_Async = function(");
234:
235: for (int j = 0; j < parameters.length; j++) {
236: signaturBuf.append(j > 0 ? "," : "");
237: signaturBuf.append(parameters[j].getName());
238: }
239: if (parameters.length == 0) {
240: signaturBuf.append("callback,context)");
241: } else {
242: signaturBuf.append(",callback,context)");
243: }
244: String signaturStr = signaturBuf.toString();
245:
246: m_out.print(signaturStr);
247:
248: m_out.print("\n");
249: m_out.print(" {\n");
250:
251: if (parameters.length != 0) {
252: m_out.print(" var args = new Array(");
253: for (int j = 0; j < parameters.length; j++) {
254: m_out.print(j > 0 ? "," : "");
255: String type = parameters[j].getType();
256: if (Helper.isPrimitive(type) == true) {
257: m_out.print("\"<" + type + ">\" + arg" + j
258: + " + \"</" + type + ">\"");
259: } else if (Helper.isBase(type) == true) {
260: type = Helper.getBase(type);
261:
262: m_out.print("\"<" + type + ">\" + arg" + j
263: + " + \"</" + type + ">\"");
264: } else if (type.equals("java.lang.String") == true) {
265: m_out.print("\"<string>\" + arg" + j
266: + " + \"</string>\"");
267: } else {
268: m_out.print("arg" + j);
269: }
270: }
271: m_out.print(");\n");
272:
273: m_out
274: .print(" this.database.invokeMethod_Async(this.identifier,"
275: + getMethodIndex(name, parameters)
276: + ",args,"
277: + accessLevel
278: + ",callback,context);\n");
279: } else {
280: m_out
281: .print(" this.database.invokeMethod_Async(this.identifier,"
282: + getMethodIndex(name, parameters)
283: + ",null,"
284: + accessLevel
285: + ",callback,context);\n");
286: }
287: }
288:
289: m_out.print(" }\n");
290: }
291: }
292:
293: public void endClass() throws GeneratorException {
294: m_out.print("}\n");
295: m_out.close();
296:
297: System.out.println(" END - Generating Script for: " + m_file);
298: }
299: }
|