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 org.apache.log4j.Logger;
022import org.apache.wiki.api.core.Context;
023import org.apache.wiki.api.filters.BasePageFilter;
024import org.apache.wiki.util.TextUtil;
025
026import java.io.BufferedReader;
027import java.io.IOException;
028import java.io.InputStream;
029import java.io.InputStreamReader;
030import java.util.ArrayList;
031import java.util.List;
032
033/**
034 *  This class is an example of how to have a simple filter.  It removes
035 *  all nasty words located at <code>profanity.properties</code> file, inside 
036 *  <code>org/apache/wiki/filters</code> package. The search of profanities
037 *  is case unsensitive.
038 *
039 */
040public class ProfanityFilter extends BasePageFilter {
041    
042    private static final Logger log = Logger.getLogger( ProfanityFilter.class );
043    
044    private static final String PROPERTYFILE = "org/apache/wiki/filters/profanity.properties";
045    private static String[] c_profanities = new String[0];
046    
047    static {
048        final ClassLoader loader = ProfanityFilter.class.getClassLoader();
049        try( final InputStream in = loader.getResourceAsStream( PROPERTYFILE ) ) {
050            if( in == null ) {
051                throw new IOException( "No property file found! (Check the installation, it should be there.)" );
052            }
053            try( final BufferedReader br =  new BufferedReader( new InputStreamReader( in ) ) ) {
054                final List< String > profs = new ArrayList<>();
055
056                String str;
057                while ( ( str = br.readLine() ) != null ) {
058                    if( str.length() > 0 && !str.startsWith( "#" ) ) { // allow comments on profanities file
059                        profs.add( str );
060                    }
061                }
062                c_profanities = profs.toArray( new String[0] );
063            }
064        } catch( final IOException e ) {
065            log.error( "Unable to load profanities from " + PROPERTYFILE, e );
066        } catch( final Exception e ) {
067            log.error( "Unable to initialize Profanity Filter", e );
068        }
069    }
070
071    /**
072     *  {@inheritDoc}
073     */
074    @Override
075    public String preTranslate( final Context context, String content ) {
076        for( final String word : c_profanities ) {
077            final String replacement = word.charAt( 0 ) + "*" + word.charAt( word.length() - 1 );
078            content = TextUtil.replaceStringCaseUnsensitive( content, word, replacement );
079        }
080
081        return content;
082    }
083
084}