View Javadoc

1   /*
2    * Copyright 2004-2010 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  
17  package org.seasar.cubby.internal.util;
18  
19  import java.io.UnsupportedEncodingException;
20  import java.net.URLEncoder;
21  
22  /**
23   * パラメータ文字列を作成します。
24   * <p>
25   * パラメータ名、値ともURLエンコードされます。デフォルトのエンコードはUTF-8です。
26   * 
27   * <pre>
28   * QueryStringBuilder query = new QueryStringBuilder();
29   * query.addParam(&quot;p1&quot;, &quot;v1&quot;);
30   * query.addParam(&quot;p2&quot;, null);
31   * query.addParam(&quot;p3&quot;, new String[] { &quot;v2&quot;, &quot;v3&quot; });
32   * assertEquals(&quot;p1=v1&amp;p2=&amp;p3=v2&amp;p3=v3&quot;, query.toString());
33   * </pre>
34   * 
35   * @author agata
36   */
37  public class QueryStringBuilder {
38  
39  	/**
40  	 * パラメータ文字列
41  	 */
42  	private final StringBuilder queryString = new StringBuilder();
43  
44  	/**
45  	 * エンコード
46  	 */
47  	private String encode = "UTF-8";
48  
49  	/**
50  	 * URI 部分
51  	 */
52  	private final String baseUri;
53  
54  	/**
55  	 * URI 部分なしでインスタンスを生成します。
56  	 * <p>
57  	 * {@code QueryStringBuilder#toString()}が呼び出された時に、URI 部分は付加されません。
58  	 * </p>
59  	 */
60  	public QueryStringBuilder() {
61  		this(null);
62  	}
63  
64  	/**
65  	 * URI 部分を指定してインスタンスを生成します。
66  	 * <p>
67  	 * {@code QueryStringBuilder#toString()}が呼び出された時に、URI 部分と「?」が付加されます。
68  	 * </p>
69  	 * 
70  	 * @param baseUri
71  	 *            URI部分
72  	 */
73  	public QueryStringBuilder(final String baseUri) {
74  		this.baseUri = baseUri;
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 			final 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 IllegalArgumentException(e);
143 		}
144 	}
145 
146 }