View Javadoc

1   /*
2    * Copyright 2004-2008 the Seasar Foundation and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.seasar.cubby.util;
17  
18  import java.io.UnsupportedEncodingException;
19  import java.net.URLEncoder;
20  
21  import org.seasar.framework.exception.IORuntimeException;
22  
23  /**
24   * パラメータ文字列を作成します。
25   * <p>
26   * パラメータ名、値ともURLエンコードされます。デフォルトのエンコードはUTF-8です。
27   * 
28   * <pre>
29   * QueryStringBuilder query = new QueryStringBuilder();
30   * query.addParam(&quot;p1&quot;, &quot;v1&quot;);
31   * query.addParam(&quot;p2&quot;, null);
32   * query.addParam(&quot;p3&quot;, new String[] { &quot;v2&quot;, &quot;v3&quot; });
33   * assertEquals(&quot;p1=v1&amp;p2=&amp;p3=v2&amp;p3=v3&quot;, query.toString());
34   * </pre>
35   * 
36   * @author agata
37   * @since 1.0.0
38   */
39  public class QueryStringBuilder {
40  
41  	/**
42  	 * パラメータ文字列
43  	 */
44  	private StringBuilder queryString = new StringBuilder();
45  
46  	/**
47  	 * エンコード
48  	 */
49  	private String encode = "UTF-8";
50  
51  	/** 
52  	 * URI部分
53  	 */
54  	private final String baseUri;
55  
56  	/**
57  	 * URI部分なしでインスタンスを生成します。
58  	 * <p>
59  	 * {@code QueryStringBuilder#toString()}が呼び出された時に、URI部分は付加されません。
60  	 * </p>
61  	 */
62  	public QueryStringBuilder() {
63  		this(null);
64  	}
65  
66  	/**
67  	 * URI部分を指定してインスタンスを生成します。
68  	 * <p>
69  	 * {@code QueryStringBuilder#toString()}が呼び出された時に、URI部分と「?」が付加されます。
70  	 * </p>
71  	 * @param baseUri URI部分
72  	 */
73  	public QueryStringBuilder(String baseUrl) {
74  		this.baseUri = baseUrl;
75  	}
76  
77  	/**
78  	 * エンコードをセットします。
79  	 * 
80  	 * @param encode
81  	 */
82  	public void setEncode(final String encode) {
83  		this.encode = encode;
84  	}
85  
86  	/**
87  	 * パラメータを追加します。
88  	 * 
89  	 * @param name
90  	 *            パラメータ名
91  	 * @param value
92  	 *            値。配列の場合、要素数分パラメータが追加されます。
93  	 */
94  	public void addParam(final String name, final Object value) {
95  		if (value != null && value.getClass().isArray()) {
96  			final Object[] values = (Object[]) value;
97  			for (final Object v : values) {
98  				appendParams(name, v);
99  			}
100 		} else {
101 			appendParams(name, value);
102 		}
103 	}
104 
105 	/**
106 	 * パラメータ文字列を取得します。
107 	 */
108 	@Override
109 	public String toString() {
110 		if (this.baseUri == null) {
111 			return queryString.toString();
112 		} else {
113 			StringBuilder baseUrlBuf = new StringBuilder(this.baseUri);
114 			if (baseUrlBuf.indexOf("?") == -1) {
115 				baseUrlBuf.append("?");
116 			} else if (queryString.indexOf("?") < queryString.length()) {
117 				baseUrlBuf.append("&");
118 			}
119 			return baseUrlBuf.toString() + queryString.toString();
120 		}
121 	}
122 
123 	/**
124 	 * パラメータ文字列を追加します。
125 	 * 
126 	 * @param name
127 	 *            パラメータ名
128 	 * @param value
129 	 *            値
130 	 */
131 	private void appendParams(final String name, final Object value) {
132 		if (queryString.length() > 0) {
133 			queryString.append("&");
134 		}
135 		try {
136 			queryString.append(URLEncoder.encode(name, encode));
137 			queryString.append("=");
138 			if (value != null) {
139 				queryString.append(URLEncoder.encode(value.toString(), encode));
140 			}
141 		} catch (final UnsupportedEncodingException e) {
142 			throw new IORuntimeException(e);
143 		}
144 	}
145 }