1 25 package org.nemesis.forum.filter; 26 27 import java.util.Enumeration ; 28 import java.util.Map ; 29 import java.util.Properties ; 30 import java.util.StringTokenizer ; 31 32 import org.nemesis.forum.Message; 33 import org.nemesis.forum.MessageFilter; 34 35 38 public class FilterProfanity extends MessageFilter { 39 40 43 private String [] filterList; 44 45 48 private boolean ignoreCase = true; 49 50 53 private Properties props; 54 public Map getFilterProperties(){ 56 return props; 57 } 58 61 private Properties propDescriptions; 62 public Map getFilterPropertiesDescription(){ 64 return propDescriptions; 65 } 66 71 public FilterProfanity() { 72 super(); 73 this.props = new Properties (); 74 this.propDescriptions = new Properties (); 75 initializeProperties(); 76 } 77 78 86 public FilterProfanity( 87 Message message, 88 Properties props, 89 Properties propDescriptions, 90 String [] filterList, 91 boolean ignoreCase) { 92 super(message); 93 this.props = new Properties (props); 94 this.propDescriptions = new Properties (propDescriptions); 95 this.filterList = filterList; 96 this.ignoreCase = ignoreCase; 97 } 98 99 105 public MessageFilter clone(Message message) { 106 return new FilterProfanity(message, props, propDescriptions, filterList, ignoreCase); 107 } 108 109 112 public String getName() { 113 return "Profanity Filter"; 114 } 115 116 119 public String getDescription() { 120 return "Removes profanity from messages using a custom word list."; 121 } 122 123 126 public String getAuthor() { 127 return "CoolServlets.com"; 128 } 129 130 133 public int getMajorVersion() { 134 return 1; 135 } 136 137 140 public int getMinorVersion() { 141 return 0; 142 } 143 144 150 public String getFilterProperty(String name) { 151 return props.getProperty(name); 152 } 153 154 160 public String getFilterPropertyDescription(String name) { 161 return propDescriptions.getProperty(name); 162 } 163 164 167 public Enumeration getFilterPropertyNames() { 168 return props.propertyNames(); 169 } 170 171 181 public void setFilterProperty(String name, String value) throws IllegalArgumentException { 182 if (props.getProperty(name) == null) { 183 throw new IllegalArgumentException (); 184 } 185 props.put(name, value); 186 applyProperties(); 187 } 188 189 193 public String getSubject() { 194 return filterProfanity(message.getSubject()); 195 } 196 197 201 public String getBody() { 202 return filterProfanity(message.getBody()); 203 } 204 205 208 private void initializeProperties() { 209 filterList = new String [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 ) props.getProperty("ignoreCase")).equals("on"); 224 String list = (String ) props.get("filterList"); 225 StringTokenizer tokens = new StringTokenizer (list, ","); 226 String [] newFilterList = new String [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 240 private String filterProfanity(String str) { 241 if (str == null || "".equals(str)) { 243 return str; 244 } 245 String 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 261 private String 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 (newWord); 267 } 268 269 272 private String replace(String line, String lowerCaseLine, String oldString, String 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 buf = new StringBuffer (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
|