annotate lib/org.json_2.0/src/org/json/CookieList.java @ 0:db87c1b7eb6d

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