KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.Enumeration JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.Properties JavaDoc;
30 import java.util.StringTokenizer JavaDoc;
31
32 import org.nemesis.forum.Message;
33 import org.nemesis.forum.MessageFilter;
34
35 /**
36  * A ForumMessageFilter that replaces profanity.
37  */

38 public class FilterProfanity extends MessageFilter {
39
40     /**
41      * Array of all the bad words to filter.
42      */

43     private String JavaDoc[] filterList;
44
45     /**
46      * Indicates if case of words should be ignored.
47      */

48     private boolean ignoreCase = true;
49
50     /**
51      * Property values of the filter.
52      */

53     private Properties JavaDoc props;
54     // AJOUT
55
public Map JavaDoc getFilterProperties(){
56                  return props;
57          }
58     /**
59      * Property descriptions of the filter.
60      */

61     private Properties JavaDoc propDescriptions;
62 // AJOUT
63
public Map JavaDoc getFilterPropertiesDescription(){
64                       return propDescriptions;
65               }
66     /**
67      * Creates a new filter not associated with a message. This is
68      * generally only useful for defining a template filter that other
69      * fitlers will be cloned from.
70      */

71     public FilterProfanity() {
72         super();
73         this.props = new Properties JavaDoc();
74         this.propDescriptions = new Properties JavaDoc();
75         initializeProperties();
76     }
77
78     /**
79      * Creates a new filter wrapped around the specified message. This
80      * constructor is normally called when cloning a filter template.
81      *
82      * @param message the ForumMessage to wrap the new filter around.
83      * @param properties the property values for the filter.
84      * @param propertyDescriptions the property descriptions for the filter.
85      */

86     public FilterProfanity(
87         Message message,
88         Properties JavaDoc props,
89         Properties JavaDoc propDescriptions,
90         String JavaDoc[] filterList,
91         boolean ignoreCase) {
92         super(message);
93         this.props = new Properties JavaDoc(props);
94         this.propDescriptions = new Properties JavaDoc(propDescriptions);
95         this.filterList = filterList;
96         this.ignoreCase = ignoreCase;
97     }
98
99     /**
100      * Clones a new filter that will have the same properties and that
101      * will wrap around the specified message.
102      *
103      * @param message the ForumMessage to wrap the new filter around.
104      */

105     public MessageFilter clone(Message message) {
106         return new FilterProfanity(message, props, propDescriptions, filterList, ignoreCase);
107     }
108
109     /**
110      * Returns the name of the filter.
111      */

112     public String JavaDoc getName() {
113         return "Profanity Filter";
114     }
115
116     /**
117      * Returns a description of the filter.
118      */

119     public String JavaDoc getDescription() {
120         return "Removes profanity from messages using a custom word list.";
121     }
122
123     /**
124      * Returns the author of the filter.
125      */

126     public String JavaDoc getAuthor() {
127         return "CoolServlets.com";
128     }
129
130     /**
131      * Returns the major version number of the filter.
132      */

133     public int getMajorVersion() {
134         return 1;
135     }
136
137     /**
138      * Returns the minor version number of the filter.
139      */

140     public int getMinorVersion() {
141         return 0;
142     }
143
144     /**
145      * Returns the value of a property of the filter.
146      *
147      * @param name the name of the property.
148      * @returns the value of the property.
149      */

150     public String JavaDoc getFilterProperty(String JavaDoc name) {
151         return props.getProperty(name);
152     }
153
154     /**
155      * Returns the description of a property of the filter.
156      *
157      * @param name the name of the property.
158      * @return the description of the property.
159      */

160     public String JavaDoc getFilterPropertyDescription(String JavaDoc name) {
161         return propDescriptions.getProperty(name);
162     }
163
164     /**
165      * Returns an Enumeration of all the property names.
166      */

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

181     public void setFilterProperty(String JavaDoc name, String JavaDoc value) throws IllegalArgumentException JavaDoc {
182         if (props.getProperty(name) == null) {
183             throw new IllegalArgumentException JavaDoc();
184         }
185         props.put(name, value);
186         applyProperties();
187     }
188
189     /**
190      * <b>Overloaded</b> to return the subject of the message with profanity
191      * filtered out.
192      */

193     public String JavaDoc getSubject() {
194         return filterProfanity(message.getSubject());
195     }
196
197     /**
198      * <b>Overloaded</b> to return the body of the message with profanity
199      * filtered out.
200      */

201     public String JavaDoc getBody() {
202         return filterProfanity(message.getBody());
203     }
204
205     /**
206      * Creates properties and sets their descriptions.
207      */

208     private void initializeProperties() {
209         filterList = new String JavaDoc[0];
210         props.put("filterList", "");
211         props.put("ignoreCase", "on");
212
213         propDescriptions.put("filterList", "A comma delimitted list of " + "the bad words to filter out.");
214         propDescriptions.put(
215             "ignoreCase",
216             "Indicates whether the case "
217                 + "of words should be ignored or not. For example, when on, the "
218                 + "words 'CRap' and 'crap' would both be filterd if an entry of "
219                 + "'CRAP' was found in the filter list.");
220     }
221
222     private void applyProperties() {
223         ignoreCase = ((String JavaDoc) props.getProperty("ignoreCase")).equals("on");
224         String JavaDoc list = (String JavaDoc) props.get("filterList");
225         StringTokenizer JavaDoc tokens = new StringTokenizer JavaDoc(list, ",");
226         String JavaDoc[] newFilterList = new String JavaDoc[tokens.countTokens()];
227         for (int i = 0; i < newFilterList.length; i++) {
228             if (ignoreCase) {
229                 newFilterList[i] = tokens.nextToken().toLowerCase().trim();
230             } else {
231                 newFilterList[i] = tokens.nextToken().trim();
232             }
233         }
234         filterList = newFilterList;
235     }
236
237     /**
238      * Filters out bad words.
239      */

240     private String JavaDoc filterProfanity(String JavaDoc str) {
241         // Check to see if the string is null or zero-length
242
if (str == null || "".equals(str)) {
243             return str;
244         }
245         String JavaDoc lower;
246         if (ignoreCase) {
247             lower = str.toLowerCase();
248         } else {
249             lower = str;
250         }
251         for (int i = 0; i < filterList.length; i++) {
252             str = replace(str, lower, filterList[i], cleanWord(filterList[i].length()));
253         }
254         return str;
255     }
256
257     /**
258      * Generates a string of characters of specified length. For example:
259      * !@%$ or %!@$%!@@ or *****
260      */

261     private String JavaDoc cleanWord(int length) {
262         char[] newWord = new char[length];
263         for (int i = 0; i < newWord.length; i++) {
264             newWord[i] = '*';
265         }
266         return new String JavaDoc(newWord);
267     }
268
269     /**
270      * Replaces all instances of oldString with newString in the String line.
271      */

272     private String JavaDoc replace(String JavaDoc line, String JavaDoc lowerCaseLine, String JavaDoc oldString, String JavaDoc newString) {
273         int i = 0;
274         if ((i = lowerCaseLine.indexOf(oldString, i)) >= 0) {
275             int oLength = oldString.length();
276             int nLength = newString.length();
277             StringBuffer JavaDoc buf = new StringBuffer JavaDoc(line.length() + 15);
278             buf.append(line.substring(0, i)).append(newString);
279             i += oLength;
280             int j = i;
281             while ((i = lowerCaseLine.indexOf(oldString, i)) > 0) {
282                 buf.append(line.substring(j, i)).append(newString);
283                 i += oLength;
284                 j = i;
285             }
286             buf.append(line.substring(j));
287             return buf.toString();
288         }
289         return line;
290     }
291 }
292
Popular Tags