KickJava   Java API By Example, From Geeks To Geeks.

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


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
32 import org.nemesis.forum.Message;
33 import org.nemesis.forum.MessageFilter;
34 import org.nemesis.forum.util.StringUtils;
35
36 /**
37  * A ForumMessageFilter that replaces [b][/b] and [i][/i] tags with their HTML
38  * tag equivalents. This filter should only be run after any HTML stripping
39  * filters are.
40  */

41 public class FilterFontStyle extends MessageFilter implements Serializable JavaDoc {
42
43     /**
44      * Property values of the filter.
45      */

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

51     private Properties JavaDoc propDescriptions;
52     //AJOUT
53
public Map JavaDoc getFilterPropertiesDescription(){
54                     return propDescriptions;
55             }
56     /**
57      * Creates a new filter not associated with a message. This is
58      * generally only useful for defining a template filter that other
59      * fitlers will be cloned from.
60      */

61     public FilterFontStyle() {
62         super();
63         this.props = new Properties JavaDoc(props);
64         this.propDescriptions = new Properties JavaDoc(propDescriptions);
65         initializeProperties();
66     }
67     
68       // AJOUT
69
public Map JavaDoc getFilterProperties(){
70               return props;
71       }
72
73     /**
74      * Creates a new filter wrapped around the specified message. This
75      * constructor is normally called when cloning a filter template.
76      *
77      * @param message the ForumMessage to wrap the new filter around.
78      * @param properties the property values for the filter.
79      * @param propertyDescriptions the property descriptions for the filter.
80      */

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

93     public MessageFilter clone(Message message) {
94         return new FilterFontStyle(message, props, propDescriptions);
95     }
96
97     /**
98      * Returns the name of the filter.
99      */

100     public String JavaDoc getName() {
101         return "Font Stylizer";
102     }
103
104     /**
105      * Returns a description of the filter.
106      */

107     public String JavaDoc getDescription() {
108         return "Converts custom font tags into HTML equivalents. It " + "currently supports [b][/b] and [i][/i] tags.";
109     }
110
111     /**
112      * Returns the author of the filter.
113      */

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

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

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

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

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

155     public Enumeration JavaDoc getFilterPropertyNames() {
156         return props.propertyNames();
157     }
158
159     /**
160      * Sets a property of the filter. Each filter has a set number of
161      * properties that are determined by the filter author.
162      *
163      * @param name the name of the property to set.
164      * @param value the new value for the property.
165      *
166      * @throws IllegalArgumentException if the property trying to be set doesn't
167      * exist.
168      */

169     public void setFilterProperty(String JavaDoc name, String JavaDoc value) throws IllegalArgumentException JavaDoc {
170         if (props.getProperty(name) == null) {
171             throw new IllegalArgumentException JavaDoc();
172         }
173         props.put(name, value);
174     }
175
176     /**
177      * <b>Overloaded</b> to return the body of the message with custom font
178      * stylizing tags translated to HTML.
179      */

180     public String JavaDoc getBody() {
181         return fontStyleToHTML(message.getBody());
182     }
183
184     /**
185      * Creates properties and sets their descriptions.
186      */

187     private void initializeProperties() {
188         props.put("Bold", "on");
189         props.put("Italics", "on");
190
191         propDescriptions.put("Bold", "Toggles translation of [b][/b] to HTML bold tags.");
192         propDescriptions.put("Italics", "Toggles translation of [i][/i] to HTML bold tags.");
193     }
194
195     /**
196      * This method takes a string which may contain CoolServlets tags that
197      * the style of a font. These will be converted to HTML tags.
198      *
199      * @param input The text to be converted.
200      * @return The input string with the CoolServlets tags [b] (indicating
201      * bold, and [i] (indicating italics) changed to the HTML tags <b> and
202      * <i>, respectively. In addition, the corresponding tags [/b] and [/i]
203      * will be changed to </b> and </i>
204      */

205     private String JavaDoc fontStyleToHTML(String JavaDoc input) {
206         // Check if the string is null or zero length -- if so, return what was sent in.
207
if (input == null || input.length() == 0) {
208             return input;
209         } else {
210             // Create int [] objects to determine if all bold and italic
211
// tags have been closed
212
int[] boldStartCount = new int[1];
213             int[] italicsStartCount = new int[1];
214             int[] boldEndCount = new int[1];
215             int[] italicsEndCount = new int[1];
216             if (props.getProperty("Bold").equals("on")) {
217                 input = StringUtils.replace(input, "[b]", "<b>", boldStartCount);
218                 input = StringUtils.replace(input, "[/b]", "</b>", boldEndCount);
219                 int bStartCount = boldStartCount[0];
220                 int bEndCount = boldEndCount[0];
221                 while (bStartCount > bEndCount) {
222                     input = input.concat("</b>");
223                     bEndCount++;
224                 }
225             }
226             if (props.getProperty("Italics").equals("on")) {
227                 input = StringUtils.replace(input, "[i]", "<i>", italicsStartCount);
228                 input = StringUtils.replace(input, "[/i]", "</i>", italicsEndCount);
229                 int iStartCount = italicsStartCount[0];
230                 int iEndCount = italicsEndCount[0];
231                 while (iStartCount > iEndCount) {
232                     input = input.concat("</i>");
233                     iEndCount++;
234                 }
235             }
236         }
237         return input;
238     }
239 }
240
Popular Tags