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.logging.log4j.LogManager;
022import org.apache.logging.log4j.Logger;
023import org.apache.wiki.api.core.Context;
024import org.apache.wiki.api.filters.BasePageFilter;
025import org.apache.wiki.util.TextUtil;
026
027import java.io.BufferedReader;
028import java.io.IOException;
029import java.io.InputStream;
030import java.io.InputStreamReader;
031import java.util.ArrayList;
032import java.util.List;
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 BasePageFilter {
042    
043    private static final Logger log = LogManager.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        final ClassLoader loader = ProfanityFilter.class.getClassLoader();
050        try( final InputStream in = loader.getResourceAsStream( PROPERTYFILE ) ) {
051            if( in == null ) {
052                throw new IOException( "No property file found! (Check the installation, it should be there.)" );
053            }
054            try( final BufferedReader br =  new BufferedReader( new InputStreamReader( in ) ) ) {
055                final List< String > profs = new ArrayList<>();
056
057                String str;
058                while ( ( str = br.readLine() ) != null ) {
059                    if( !str.isEmpty() && !str.startsWith( "#" ) ) { // allow comments on profanities file
060                        profs.add( str );
061                    }
062                }
063                c_profanities = profs.toArray( new String[0] );
064            }
065        } catch( final IOException e ) {
066            log.error( "Unable to load profanities from " + PROPERTYFILE, e );
067        } catch( final Exception e ) {
068            log.error( "Unable to initialize Profanity Filter", e );
069        }
070    }
071
072    /**
073     *  {@inheritDoc}
074     */
075    @Override
076    public String preTranslate( final Context context, String content ) {
077        for( final String word : c_profanities ) {
078            final String replacement = word.charAt( 0 ) + "*" + word.charAt( word.length() - 1 );
079            content = TextUtil.replaceStringCaseUnsensitive( content, word, replacement );
080        }
081
082        return content;
083    }
084
085}