001: /*
002: * Copyright 2005 Joe Walker
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.directwebremoting;
017:
018: import java.util.ArrayList;
019: import java.util.List;
020:
021: import org.directwebremoting.io.StringWrapper;
022: import org.directwebremoting.proxy.ScriptProxy;
023:
024: /**
025: * A ScriptBuffer is like a StringBuffer except that it is used to create
026: * Javascript commands. There are 2 version of the <code>append()</code> method:
027: * <p>The first is {@link #appendScript(String)} which assumes that the
028: * parameter is to be inserted literally into the output.
029: * <p>The second is {@link #appendData(String)} (and variants for Object and
030: * primitive types) which assumes that the parameter is a variable which should
031: * be properly converted, escaped and quoted.
032: * @author Joe Walker [joe at getahead dot ltd dot uk]
033: */
034: public class ScriptBuffer {
035: /**
036: * Create an empty ScriptBuffer.
037: */
038: public ScriptBuffer() {
039: }
040:
041: /**
042: * Create a ScriptBuffer with some initial content.
043: * {@link #appendScript(String)} is called with the passed string
044: * @param str The initial string to place in the buffer
045: */
046: public ScriptBuffer(String str) {
047: appendScript(str);
048: }
049:
050: /**
051: * @param buffer The ScriptBuffer to merge into this script
052: * @return this. To allow buffer.append(x).append(y).append(z);
053: * @see java.lang.StringBuffer#append(java.lang.String)
054: */
055: public ScriptBuffer appendAll(ScriptBuffer buffer) {
056: for (Object part : buffer.parts) {
057: parts.add(part);
058: }
059: return this ;
060: }
061:
062: /**
063: * @param str The String to add to the script
064: * @return this. To allow buffer.append(x).append(y).append(z);
065: * @see java.lang.StringBuffer#append(java.lang.String)
066: */
067: public ScriptBuffer appendScript(String str) {
068: parts.add(new StringWrapper(str));
069: return this ;
070: }
071:
072: /**
073: * @param b The boolean to add to the script
074: * @return this. To allow buffer.append(x).append(y).append(z);
075: * @see java.lang.StringBuffer#append(boolean)
076: */
077: public ScriptBuffer appendData(boolean b) {
078: Boolean data = b ? Boolean.TRUE : Boolean.FALSE;
079: parts.add(data);
080: return this ;
081: }
082:
083: /**
084: * @param c The char to add to the script
085: * @return this. To allow buffer.append(x).append(y).append(z);
086: * @see java.lang.StringBuffer#append(char)
087: */
088: public ScriptBuffer appendData(char c) {
089: parts.add(c);
090: return this ;
091: }
092:
093: /**
094: * @param d The double to add to the script
095: * @return this. To allow buffer.append(x).append(y).append(z);
096: * @see java.lang.StringBuffer#append(double)
097: */
098: public ScriptBuffer appendData(double d) {
099: parts.add(d);
100: return this ;
101: }
102:
103: /**
104: * @param f The float to add to the script
105: * @return this. To allow buffer.append(x).append(y).append(z);
106: * @see java.lang.StringBuffer#append(float)
107: */
108: public ScriptBuffer appendData(float f) {
109: parts.add(f);
110: return this ;
111: }
112:
113: /**
114: * @param i The int to add to the script
115: * @return this. To allow buffer.append(x).append(y).append(z);
116: * @see java.lang.StringBuffer#append(int)
117: */
118: public ScriptBuffer appendData(int i) {
119: parts.add(i);
120: return this ;
121: }
122:
123: /**
124: * @param l The long to add to the script
125: * @return this. To allow buffer.append(x).append(y).append(z);
126: * @see java.lang.StringBuffer#append(long)
127: */
128: public ScriptBuffer appendData(long l) {
129: parts.add(l);
130: return this ;
131: }
132:
133: /**
134: * @param obj The Object to add to the script
135: * @return this. To allow buffer.append(x).append(y).append(z);
136: * @see java.lang.StringBuffer#append(java.lang.Object)
137: */
138: public ScriptBuffer appendData(Object obj) {
139: parts.add(obj);
140: return this ;
141: }
142:
143: /**
144: * @param str The String to add to the script
145: * @return this. To allow buffer.append(x).append(y).append(z);
146: * @see java.lang.StringBuffer#append(java.lang.String)
147: */
148: public ScriptBuffer appendData(String str) {
149: parts.add(str);
150: return this ;
151: }
152:
153: /**
154: * Call a named function with no parameters.
155: * @param funcName The name of the function to call
156: * @return this. To allow buffer.append(x).append(y).append(z);
157: * @see ScriptProxy#addFunctionCall(String)
158: */
159: public ScriptBuffer appendCall(String funcName) {
160: appendScript(funcName);
161: appendScript("();");
162: return this ;
163: }
164:
165: /**
166: * Call a named function with one parameter.
167: * @param funcName The name of the function to call
168: * @param param1 The first parameter to the above function
169: * @return this. To allow buffer.append(x).append(y).append(z);
170: * @see ScriptProxy#addFunctionCall(String, Object)
171: */
172: public ScriptBuffer appendCall(String funcName, Object param1) {
173: appendScript(funcName);
174: appendScript("(");
175: appendData(param1);
176: appendScript(");");
177: return this ;
178: }
179:
180: /**
181: * Call a named function with one parameter.
182: * @param funcName The name of the function to call
183: * @param param1 The first parameter to the above function
184: * @param param2 The second parameter to the above function
185: * @return this. To allow buffer.append(x).append(y).append(z);
186: * @see ScriptProxy#addFunctionCall(String, Object, Object)
187: */
188: public ScriptBuffer appendCall(String funcName, Object param1,
189: Object param2) {
190: appendScript(funcName);
191: appendScript("(");
192: appendData(param1);
193: appendScript(",");
194: appendData(param2);
195: appendScript(");");
196: return this ;
197: }
198:
199: /**
200: * Call a named function with one parameter.
201: * @param funcName The name of the function to call
202: * @param param1 The first parameter to the above function
203: * @param param2 The second parameter to the above function
204: * @param param3 The third parameter to the above function
205: * @return this. To allow buffer.append(x).append(y).append(z);
206: * @see ScriptProxy#addFunctionCall(String, Object, Object, Object)
207: */
208: public ScriptBuffer appendCall(String funcName, Object param1,
209: Object param2, Object param3) {
210: appendScript(funcName);
211: appendScript("(");
212: appendData(param1);
213: appendScript(",");
214: appendData(param2);
215: appendScript(",");
216: appendData(param3);
217: appendScript(");");
218: return this ;
219: }
220:
221: /**
222: * Call a named function with one parameter.
223: * @param funcName The name of the function to call
224: * @param param1 The first parameter to the above function
225: * @param param2 The second parameter to the above function
226: * @param param3 The third parameter to the above function
227: * @param param4 The fourth parameter to the above function
228: * @return this. To allow buffer.append(x).append(y).append(z);
229: * @see ScriptProxy#addFunctionCall(String, Object, Object, Object, Object)
230: */
231: public ScriptBuffer appendCall(String funcName, Object param1,
232: Object param2, Object param3, Object param4) {
233: appendScript(funcName);
234: appendScript("(");
235: appendData(param1);
236: appendScript(",");
237: appendData(param2);
238: appendScript(",");
239: appendData(param3);
240: appendScript(",");
241: appendData(param4);
242: appendScript(");");
243: return this ;
244: }
245:
246: /**
247: * Call a named function with one parameter.
248: * @param funcName The name of the function to call
249: * @param param1 The first parameter to the above function
250: * @param param2 The second parameter to the above function
251: * @param param3 The third parameter to the above function
252: * @param param4 The fourth parameter to the above function
253: * @param param5 The fifth parameter to the above function
254: * @return this. To allow buffer.append(x).append(y).append(z);
255: * @see ScriptProxy#addFunctionCall(String, Object, Object, Object, Object, Object)
256: */
257: public ScriptBuffer appendCall(String funcName, Object param1,
258: Object param2, Object param3, Object param4, Object param5) {
259: appendScript(funcName);
260: appendScript("(");
261: appendData(param1);
262: appendScript(",");
263: appendData(param2);
264: appendScript(",");
265: appendData(param3);
266: appendScript(",");
267: appendData(param4);
268: appendScript(",");
269: appendData(param5);
270: return this ;
271: }
272:
273: /**
274: * Call a named function with one parameter.
275: * @param funcName The name of the function to call
276: * @param param1 The first parameter to the above function
277: * @param param2 The second parameter to the above function
278: * @param param3 The third parameter to the above function
279: * @param param4 The fourth parameter to the above function
280: * @param param5 The fifth parameter to the above function
281: * @param param6 The sixth parameter to the above function
282: * @return this. To allow buffer.append(x).append(y).append(z);
283: * @see ScriptProxy#addFunctionCall(String, Object, Object, Object, Object, Object, Object)
284: */
285: public ScriptBuffer appendCall(String funcName, Object param1,
286: Object param2, Object param3, Object param4, Object param5,
287: Object param6) {
288: appendScript(funcName);
289: appendScript("(");
290: appendData(param1);
291: appendScript(",");
292: appendData(param2);
293: appendScript(",");
294: appendData(param3);
295: appendScript(",");
296: appendData(param4);
297: appendScript(",");
298: appendData(param5);
299: appendScript(",");
300: appendData(param6);
301: return this ;
302: }
303:
304: /**
305: * Call a named function with one parameter.
306: * @param funcName The name of the function to call
307: * @param param1 The first parameter to the above function
308: * @param param2 The second parameter to the above function
309: * @param param3 The third parameter to the above function
310: * @param param4 The fourth parameter to the above function
311: * @param param5 The fifth parameter to the above function
312: * @param param6 The sixth parameter to the above function
313: * @param param7 The seventh parameter to the above function
314: * @return this. To allow buffer.append(x).append(y).append(z);
315: * @see ScriptProxy#addFunctionCall(String, Object, Object, Object, Object, Object, Object, Object)
316: */
317: public ScriptBuffer appendCall(String funcName, Object param1,
318: Object param2, Object param3, Object param4, Object param5,
319: Object param6, Object param7) {
320: appendScript(funcName);
321: appendScript("(");
322: appendData(param1);
323: appendScript(",");
324: appendData(param2);
325: appendScript(",");
326: appendData(param3);
327: appendScript(",");
328: appendData(param4);
329: appendScript(",");
330: appendData(param5);
331: appendScript(",");
332: appendData(param6);
333: appendScript(",");
334: appendData(param7);
335: return this ;
336: }
337:
338: /**
339: * Call a named function with one parameter.
340: * @param funcName The name of the function to call
341: * @param param1 The first parameter to the above function
342: * @param param2 The second parameter to the above function
343: * @param param3 The third parameter to the above function
344: * @param param4 The fourth parameter to the above function
345: * @param param5 The fifth parameter to the above function
346: * @param param6 The sixth parameter to the above function
347: * @param param7 The seventh parameter to the above function
348: * @param param8 The eighth parameter to the above function
349: * @return this. To allow buffer.append(x).append(y).append(z);
350: * @see ScriptProxy#addFunctionCall(String, Object, Object, Object, Object, Object, Object, Object)
351: */
352: public ScriptBuffer appendCall(String funcName, Object param1,
353: Object param2, Object param3, Object param4, Object param5,
354: Object param6, Object param7, Object param8) {
355: appendScript(funcName);
356: appendScript("(");
357: appendData(param1);
358: appendScript(",");
359: appendData(param2);
360: appendScript(",");
361: appendData(param3);
362: appendScript(",");
363: appendData(param4);
364: appendScript(",");
365: appendData(param5);
366: appendScript(",");
367: appendData(param6);
368: appendScript(",");
369: appendData(param7);
370: appendScript(",");
371: appendData(param8);
372: return this ;
373: }
374:
375: /**
376: * Call a named function with one parameter.
377: * @param funcName The name of the function to call
378: * @param param1 The first parameter to the above function
379: * @param param2 The second parameter to the above function
380: * @param param3 The third parameter to the above function
381: * @param param4 The fourth parameter to the above function
382: * @param param5 The fifth parameter to the above function
383: * @param param6 The sixth parameter to the above function
384: * @param param7 The seventh parameter to the above function
385: * @param param8 The eighth parameter to the above function
386: * @param param9 The ninth parameter to the above function
387: * @return this. To allow buffer.append(x).append(y).append(z);
388: * @see ScriptProxy#addFunctionCall(String, Object, Object, Object, Object, Object, Object, Object)
389: */
390: public ScriptBuffer appendCall(String funcName, Object param1,
391: Object param2, Object param3, Object param4, Object param5,
392: Object param6, Object param7, Object param8, Object param9) {
393: appendScript(funcName);
394: appendScript("(");
395: appendData(param1);
396: appendScript(",");
397: appendData(param2);
398: appendScript(",");
399: appendData(param3);
400: appendScript(",");
401: appendData(param4);
402: appendScript(",");
403: appendData(param5);
404: appendScript(",");
405: appendData(param6);
406: appendScript(",");
407: appendData(param7);
408: appendScript(",");
409: appendData(param8);
410: appendScript(",");
411: appendData(param9);
412: return this ;
413: }
414:
415: /**
416: * Call a named function with one parameter.
417: * @param funcName The name of the function to call
418: * @param param1 The first parameter to the above function
419: * @param param2 The second parameter to the above function
420: * @param param3 The third parameter to the above function
421: * @param param4 The fourth parameter to the above function
422: * @param param5 The fifth parameter to the above function
423: * @param param6 The sixth parameter to the above function
424: * @param param7 The seventh parameter to the above function
425: * @param param8 The eighth parameter to the above function
426: * @param param9 The ninth parameter to the above function
427: * @param param10 The tenth parameter to the above function
428: * @return this. To allow buffer.append(x).append(y).append(z);
429: * @see ScriptProxy#addFunctionCall(String, Object, Object, Object, Object, Object, Object, Object)
430: */
431: public ScriptBuffer appendCall(String funcName, Object param1,
432: Object param2, Object param3, Object param4, Object param5,
433: Object param6, Object param7, Object param8, Object param9,
434: Object param10) {
435: appendScript(funcName);
436: appendScript("(");
437: appendData(param1);
438: appendScript(",");
439: appendData(param2);
440: appendScript(",");
441: appendData(param3);
442: appendScript(",");
443: appendData(param4);
444: appendScript(",");
445: appendData(param5);
446: appendScript(",");
447: appendData(param6);
448: appendScript(",");
449: appendData(param7);
450: appendScript(",");
451: appendData(param8);
452: appendScript(",");
453: appendData(param9);
454: appendScript(",");
455: appendData(param10);
456: return this ;
457: }
458:
459: /* (non-Javadoc)
460: * @see java.lang.Object#toString()
461: */
462: @Override
463: public String toString() {
464: return parts.toString();
465: }
466:
467: /**
468: * For DWR use only - This method is not part of the public API.
469: * Do not use it without understanding the implications for future proofing.
470: * @return The list of parts of the final output script
471: */
472: public List<? extends Object> getParts() {
473: return parts;
474: }
475:
476: /**
477: * This is where we store all the script components waiting to be serialized
478: */
479: private final List<Object> parts = new ArrayList<Object>();
480: }
|