1717
1818import org .apache .commons .logging .Log ;
1919import org .apache .commons .logging .LogFactory ;
20+ import org .codehaus .groovy .runtime .InvokerHelper ;
2021
2122import javax .servlet .ServletResponse ;
2223import java .io .*;
@@ -41,8 +42,9 @@ public class GSPResponseWriter extends PrintWriter {
4142 private int max ;
4243 private boolean trouble = false ;
4344 private int totalLength ;
45+ private static final String BLANK_STRING = "" ;
4446
45- /**
47+ /**
4648 * Private constructor. Use getInstance() instead.
4749 * @param response
4850 * @param out
@@ -170,9 +172,9 @@ static GSPResponseWriter getInstance(Writer target, int max) {
170172 * @see java.lang.Object#toString()
171173 */
172174 public void print (Object obj ) {
173- if (obj == null ) obj = "" ;
175+ if (obj == null ) obj = BLANK_STRING ;
174176 String out = String .valueOf (obj );
175- if (out == null )out = "" ;
177+ if (out == null )out = BLANK_STRING ;
176178 write (out );
177179 }
178180
@@ -186,11 +188,24 @@ public void print(Object obj) {
186188 * @param s The <code>String</code> to be printed
187189 */
188190 public void print (String s ) {
189- if (s == null ) s = "" ;
191+ if (s == null ) s = BLANK_STRING ;
190192 write (s );
191193 } // print()
192194
193195 /**
196+ * Writes a string. If the argument is <code>null</code> then the string
197+ * <code>""</code> is printed.
198+ *
199+ * @param s The <code>String</code> to be printed
200+ */
201+ public void write (String s ) {
202+ if (s == null ) s = BLANK_STRING ;
203+ super .write (s );
204+ }
205+
206+
207+
208+ /**
194209 * Write a single character.
195210 * @param c int specifying a character to be written.
196211 */
@@ -230,4 +245,18 @@ public void write(String s, int off, int len) {
230245 }
231246 } // write()
232247
248+ /**
249+ * Provides Groovy << left shift operator, but intercepts call to make sure nulls are converted
250+ * to "" strings
251+ *
252+ * @param value The value
253+ * @return Returns this object
254+ * @throws IOException
255+ */
256+ public GSPResponseWriter leftShift (Object value ) throws IOException {
257+ if (value ==null ) value = BLANK_STRING ;
258+ InvokerHelper .write (this , value );
259+ return this ;
260+ }
261+
233262} // GSPResponseWriter
0 commit comments