📄 profanity.java
字号:
/** * $RCSfile: Profanity.java,v $ * $Revision: 1.3 $ * $Date: 2002/07/16 16:45:12 $ * * Copyright (C) 1999-2001 CoolServlets, Inc. All rights reserved. * * This software is the proprietary information of CoolServlets, Inc. * Use is subject to license terms. */package com.jivesoftware.forum.filter;import java.util.*;import java.text.BreakIterator;import com.jivesoftware.forum.*;/** * A ForumMessageFilter that filters out user-specified profanity. */public class Profanity extends ForumMessageFilter { /** * Map of all the bad words to filter. */ private Map words = new HashMap(); /** * Comma delimited list of words to filter. */ private String wordList = null; /** * A char array of '*' characters that can be used to replace profanity. */ private static char[] filler = new char[255]; static { for (int i=0; i<filler.length; i++) { filler[i] = '*'; } } /** * Clones a new filter that will have the same properties and that * will wrap around the specified message. * * @param message the ForumMessage to wrap the new filter around. */ public ForumMessageFilter clone(ForumMessage message){ Profanity filter = new Profanity(); // Set all properties on the new filter. filter.wordList = wordList; filter.applyProperties(); filter.message = message; return filter; } public boolean isCacheable() { return true; } /** * Returns the comma delimited list of words that will be filtered. * * @return the list of words to be filtered. */ public String getWordList() { return wordList; } /** * Sets the list of words to be filtered. Each word must seperated by a * comma. * * @param wordList comma delimited list of words to filter. */ public void setWordList(String wordList) { this.wordList = wordList; } /** * <b>Overloaded</b> to return the subject of the message with profanity * filtered out. */ public String getSubject() { return filterProfanity(message.getSubject()); } /** * <b>Overloaded</b> to return the body of the message with profanity * filtered out. */ public String getBody() { return filterProfanity(message.getBody()); } /** * Applies new property values so the filter is ready for futher processing. */ private void applyProperties() { words.clear(); if (wordList == null || wordList.equals("")) { return; } StringTokenizer tokens = new StringTokenizer(wordList,","); while (tokens.hasMoreTokens()) { words.put(tokens.nextToken().toLowerCase().trim(), ""); } } /** * Returns the string with all profanity converted to '*' characters. * * @param str the String to filter. * @return the String with profanity removed. */ private String filterProfanity(String str) { // Check to see if the string is null or zero-length if (str == null || "".equals(str) || words.size() == 0) { return str; } StringBuffer result = new StringBuffer(str.length()); BreakIterator wb = BreakIterator.getWordInstance(); wb.setText(str); int last = 0; int current = wb.next(); // Accumulate the letters in a word into a StringBuffer. StringBuffer tempBuffer = new StringBuffer(); // Loop through all words in the text. while (current != BreakIterator.DONE) { // Loop through all chracters in the word. for (int i=last; i<current; i++) { char c = str.charAt(i); if (!Character.isLetter(c)) { // First, flush out the temp buffer since we found a non-character. if (tempBuffer.length() > 0) { String temp = tempBuffer.toString(); // If the word is in the profanity list, get rid of it. if (words.containsKey(temp.toLowerCase())) { result.append(filler, 0, temp.length()); } else { result.append(temp); } tempBuffer.setLength(0); } // Now append new character. result.append(c); } // Otherwise, this is a character in a word, so append it to the temp buffer. else { tempBuffer.append(c); } } // Flush out buffer. if (tempBuffer.length() > 0) { String temp = tempBuffer.toString(); // If the word is in the profanity list, get rid of it. if (words.containsKey(temp.toLowerCase())) { result.append(filler, 0, temp.length()); } else { result.append(temp); } tempBuffer.setLength(0); } last = current; current = wb.next(); } return result.toString(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -