001: //** Copyright Statement ***************************************************
002: //The Salmon Open Framework for Internet Applications (SOFIA)
003: // Copyright (C) 1999 - 2002, Salmon LLC
004: //
005: // This program is free software; you can redistribute it and/or
006: // modify it under the terms of the GNU General Public License version 2
007: // as published by the Free Software Foundation;
008: //
009: // This program 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 General Public License for more details.
013: //
014: // You should have received a copy of the GNU General Public License
015: // along with this program; if not, write to the Free Software
016: // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
017: //
018: // For more information please visit http://www.salmonllc.com
019: //** End Copyright Statement ***************************************************
020: package com.salmonllc.util;
021:
022: import com.salmonllc.jsp.*;
023: import java.util.*;
024: import java.io.*;
025:
026: /**
027: * Used by the framework to generate a stub controllers for a view.
028: */
029: public class CodeGenerater {
030:
031: private static String BASECLASS = "JspController";
032: private static String INITMETHOD = "public void initialize()";
033: private static String CLASSDEF = "public class";
034: private static String IMPORT = "import";
035: private static String PACKAGE = "package";
036: private static String SPACE = " ";
037: private static String IMPLEMENTS = "implements";
038: private static String EXTENDS = "extends";
039: private static String COMMA = ",";
040: private static String EVENT_PARAMETER = "event";
041:
042: private static String TAB = " ";
043:
044: private static final Vector INTERFACES;
045:
046: static {
047: INTERFACES = new Vector();
048: INTERFACES.addElement("SubmitListener");
049: INTERFACES.addElement("PageListener");
050: }
051:
052: private static void addListeners(PrintWriter pw) {
053: // add SubmitListener
054: if (INTERFACES.contains("SubmitListener")) {
055: println(pw, SPACE);
056: println(pw, "/**");
057: println(pw, " * Process the given submit event");
058: println(pw, " * @param " + EVENT_PARAMETER
059: + " the submit event to be processed");
060: println(
061: pw,
062: " * @return true to continue processing events, false to stop processing events");
063: println(pw, " */");
064: println(pw, "public boolean submitPerformed(SubmitEvent "
065: + EVENT_PARAMETER + ") {");
066: println(pw, TAB + "return true;");
067: println(pw, "}");
068: }
069:
070: // Add PageListener
071: if (INTERFACES.contains("PageListener")) {
072: println(pw, SPACE);
073: println(pw, "/**");
074: println(pw, " * Process the page requested event");
075: println(pw, " * @param " + EVENT_PARAMETER
076: + " the page event to be processed");
077: println(pw, " */");
078: println(pw, "public void pageRequested(PageEvent "
079: + EVENT_PARAMETER + ") {");
080: println(pw, "}");
081:
082: println(pw, SPACE);
083: println(pw, "/**");
084: println(pw, " * Process the page request end event");
085: println(pw, " * @param " + EVENT_PARAMETER
086: + " the page event to be processed");
087: println(pw, " */");
088: println(pw, "public void pageRequestEnd(PageEvent "
089: + EVENT_PARAMETER + ") {");
090: println(pw, "}");
091:
092: println(pw, SPACE);
093: println(pw, "/**");
094: println(pw, " * Process the page submit end event");
095: println(pw, " * @param " + EVENT_PARAMETER
096: + " the page event to be processed");
097: println(pw, " */");
098: println(pw, "public void pageSubmitEnd(PageEvent "
099: + EVENT_PARAMETER + ") {");
100: println(pw, "}");
101:
102: println(pw, SPACE);
103: println(pw, "/**");
104: println(pw, " * Process the page submit event");
105: println(pw, " * @param " + EVENT_PARAMETER
106: + " the page event to be processed");
107: println(pw, " */");
108: println(pw, "public void pageSubmitted(PageEvent "
109: + EVENT_PARAMETER + "){");
110: println(pw, "}");
111: }
112:
113: // add ValueChangeListener
114: if (INTERFACES.contains("ValueChangedListener")) {
115: println(pw, SPACE);
116: println(pw, "/**");
117: println(pw, " * Process the value change event");
118: println(pw, " *");
119: println(pw, " * @param " + EVENT_PARAMETER
120: + " the event to be processed");
121: println(pw, " *");
122: println(
123: pw,
124: " * @return true to continue processing events, false to stop processing events");
125: println(pw, " *");
126: println(pw, " */");
127: println(pw,
128: "public boolean valueChanged(ValueChangedEvent "
129: + EVENT_PARAMETER + "){");
130: println(pw, TAB + "return true;");
131: println(pw, "}");
132: }
133:
134: // add TreeListener
135: if (INTERFACES.contains("TreeListener")) {
136: println(pw, SPACE);
137: println(pw, "/**");
138: println(pw, " * Process the item contracted event");
139: println(pw, " *");
140: println(pw, " * @param " + EVENT_PARAMETER
141: + " the event to be processed");
142: println(pw, " *");
143: println(pw, " */");
144: println(pw,
145: "public void itemContracted(TreeExpandContractEvent "
146: + EVENT_PARAMETER + "){");
147: println(pw, "}");
148:
149: println(pw, SPACE);
150: println(pw, "/**");
151: println(pw, " * Process the item expanded event");
152: println(pw, " *");
153: println(pw, " * @param " + EVENT_PARAMETER
154: + " the event to be processed");
155: println(pw, " *");
156: println(pw, " */");
157: println(pw,
158: "public void itemExpanded(TreeExpandContractEvent "
159: + EVENT_PARAMETER + ")");
160: println(pw, "{");
161: println(pw, "}");
162:
163: }
164:
165: // FileUploadListener
166: if (INTERFACES.contains("FileUploadListener")) {
167: println(pw, SPACE);
168: println(pw, "/**");
169: println(pw, " * Process the file upload event");
170: println(pw, " *");
171: println(pw, " * @param " + EVENT_PARAMETER
172: + " the event to be processed");
173: println(pw, " *");
174: println(pw, " */");
175: println(pw, "void fileUploaded(FileUploadEvent "
176: + EVENT_PARAMETER + "){");
177: println(pw, "}");
178: }
179:
180: // Calendar Listener
181: if (INTERFACES.contains("CalendarListener")) {
182: println(pw, SPACE);
183: println(pw, "/**");
184: println(pw, " * Process the calendar date selection event");
185: println(pw, " *");
186: println(pw, " * @param " + EVENT_PARAMETER
187: + " the event to be processed");
188: println(pw, " *");
189: println(pw, " */");
190: println(pw, "void dateSelected(CalendarDateSelectedEvent "
191: + EVENT_PARAMETER + "){");
192: println(pw, "}");
193:
194: println(pw, SPACE);
195: println(pw, "/**");
196: println(pw, " * Process the month change event");
197: println(pw, " *");
198: println(pw, " * @param " + EVENT_PARAMETER
199: + " the event to be processed");
200: println(pw, " *");
201: println(
202: pw,
203: " * @return true to continue processing events, false to stop processing events");
204: println(pw, " *");
205: println(pw, " */");
206: println(pw,
207: "boolean monthChanged(CalendarMonthChangeEvent "
208: + EVENT_PARAMETER + "){");
209: println(pw, TAB + "return true;");
210: println(pw, "}");
211: }
212: }
213:
214: private static String computeFieldName(String fieldName) {
215: int TO_UPPER = 1;
216: int TO_LOWER = 2;
217: int NO_CHANGE = 0;
218:
219: StringBuffer sb = new StringBuffer(fieldName.length() + 1);
220: sb.append('_');
221: int nextChange = TO_LOWER;
222: for (int i = 0; i < fieldName.length(); i++) {
223: char c = fieldName.charAt(i);
224: if (c == '.' || c == ' ')
225: nextChange = TO_UPPER;
226: else if (nextChange == TO_LOWER) {
227: sb.append(Character.toLowerCase(c));
228: nextChange = NO_CHANGE;
229: } else if (nextChange == TO_UPPER) {
230: sb.append(Character.toUpperCase(c));
231: nextChange = NO_CHANGE;
232: } else
233: sb.append(c);
234: }
235: return sb.toString();
236: }
237:
238: /**
239: * Generates a stub controller for a view.
240: */
241: public static void generateCode(java.io.PrintWriter p,
242: Hashtable hashFields, JspController cont,
243: String controllerName) {
244: try {
245: PrintWriter pw = p;
246: println(pw, "<BR>");
247: println(pw, "");
248: String cName = controllerName;
249: String packageName = "";
250: if (cName == null || cName.equals("")
251: || cName.equals("com.salmonllc.jsp.JspController"))
252: cName = "GeneratedController";
253: int pos = cName.lastIndexOf(".");
254: if (pos > 0) {
255: packageName = cName.substring(0, pos);
256: cName = cName.substring(pos + 1);
257: }
258:
259: // create the package statement
260: println(pw, "//package statement");
261: if (packageName != null)
262: println(pw, PACKAGE + SPACE + packageName + ";");
263:
264: // create some standard import statements
265: println(pw, "");
266: println(pw, "//Salmon import statements");
267: println(pw, IMPORT + SPACE + "com.salmonllc.jsp.*;");
268: println(pw, IMPORT + SPACE + "com.salmonllc.html.events.*;");
269: println(pw, "");
270: println(pw, "");
271:
272: // Start the Class definition
273: println(pw, "/**");
274: println(pw, " * " + cName
275: + ": a SOFIA generated controller");
276: println(pw, " */");
277: pw.print(CLASSDEF + SPACE + cName + SPACE);
278: pw.print(EXTENDS + SPACE + BASECLASS + SPACE);
279:
280: if (INTERFACES != null && INTERFACES.size() > 0) {
281: pw.print(IMPLEMENTS + SPACE);
282: pw.print(INTERFACES.elementAt(0));
283: for (int i = 1; i < INTERFACES.size(); i++) {
284: pw.print(COMMA + SPACE);
285: pw.print(INTERFACES.elementAt(i));
286: }
287: }
288: println(pw, " {");
289:
290: // Add the variables
291: cont.printVars(pw);
292:
293: // add the Initialize Method
294: println(pw, SPACE);
295: println(pw, "/**");
296: println(
297: pw,
298: " * Initialize the page. Set up listeners and perform other initialization activities.");
299: println(pw, " */");
300: println(pw, INITMETHOD + "{");
301:
302: //For Page Listeners
303: if (INTERFACES.contains(new String("PageListener")))
304: println(pw, TAB + "addPageListener(this);");
305:
306: if (hashFields != null) {
307: Enumeration en = hashFields.keys();
308: while (en.hasMoreElements() && INTERFACES != null) {
309: String key = (String) en.nextElement();
310: Object obj = hashFields.get(key);
311:
312: // For Buttons
313: if (INTERFACES
314: .contains(new String("SubmitListener"))) {
315: if (obj instanceof com.salmonllc.html.HtmlSubmitButton
316: || obj instanceof com.salmonllc.html.HtmlSubmitImage)
317: println(pw, TAB + computeFieldName(key)
318: + ".addSubmitListener(this);");
319: }
320:
321: //For Value Change Listeners
322: if (INTERFACES.contains(new String(
323: "ValueChangedListener"))) {
324: if (obj instanceof com.salmonllc.html.HtmlCheckBox
325: || obj instanceof com.salmonllc.html.HtmlRadioButton
326: || obj instanceof com.salmonllc.html.HtmlDropDownList)
327: println(pw, TAB + computeFieldName(key)
328: + ".addValueChangeListener(this);");
329: }
330:
331: //For Tree Listeners
332: if (INTERFACES.contains(new String("TreeListener"))) {
333: if (obj instanceof com.salmonllc.html.HtmlTreeControl)
334: println(pw, TAB + computeFieldName(key)
335: + ".addTreeListener(this);");
336: }
337: } // while loop
338: } // hashatable not null
339:
340: println(pw, "}");
341:
342: // add the listeners
343: addListeners(pw);
344:
345: // End the class definition
346: println(pw, "");
347: println(pw, "}");
348:
349: pw.flush();
350:
351: } catch (java.io.IOException e) {
352: System.out.println("Cannot create Java File.");
353: e.printStackTrace();
354: }
355: }
356:
357: private static void println(PrintWriter pw, String strToPrint) {
358: pw.println(strToPrint);
359: pw.println("<BR>");
360: }
361:
362: }
|