Source code: demo/taglib/EscapeHtmlTag.java
1 /*
2 * The contents of this file are subject to the terms
3 * of the Common Development and Distribution License
4 * (the License). You may not use this file except in
5 * compliance with the License.
6 *
7 * You can obtain a copy of the License at
8 * https://javaserverfaces.dev.java.net/CDDL.html or
9 * legal/CDDLv1.0.txt.
10 * See the License for the specific language governing
11 * permission and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL
14 * Header Notice in each file and include the License file
15 * at legal/CDDLv1.0.txt.
16 * If applicable, add the following below the CDDL Header,
17 * with the fields enclosed by brackets [] replaced by
18 * your own identifying information:
19 * "Portions Copyrighted [year] [name of copyright owner]"
20 *
21 * [Name of File] [ver.__] [Date]
22 *
23 * Copyright 2005 Sun Microsystems Inc. All Rights Reserved
24 */
25
26 /*
27 * The Apache Software License, Version 1.1
28 *
29 * Copyright (c) 1999 The Apache Software Foundation. All rights
30 * reserved.
31 *
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
34 * are met:
35 *
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 *
39 * 2. Redistributions in binary form must reproduce the above copyright
40 * notice, this list of conditions and the following disclaimer in
41 * the documentation and/or other materials provided with the
42 * distribution.
43 *
44 * 3. The end-user documentation included with the redistribution, if
45 * any, must include the following acknowlegement:
46 * "This product includes software developed by the
47 * Apache Software Foundation (http://www.apache.org/)."
48 * Alternately, this acknowlegement may appear in the software itself,
49 * if and wherever such third-party acknowlegements normally appear.
50 *
51 * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
52 * Foundation" must not be used to endorse or promote products derived
53 * from this software without prior written permission. For written
54 * permission, please contact apache@apache.org.
55 *
56 * 5. Products derived from this software may not be called "Apache"
57 * nor may "Apache" appear in their names without prior written
58 * permission of the Apache Group.
59 *
60 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
61 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
62 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
63 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
64 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
65 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
66 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
67 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
68 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
69 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
70 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
71 * SUCH DAMAGE.
72 * ====================================================================
73 *
74 * This software consists of voluntary contributions made by many
75 * individuals on behalf of the Apache Software Foundation. For more
76 * information on the Apache Software Foundation, please see
77 * <http://www.apache.org/>.
78 *
79 */
80
81 package demo.taglib;
82
83 import javax.servlet.jsp.JspException;
84 import javax.servlet.jsp.JspTagException;
85 import javax.servlet.jsp.tagext.BodyTagSupport;
86
87 import java.io.IOException;
88 import java.io.InputStream;
89 import java.io.InputStreamReader;
90 import java.io.OutputStream;
91 import java.io.OutputStreamWriter;
92 import java.io.Reader;
93 import java.io.StringReader;
94 import java.io.Writer;
95
96 /**
97 * <p>Tag handler for <escapeHtml>
98 *
99 * @author Pierre Delisle
100 * @version $Revision: 1.4 $ $Date: 2006/03/07 17:21:01 $
101 */
102 public class EscapeHtmlTag extends BodyTagSupport {
103
104 //*********************************************************************
105 // Instance variables
106
107 private Reader reader;
108 private Writer writer;
109
110 //*********************************************************************
111 // Constructors
112
113 public EscapeHtmlTag() {
114 super();
115 init();
116 }
117
118
119 private void init() {
120 reader = null;
121 writer = null;
122 }
123
124 //*********************************************************************
125 // Tag's properties
126
127 /** Tag's 'reader' attribute */
128 public void setReader(Reader reader) {
129 this.reader = reader;
130 }
131
132
133 /** Tag's 'writer' attribute */
134 public void setWriter(Writer writer) {
135 this.writer = writer;
136 }
137
138 //*********************************************************************
139 // TagSupport methods
140
141 public int doEndTag() throws JspException {
142 Reader in;
143 Writer out;
144
145 if (reader == null) {
146 String bcs = getBodyContent().getString().trim();
147 if (bcs == null || bcs.equals("")) {
148 throw new JspTagException("In <escapeHtml>, 'reader' " +
149 "not specified and no non-whitespace content inside the tag.");
150 }
151 in = castToReader(bcs);
152 } else {
153 in = reader;
154 }
155
156 if (writer == null) {
157 out = pageContext.getOut();
158 } else {
159 out = writer;
160 }
161
162 transform(in, out);
163 return EVAL_PAGE;
164 }
165
166
167 /** Releases any resources we may have (or inherit) */
168 public void release() {
169 super.release();
170 init();
171 }
172
173 //*********************************************************************
174 // Tag's scific behavior methods
175
176 /** Transform */
177 public void transform(Reader reader, Writer writer)
178 throws JspException {
179 int c;
180 try {
181 writer.write("<pre>");
182 while ((c = reader.read()) != -1) {
183 if (c == '<') {
184 writer.write("<");
185 } else if (c == '>') {
186 writer.write(">");
187 } else {
188 writer.write(c);
189 }
190 }
191 writer.write("</pre>");
192 } catch (IOException ex) {
193 throw new JspException("EscapeHtml: " +
194 "error copying chars", ex);
195 }
196 }
197
198 //*********************************************************************
199 // Utility methods
200
201
202 public static Reader castToReader(Object obj) throws JspException {
203 if (obj instanceof InputStream) {
204 return new InputStreamReader((InputStream) obj);
205 } else if (obj instanceof Reader) {
206 return (Reader) obj;
207 } else if (obj instanceof String) {
208 return new StringReader((String) obj);
209 }
210 throw new JspException("Invalid type '" + obj.getClass().getName() +
211 "' for castToReader()");
212 }
213
214
215 public static Writer castToWriter(Object obj) throws JspException {
216 if (obj instanceof OutputStream) {
217 return new OutputStreamWriter((OutputStream) obj);
218 } else if (obj instanceof Writer) {
219 return (Writer) obj;
220 /*@@@
221 } else if (obj instanceof String) {
222 return new StringWriter();
223 */
224 }
225 throw new JspException("Invalid type '" + obj.getClass().getName() +
226 "' for castToWriter()");
227 }
228
229 }