KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > directwebremoting > ScriptBuffer


1 /*
2  * Copyright 2005 Joe Walker
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.directwebremoting;
17
18 import java.util.ArrayList JavaDoc;
19 import java.util.List JavaDoc;
20
21 /**
22  * A ScriptBuffer is like a StringBuffer except that it is used to create
23  * Javascript commands. There are 2 version of the <code>append()</code> method:
24  * <p>The first is {@link #appendScript(String)} which assumes that the
25  * parameter is to be inserted literally into the output.
26  * <p>The second is {@link #appendData(String)} (and variants for Object and
27  * primitive types) which assumes that the parameter is a variable which should
28  * be properly converted, escaped and quoted.
29  * @author Joe Walker [joe at getahead dot ltd dot uk]
30  */

31 public class ScriptBuffer
32 {
33     /**
34      * Create an empty ScriptBuffer.
35      */

36     public ScriptBuffer()
37     {
38     }
39
40     /**
41      * Create a ScriptBuffer with some initial content.
42      * {@link #appendScript(String)} is called with the passed string
43      * @param str The initial string to place in the buffer
44      */

45     public ScriptBuffer(String JavaDoc str)
46     {
47         appendScript(str);
48     }
49
50     /**
51      * @param str The String to add to the script
52      * @return this. To allow sv.append(x).append(y).append(z);
53      * @see java.lang.StringBuffer#append(java.lang.String)
54      */

55     public ScriptBuffer appendScript(String JavaDoc str)
56     {
57         parts.add(new StringWrapper(str));
58         return this;
59     }
60
61     /**
62      * @param b The boolean to add to the script
63      * @return this. To allow sv.append(x).append(y).append(z);
64      * @see java.lang.StringBuffer#append(boolean)
65      */

66     public ScriptBuffer appendData(boolean b)
67     {
68         Boolean JavaDoc data = b ? Boolean.TRUE : Boolean.FALSE;
69         parts.add(data);
70         return this;
71     }
72
73     /**
74      * @param c The char to add to the script
75      * @return this. To allow sv.append(x).append(y).append(z);
76      * @see java.lang.StringBuffer#append(char)
77      */

78     public ScriptBuffer appendData(char c)
79     {
80         parts.add(new Character JavaDoc(c));
81         return this;
82     }
83
84     /**
85      * @param d The double to add to the script
86      * @return this. To allow sv.append(x).append(y).append(z);
87      * @see java.lang.StringBuffer#append(double)
88      */

89     public ScriptBuffer appendData(double d)
90     {
91         parts.add(new Double JavaDoc(d));
92         return this;
93     }
94
95     /**
96      * @param f The float to add to the script
97      * @return this. To allow sv.append(x).append(y).append(z);
98      * @see java.lang.StringBuffer#append(float)
99      */

100     public ScriptBuffer appendData(float f)
101     {
102         parts.add(new Float JavaDoc(f));
103         return this;
104     }
105
106     /**
107      * @param i The int to add to the script
108      * @return this. To allow sv.append(x).append(y).append(z);
109      * @see java.lang.StringBuffer#append(int)
110      */

111     public ScriptBuffer appendData(int i)
112     {
113         parts.add(new Integer JavaDoc(i));
114         return this;
115     }
116
117     /**
118      * @param l The long to add to the script
119      * @return this. To allow sv.append(x).append(y).append(z);
120      * @see java.lang.StringBuffer#append(long)
121      */

122     public ScriptBuffer appendData(long l)
123     {
124         parts.add(new Long JavaDoc(l));
125         return this;
126     }
127
128     /**
129      * @param obj The Object to add to the script
130      * @return this. To allow sv.append(x).append(y).append(z);
131      * @see java.lang.StringBuffer#append(java.lang.Object)
132      */

133     public ScriptBuffer appendData(Object JavaDoc obj)
134     {
135         parts.add(obj);
136         return this;
137     }
138
139     /**
140      * @param str The String to add to the script
141      * @return this. To allow sv.append(x).append(y).append(z);
142      * @see java.lang.StringBuffer#append(java.lang.String)
143      */

144     public ScriptBuffer appendData(String JavaDoc str)
145     {
146         parts.add(str);
147         return this;
148     }
149
150     /* (non-Javadoc)
151      * @see java.lang.Object#toString()
152      */

153     public String JavaDoc toString()
154     {
155         return parts.toString();
156     }
157
158     /**
159      * For DWR use only - This method is not part of the public API.
160      * Do not use it without understanding the implications for future proofing.
161      * @return The list of parts of the final output script
162      */

163     public List JavaDoc getParts()
164     {
165         return parts;
166     }
167
168     /**
169      * A wrapper around a string to distinguish a string entered into this
170      * buffer as code and a string entered as data
171      */

172     public class StringWrapper
173     {
174         StringWrapper(String JavaDoc data)
175         {
176             this.data = data;
177         }
178
179         String JavaDoc data;
180
181         /* (non-Javadoc)
182          * @see java.lang.Object#toString()
183          */

184         public String JavaDoc toString()
185         {
186             return data;
187         }
188     }
189
190     /**
191      * This is where we store all the script components waiting to be serialized
192      */

193     private List JavaDoc parts = new ArrayList JavaDoc();
194 }
195
Popular Tags