source: other-projects/tipple-android/tipple-lib/src/org/json1/CookieList.java@ 26899

Last change on this file since 26899 was 26899, checked in by davidb, 11 years ago

Tipple reborn after Chris's Summer of Code 2013

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