Coverage Report - org.apache.tapestry.json.CookieList
 
Classes in this File Line Coverage Branch Coverage Complexity
CookieList
0%
0/23
0%
0/8
2.333
 
 1  
 package org.apache.tapestry.json;
 2  
 
 3  
 /*
 4  
  Copyright (c) 2002 JSON.org
 5  
 
 6  
  Permission is hereby granted, free of charge, to any person obtaining a copy
 7  
  of this software and associated documentation files (the "Software"), to deal
 8  
  in the Software without restriction, including without limitation the rights
 9  
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 10  
  copies of the Software, and to permit persons to whom the Software is
 11  
  furnished to do so, subject to the following conditions:
 12  
 
 13  
  The above copyright notice and this permission notice shall be included in all
 14  
  copies or substantial portions of the Software.
 15  
 
 16  
  The Software shall be used for Good, not Evil.
 17  
 
 18  
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 19  
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 20  
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 21  
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 22  
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 23  
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 24  
  SOFTWARE.
 25  
  */
 26  
 
 27  
 import java.text.ParseException;
 28  
 import java.util.Iterator;
 29  
 
 30  
 /**
 31  
  * Convert a web browser cookie list string to a JSONObject and back.
 32  
  * 
 33  
  * @author JSON.org
 34  
  * @version 0.1
 35  
  */
 36  
 public final class CookieList
 37  
 {
 38  
 
 39  
     /* defeat instantiation */
 40  0
     private CookieList() { }
 41  
     
 42  
     /**
 43  
      * Convert a cookie list into a JSONObject. A cookie list is a sequence of
 44  
      * name/value pairs. The names are separated from the values by '='. The
 45  
      * pairs are separated by ';'. The names and the values will be unescaped,
 46  
      * possibly converting '+' and '%' sequences. To add a cookie to a cooklist,
 47  
      * cookielistJSONObject.put(cookieJSONObject.getString("name"),
 48  
      * cookieJSONObject.getString("value"));
 49  
      * 
 50  
      * @param string
 51  
      *            A cookie list string
 52  
      * @return A JSONObject
 53  
      * @throws ParseException
 54  
      */
 55  
     public static JSONObject toJSONObject(String string)
 56  
         throws ParseException
 57  
     {
 58  0
         JSONObject o = new JSONObject();
 59  0
         JSONTokener x = new JSONTokener(string);
 60  0
         while(x.more())
 61  
         {
 62  0
             String name = JSONTokener.unescape(x.nextTo('='));
 63  0
             x.next('=');
 64  0
             o.put(name, JSONTokener.unescape(x.nextTo(';')));
 65  0
             x.next();
 66  0
         }
 67  0
         return o;
 68  
     }
 69  
 
 70  
     /**
 71  
      * Convert a JSONObject into a cookie list. A cookie list is a sequence of
 72  
      * name/value pairs. The names are separated from the values by '='. The
 73  
      * pairs are separated by ';'. The characters '%', '+', '=', and ';' in the
 74  
      * names and values are replaced by "%hh".
 75  
      * 
 76  
      * @param o
 77  
      *            A JSONObject
 78  
      * @return A cookie list string
 79  
      */
 80  
     public static String toString(JSONObject o)
 81  
     {
 82  0
         boolean b = false;
 83  0
         Iterator keys = o.keys();
 84  
         String s;
 85  0
         StringBuffer sb = new StringBuffer();
 86  0
         while(keys.hasNext())
 87  
         {
 88  0
             s = keys.next().toString();
 89  0
             if (!o.isNull(s))
 90  
             {
 91  0
                 if (b)
 92  
                 {
 93  0
                     sb.append(';');
 94  
                 }
 95  0
                 sb.append(Cookie.escape(s));
 96  0
                 sb.append("=");
 97  0
                 sb.append(Cookie.escape(o.getString(s)));
 98  0
                 b = true;
 99  
             }
 100  
         }
 101  0
         return sb.toString();
 102  
     }
 103  
 }