devdaily home | apple | java | perl | unix | directory | blog

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/**
 * GuestbookFilter.java
 * Pete Willemsen
 * CoolServlets.com
 * February 3, 2000
 * Version 1.0.3
 *
 * Any errors or feature requests can be reported on CoolServlets.com.
 * We hope you enjoy this program!
 *
 *    Copyright (C) 2000  Pete Willemsen
 *
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 2 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program; if not, write to the Free Software
 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package com.coolservlets.guestbook;

import java.util.Vector;
import java.util.StringTokenizer;

public class GuestbookFilter {

  public GuestbookFilter( boolean b ) {

      _use_filter = b;
      
      if (b) {
	  _filter_vec = new Vector();
	  _filter_vec.addElement( new String( "fuck" ) );
	  _filter_vec.addElement( new String( "fucking" ) );
	  _filter_vec.addElement( new String( "shit" ) );
	  _filter_vec.addElement( new String( "damn" ) );
	  _filter_vec.addElement( new String( "cunt" ) );
	  _filter_vec.addElement( new String( "bitch" ) );
      }
      
  }

  public GuestbookFilter( boolean b, String filter_list ) {

      _use_filter = b;

      _filter_vec = new Vector();

      String delim_list = ",";
      StringTokenizer st = new StringTokenizer(filter_list, delim_list);      

      while (st.hasMoreTokens()) {
	  String token = st.nextToken();

	  // Remove any leading and ending quotes
	  if ( token.charAt( 0 ) == '"' ) 
	      token = token.substring(1);
	  if ( token.charAt( token.length()-1 ) == '"' ) 
	      token = token.substring( 0, token.length()-1 );

	  // make sure to add the space back
	  _filter_vec.addElement( token );
      }

  }

  public String filterWord( String w ) {
      String ret_string = w;

      // make everything lowercase to ease comparison and use this
      // string for comparison
      String tmp_string = w.toLowerCase();
      
      int i;
      for (i=0; i<_filter_vec.size(); i++) {
	  if ( tmp_string.equals( (String)_filter_vec.elementAt( i ) ) ) {
	      ret_string = "xxxxxx";
	      break;
	  }
      }
      
      return ret_string;
  }

  public String filterBadwords(String message) {
      
      if (message == null) 
	  return "";

      if (_use_filter) {

	  String filtered_message = "";

	  // create the StringTokenizer with a decent set of
	  // "delimiters" to try and separate the words from
	  // everything else.  Characters like ' ', ',', ':', ';',
	  // '/', '\', '[', ']', '(', ')', '{', '}'
	  //
	  // NOTE: make sure an escape-out the '\' character
	  //
	  // Also, we need to have the tokenizer treat the delimiters
	  // as tokens so we can always make sure to put back the
	  // delimiter into the output string.
	  String delim_list = " .,:;@!?#$%^&*+-_=|\\/[](){}";
	  StringTokenizer st = new StringTokenizer(message, delim_list, true);

	  while (st.hasMoreTokens()) {
	      String token = st.nextToken();

	      // In this version, it's not clear whether sending the
	      // single character delimiters into the filterWord
	      // function will be more expensize than doing an initial
	      // check right here.  For the time being, the latter
	      // will be done.
	      
	      // do a quick check if the token has a length of 1 to
	      // see if it's a delimiter
	      if ( token.length() == 1 &&
		   (token.charAt( 0 ) == ' ' || token.charAt( 0 ) == '.' ||
		    token.charAt( 0 ) == ',' || token.charAt( 0 ) == ':' ||
		    token.charAt( 0 ) == ';' || token.charAt( 0 ) == '@' ||
		    token.charAt( 0 ) == '!' || token.charAt( 0 ) == '?' ||
		    token.charAt( 0 ) == '#' || token.charAt( 0 ) == '$' ||
		    token.charAt( 0 ) == '%' || token.charAt( 0 ) == '^' ||
		    token.charAt( 0 ) == '&' || token.charAt( 0 ) == '*' ||
		    token.charAt( 0 ) == '+' || token.charAt( 0 ) == '-' ||
		    token.charAt( 0 ) == '_' || token.charAt( 0 ) == '=' ||
		    token.charAt( 0 ) == '|' || token.charAt( 0 ) == '\\' ||
		    token.charAt( 0 ) == '/' || token.charAt( 0 ) == '[' ||
		    token.charAt( 0 ) == ']' || token.charAt( 0 ) == '(' ||
		    token.charAt( 0 ) == ')' || token.charAt( 0 ) == '{' ||
		    token.charAt( 0 ) == '}') ) {
		  
		  // just append the delimiter
		  filtered_message += token;

	      }
	      else {
		  // filter the word and append
		  filtered_message += filterWord( token );
	      }
	  }

	  return filtered_message;  // no filtering being done yet

      }
      else {
	  return message;
      }
  }

  private Vector  _filter_vec;
  private boolean _use_filter = false;

} 





Copyright 1998-2008 Alvin Alexander
All Rights Reserved.
 
devdaily.com is based in louisville, kentucky, and this web site is hosted by godaddy.com