KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > freemarker > template > utility > HtmlEscape


1 /*
2  * Copyright (c) 2003 The Visigoth Software Society. All rights
3  * reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in
14  * the documentation and/or other materials provided with the
15  * distribution.
16  *
17  * 3. The end-user documentation included with the redistribution, if
18  * any, must include the following acknowledgement:
19  * "This product includes software developed by the
20  * Visigoth Software Society (http://www.visigoths.org/)."
21  * Alternately, this acknowledgement may appear in the software itself,
22  * if and wherever such third-party acknowledgements normally appear.
23  *
24  * 4. Neither the name "FreeMarker", "Visigoth", nor any of the names of the
25  * project contributors may be used to endorse or promote products derived
26  * from this software without prior written permission. For written
27  * permission, please contact visigoths@visigoths.org.
28  *
29  * 5. Products derived from this software may not be called "FreeMarker" or "Visigoth"
30  * nor may "FreeMarker" or "Visigoth" appear in their names
31  * without prior written permission of the Visigoth Software Society.
32  *
33  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
34  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
35  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
36  * DISCLAIMED. IN NO EVENT SHALL THE VISIGOTH SOFTWARE SOCIETY OR
37  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
38  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
39  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
40  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
41  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
42  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
43  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44  * SUCH DAMAGE.
45  * ====================================================================
46  *
47  * This software consists of voluntary contributions made by many
48  * individuals on behalf of the Visigoth Software Society. For more
49  * information on the Visigoth Software Society, please see
50  * http://www.visigoths.org/
51  */

52
53 package freemarker.template.utility;
54
55 import java.io.IOException JavaDoc;
56 import java.io.Writer JavaDoc;
57 import java.util.Map JavaDoc;
58
59 import freemarker.template.TemplateTransformModel;
60
61 /**
62  * Performs an HTML escape of a given template fragment. Specifically,
63  * < > " and & are all turned into entities.
64  *
65  * <p>Usage:<br />
66  * From java:</p>
67  * <pre>
68  * SimpleHash root = new SimpleHash();
69  *
70  * root.put( "htmlEscape", new freemarker.template.utility.HtmlEscape() );
71  *
72  * ...
73  * </pre>
74  *
75  * <p>From your FreeMarker template:</p>
76  * <pre>
77  *
78  * The following is HTML-escaped:
79  * &lt;transform htmlEscape&gt;
80  * &lt;p&gt;This paragraph has all HTML special characters escaped.&lt;/p&gt;
81  * &lt;/transform&gt;
82  *
83  * ...
84  * </pre>
85  *
86  * @version $Id: HtmlEscape.java,v 1.29 2003/02/25 00:28:16 revusky Exp $
87  * @see freemarker.template.utility.XmlEscape
88  */

89 public class HtmlEscape implements TemplateTransformModel {
90
91     private static final char[] LT = "&lt;".toCharArray();
92     private static final char[] GT = "&gt;".toCharArray();
93     private static final char[] AMP = "&amp;".toCharArray();
94     private static final char[] QUOT = "&quot;".toCharArray();
95
96     public Writer JavaDoc getWriter(final Writer JavaDoc out, Map JavaDoc args)
97     {
98         return new Writer JavaDoc()
99         {
100             public void write(char c)
101             throws
102                 IOException JavaDoc
103             {
104                 switch(c)
105                 {
106                     case '<': out.write(LT, 0, 4); break;
107                     case '>': out.write(GT, 0, 4); break;
108                     case '&': out.write(AMP, 0, 5); break;
109                     case '"': out.write(QUOT, 0, 6); break;
110                     default: out.write(c);
111                 }
112             }
113
114             public void write(char cbuf[], int off, int len)
115             throws
116                 IOException JavaDoc
117             {
118                 int lastoff = off;
119                 int lastpos = off + len;
120                 for (int i = off; i < lastpos; i++)
121                 {
122                     switch (cbuf[i])
123                     {
124                         case '<': out.write(cbuf, lastoff, i - lastoff); out.write(LT, 0, 4); lastoff = i + 1; break;
125                         case '>': out.write(cbuf, lastoff, i - lastoff); out.write(GT, 0, 4); lastoff = i + 1; break;
126                         case '&': out.write(cbuf, lastoff, i - lastoff); out.write(AMP, 0, 5); lastoff = i + 1; break;
127                         case '"': out.write(cbuf, lastoff, i - lastoff); out.write(QUOT, 0, 6); lastoff = i + 1; break;
128                     }
129                 }
130                 int remaining = lastpos - lastoff;
131                 if(remaining > 0)
132                 {
133                     out.write(cbuf, lastoff, remaining);
134                 }
135             }
136             public void flush() throws IOException JavaDoc {
137                 out.flush();
138             }
139
140             public void close() {
141             }
142         };
143     }
144 }
145
Popular Tags