PageRenderTime 27ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/core/api/main/java/org/directwebremoting/ScriptBuffer.java

http://github.com/burris/dwr
Java | 214 lines | 93 code | 20 blank | 101 comment | 4 complexity | 98204e3337e280cc6c251f7028361c1f MD5 | raw file
  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. import java.util.ArrayList;
  18. import java.util.List;
  19. import org.directwebremoting.io.StringWrapper;
  20. /**
  21. * A ScriptBuffer is like a StringBuffer except that it is used to create
  22. * Javascript commands. There are 2 version of the <code>append()</code> method:
  23. * <p>The first is {@link #appendScript(String)} which assumes that the
  24. * parameter is to be inserted literally into the output.
  25. * <p>The second is {@link #appendData(String)} (and variants for Object and
  26. * primitive types) which assumes that the parameter is a variable which should
  27. * be properly converted, escaped and quoted.
  28. * @author Joe Walker [joe at getahead dot ltd dot uk]
  29. */
  30. public class ScriptBuffer
  31. {
  32. /**
  33. * Create an empty ScriptBuffer.
  34. */
  35. public ScriptBuffer()
  36. {
  37. }
  38. /**
  39. * Create a ScriptBuffer with some initial content.
  40. * {@link #appendScript(String)} is called with the passed string
  41. * @param str The initial string to place in the buffer
  42. */
  43. public ScriptBuffer(String str)
  44. {
  45. appendScript(str);
  46. }
  47. /**
  48. * @param buffer The ScriptBuffer to merge into this script
  49. * @return this. To allow buffer.append(x).append(y).append(z);
  50. * @see java.lang.StringBuffer#append(java.lang.String)
  51. */
  52. public ScriptBuffer appendAll(ScriptBuffer buffer)
  53. {
  54. for (Object part : buffer.parts)
  55. {
  56. parts.add(part);
  57. }
  58. return this;
  59. }
  60. /**
  61. * @param str The String to add to the script
  62. * @return this. To allow buffer.append(x).append(y).append(z);
  63. * @see java.lang.StringBuffer#append(java.lang.String)
  64. */
  65. public ScriptBuffer appendScript(String str)
  66. {
  67. parts.add(new StringWrapper(str));
  68. return this;
  69. }
  70. /**
  71. * @param b The boolean to add to the script
  72. * @return this. To allow buffer.append(x).append(y).append(z);
  73. * @see java.lang.StringBuffer#append(boolean)
  74. */
  75. public ScriptBuffer appendData(boolean b)
  76. {
  77. Boolean data = b ? Boolean.TRUE : Boolean.FALSE;
  78. parts.add(data);
  79. return this;
  80. }
  81. /**
  82. * @param c The char to add to the script
  83. * @return this. To allow buffer.append(x).append(y).append(z);
  84. * @see java.lang.StringBuffer#append(char)
  85. */
  86. public ScriptBuffer appendData(char c)
  87. {
  88. parts.add(c);
  89. return this;
  90. }
  91. /**
  92. * @param d The double to add to the script
  93. * @return this. To allow buffer.append(x).append(y).append(z);
  94. * @see java.lang.StringBuffer#append(double)
  95. */
  96. public ScriptBuffer appendData(double d)
  97. {
  98. parts.add(d);
  99. return this;
  100. }
  101. /**
  102. * @param f The float to add to the script
  103. * @return this. To allow buffer.append(x).append(y).append(z);
  104. * @see java.lang.StringBuffer#append(float)
  105. */
  106. public ScriptBuffer appendData(float f)
  107. {
  108. parts.add(f);
  109. return this;
  110. }
  111. /**
  112. * @param i The int to add to the script
  113. * @return this. To allow buffer.append(x).append(y).append(z);
  114. * @see java.lang.StringBuffer#append(int)
  115. */
  116. public ScriptBuffer appendData(int i)
  117. {
  118. parts.add(i);
  119. return this;
  120. }
  121. /**
  122. * @param l The long to add to the script
  123. * @return this. To allow buffer.append(x).append(y).append(z);
  124. * @see java.lang.StringBuffer#append(long)
  125. */
  126. public ScriptBuffer appendData(long l)
  127. {
  128. parts.add(l);
  129. return this;
  130. }
  131. /**
  132. * @param obj The Object to add to the script
  133. * @return this. To allow buffer.append(x).append(y).append(z);
  134. * @see java.lang.StringBuffer#append(java.lang.Object)
  135. */
  136. public ScriptBuffer appendData(Object obj)
  137. {
  138. parts.add(obj);
  139. return this;
  140. }
  141. /**
  142. * @param str The String to add to the script
  143. * @return this. To allow buffer.append(x).append(y).append(z);
  144. * @see java.lang.StringBuffer#append(java.lang.String)
  145. */
  146. public ScriptBuffer appendData(String str)
  147. {
  148. parts.add(str);
  149. return this;
  150. }
  151. /**
  152. * Call a named function with one parameter.
  153. * @param funcName The name of the function to call
  154. * @param params The parameters to the above function
  155. * @return this. To allow buffer.append(x).append(y).append(z);
  156. * @see org.directwebremoting.ScriptSessions#addFunctionCall
  157. */
  158. public ScriptBuffer appendCall(String funcName, Object... params)
  159. {
  160. appendScript(funcName);
  161. appendScript("(");
  162. for (int i = 0; i < params.length; i++)
  163. {
  164. if (i != 0)
  165. {
  166. appendScript(",");
  167. }
  168. appendData(params[i]);
  169. }
  170. appendScript(");");
  171. return this;
  172. }
  173. /* (non-Javadoc)
  174. * @see java.lang.Object#toString()
  175. */
  176. @Override
  177. public String toString()
  178. {
  179. return parts.toString();
  180. }
  181. /**
  182. * For DWR use only - This method is not part of the public API.
  183. * Do not use it without understanding the implications for future proofing.
  184. * @return The list of parts of the final output script
  185. */
  186. public List<?> getParts()
  187. {
  188. return parts;
  189. }
  190. /**
  191. * This is where we store all the script components waiting to be serialized
  192. */
  193. private final List<Object> parts = new ArrayList<Object>();
  194. }