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