### Eclipse Workspace Patch 1.0 #P JSPWiki Index: src/com/ecyrd/jspwiki/filters/ProfanityFilter.java =================================================================== RCS file: /p/cvs/JSPWiki/src/com/ecyrd/jspwiki/filters/ProfanityFilter.java,v retrieving revision 1.2 diff -u -r1.2 ProfanityFilter.java --- src/com/ecyrd/jspwiki/filters/ProfanityFilter.java 12 May 2004 22:19:10 -0000 1.2 +++ src/com/ecyrd/jspwiki/filters/ProfanityFilter.java 10 Oct 2006 08:38:18 -0000 @@ -1,7 +1,13 @@ package com.ecyrd.jspwiki.filters; -import com.ecyrd.jspwiki.WikiContext; +import java.io.*; +import java.util.ArrayList; + +import javax.servlet.ServletContext; + import com.ecyrd.jspwiki.TextUtil; +import com.ecyrd.jspwiki.WikiContext; +import com.ecyrd.jspwiki.WikiEngine; public class ProfanityFilter extends BasicPageFilter @@ -9,15 +15,77 @@ private static final String[] c_profanities = { "fuck", "shit" }; + + // Could be retrieved from jspwiki.properties + private static final String PROFANITY_TXT = "WEB-INF" + File.separator + "profanity.txt"; public String preTranslate( WikiContext context, String content ) { - for( int i = 0; i < c_profanities.length; i++ ) + FileReader fr = null; + + String prof = null; + + if (context != null) + { + WikiEngine engine = context.getEngine(); + if ( engine != null ) { + ServletContext sc = engine.getServletContext(); + if ( sc != null ) + { + prof = sc.getRealPath(PROFANITY_TXT); + try { + File f = new File( prof ); + if ( f != null ) + { + fr = new FileReader( f ); + } + } + catch ( FileNotFoundException fnfe ) + { + fnfe.printStackTrace(); + } + catch ( Exception e ) + { + e.printStackTrace(); + } + } + } + } + + ArrayList al = new ArrayList(); + String[] profanities = null; + + if ( fr != null ) + { + try + { + BufferedReader in = new BufferedReader( fr ); + String str; + while ( ( str = in.readLine() ) != null ) + { + al.add( str ); + } + in.close(); + + } + catch ( IOException ioe ) + { + ioe.printStackTrace(); + } + profanities = ( String[] ) al.toArray( new String[ al.size() ] ); + + } + else + { + profanities = c_profanities; + } + + for( int i = 0; i < profanities.length; i++ ) { - String word = c_profanities[i]; + String word = profanities[i]; String replacement = word.charAt(0)+"*"+word.charAt(word.length()-1); - content = TextUtil.replaceString( content, word, replacement ); + content = TextUtil.replaceStringCaseUnsensitive( content, word, replacement ); } return content; Index: src/com/ecyrd/jspwiki/TextUtil.java =================================================================== RCS file: /p/cvs/JSPWiki/src/com/ecyrd/jspwiki/TextUtil.java,v retrieving revision 1.44 diff -u -r1.44 TextUtil.java --- src/com/ecyrd/jspwiki/TextUtil.java 1 Aug 2006 11:17:40 -0000 1.44 +++ src/com/ecyrd/jspwiki/TextUtil.java 10 Oct 2006 08:38:18 -0000 @@ -284,6 +284,37 @@ return res.toString(); } + + /** + * Replaces a string with an other string. The search of the string to find is case unsensitive + * + * @param orig Original string. Null is safe. + * @param src The string to find. + * @param dest The string to replace src with. + */ + + public static String replaceStringCaseUnsensitive( String orig, String src, String dest ) + { + if( orig == null ) return null; + + StringBuffer res = new StringBuffer(); + int start, end = 0, last = 0; + + String origCaseUnsn = orig.toLowerCase(); + String srcCaseUnsn = src.toLowerCase(); + + while( (start = origCaseUnsn.indexOf(srcCaseUnsn, end)) != -1 ) + { + res.append( orig.substring( last, start ) ); + res.append( dest ); + end = start+src.length(); + last = start+src.length(); + } + + res.append( orig.substring( end ) ); + + return res.toString(); + } /** * Replaces a part of a string with a new String.