Coverage Report - org.seasar.cubby.util.QueryStringBuilder
 
Classes in this File Line Coverage Branch Coverage Complexity
QueryStringBuilder
100%
19/19
N/A
0
 
 1  
 package org.seasar.cubby.util;
 2  
 
 3  
 import java.io.UnsupportedEncodingException;
 4  
 import java.net.URLEncoder;
 5  
 
 6  
 import org.seasar.framework.exception.IORuntimeException;
 7  
 
 8  
 /**
 9  
  * パラメータ?字?を作?します??
 10  
  * <p>
 11  
  * パラメータ名??ともURLエンコードされます?デフォルト?エンコード?UTF-8です??
 12  
  * 
 13  
  * <pre>
 14  
  * QueryStringBuilder query = new QueryStringBuilder();
 15  
  * query.addParam(&quot;p1&quot;, &quot;v1&quot;);
 16  
  * query.addParam(&quot;p2&quot;, null);
 17  
  * query.addParam(&quot;p3&quot;, new String[] { &quot;v2&quot;, &quot;v3&quot; });
 18  
  * assertEquals(&quot;p1=v1&amp;p2=&amp;p3=v2&amp;p3=v3&quot;, query.toString());
 19  
  * </pre>
 20  
  * 
 21  
  * @author agata
 22  
  * 
 23  
  */
 24  1
 public class QueryStringBuilder {
 25  
 
 26  
         /**
 27  
          * パラメータ?字??
 28  
          */
 29  1
         private StringBuilder sb = new StringBuilder();
 30  
 
 31  
         /**
 32  
          * エンコー?
 33  
          */
 34  1
         private String encode = "UTF-8";
 35  
 
 36  
         /**
 37  
          * エンコードをセ?トします??
 38  
          * 
 39  
          * @param encode
 40  
          */
 41  
         public void setEncode(final String encode) {
 42  
                 this.encode = encode;
 43  
         }
 44  
 
 45  
         /**
 46  
          * パラメータを追?します??
 47  
          * 
 48  
          * @param name
 49  
          *            パラメータ?
 50  
          * @param value
 51  
          *            値。???場合?要?数?パラメータが追?されます??
 52  
          */
 53  
         public void addParam(final String name, final Object value) {
 54  3
                 if (value != null && value.getClass().isArray()) {
 55  1
                         final Object[] values = (Object[]) value;
 56  3
                         for (final Object v : values) {
 57  2
                                 appendParams(name, v);
 58  
                         }
 59  1
                 } else {
 60  2
                         appendParams(name, value);
 61  
                 }
 62  3
         }
 63  
 
 64  
         /**
 65  
          * パラメータ?字?を取得します??
 66  
          */
 67  
         @Override
 68  
         public String toString() {
 69  1
                 return sb.toString();
 70  
         }
 71  
 
 72  
         /**
 73  
          * パラメータ?字?を追?します??
 74  
          * 
 75  
          * @param name
 76  
          *            パラメータ?
 77  
          * @param value
 78  
          *            値
 79  
          */
 80  
         private void appendParams(final String name, final Object value) {
 81  4
                 if (sb.length() > 0) {
 82  3
                         sb.append("&");
 83  
                 }
 84  
                 try {
 85  4
                         sb.append(URLEncoder.encode(name, encode));
 86  4
                         sb.append("=");
 87  4
                         if (value != null) {
 88  3
                                 sb.append(URLEncoder.encode(value.toString(), encode));
 89  
                         }
 90  
                 } catch (final UnsupportedEncodingException e) {
 91  
                         throw new IORuntimeException(e);
 92  4
                 }
 93  4
         }
 94  
 }