| 1 16 17 package org.springframework.web.servlet.tags; 18 19 import java.io.IOException ; 20 21 import javax.servlet.jsp.JspException ; 22 import javax.servlet.jsp.tagext.BodyContent ; 23 import javax.servlet.jsp.tagext.BodyTag ; 24 25 import org.springframework.web.util.ExpressionEvaluationUtils; 26 import org.springframework.web.util.HtmlUtils; 27 import org.springframework.web.util.JavaScriptUtils; 28 29 46 public class EscapeBodyTag extends HtmlEscapingAwareTag implements BodyTag { 47 48 private boolean javaScriptEscape = false; 49 50 private BodyContent bodyContent; 51 52 53 57 public void setJavaScriptEscape(String javaScriptEscape) throws JspException { 58 this.javaScriptEscape = 59 ExpressionEvaluationUtils.evaluateBoolean("javaScriptEscape", javaScriptEscape, pageContext); 60 } 61 62 63 protected int doStartTagInternal() { 64 return EVAL_BODY_BUFFERED; 66 } 67 68 public void doInitBody() { 69 } 71 72 public void setBodyContent(BodyContent bodyContent) { 73 this.bodyContent = bodyContent; 74 } 75 76 public int doAfterBody() throws JspException { 77 try { 78 String content = readBodyContent(); 79 content = isHtmlEscape() ? HtmlUtils.htmlEscape(content) : content; 81 content = this.javaScriptEscape ? JavaScriptUtils.javaScriptEscape(content) : content; 82 writeBodyContent(content); 83 } 84 catch (IOException ex) { 85 throw new JspException ("Could not write escaped body", ex); 86 } 87 return (SKIP_BODY); 88 } 89 90 95 protected String readBodyContent() throws IOException { 96 return this.bodyContent.getString(); 97 } 98 99 105 protected void writeBodyContent(String content) throws IOException { 106 this.bodyContent.getEnclosingWriter().print(content); 107 } 108 109 } 110 | Popular Tags |