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 | 7 | public class QueryStringBuilder { |
40 | ||
41 | /** | |
42 | * パラメータ文字列 | |
43 | */ | |
44 | 7 | private StringBuilder sb = new StringBuilder(); |
45 | ||
46 | /** | |
47 | * エンコード | |
48 | */ | |
49 | 7 | private String encode = "UTF-8"; |
50 | ||
51 | /** | |
52 | * エンコードをセットします。 | |
53 | * | |
54 | * @param encode | |
55 | */ | |
56 | public void setEncode(final String encode) { | |
57 | 6 | this.encode = encode; |
58 | 6 | } |
59 | ||
60 | /** | |
61 | * パラメータを追加します。 | |
62 | * | |
63 | * @param name | |
64 | * パラメータ名 | |
65 | * @param value | |
66 | * 値。配列の場合、要素数分パラメータが追加されます。 | |
67 | */ | |
68 | public void addParam(final String name, final Object value) { | |
69 | 11 | if (value != null && value.getClass().isArray()) { |
70 | 1 | final Object[] values = (Object[]) value; |
71 | 3 | for (final Object v : values) { |
72 | 2 | appendParams(name, v); |
73 | } | |
74 | 1 | } else { |
75 | 10 | appendParams(name, value); |
76 | } | |
77 | 11 | } |
78 | ||
79 | /** | |
80 | * パラメータ文字列を取得します。 | |
81 | */ | |
82 | @Override | |
83 | public String toString() { | |
84 | 7 | return sb.toString(); |
85 | } | |
86 | ||
87 | /** | |
88 | * パラメータ文字列を追加します。 | |
89 | * | |
90 | * @param name | |
91 | * パラメータ名 | |
92 | * @param value | |
93 | * 値 | |
94 | */ | |
95 | private void appendParams(final String name, final Object value) { | |
96 | 12 | if (sb.length() > 0) { |
97 | 5 | sb.append("&"); |
98 | } | |
99 | try { | |
100 | 12 | sb.append(URLEncoder.encode(name, encode)); |
101 | 12 | sb.append("="); |
102 | 12 | if (value != null) { |
103 | 11 | sb.append(URLEncoder.encode(value.toString(), encode)); |
104 | } | |
105 | 0 | } catch (final UnsupportedEncodingException e) { |
106 | 0 | throw new IORuntimeException(e); |
107 | 12 | } |
108 | 12 | } |
109 | } |