Java,责任链模式:replaceAll报错Unclosed group near index 1

         闲着没事儿做,写了一个 责任链模式 的demo,发现跟之前看过一个框架 Apache Commons Chain功能很像,当然从框架名应该也能看出来。

主要就是链条类和责任类。我的代码是模拟web功能里面的filterChain,通过多个filter来检验request的合理性。不多废话,上代码。

Request类:

public class Request {
	private String content;

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}
	
	
}
Response类:

public class Response {
	
	private String answer;

	public String getAnswer() {
		return answer;
	}

	public void setAnswer(String answer) {
		this.answer = answer;
	}
	
}

Filter类:责任类

public interface Filter {
	
	void doFilter(Request request,Response response,FilterChain chain);
	
}
HtmlFilter类:用来将request中content内容里的“(”替换成“[”

public class HtmlFilter implements Filter {

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.edu.bjut.filter3.Filter#doFilter(cn.edu.bjut.filter3.Request,
	 * cn.edu.bjut.filter3.Response, cn.edu.bjut.filter3.FilterChain)
	 * 将request里的content中所有的(替换成[,所有的)替换成]
	 */
	@Override
	public void doFilter(Request request, Response response, FilterChain chain) {
		// TODO Auto-generated method stub
		String content = request.getContent();
		String tempContent = content.replaceAll("\\(", "[");//注意此处传入参数
		String tempContent1 = tempContent.replaceAll("\\)", "]");//注意此处传入参数
		if (chain.getIndex() == 0)
			response.setAnswer(tempContent1);
		else
			request.setContent(tempContent1);
		chain.doFilter(request, response);
	}

}
SensitiveFilter类:用来将request中content内容里的“敏感”替换成“和谐”

public class SensitiveFilter implements Filter {

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.edu.bjut.filter3.Filter#doFilter(cn.edu.bjut.filter3.Request,
	 * cn.edu.bjut.filter3.Response, cn.edu.bjut.filter3.FilterChain)
	 * 将request里面的“敏感”换成“和谐”
	 */
	@Override
	public void doFilter(Request request, Response response, FilterChain chain) {
		String tempContent1 = request.getContent().replaceAll("敏感", "和谐");
		if (chain.getIndex() == 0)
			response.setAnswer(tempContent1);
		else
			request.setContent(tempContent1);
		chain.doFilter(request, response);
	}

}

FilterChain类:即链条类,可以将各责任类加入到该链条中
public class FilterChain {

	List filters = new LinkedList<>();
	int index = 0;

	public int getIndex() {
		return index;
	}

	public void setIndex(int index) {
		this.index = index;
	}

	public List getFilters() {
		return filters;
	}

	public void setFilters(List filters) {
		this.filters = filters;
	}

	public FilterChain addFilter(Filter filter) {
		filters.add(filter);
		index++;
		return this;
	}

	public void removeFilter(Filter filter) {
		filters.remove(filter);
		index--;
	}

	void doFilter(Request request, Response response) {
		index--;
		if (index >= 0) {
			Filter filter = filters.get(index);
			filter.doFilter(request, response, this);
		}

	}

	public static void main(String[] args) {
		FilterChain chain = new FilterChain();
		Filter htmlFilter = new HtmlFilter();
		Filter sensitiveFilter = new SensitiveFilter();
		chain.addFilter(htmlFilter).addFilter(sensitiveFilter);
		Request request = new Request();
		String content = "我们的祖国是花园,花园的花朵不敏感,(哇哈哈),(娃哈哈)";
		request.setContent(content);
		Response response = new Response();
		chain.doFilter(request, response);
		String answer = response.getAnswer();
		System.out.println(answer);
	}

}


注意HtmlFilter里面调用replaceAll方法时,可能出的问题,要是写成String tempContent = content.replaceAll("(", "[");就会提示如下异常:

Exception in thread "main" java.util.regex.PatternSyntaxException: Unclosed group near index 1
(
 ^
	at java.util.regex.Pattern.error(Pattern.java:1924)
	at java.util.regex.Pattern.accept(Pattern.java:1782)
	at java.util.regex.Pattern.group0(Pattern.java:2857)
	at java.util.regex.Pattern.sequence(Pattern.java:2018)
	at java.util.regex.Pattern.expr(Pattern.java:1964)
	at java.util.regex.Pattern.compile(Pattern.java:1665)
	at java.util.regex.Pattern.(Pattern.java:1337)
	at java.util.regex.Pattern.compile(Pattern.java:1022)
	at java.lang.String.replaceAll(String.java:2210)
	at cn.edu.bjut.filter3.HtmlFilter.doFilter(HtmlFilter.java:16)
	at cn.edu.bjut.filter3.FilterChain.doFilter(FilterChain.java:44)
	at cn.edu.bjut.filter3.SensitiveFilter.doFilter(SensitiveFilter.java:19)
	at cn.edu.bjut.filter3.FilterChain.doFilter(FilterChain.java:44)
	at cn.edu.bjut.filter3.FilterChain.main(FilterChain.java:58)

只看到是正则表达式里的Pattern类异常,不知原因,这种情况,我都是直接搜索解答。最简单的改法是像我上面那种写法。

下段是摘自别人的博客:

在正则表达式中,有个“捕获组”的概念,其使用了小括号;因此分析,当正则表达式解析到左括号时,没有发现对应的右括号,从而报错。

不夺其美,特将博主的博客地址贴下:

http://blog.csdn.net/wangjunjun2008/article/details/39479423



你可能感兴趣的:(Java)