http://www.flyne.org/article/693
-
import java.util.ArrayList;
-
import java.util.Iterator;
-
import java.util.List;
-
-
interface Filter {
-
String doFilter(String str, FilterChain chain);
-
}
-
-
class HtmlFilter implements Filter {
-
public String doFilter(String msg, FilterChain chain) {
-
String r = msg;
-
-
r = r.replace("<", "<").replace(">", ">");
-
r = chain.doFilter(r, chain);
-
return r;
-
}
-
}
-
-
class SensitiveFilter implements Filter {
-
public String doFilter(String msg, FilterChain chain) {
-
String r = msg;
-
-
r = r.replace("敏感", "").replace("被就业", "就业");
-
-
r = chain.doFilter(r, chain);
-
return r;
-
}
-
}
-
-
class FilterChain implements Filter {
-
-
public List<Filter> filters = new ArrayList<Filter>();
-
-
public FilterChain addFilter(Filter f) {
-
filters.add(f);
-
return this;
-
}
-
-
Iterator it = null;
-
-
public String doFilter(String msg, FilterChain chain) {
-
if (it == null) {
-
it = filters.iterator();
-
}
-
String result = msg;
-
-
if (it.hasNext()) {
-
Filter f = (Filter) it.next();
-
result = (String) f.doFilter(msg, chain);
-
}
-
return result;
-
}
-
}
-
-
class MsgProcessor {
-
private String msg;
-
private FilterChain chain;
-
-
public MsgProcessor(String msg, FilterChain chain) {
-
this.msg = msg;
-
this.chain = chain;
-
}
-
-
public String process() {
-
return chain.doFilter(msg, chain);
-
}
-
}
-
-
public class MainClass {
-
public static void main(String[] args) {
-
-
String msg = "被就业了:),敏感信息,<script>";
-
-
-
FilterChain chain = new FilterChain();
-
chain.addFilter(new HtmlFilter()).addFilter(new SensitiveFilter());
-
-
MsgProcessor mp = new MsgProcessor(msg, chain);
-
String r = mp.process();
-
-
System.out.println(r);
-
}
-
}
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29254281/viewspace-2135694/
,如需转载,请注明出处,否则将追究法律责任。