Skip to content

Commit e4ae9de

Browse files
author
graeme
committed
fix for GRAILS-1097
git-svn-id: https://svn.codehaus.org/grails/trunk@4133 1cfb16fd-6d17-0410-8ff1-b7e8e1e2867d
1 parent baeda5a commit e4ae9de

File tree

2 files changed

+36
-8
lines changed

2 files changed

+36
-8
lines changed

src/web/org/codehaus/groovy/grails/web/pages/GSPResponseWriter.java

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import org.apache.commons.logging.Log;
1919
import org.apache.commons.logging.LogFactory;
20+
import org.codehaus.groovy.runtime.InvokerHelper;
2021

2122
import javax.servlet.ServletResponse;
2223
import 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

src/web/org/codehaus/groovy/grails/web/pages/GroovyPage.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@
2424
import org.codehaus.groovy.grails.web.servlet.DefaultGrailsApplicationAttributes;
2525
import org.codehaus.groovy.grails.web.servlet.GrailsApplicationAttributes;
2626
import org.codehaus.groovy.grails.web.servlet.mvc.GrailsWebRequest;
27+
import org.codehaus.groovy.grails.web.taglib.GroovyPageTagWriter;
2728
import org.codehaus.groovy.grails.web.taglib.exceptions.GrailsTagException;
2829
import org.springframework.beans.BeanWrapper;
2930

3031
import javax.servlet.ServletContext;
3132
import javax.servlet.http.HttpServletRequest;
3233
import javax.servlet.http.HttpServletResponse;
3334
import java.io.IOException;
34-
import java.io.PrintWriter;
3535
import java.io.StringWriter;
3636
import java.io.Writer;
3737
import java.util.HashMap;
@@ -302,8 +302,7 @@ public static String captureTagOutput(GroovyObject tagLib, String methodName, Ma
302302
Object tagLibProp;// retrieve tag lib and create wrapper writer
303303
Writer originalOut = webRequest.getOut();
304304
try {
305-
StringWriter capturedOut = new StringWriter();
306-
final Writer out = new PrintWriter(capturedOut);
305+
final GroovyPageTagWriter out = new GroovyPageTagWriter(new StringWriter());
307306
webRequest.setOut(out);
308307

309308
// in a direct invocation the body is expected to return a string
@@ -325,7 +324,7 @@ public static String captureTagOutput(GroovyObject tagLib, String methodName, Ma
325324
}else {
326325
throw new GrailsTagException("Tag ["+methodName+"] does not specify expected number of params in tag library ["+bean.getWrappedClass().getName()+"]");
327326
}
328-
return capturedOut.toString();
327+
return out.getValue();
329328
}else {
330329
throw new GrailsTagException("Tag ["+methodName+"] does not exist in tag library ["+bean.getWrappedClass().getName()+"]");
331330
}

0 commit comments

Comments
 (0)