首页 > 代码库 > Filter过滤链条
Filter过滤链条
Filter.java
package com.bjsxt.dp.filter; public interface Filter { String doFilter(String str); }
SesitiveFilter.java
package com.bjsxt.dp.filter; public class SesitiveFilter implements Filter { @Override public String doFilter(String str) { // process the sensitive words String r = str.replace("被就业", "就业").replace("敏感", ""); return r; } }
FaceFilter.java
package com.bjsxt.dp.filter; public class FaceFilter implements Filter { @Override public String doFilter(String str) { return str.replace(":)", "^V^"); } }
HTMLFilter.java
package com.bjsxt.dp.filter; public class HTMLFilter implements Filter { @Override public String doFilter(String str) { //process the html tag <> String r = str.replace(‘<‘, ‘[‘) .replace(‘>‘, ‘]‘); return r; } }
FilterChain.java
package com.bjsxt.dp.filter; import java.util.ArrayList; import java.util.List; public class FilterChain implements Filter { List<Filter> filters = new ArrayList<Filter>(); public FilterChain addFilter(Filter f) { this.filters.add(f); return this; } public String doFilter(String str) { String r = str; for(Filter f: filters) { r = f.doFilter(r); } return r; } }
MsgProcessor.java
package com.bjsxt.dp.filter; public class MsgProcessor { private String msg; FilterChain fc; public FilterChain getFc() { return fc; } public void setFc(FilterChain fc) { this.fc = fc; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public String process() { return fc.doFilter(msg); } }
Main.java
package com.bjsxt.dp.filter; public class Main { /** * @author admin */ public static void main(String[] args) { String msg = "大家好:),<script>,敏感,被就业,网络授课没感觉,因为看不见大家伙儿"; MsgProcessor mp = new MsgProcessor(); mp.setMsg(msg); FilterChain fc = new FilterChain(); fc.addFilter(new HTMLFilter()).addFilter(new SesitiveFilter()); FilterChain fc2 = new FilterChain(); fc2.addFilter(new FaceFilter()); fc.addFilter(fc2); mp.setFc(fc); String result = mp.process(); System.out.println(result); } }
各类之间的关系图:
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。