KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > standard > examples > taglib > EscapeHtmlTag


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
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.apache.taglibs.standard.examples.taglib;
18
19 import java.io.IOException JavaDoc;
20 import java.io.Reader JavaDoc;
21 import java.io.Writer JavaDoc;
22
23 import javax.servlet.jsp.JspException JavaDoc;
24 import javax.servlet.jsp.JspTagException JavaDoc;
25 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
26
27 import org.apache.taglibs.standard.examples.util.Util;
28
29 /**
30  * <p>Tag handler for &lt;escapeHtml&gt;
31  *
32  * @author Pierre Delisle
33  * @version $Revision: 1.3 $ $Date: 2004/02/28 01:01:41 $
34  */

35 public class EscapeHtmlTag extends BodyTagSupport JavaDoc {
36     
37     //*********************************************************************
38
// Instance variables
39

40     private Object JavaDoc reader;
41     private Object JavaDoc writer;
42     
43     //*********************************************************************
44
// Constructors
45

46     public EscapeHtmlTag() {
47         super();
48         init();
49     }
50     
51     private void init() {
52         reader = null;
53         writer = null;
54     }
55     
56     
57     //*********************************************************************
58
// Tag's properties
59

60     /**
61      * Tag's 'reader' attribute
62      */

63     public void setReader(Object JavaDoc reader) {
64         this.reader = reader;
65     }
66     
67     /**
68      * Tag's 'writer' attribute
69      */

70     public void setWriter(Object JavaDoc writer) {
71         this.writer = writer;
72     }
73     
74     //*********************************************************************
75
// TagSupport methods
76

77     public int doEndTag() throws JspException JavaDoc {
78         Reader JavaDoc in;
79         Writer JavaDoc out;
80         
81         if (reader == null) {
82             String JavaDoc bcs = getBodyContent().getString().trim();
83             if (bcs == null || bcs.equals("")) {
84                 throw new JspTagException JavaDoc("In &lt;escapeHtml&gt;, 'reader' " +
85                 "not specified and no non-whitespace content inside the tag.");
86             }
87             in = Util.castToReader(bcs);
88         } else {
89             in = Util.castToReader(reader);
90         }
91         
92         if (writer == null) {
93             out = pageContext.getOut();
94         } else {
95             out = Util.castToWriter(writer);
96         }
97         
98         transform(in, out);
99         return EVAL_PAGE;
100     }
101     
102     /**
103      * Releases any resources we may have (or inherit)
104      */

105     public void release() {
106         super.release();
107         init();
108     }
109     
110     //*********************************************************************
111
// Tag's scific behavior methods
112

113     /**
114      * Transform
115      */

116     public void transform(Reader JavaDoc reader, Writer JavaDoc writer)
117     throws JspException JavaDoc {
118         int c;
119         try {
120             writer.write("<pre>");
121             while ((c = reader.read()) != -1) {
122                 if (c == '<') {
123                     writer.write("&lt;");
124                 } else if (c == '>') {
125                     writer.write("&gt;");
126                 } else {
127                     writer.write(c);
128                 }
129             }
130             writer.write("</pre>");
131         } catch (IOException JavaDoc ex) {
132             throw new JspException JavaDoc("EscapeHtml: " +
133             "error copying chars", ex);
134         }
135     }
136 }
137
Popular Tags