001/* 
002    Licensed to the Apache Software Foundation (ASF) under one
003    or more contributor license agreements.  See the NOTICE file
004    distributed with this work for additional information
005    regarding copyright ownership.  The ASF licenses this file
006    to you under the Apache License, Version 2.0 (the
007    "License"); you may not use this file except in compliance
008    with the License.  You may obtain a copy of the License at
009
010       http://www.apache.org/licenses/LICENSE-2.0
011
012    Unless required by applicable law or agreed to in writing,
013    software distributed under the License is distributed on an
014    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015    KIND, either express or implied.  See the License for the
016    specific language governing permissions and limitations
017    under the License.  
018 */
019package org.apache.wiki.filters;
020
021import java.io.BufferedReader;
022import java.io.IOException;
023import java.io.InputStream;
024import java.io.InputStreamReader;
025import java.util.ArrayList;
026import java.util.List;
027
028import org.apache.commons.io.IOUtils;
029import org.apache.log4j.Logger;
030import org.apache.wiki.WikiContext;
031import org.apache.wiki.api.filters.BasicPageFilter;
032import org.apache.wiki.util.TextUtil;
033
034/**
035 *  This class is an example of how to have a simple filter.  It removes
036 *  all nasty words located at <code>profanity.properties</code> file, inside 
037 *  <code>org/apache/wiki/filters</code> package. The search of profanities
038 *  is case unsensitive.
039 *
040 */
041public class ProfanityFilter extends BasicPageFilter {
042    
043    private static final Logger log = Logger.getLogger( ProfanityFilter.class );
044    
045    private static final String PROPERTYFILE = "org/apache/wiki/filters/profanity.properties";
046    private static String[] c_profanities = new String[0];
047    
048    static {
049        BufferedReader br = null;
050        InputStream in = null;
051        try {
052            ClassLoader loader = ProfanityFilter.class.getClassLoader();
053            in = loader.getResourceAsStream( PROPERTYFILE );
054            
055            if( in == null ) {
056                throw new IOException( "No property file found! (Check the installation, it should be there.)" );
057            }
058            
059            br = new BufferedReader( new InputStreamReader( in ) );
060            List< String > profs = new ArrayList< String >();
061            
062            String str;
063            while ( ( str = br.readLine() ) != null ) {
064                if( str.length() > 0 && !str.startsWith( "#" ) ) { // allow comments on profanities file
065                    profs.add( str );
066                }
067            }
068            c_profanities = profs.toArray( new String[0] );
069        } catch( IOException e ) {
070            log.error( "Unable to load profanities from " + PROPERTYFILE, e );
071        } catch( Exception e ) {
072            log.error( "Unable to initialize Profanity Filter", e );
073        } finally {
074            IOUtils.closeQuietly( br );
075            IOUtils.closeQuietly( in );
076        }
077    }
078
079    /**
080     *  {@inheritDoc}
081     */
082    public String preTranslate( WikiContext context, String content ) {
083        for( int i = 0; i < c_profanities.length; i++ ) {
084            String word = c_profanities[ i ];
085            String replacement = word.charAt( 0 ) + "*" + word.charAt( word.length() - 1 );
086
087            content = TextUtil.replaceStringCaseUnsensitive( content, word, replacement );
088        }
089
090        return content;
091    }
092}