001: /*
002: * This file is part of PFIXCORE.
003: *
004: * PFIXCORE is free software; you can redistribute it and/or modify
005: * it under the terms of the GNU Lesser General Public License as published by
006: * the Free Software Foundation; either version 2 of the License, or
007: * (at your option) any later version.
008: *
009: * PFIXCORE is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public License
015: * along with PFIXCORE; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: */
019: package de.schlund.pfixcore.webservice.jsgen;
020:
021: import java.io.IOException;
022: import java.io.OutputStream;
023: import java.util.StringTokenizer;
024:
025: /**
026: * Javascript class representation.
027: *
028: * @author mleidig@schlund.de
029: */
030: public class JsClass {
031:
032: String name;
033: String baseName;
034: JsMethod[] methods;
035: JsBlock constBody;
036: JsParam[] constParams;
037:
038: public JsClass(String name, String baseName, JsParam[] constParams) {
039: this .name = name;
040: this .baseName = baseName;
041: methods = new JsMethod[0];
042: constBody = new JsBlock();
043: this .constParams = constParams;
044: }
045:
046: public String getName() {
047: return name;
048: }
049:
050: public String getBaseName() {
051: return baseName;
052: }
053:
054: public void addMethod(JsMethod method) {
055: JsMethod[] upd = new JsMethod[methods.length + 1];
056: for (int i = 0; i < methods.length; i++)
057: upd[i] = methods[i];
058: upd[upd.length - 1] = method;
059: methods = upd;
060: }
061:
062: public JsMethod[] getMethods() {
063: return methods;
064: }
065:
066: public JsBlock getConstructorBody() {
067: return constBody;
068: }
069:
070: public String getConstructorParamList() {
071: StringBuffer sb = new StringBuffer();
072: for (int i = 0; i < constParams.length; i++) {
073: sb.append(constParams[i].getName());
074: if (i < constParams.length - 1)
075: sb.append(",");
076: }
077: return sb.toString();
078: }
079:
080: public void printCode(OutputStream out) throws IOException {
081: StringBuffer sb = new StringBuffer();
082: sb
083: .append("//Autogenerated webservice stub (don't modify this code manually!)\n");
084: if (getName().contains(".")) {
085: StringTokenizer st = new StringTokenizer(getName(), ".");
086: String subNs = null;
087: while (st.hasMoreTokens()) {
088: String ns = st.nextToken();
089: if (subNs == null) {
090: sb
091: .append("if(!window." + ns + ") " + ns
092: + "={};\n");
093: subNs = ns;
094: } else if (st.hasMoreTokens()) {
095: subNs = subNs + "." + ns;
096: sb.append("if(!" + subNs + ") " + subNs + "={};\n");
097: }
098: }
099: }
100: sb.append("function " + getName() + "("
101: + getConstructorParamList() + ") {\n");
102: out.write(sb.toString().getBytes());
103: getConstructorBody().printCode(" ", out);
104: sb = new StringBuffer();
105: sb.append("}\n");
106: sb.append(name + ".prototype=new " + baseName + ";\n");
107: out.write(sb.toString().getBytes());
108: for (int i = 0; i < methods.length; i++)
109: methods[i].printCode(" ", out);
110: }
111:
112: }
|