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