| 1 53 54 106 107 package com.Yasna.forum.filter; 108 109 import java.util.*; 110 111 import com.Yasna.forum.*; 112 113 116 public class FilterProfanity extends ForumMessageFilter { 117 118 121 private String [] filterList; 122 123 126 private boolean ignoreCase = true; 127 128 131 private Properties props; 132 133 136 private Properties propDescriptions; 137 138 143 public FilterProfanity() { 144 super(); 145 this.props = new Properties(); 146 this.propDescriptions = new Properties(); 147 initializeProperties(); 148 } 149 150 158 public FilterProfanity(ForumMessage message, Properties props, 159 Properties propDescriptions, String [] filterList, boolean ignoreCase) 160 { 161 super(message); 162 this.props = new Properties(props); 163 this.propDescriptions = new Properties(propDescriptions); 164 this.filterList = filterList; 165 this.ignoreCase = ignoreCase; 166 } 167 168 174 public ForumMessageFilter clone(ForumMessage message){ 175 return new FilterProfanity(message, props, propDescriptions, 176 filterList, ignoreCase); 177 } 178 179 182 public String getName() { 183 return "Profanity Filter"; 184 } 185 186 189 public String getDescription() { 190 return "Removes profanity from messages using a custom word list."; 191 } 192 193 196 public String getAuthor() { 197 return "CoolServlets.com"; 198 } 199 200 203 public int getMajorVersion() { 204 return 1; 205 } 206 207 210 public int getMinorVersion() { 211 return 0; 212 } 213 214 220 public String getFilterProperty(String name) { 221 return props.getProperty(name); 222 } 223 224 230 public String getFilterPropertyDescription(String name) { 231 return propDescriptions.getProperty(name); 232 } 233 234 237 public Enumeration filterPropertyNames() { 238 return props.propertyNames(); 239 } 240 241 251 public void setFilterProperty(String name, String value) 252 throws IllegalArgumentException  253 { 254 if (props.getProperty(name) == null) { 255 throw new IllegalArgumentException (); 256 } 257 props.put(name, value); 258 applyProperties(); 259 } 260 261 265 public String getSubject() { 266 return filterProfanity(message.getSubject()); 267 } 268 269 273 public String getBody() { 274 return filterProfanity(message.getBody()); 275 } 276 277 280 private void initializeProperties() { 281 filterList = new String [0]; 282 props.put("filterList",""); 283 props.put("ignoreCase","on"); 284 285 propDescriptions.put("filterList","A comma delimitted list of " 286 + "the bad words to filter out."); 287 propDescriptions.put("ignoreCase","Indicates whether the case " 288 + "of words should be ignored or not. For example, when on, the " 289 + "words 'CRap' and 'crap' would both be filterd if an entry of " 290 + "'CRAP' was found in the filter list."); 291 } 292 293 private void applyProperties() { 294 ignoreCase = ((String )props.getProperty("ignoreCase")).equals("on"); 295 String list = (String )props.get("filterList"); 296 StringTokenizer tokens = new StringTokenizer(list,","); 297 String [] newFilterList = new String [tokens.countTokens()]; 298 for (int i=0; i<newFilterList.length; i++) { 299 if (ignoreCase) { 300 newFilterList[i] = tokens.nextToken().toLowerCase().trim(); 301 } 302 else { 303 newFilterList[i] = tokens.nextToken().trim(); 304 } 305 } 306 filterList = newFilterList; 307 } 308 309 312 private String filterProfanity(String str) { 313 if (str == null || "".equals(str)) { 315 return str; 316 } 317 String lower; 318 if (ignoreCase) { 319 lower = str.toLowerCase(); 320 } 321 else { 322 lower = str; 323 } 324 for (int i=0; i<filterList.length; i++) { 325 str = replace(str, lower, filterList[i], cleanWord(filterList[i].length())); 326 } 327 return str; 328 } 329 330 334 private String cleanWord(int length) { 335 char[] newWord = new char[length]; 336 for (int i=0; i<newWord.length; i++) { 337 newWord[i] = '*'; 338 } 339 return new String (newWord); 340 } 341 342 345 private String replace(String line, String lowerCaseLine, 346 String oldString, String newString ) 347 { 348 int i=0; 349 if ( ( i=lowerCaseLine.indexOf( oldString, i ) ) >= 0 ) { 350 int oLength = oldString.length(); 351 int nLength = newString.length(); 352 StringBuffer buf = new StringBuffer (line.length()+15); 353 buf.append(line.substring(0,i)).append(newString); 354 i += oLength; 355 int j = i; 356 while( ( i=lowerCaseLine.indexOf( oldString, i ) ) > 0 ) { 357 buf.append(line.substring(j,i)).append(newString); 358 i += oLength; 359 j = i; 360 } 361 buf.append(line.substring(j)); 362 return buf.toString(); 363 } 364 return line; 365 } 366 } 367 368 | Popular Tags |