过滤词组件

[代码] 过滤词完结 public class WordFilterImpl implements IWordFilter {

	private final ConcurrentMap dictionary; //过滤词字典集结
	private String maskCharacter = "*";                      //替代字符
	p http://www.superkp111.com rivate String wordsFile;  //过滤词文件
	private Map maskMap;              //替代字符串
	private boolean isOpenResultMode = false;                //能否打开效果核算
	
	public WordFilterImpl() {
		dictionary = new ConcurrentHashMap();
		maskMap    = new HashMap();
	}
	
	public WordFilterImpl(String wordsFile) {
		this();
		this.wordsFile = wordsFile;
		init();
	}
	
	private void init(){
		//初步监控过滤词文件改动
		new FileWatchDog(this.wordsFile).start();
	}
	
	private void initWordsFromFile(){
		if ( null != wordsFile ) {
			File file = new File(wordsFile);
			if (!file.exists())
				return;
			
			this.dictionary.clear();
			
			BufferedReader br = null;
			String line;
			try {
				br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "utf-8"));
				while( (line=br.readLine())!=null ){
					addExpression(line);
				}
			} catch (FileNotFoundException e) {
			} catch (IOException e) {
			}finally{
				if (null != br) {
					try {
						br.close();
					} catch (IOException e) {
					}
				}
			}
		}
	}

	public void setOpenResultMode(boolean isOpenResultMode) {
		this.isOpenResultMode = isOpenResultMode;
	}
	
	public boolean isOpenResultMode(){
		return this.isOpenResultMode;
	}
	
	public void addExpression(String word) {
		this.dictionary.putIfAbsent(word, Pattern.compile(word, Pattern.CASE_INSENSITIVE));
	}

	public void clearAllExpression(){
		this.dictionary.clear();
	}
	
	public void clearExpression(String word){
		this.dictionary.remove(word);
	}
	
	public FilteredMessage apply(String message) {
		FilteredMessage filteredMessage = new FilteredMessage();
		if ( this.dictionary.isEmpty() ) {
			filteredMessage.setMessage(message);
			return filteredMessage;
		}
		
		Iterator wordsIter = this.dictionary.values().iterator();
		Pattern pattern;
		Matcher match;
		
		StringBuilder buffer = new StringBuilder(message);
		boolean isIsOccurred = false;
		
		Map occurredResult = new HashMap();
		String occurredWord;
		
		while ( wordsIter.hasNext() ) {
			pattern = wordsIter.next();
			match = pattern.matcher(message);
			
			while ( match.find() ) {
				maskBadWord(buffer, match.start(), match.end());
				isIsOccurred = true;
				
				//核算磕碰效果,建议不打开,除非特殊需要
				if ( isOpenResultMode ) {
					occurredWord = match.group();
					record(occurredResult, occurredWord);
				}
			}
		}
		
		filteredMessage.setIsOccurred(isIsOccurred);
		filteredMessage.setMessage(buffer.toString());
		filteredMessage.setOccurredResult(occurredResult);
		
		return filteredMessage;
	}
	
	//核算磕碰效果
	private void record(Map occurredResult, String word){
		if ( null==word || "".equals(word) ) {
			return;
		}
		if ( !occurredResult.containsKey(word) ) {
			occurredResult.put(word, 1);
		}else{
			Integer count = occurredResult.get(word);
			occurredResult.put(word, count 1);
		}
	}
	
	private void maskBadWord(StringBuilder str, int startPos, int endPos) {
		str.replace(startPos, endPos, getStringMask(endPos - startPos));
	}

	private String getStringMask(int len) {
		if ( this.maskMap.containsKey(len) ) {
			return this.maskMap.get(len);
		}
		StringBuilder buf = new StringBuilder();
		for (int j = 0; j < len; j  ) {
			buf.append(this.maskCharacter);
		}
		this.maskMap.put(len, buf.toString());
		return buf.toString();
	}
	

	class FileWatchDog extends Thread {

		public static final long DEFAULT_DELAY = 60000;

		protected String filename;

		protected long delay = DEFAULT_DELAY;

		File file;
		long lastModif = 0;
		boolean warnedAlready = false;
		boolean interrupted = false;

		protected FileWatchDog(String filename) {
			this(filename, DEFAULT_DELAY);
		}

		protected FileWatchDog(String filename, long delay) {
			super("FileWatchdog");
			this.filename = filename;
			file = new File(filename);
			this.delay = delay;
			setDaemon(true);
			checkAndConfigure();
		}

		public void setDelay(long delay) {
			this.delay = delay;
		}

		protected void doOnChange(){
			initWordsFromFile();
		}

		protected void checkAndConfigure() {
			boolean fileExists;
			try {
				fileExists = file.exists();
			} catch (SecurityException e) {
				interrupted = true; // there is no point in continuing
				return;
			}

			if (fileExists) {
				long l = file.lastModified(); // this can also throw a
				if (l > lastModif) { // however, if we reached this point this
					lastModif = l; // is very unlikely.
					doOnChange();
					warnedAlready = false;
				}
			} else {
				if (!warnedAlready) {
					warnedAlready = true;
				}
			}
		}

		public void run() {
			while (!interrupted) {
				try {
					Thread.sleep(delay);
				} catch (InterruptedException e) {
				}
				checkAndConfigure();
			}
		}
	}
} http://www.haofapiao.com 
posted @ 2013-04-19 05:35  chinadiy197601  阅读(205)  评论(0编辑  收藏  举报