KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > servlet > tags > EscapeBodyTag


1 /*
2  * Copyright 2002-2005 the original author or authors.
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
17 package org.springframework.web.servlet.tags;
18
19 import java.io.IOException JavaDoc;
20
21 import javax.servlet.jsp.JspException JavaDoc;
22 import javax.servlet.jsp.tagext.BodyContent JavaDoc;
23 import javax.servlet.jsp.tagext.BodyTag JavaDoc;
24
25 import org.springframework.web.util.ExpressionEvaluationUtils;
26 import org.springframework.web.util.HtmlUtils;
27 import org.springframework.web.util.JavaScriptUtils;
28
29 /**
30  * Custom JSP tag to escape its enclosed body content,
31  * applying HTML escaping and/or JavaScript escaping.
32  *
33  * <p>Provides a "htmlEscape" property for explicitly specifying whether to
34  * apply HTML escaping. If not set, a page-level default (e.g. from the
35  * HtmlEscapeTag) or an application-wide default (the "defaultHtmlEscape"
36  * context-param in web.xml) is used.
37  *
38  * <p>Provides a "javaScriptEscape" property for specifying whether to apply
39  * JavaScript escaping. Can be combined with HTML escaping or used standalone.
40  *
41  * @author Juergen Hoeller
42  * @since 1.1.1
43  * @see org.springframework.web.util.HtmlUtils
44  * @see org.springframework.web.util.JavaScriptUtils
45  */

46 public class EscapeBodyTag extends HtmlEscapingAwareTag implements BodyTag JavaDoc {
47
48     private boolean javaScriptEscape = false;
49
50     private BodyContent JavaDoc bodyContent;
51
52
53     /**
54      * Set JavaScript escaping for this tag, as boolean value.
55      * Default is "false".
56      */

57     public void setJavaScriptEscape(String JavaDoc javaScriptEscape) throws JspException JavaDoc {
58         this.javaScriptEscape =
59                 ExpressionEvaluationUtils.evaluateBoolean("javaScriptEscape", javaScriptEscape, pageContext);
60     }
61
62
63     protected int doStartTagInternal() {
64         // do nothing
65
return EVAL_BODY_BUFFERED;
66     }
67
68     public void doInitBody() {
69         // do nothing
70
}
71
72     public void setBodyContent(BodyContent JavaDoc bodyContent) {
73         this.bodyContent = bodyContent;
74     }
75
76     public int doAfterBody() throws JspException JavaDoc {
77         try {
78             String JavaDoc content = readBodyContent();
79             // HTML and/or JavaScript escape, if demanded
80
content = isHtmlEscape() ? HtmlUtils.htmlEscape(content) : content;
81             content = this.javaScriptEscape ? JavaScriptUtils.javaScriptEscape(content) : content;
82             writeBodyContent(content);
83         }
84         catch (IOException JavaDoc ex) {
85             throw new JspException JavaDoc("Could not write escaped body", ex);
86         }
87         return (SKIP_BODY);
88     }
89
90     /**
91      * Read the unescaped body content from the page.
92      * @return the original content
93      * @throws IOException if reading failed
94      */

95     protected String JavaDoc readBodyContent() throws IOException JavaDoc {
96         return this.bodyContent.getString();
97     }
98
99     /**
100      * Write the escaped body content to the page.
101      * <p>Can be overridden in subclasses, e.g. for testing purposes.
102      * @param content the content to write
103      * @throws IOException if writing failed
104      */

105     protected void writeBodyContent(String JavaDoc content) throws IOException JavaDoc {
106         this.bodyContent.getEnclosingWriter().print(content);
107     }
108
109 }
110
Popular Tags