KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > Yasna > forum > filter > FilterCodeHighlight


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

53
54 /**
55  * Copyright (C) 2000 CoolServlets.com. All rights reserved.
56  *
57  * ===================================================================
58  * The Apache Software License, Version 1.1
59  *
60  * Redistribution and use in source and binary forms, with or without
61  * modification, are permitted provided that the following conditions
62  * are met:
63  *
64  * 1. Redistributions of source code must retain the above copyright
65  * notice, this list of conditions and the following disclaimer.
66  *
67  * 2. Redistributions in binary form must reproduce the above copyright
68  * notice, this list of conditions and the following disclaimer in
69  * the documentation and/or other materials provided with the
70  * distribution.
71  *
72  * 3. The end-user documentation included with the redistribution,
73  * if any, must include the following acknowledgment:
74  * "This product includes software developed by
75  * CoolServlets.com (http://www.coolservlets.com)."
76  * Alternately, this acknowledgment may appear in the software itself,
77  * if and wherever such third-party acknowledgments normally appear.
78  *
79  * 4. The names "Jive" and "CoolServlets.com" must not be used to
80  * endorse or promote products derived from this software without
81  * prior written permission. For written permission, please
82  * contact webmaster@coolservlets.com.
83  *
84  * 5. Products derived from this software may not be called "Jive",
85  * nor may "Jive" appear in their name, without prior written
86  * permission of CoolServlets.com.
87  *
88  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
89  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
90  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
91  * DISCLAIMED. IN NO EVENT SHALL COOLSERVLETS.COM OR
92  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
93  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
94  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
95  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
96  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
97  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
98  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
99  * SUCH DAMAGE.
100  * ====================================================================
101  *
102  * This software consists of voluntary contributions made by many
103  * individuals on behalf of CoolServlets.com. For more information
104  * on CoolServlets.com, please see <http://www.coolservlets.com>.
105  */

106
107 package com.Yasna.forum.filter;
108
109 import java.io.*;
110 import java.util.*;
111 import com.Yasna.forum.*;
112 import com.Yasna.codeviewer.*;
113
114 /**
115  * A ForumMessageFilter that syntax highlights Java code appearing between
116  * [code][/code] tags in the body of ForumMessage.
117  */

118 public class FilterCodeHighlight extends ForumMessageFilter
119         implements Serializable
120 {
121
122     private static final String JavaDoc NEW_LINE = System.getProperty("line.separator");
123
124     /**
125      * Property values of the filter.
126      */

127     private Properties props;
128
129     /**
130      * Property descriptions of the filter.
131      */

132     private Properties propDescriptions;
133
134     /**
135      * Much of the work of this filter is done by a CodeViewer object. We
136      * make it a transient variable since there is no reason to serialize it.
137      */

138     private transient CodeViewer cv;
139
140     /**
141      * Creates a new filter not associated with a message. This is
142      * generally only useful for defining a template filter that other
143      * fitlers will be cloned from.
144      */

145     public FilterCodeHighlight() {
146         super();
147         props = new Properties();
148         propDescriptions = new Properties();
149         cv = new CodeViewer();
150         initializeProperties();
151     }
152
153     /**
154      * Creates a new filter wrapped around the specified message. This
155      * constructor is normally called when cloning a filter template.
156      *
157      * @param message the ForumMessage to wrap the new filter around.
158      * @param properties the property values for the filter.
159      * @param propertyDescriptions the property descriptions for the filter.
160      */

161     public FilterCodeHighlight(ForumMessage message, Properties properties,
162             Properties propertyDescriptions)
163     {
164         super(message);
165         this.props = new Properties(properties);
166         this.propDescriptions = new Properties(propertyDescriptions);
167         cv = new CodeViewer();
168         applyProperties();
169     }
170
171     /**
172      * Clones a new filter that will have the same properties and that
173      * will wrap around the specified message.
174      *
175      * @param message the ForumMessage to wrap the new filter around.
176      */

177     public ForumMessageFilter clone(ForumMessage message){
178         return new FilterCodeHighlight(message, props, propDescriptions);
179     }
180
181     /**
182      * Returns the name of the filter.
183      */

184     public String JavaDoc getName() {
185         return "Java Code Syntax Highlighter";
186     }
187
188     /**
189      * Returns a description of the filter.
190      */

191     public String JavaDoc getDescription() {
192         return "Highlights Java code that appears between [code][/code] tags";
193     }
194
195     /**
196      * Returns the author of the filter.
197      */

198     public String JavaDoc getAuthor() {
199         return "CoolServlets.com";
200     }
201
202     /**
203      * Returns the major version number of the filter.
204      */

205     public int getMajorVersion() {
206         return 1;
207     }
208
209     /**
210      * Returns the minor version number of the filter.
211      */

212     public int getMinorVersion() {
213         return 0;
214     }
215
216     /**
217      * Returns the value of a property of the filter.
218      *
219      * @param name the name of the property.
220      * @returns the value of the property.
221      */

222     public String JavaDoc getFilterProperty(String JavaDoc name) {
223         return props.getProperty(name);
224     }
225
226     /**
227      * Returns the description of a property of the filter.
228      *
229      * @param name the name of the property.
230      * @return the description of the property.
231      */

232     public String JavaDoc getFilterPropertyDescription(String JavaDoc name) {
233         return propDescriptions.getProperty(name);
234     }
235
236     /**
237      * Returns an Enumeration of all the property names.
238      */

239     public Enumeration filterPropertyNames() {
240         return props.propertyNames();
241     }
242
243     /**
244      * Sets a property of the filter. Each filter has a set number of
245      * properties that are determined by the filter author.
246      *
247      * @param name the name of the property to set.
248      * @param value the new value for the property.
249      *
250      * @throws IllegalArgumentException if the property trying to be set doesn't
251      * exist.
252      */

253     public void setFilterProperty(String JavaDoc name, String JavaDoc value)
254             throws IllegalArgumentException JavaDoc
255     {
256         if (props.getProperty(name) == null) {
257             throw new IllegalArgumentException JavaDoc();
258         }
259         props.put(name, value);
260         applyProperties();
261     }
262
263     /**
264      * <b>Overloaded</b> to return the body of message with Java code between
265      * [code] [/code] tags syntax highlighted.
266      */

267     public String JavaDoc getBody() {
268         return highlightCode(message.getBody());
269     }
270
271     /**
272      * Creates properties and sets their descriptions.
273      */

274     private void initializeProperties() {
275         props.put("commentStart",cv.getCommentStart());
276         props.put("commentEnd",cv.getCommentEnd());
277         props.put("stringStart",cv.getStringStart());
278         props.put("stringEnd",cv.getStringEnd());
279         props.put("reservedWordStart",cv.getReservedWordStart());
280         props.put("reservedWordEnd",cv.getReservedWordEnd());
281
282         propDescriptions.put("commentStart","A HTML start tag that determines how comments will be displayed");
283         propDescriptions.put("commentEnd","A HTML end tag that should correspond to the commentStart tag");
284         propDescriptions.put("stringStart","A HTML start tag that determines how strings will be displayed");
285         propDescriptions.put("stringEnd","A HTML end tag that should correspond to the stringStart tag");
286         propDescriptions.put("reservedWordStart","A HTML start tag that determines how reserved words will be displayed");
287         propDescriptions.put("reservedWordEnd","A HTML end tag that should correspond to the reservedWordStart tag");
288     }
289
290     private void applyProperties() {
291         cv.setCommentStart(props.getProperty("commentStart"));
292         cv.setCommentEnd(props.getProperty("commentEnd"));
293         cv.setStringStart(props.getProperty("stringStart"));
294         cv.setStringEnd(props.getProperty("stringEnd"));
295         cv.setReservedWordStart(props.getProperty("reservedWordStart"));
296         cv.setReservedWordEnd(props.getProperty("reservedWordEnd"));
297     }
298
299     /**
300     * This method takes a string which may contain Java code.
301     * The Java code will be highlighted.
302     *
303     * @param input The text to be converted.
304     * @return The input string with any Java code highlighted.
305     */

306     private String JavaDoc highlightCode( String JavaDoc input ) {
307         // Check if the string is null or zero length -- if so, return what was sent in.
308
if( input == null || input.length() == 0 ) {
309             return input;
310         }
311         else {
312             StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
313             int i = 0, j = 0, oldend = 0;
314
315             while ( ( i=input.indexOf( "[code]", oldend ) ) >= 0 ) {
316                 //Check to see where the ending code tag is and store than in j
317
if ( (j=input.indexOf( "[/code]", i+6 ) ) < 0) {
318                     //End at end of input if no closing tag is given
319
j = input.length()-7;
320                 }
321                 // Take the string up to the code, append the string returned by CodeViewer
322
buf.append(input.substring(oldend,i));
323                 buf.append("<pre>");
324                 //Read line by line and filter accordingly.
325
//StringTokenizer tokens = new StringTokenizer(input.substring(i+6,j-1), NEW_LINE);
326
StringTokenizer tokens = new StringTokenizer(input.substring(i+6,j), NEW_LINE);
327                 while (tokens.hasMoreTokens()) {
328                     buf.append(cv.syntaxHighlight(tokens.nextToken()));
329                     buf.append(NEW_LINE);
330                 }
331                 buf.append("</pre>");
332                 // Next time, want to start looking after ending [/code] tag
333
oldend = j+7;
334             }
335             buf.append(input.substring(oldend,input.length()));
336             return buf.toString();
337         }
338     }
339 }
340
Popular Tags