You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jspwiki.apache.org by ju...@apache.org on 2008/04/15 01:56:39 UTC
svn commit: r648070 - in /incubator/jspwiki/trunk: ChangeLog
src/com/ecyrd/jspwiki/TextUtil.java
src/com/ecyrd/jspwiki/filters/ProfanityFilter.java
src/com/ecyrd/jspwiki/filters/profanity.properties
tests/com/ecyrd/jspwiki/TextUtilTest.java
Author: juanpablo
Date: Mon Apr 14 16:56:37 2008
New Revision: 648070
URL: http://svn.apache.org/viewvc?rev=648070&view=rev
Log:
* 2.7.0-svn-13
* ProfanityFilter minor improvement: profanities are read from a file
instead of being hard-coded strings.
Added:
incubator/jspwiki/trunk/src/com/ecyrd/jspwiki/filters/profanity.properties (with props)
Modified:
incubator/jspwiki/trunk/ChangeLog
incubator/jspwiki/trunk/src/com/ecyrd/jspwiki/TextUtil.java
incubator/jspwiki/trunk/src/com/ecyrd/jspwiki/filters/ProfanityFilter.java
incubator/jspwiki/trunk/tests/com/ecyrd/jspwiki/TextUtilTest.java
Modified: incubator/jspwiki/trunk/ChangeLog
URL: http://svn.apache.org/viewvc/incubator/jspwiki/trunk/ChangeLog?rev=648070&r1=648069&r2=648070&view=diff
==============================================================================
--- incubator/jspwiki/trunk/ChangeLog (original)
+++ incubator/jspwiki/trunk/ChangeLog Mon Apr 14 16:56:37 2008
@@ -3,10 +3,13 @@
* 2.7.0-svn-13
* [JSPWIKI-178] Patch from Jorge Ferrer to keep the order of search (WEB-INF
- then classpath then file system) of filters.xml
+ then classpath then file system) of filters.xml.
* [JSPWIKI-231] Updated es translation and applied patches from Florian
- Holeczek (de translation) and Harry Metske (nl translation)
+ Holeczek (de translation) and Harry Metske (nl translation).
+
+ * ProfanityFilter minor improvement: profanities are read from a file
+ instead of being hard-coded strings.
2008-04-13 Janne Jalkanen <ja...@apache.org>
Modified: incubator/jspwiki/trunk/src/com/ecyrd/jspwiki/TextUtil.java
URL: http://svn.apache.org/viewvc/incubator/jspwiki/trunk/src/com/ecyrd/jspwiki/TextUtil.java?rev=648070&r1=648069&r2=648070&view=diff
==============================================================================
--- incubator/jspwiki/trunk/src/com/ecyrd/jspwiki/TextUtil.java (original)
+++ incubator/jspwiki/trunk/src/com/ecyrd/jspwiki/TextUtil.java Mon Apr 14 16:56:37 2008
@@ -345,6 +345,36 @@
return buf.toString();
}
+
+ /**
+ * Replaces a string with an other string. Case unsensitive matching is used
+ *
+ * @param orig Original string. Null is safe.
+ * @param src The string to find.
+ * @param dest The string to replace <I>src</I> 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();
+ }
/**
* Parses an integer parameter, returning a default value
Modified: incubator/jspwiki/trunk/src/com/ecyrd/jspwiki/filters/ProfanityFilter.java
URL: http://svn.apache.org/viewvc/incubator/jspwiki/trunk/src/com/ecyrd/jspwiki/filters/ProfanityFilter.java?rev=648070&r1=648069&r2=648070&view=diff
==============================================================================
--- incubator/jspwiki/trunk/src/com/ecyrd/jspwiki/filters/ProfanityFilter.java (original)
+++ incubator/jspwiki/trunk/src/com/ecyrd/jspwiki/filters/ProfanityFilter.java Mon Apr 14 16:56:37 2008
@@ -20,20 +20,65 @@
*/
package com.ecyrd.jspwiki.filters;
-import com.ecyrd.jspwiki.WikiContext;
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.log4j.Logger;
+
import com.ecyrd.jspwiki.TextUtil;
+import com.ecyrd.jspwiki.WikiContext;
/**
- * This class is an example of how to have a simple filter. Not really usable
- * for anything.
+ * This class is an example of how to have a simple filter. It removes
+ * all nasty words located at <code>profanity.properties</code> file, inside
+ * <code>com/ecyrd/jspwiki/filters</code> package. The search of profanities
+ * is case unsensitive.
*
*/
-public class ProfanityFilter
- extends BasicPageFilter
+public class ProfanityFilter extends BasicPageFilter
{
- private static final String[] c_profanities = {
- "fuck",
- "shit" };
+ private static Logger log = Logger.getLogger(ProfanityFilter.class);
+
+ private static final String PROPERTYFILE = "com/ecyrd/jspwiki/filters/profanity.properties";
+ private static String[] c_profanities;
+
+ static
+ {
+ try
+ {
+ ClassLoader loader = ProfanityFilter.class.getClassLoader();
+ InputStream in = loader.getResourceAsStream( PROPERTYFILE );
+
+ if( in == null )
+ {
+ throw new IOException("No property file found! (Check the installation, it should be there.)");
+ }
+
+ BufferedReader br = new BufferedReader( new InputStreamReader( in ) );
+ List l_profs = new ArrayList();
+
+ String str;
+ while ( ( str = br.readLine() ) != null )
+ {
+ if( str.length() > 0 && !str.startsWith( "#" ) ) { // allow comments on profanities file
+ l_profs.add( str );
+ }
+ }
+ c_profanities = ( String[] )l_profs.toArray( new String[ l_profs.size() ] );
+ }
+ catch( IOException e )
+ {
+ log.error( "Unable to load profanities from "+PROPERTYFILE, e );
+ }
+ catch( Exception e )
+ {
+ log.error( "Unable to initialize Profanity Filter", e );
+ }
+ }
public String preTranslate( WikiContext context, String content )
{
@@ -42,7 +87,7 @@
String word = c_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;
Added: incubator/jspwiki/trunk/src/com/ecyrd/jspwiki/filters/profanity.properties
URL: http://svn.apache.org/viewvc/incubator/jspwiki/trunk/src/com/ecyrd/jspwiki/filters/profanity.properties?rev=648070&view=auto
==============================================================================
--- incubator/jspwiki/trunk/src/com/ecyrd/jspwiki/filters/profanity.properties (added)
+++ incubator/jspwiki/trunk/src/com/ecyrd/jspwiki/filters/profanity.properties Mon Apr 14 16:56:37 2008
@@ -0,0 +1,5 @@
+# Feel free to fill this file with your preferred profanities, at the rate of one
+# profanity per line. The match performed against this file is case-unsensitive
+
+fuck
+shit
\ No newline at end of file
Propchange: incubator/jspwiki/trunk/src/com/ecyrd/jspwiki/filters/profanity.properties
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified: incubator/jspwiki/trunk/tests/com/ecyrd/jspwiki/TextUtilTest.java
URL: http://svn.apache.org/viewvc/incubator/jspwiki/trunk/tests/com/ecyrd/jspwiki/TextUtilTest.java?rev=648070&r1=648069&r2=648070&view=diff
==============================================================================
--- incubator/jspwiki/trunk/tests/com/ecyrd/jspwiki/TextUtilTest.java (original)
+++ incubator/jspwiki/trunk/tests/com/ecyrd/jspwiki/TextUtilTest.java Mon Apr 14 16:56:37 2008
@@ -144,6 +144,41 @@
assertEquals( "afafaf", TextUtil.replaceString( text, "b", "f" ) );
}
+
+ public void testReplaceStringCaseUnsensitive1()
+ {
+ String text = "aABcAa";
+
+ assertEquals( "ddBcdd", TextUtil.replaceStringCaseUnsensitive( text, "aa", "dd" ) );
+ }
+
+ public void testReplaceStringCaseUnsensitive2()
+ {
+ String text = "Abcde";
+
+ assertEquals( "fbcde", TextUtil.replaceStringCaseUnsensitive( text, "a", "f" ) );
+ }
+
+ public void testReplaceStringCaseUnsensitive3()
+ {
+ String text = "aBAbab";
+
+ assertEquals( "afAfaf", TextUtil.replaceStringCaseUnsensitive( text, "b", "f" ) );
+ }
+
+ public void testReplaceStringCaseUnsensitive4()
+ {
+ String text = "AaBAcAAfaa";
+
+ assertEquals( "ddBAcddfdd", TextUtil.replaceStringCaseUnsensitive( text, "aa", "dd" ) );
+ }
+
+ public void testReplaceStringCaseUnsensitive5()
+ {
+ String text = "aAaBaCAAafaa";
+
+ assertEquals( "dBaCdfaa", TextUtil.replaceStringCaseUnsensitive( text, "aaa", "d" ) );
+ }
// Pure UNIX.
public void testNormalizePostdata1()