View Javadoc
1   package com.foxinmy.weixin4j.api;
2   
3   import java.util.ResourceBundle;
4   import java.util.regex.Matcher;
5   import java.util.regex.Pattern;
6   
7   import com.foxinmy.weixin4j.http.weixin.WeixinRequestExecutor;
8   
9   /**
10   * API基础
11   *
12   * @className BaseApi
13   * @author jinyu(foxinmy@gmail.com)
14   * @date 2014年9月26日
15   * @since JDK 1.6
16   * @see <a href="http://mp.weixin.qq.com/wiki/index.php">微信公众平台API文档</a>
17   * @see <a href="http://qydev.weixin.qq.com/wiki/index.php">微信企业号API文档</a>
18   */
19  public abstract class BaseApi {
20  
21  	protected final WeixinRequestExecutor weixinExecutor;
22  
23  	private final  Pattern uriPattern = Pattern.compile("(\\{[^\\}]*\\})");
24  
25  	public BaseApi() {
26  		this.weixinExecutor = new WeixinRequestExecutor();
27  	}
28  
29  	protected abstract ResourceBundle weixinBundle();
30  
31  	protected String getRequestUri(String key) {
32  		String url = weixinBundle().getString(key);
33  		Matcher m = uriPattern.matcher(url);
34  		StringBuffer sb = new StringBuffer();
35  		String sub = null;
36  		while (m.find()) {
37  			sub = m.group();
38  			m.appendReplacement(sb,
39  					getRequestUri(sub.substring(1, sub.length() - 1)));
40  		}
41  		m.appendTail(sb);
42  		return sb.toString();
43  	}
44  }