KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nemesis > forum > filter > FilterCodeHighlight


1 /*
2  * NEMESIS-FORUM.
3  * Copyright (C) 2002 David Laurent(lithium2@free.fr). All rights reserved.
4  *
5  * Copyright (c) 2000 The Apache Software Foundation. All rights reserved.
6  *
7  * Copyright (C) 2001 Yasna.com. All rights reserved.
8  *
9  * Copyright (C) 2000 CoolServlets.com. All rights reserved.
10  *
11  * NEMESIS-FORUM. is free software; you can redistribute it and/or
12  * modify it under the terms of the Apache Software License, Version 1.1,
13  * or (at your option) any later version.
14  *
15  * NEMESIS-FORUM core framework, NEMESIS-FORUM backoffice, NEMESIS-FORUM frontoffice
16  * application are parts of NEMESIS-FORUM and are distributed under
17  * same terms of licence.
18  *
19  *
20  * NEMESIS-FORUM includes software developed by the Apache Software Foundation (http://www.apache.org/)
21  * and software developed by CoolServlets.com (http://www.coolservlets.com).
22  * and software developed by Yasna.com (http://www.yasna.com).
23  *
24  */

25 package org.nemesis.forum.filter;
26
27 import java.io.Serializable JavaDoc;
28 import java.util.Enumeration JavaDoc;
29 import java.util.Map JavaDoc;
30 import java.util.Properties JavaDoc;
31 import java.util.StringTokenizer JavaDoc;
32
33 import org.nemesis.forum.Message;
34 import org.nemesis.forum.MessageFilter;
35 import org.nemesis.forum.util.tool.CodeViewer;
36
37 /**
38  * A ForumMessageFilter that syntax highlights Java code appearing between
39  * [code][/code] tags in the body of ForumMessage.
40  */

41 public class FilterCodeHighlight extends MessageFilter implements Serializable JavaDoc {
42
43     private static final String JavaDoc NEW_LINE = System.getProperty("line.separator");
44
45     /**
46      * Property values of the filter.
47      */

48     private Properties JavaDoc props;
49
50     /**
51      * Property descriptions of the filter.
52      */

53     private Properties JavaDoc propDescriptions;
54
55     /**
56      * Much of the work of this filter is done by a CodeViewer object. We
57      * make it a transient variable since there is no reason to serialize it.
58      */

59     private transient CodeViewer cv=new CodeViewer();;
60
61     /**
62      * Creates a new filter not associated with a message. This is
63      * generally only useful for defining a template filter that other
64      * fitlers will be cloned from.
65      */

66     public FilterCodeHighlight() {
67         super();
68         props = new Properties JavaDoc();
69         propDescriptions = new Properties JavaDoc();
70         cv = new CodeViewer();
71         initializeProperties();
72     }
73
74     /**
75      * Creates a new filter wrapped around the specified message. This
76      * constructor is normally called when cloning a filter template.
77      *
78      * @param message the ForumMessage to wrap the new filter around.
79      * @param properties the property values for the filter.
80      * @param propertyDescriptions the property descriptions for the filter.
81      */

82     public FilterCodeHighlight(Message message, Properties JavaDoc properties, Properties JavaDoc propertyDescriptions) {
83         super(message);
84         this.props = new Properties JavaDoc(properties);
85         this.propDescriptions = new Properties JavaDoc(propertyDescriptions);
86         cv = new CodeViewer();
87         applyProperties();
88     }
89
90     /**
91      * Clones a new filter that will have the same properties and that
92      * will wrap around the specified message.
93      *
94      * @param message the ForumMessage to wrap the new filter around.
95      */

96     public MessageFilter clone(Message message) {
97         return new FilterCodeHighlight(message, props, propDescriptions);
98     }
99
100     /**
101      * Returns the name of the filter.
102      */

103     public String JavaDoc getName() {
104         return "Java Code Syntax Highlighter";
105     }
106
107     /**
108      * Returns a description of the filter.
109      */

110     public String JavaDoc getDescription() {
111         return "Highlights Java code that appears between [code][/code] tags";
112     }
113
114     /**
115      * Returns the author of the filter.
116      */

117     public String JavaDoc getAuthor() {
118         return "CoolServlets.com";
119     }
120
121     /**
122      * Returns the major version number of the filter.
123      */

124     public int getMajorVersion() {
125         return 1;
126     }
127
128     /**
129      * Returns the minor version number of the filter.
130      */

131     public int getMinorVersion() {
132         return 0;
133     }
134
135     /**
136      * Returns the value of a property of the filter.
137      *
138      * @param name the name of the property.
139      * @returns the value of the property.
140      */

141     public String JavaDoc getFilterProperty(String JavaDoc name) {
142         return props.getProperty(name);
143     }
144
145     /**
146      * Returns the description of a property of the filter.
147      *
148      * @param name the name of the property.
149      * @return the description of the property.
150      */

151     public String JavaDoc getFilterPropertyDescription(String JavaDoc name) {
152         return propDescriptions.getProperty(name);
153     }
154
155     /**
156      * Returns an Enumeration of all the property names.
157      */

158     public Enumeration JavaDoc getFilterPropertyNames() {
159         return props.propertyNames();
160     }
161     
162     //AJOUT
163
public Map JavaDoc getFilterProperties(){
164             return props;
165     }
166     public Map JavaDoc getFilterPropertiesDescription(){
167                 return propDescriptions;
168         }
169
170     /**
171      * Sets a property of the filter. Each filter has a set number of
172      * properties that are determined by the filter author.
173      *
174      * @param name the name of the property to set.
175      * @param value the new value for the property.
176      *
177      * @throws IllegalArgumentException if the property trying to be set doesn't
178      * exist.
179      */

180     public void setFilterProperty(String JavaDoc name, String JavaDoc value) throws IllegalArgumentException JavaDoc {
181         if (props.getProperty(name) == null) {
182             throw new IllegalArgumentException JavaDoc();
183         }
184         props.put(name, value);
185         applyProperties();
186     }
187
188     /**
189      * <b>Overloaded</b> to return the body of message with Java code between
190      * [code] [/code] tags syntax highlighted.
191      */

192     public String JavaDoc getBody() {
193         return highlightCode(message.getBody());
194     }
195
196     /**
197      * Creates properties and sets their descriptions.
198      */

199     private void initializeProperties() {
200         props.put("commentStart", cv.getCommentStart());
201         props.put("commentEnd", cv.getCommentEnd());
202         props.put("stringStart", cv.getStringStart());
203         props.put("stringEnd", cv.getStringEnd());
204         props.put("reservedWordStart", cv.getReservedWordStart());
205         props.put("reservedWordEnd", cv.getReservedWordEnd());
206
207         propDescriptions.put("commentStart", "A HTML start tag that determines how comments will be displayed");
208         propDescriptions.put("commentEnd", "A HTML end tag that should correspond to the commentStart tag");
209         propDescriptions.put("stringStart", "A HTML start tag that determines how strings will be displayed");
210         propDescriptions.put("stringEnd", "A HTML end tag that should correspond to the stringStart tag");
211         propDescriptions.put(
212             "reservedWordStart",
213             "A HTML start tag that determines how reserved words will be displayed");
214         propDescriptions.put("reservedWordEnd", "A HTML end tag that should correspond to the reservedWordStart tag");
215     }
216
217     private void applyProperties() {
218         cv.setCommentStart(props.getProperty("commentStart"));
219         cv.setCommentEnd(props.getProperty("commentEnd"));
220         cv.setStringStart(props.getProperty("stringStart"));
221         cv.setStringEnd(props.getProperty("stringEnd"));
222         cv.setReservedWordStart(props.getProperty("reservedWordStart"));
223         cv.setReservedWordEnd(props.getProperty("reservedWordEnd"));
224     }
225
226     /**
227     * This method takes a string which may contain Java code.
228     * The Java code will be highlighted.
229     *
230     * @param input The text to be converted.
231     * @return The input string with any Java code highlighted.
232     */

233     private String JavaDoc highlightCode(String JavaDoc input) {
234         // Check if the string is null or zero length -- if so, return what was sent in.
235
if (input == null || input.length() == 0) {
236             return input;
237         } else {
238             StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
239             int i = 0, j = 0, oldend = 0;
240
241             while ((i = input.indexOf("[code]", oldend)) >= 0) {
242                 //Check to see where the ending code tag is and store than in j
243
if ((j = input.indexOf("[/code]", i + 6)) < 0) {
244                     //End at end of input if no closing tag is given
245
j = input.length() - 7;
246                 }
247                 // Take the string up to the code, append the string returned by CodeViewer
248
buf.append(input.substring(oldend, i));
249                 buf.append("<pre>");
250                 //Read line by line and filter accordingly.
251
//StringTokenizer tokens = new StringTokenizer(input.substring(i+6,j-1), NEW_LINE);
252
StringTokenizer JavaDoc tokens = new StringTokenizer JavaDoc(input.substring(i + 6, j), NEW_LINE);
253                 while (tokens.hasMoreTokens()) {
254                     buf.append(cv.syntaxHighlight(tokens.nextToken()));
255                     buf.append(NEW_LINE);
256                 }
257                 buf.append("</pre>");
258                 // Next time, want to start looking after ending [/code] tag
259
oldend = j + 7;
260             }
261             buf.append(input.substring(oldend, input.length()));
262             return buf.toString();
263         }
264     }
265 }
266
Popular Tags