View Javadoc

1   /*
2    * Copyright 2004-2009 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.internal.util;
17  
18  import java.io.UnsupportedEncodingException;
19  import java.net.URLEncoder;
20  
21  /**
22   * パラメータ文字列を作成します。
23   * <p>
24   * パラメータ名、値ともURLエンコードされます。デフォルトのエンコードはUTF-8です。
25   * 
26   * <pre>
27   * QueryStringBuilder query = new QueryStringBuilder();
28   * query.addParam(&quot;p1&quot;, &quot;v1&quot;);
29   * query.addParam(&quot;p2&quot;, null);
30   * query.addParam(&quot;p3&quot;, new String[] { &quot;v2&quot;, &quot;v3&quot; });
31   * assertEquals(&quot;p1=v1&amp;p2=&amp;p3=v2&amp;p3=v3&quot;, query.toString());
32   * </pre>
33   * 
34   * @author agata
35   */
36  public class QueryStringBuilder {
37  
38  	/**
39  	 * パラメータ文字列
40  	 */
41  	private final StringBuilder queryString = new StringBuilder();
42  
43  	/**
44  	 * エンコード
45  	 */
46  	private String encode = "UTF-8";
47  
48  	/**
49  	 * URI 部分
50  	 */
51  	private final String baseUri;
52  
53  	/**
54  	 * URI 部分なしでインスタンスを生成します。
55  	 * <p>
56  	 * {@code QueryStringBuilder#toString()}が呼び出された時に、URI 部分は付加されません。
57  	 * </p>
58  	 */
59  	public QueryStringBuilder() {
60  		this(null);
61  	}
62  
63  	/**
64  	 * URI 部分を指定してインスタンスを生成します。
65  	 * <p>
66  	 * {@code QueryStringBuilder#toString()}が呼び出された時に、URI 部分と「?」が付加されます。
67  	 * </p>
68  	 * 
69  	 * @param baseUri
70  	 *            URI部分
71  	 */
72  	public QueryStringBuilder(final String baseUri) {
73  		this.baseUri = baseUri;
74  	}
75  
76  	/**
77  	 * エンコードをセットします。
78  	 * 
79  	 * @param encode
80  	 */
81  	public void setEncode(final String encode) {
82  		this.encode = encode;
83  	}
84  
85  	/**
86  	 * パラメータを追加します。
87  	 * 
88  	 * @param name
89  	 *            パラメータ名
90  	 * @param value
91  	 *            値。配列の場合、要素数分パラメータが追加されます。
92  	 */
93  	public void addParam(final String name, final Object value) {
94  		if (value != null && value.getClass().isArray()) {
95  			final Object[] values = (Object[]) value;
96  			for (final Object v : values) {
97  				appendParams(name, v);
98  			}
99  		} else {
100 			appendParams(name, value);
101 		}
102 	}
103 
104 	/**
105 	 * パラメータ文字列を取得します。
106 	 */
107 	@Override
108 	public String toString() {
109 		if (this.baseUri == null) {
110 			return queryString.toString();
111 		} else {
112 			final StringBuilder baseUrlBuf = new StringBuilder(this.baseUri);
113 			if (baseUrlBuf.indexOf("?") == -1) {
114 				baseUrlBuf.append("?");
115 			} else if (queryString.indexOf("?") < queryString.length()) {
116 				baseUrlBuf.append("&");
117 			}
118 			return baseUrlBuf.toString() + queryString.toString();
119 		}
120 	}
121 
122 	/**
123 	 * パラメータ文字列を追加します。
124 	 * 
125 	 * @param name
126 	 *            パラメータ名
127 	 * @param value
128 	 *            値
129 	 */
130 	private void appendParams(final String name, final Object value) {
131 		if (queryString.length() > 0) {
132 			queryString.append("&");
133 		}
134 		try {
135 			queryString.append(URLEncoder.encode(name, encode));
136 			queryString.append("=");
137 			if (value != null) {
138 				queryString.append(URLEncoder.encode(value.toString(), encode));
139 			}
140 		} catch (final UnsupportedEncodingException e) {
141 			throw new IllegalArgumentException(e);
142 		}
143 	}
144 
145 }