Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
QueryStringBuilder |
|
| 0.0;0 |
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("p1", "v1"); | |
31 | * query.addParam("p2", null); | |
32 | * query.addParam("p3", new String[] { "v2", "v3" }); | |
33 | * assertEquals("p1=v1&p2=&p3=v2&p3=v3", query.toString()); | |
34 | * </pre> | |
35 | * | |
36 | * @author agata | |
37 | * @since 1.0.0 | |
38 | */ | |
39 | public class QueryStringBuilder { | |
40 | ||
41 | /** | |
42 | * パラメータ文字列 | |
43 | */ | |
44 | 23 | private StringBuilder queryString = new StringBuilder(); |
45 | ||
46 | /** | |
47 | * エンコード | |
48 | */ | |
49 | 23 | 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 | 17 | this(null); |
64 | 17 | } |
65 | ||
66 | /** | |
67 | * URI部分を指定してインスタンスを生成します。 | |
68 | * <p> | |
69 | * {@code QueryStringBuilder#toString()}が呼び出された時に、URI部分と「?」が付加されます。 | |
70 | * </p> | |
71 | * @param baseUri URI部分 | |
72 | */ | |
73 | 23 | public QueryStringBuilder(String baseUrl) { |
74 | 23 | this.baseUri = baseUrl; |
75 | 23 | } |
76 | ||
77 | /** | |
78 | * エンコードをセットします。 | |
79 | * | |
80 | * @param encode | |
81 | */ | |
82 | public void setEncode(final String encode) { | |
83 | 16 | this.encode = encode; |
84 | 16 | } |
85 | ||
86 | /** | |
87 | * パラメータを追加します。 | |
88 | * | |
89 | * @param name | |
90 | * パラメータ名 | |
91 | * @param value | |
92 | * 値。配列の場合、要素数分パラメータが追加されます。 | |
93 | */ | |
94 | public void addParam(final String name, final Object value) { | |
95 | 36 | if (value != null && value.getClass().isArray()) { |
96 | 7 | final Object[] values = (Object[]) value; |
97 | 17 | for (final Object v : values) { |
98 | 10 | appendParams(name, v); |
99 | } | |
100 | 7 | } else { |
101 | 29 | appendParams(name, value); |
102 | } | |
103 | 36 | } |
104 | ||
105 | /** | |
106 | * パラメータ文字列を取得します。 | |
107 | */ | |
108 | @Override | |
109 | public String toString() { | |
110 | 23 | if (this.baseUri == null) { |
111 | 17 | return queryString.toString(); |
112 | } else { | |
113 | 6 | StringBuilder baseUrlBuf = new StringBuilder(this.baseUri); |
114 | 6 | if (baseUrlBuf.indexOf("?") == -1) { |
115 | 3 | baseUrlBuf.append("?"); |
116 | 3 | } else if (queryString.indexOf("?") < queryString.length()) { |
117 | 3 | baseUrlBuf.append("&"); |
118 | } | |
119 | 6 | 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 | 39 | if (queryString.length() > 0) { |
133 | 16 | queryString.append("&"); |
134 | } | |
135 | try { | |
136 | 39 | queryString.append(URLEncoder.encode(name, encode)); |
137 | 39 | queryString.append("="); |
138 | 39 | if (value != null) { |
139 | 36 | queryString.append(URLEncoder.encode(value.toString(), encode)); |
140 | } | |
141 | 0 | } catch (final UnsupportedEncodingException e) { |
142 | 0 | throw new IORuntimeException(e); |
143 | 39 | } |
144 | 39 | } |
145 | } |