KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > freemarker > testcase > models > TransformModel1


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.testcase.models;
54
55 import freemarker.template.*;
56 import java.io.*;
57 import java.util.Map JavaDoc;
58
59 /**
60  * A TemplateTransformModel that includes properties. These properties can be
61  * set at model construction time, or, for the purposes of this demonstration,
62  * can be passed in from a wrapper TemplateMethodModel.
63  *
64  * @version $Id: TransformModel1.java,v 1.21 2003/01/12 23:40:25 revusky Exp $
65  */

66 public class TransformModel1 implements TemplateTransformModel {
67
68     private boolean m_bAmpersands = false;
69     private boolean m_bQuotes = false;
70     private boolean m_bTags = false;
71     private String JavaDoc m_aComment = "";
72
73     private static final int READER_BUFFER_SIZE = 4096;
74
75     public Writer getWriter(final Writer out,
76                             final Map JavaDoc args)
77     {
78         final StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
79         return new Writer(out) {
80             public void write(char cbuf[], int off, int len) {
81                 buf.append(cbuf, off, len);
82             }
83
84             public void flush() {
85             }
86
87             public void close() throws IOException {
88                 StringReader sr = new StringReader(buf.toString());
89                 StringWriter sw = new StringWriter();
90                 transform(sr, sw);
91                 out.write(sw.toString());
92             }
93         };
94     }
95
96
97     /**
98      * Indicates whether we escape ampersands. This property can be set either
99      * while the model is being constructed, or via a property passed in through
100      * a <code>TemplateMethodModel</code>.
101      */

102     public void setAmpersands( boolean bAmpersands ) {
103         m_bAmpersands = bAmpersands;
104     }
105
106     /**
107      * Indicates whether we escape quotes. This property can be set either
108      * while the model is being constructed, or via a property passed in through
109      * a <code>TemplateMethodModel</code>.
110      */

111     public void setQuotes( boolean bQuotes ) {
112         m_bQuotes = bQuotes;
113     }
114
115     /**
116      * Indicates whether we escape tags. This property can be set either
117      * while the model is being constructed, or via a property passed in through
118      * a <code>TemplateMethodModel</code>.
119      */

120     public void setTags( boolean bTags ) {
121         m_bTags = bTags;
122     }
123
124     /**
125      * Sets a comment for this transformation. This property can be set either
126      * while the model is being constructed, or via a property passed in through
127      * a <code>TemplateMethodModel</code>.
128      */

129     public void setComment( String JavaDoc aComment ) {
130         m_aComment = aComment;
131     }
132
133     /**
134      * Performs a transformation/filter on FreeMarker output.
135      *
136      * @param source the input to be transformed
137      * @param output the destination of the transformation
138      */

139     public void transform(Reader source, Writer output)
140     throws IOException
141     {
142         // Output the source, converting unsafe certain characters to their
143
// equivalent entities.
144
int n = 0;
145         boolean bCommentSent = false;
146         char[] aBuffer = new char[ READER_BUFFER_SIZE ];
147         int i = source.read( aBuffer );
148         while (i >= 0) {
149             for ( int j = 0; j < i; j++ ) {
150                 char c = aBuffer[j];
151                 switch (c) {
152                     case '&':
153                         if ( m_bAmpersands ) {
154                             output.write("&amp;");
155                         } else {
156                             output.write( c );
157                         }
158                         break;
159                     case '<':
160                         if ( m_bTags ) {
161                             output.write("&lt;");
162                         } else {
163                             output.write( c );
164                         }
165                         break;
166                     case '>':
167                         if ( m_bTags ) {
168                             output.write("&gt;");
169                         } else {
170                             output.write( c );
171                         }
172                         break;
173                     case '"':
174                         if ( m_bQuotes ) {
175                             output.write("&quot;");
176                         } else {
177                             output.write( c );
178                         }
179                         break;
180                     case '\'':
181                         if ( m_bQuotes ) {
182                             output.write("&apos;");
183                         } else {
184                             output.write( c );
185                         }
186                         break;
187                     case '*':
188                         if ( ! bCommentSent ) {
189                             output.write( m_aComment );
190                             bCommentSent = true;
191                         } else {
192                             output.write( c );
193                         }
194                         break;
195                     default:
196                         output.write(c);
197                 }
198                 n++;
199             }
200             i = source.read( aBuffer );
201         }
202     }
203 }
204
Popular Tags