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 056 // allow comments on profanities file 057 c_profanities = br.lines().filter(str -> !str.isEmpty() && !str.startsWith("#")).toArray(String[]::new); 058 } 059 } catch( final IOException e ) { 060 LOG.error( "Unable to load profanities from " + PROPERTYFILE, e ); 061 } catch( final Exception e ) { 062 LOG.error( "Unable to initialize Profanity Filter", e ); 063 } 064 } 065 066 /** 067 * {@inheritDoc} 068 */ 069 @Override 070 public String preTranslate( final Context context, String content ) { 071 for( final String word : c_profanities ) { 072 final String replacement = word.charAt( 0 ) + "*" + word.charAt( word.length() - 1 ); 073 content = TextUtil.replaceStringCaseUnsensitive( content, word, replacement ); 074 } 075 076 return content; 077 } 078 079}