码迷,mamicode.com
首页 > 其他好文 > 详细

Filter过滤链条

时间:2014-05-12 12:08:37      阅读:249      评论:0      收藏:0      [点我收藏+]

标签:style   blog   class   code   java   c   

Filter.java

bubuko.com,布布扣
package com.bjsxt.dp.filter;

public interface Filter {
    String doFilter(String str);
}
bubuko.com,布布扣

SesitiveFilter.java

bubuko.com,布布扣
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;
    }
}
bubuko.com,布布扣

 

FaceFilter.java

bubuko.com,布布扣
package com.bjsxt.dp.filter;

public class FaceFilter implements Filter {
    @Override
    public String doFilter(String str) {
        return str.replace(":)", "^V^");
    }
}
bubuko.com,布布扣

 

HTMLFilter.java

 

bubuko.com,布布扣
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;
    }

}
bubuko.com,布布扣

 

 

 

FilterChain.java

bubuko.com,布布扣
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;
    }
}
bubuko.com,布布扣

MsgProcessor.java

bubuko.com,布布扣
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);
    }
}
bubuko.com,布布扣

Main.java

bubuko.com,布布扣
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);
    }

}
bubuko.com,布布扣

各类之间的关系图:

bubuko.com,布布扣

 

Filter过滤链条,布布扣,bubuko.com

Filter过滤链条

标签:style   blog   class   code   java   c   

原文地址:http://www.cnblogs.com/jianfengyun/p/3720334.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!