PageRenderTime 11ms CodeModel.GetById 1ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/ftr-gwt-library-file/src/main/java/org/json/CookieList.java

http://ftr-gwt-library.googlecode.com/
Java | 96 lines | 37 code | 9 blank | 50 comment | 4 complexity | 19b72f65cdacfb02225142e86453a6b5 MD5 | raw file
 1package org.json;
 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 2008-09-18
33 */
34
35
36@SuppressWarnings({
37	"rawtypes"
38})
39public class CookieList {
40
41    /**
42     * Convert a cookie list into a JSONObject. A cookie list is a sequence
43     * of name/value pairs. The names are separated from the values by '='.
44     * The pairs are separated by ';'. The names and the values
45     * will be unescaped, possibly converting '+' and '%' sequences.
46     *
47     * To add a cookie to a cooklist,
48     * cookielistJSONObject.put(cookieJSONObject.getString("name"),
49     *     cookieJSONObject.getString("value"));
50     * @param string  A cookie list string
51     * @return A JSONObject
52     * @throws JSONException
53     */
54    public static JSONObject toJSONObject(String string) throws JSONException {
55        JSONObject o = new JSONObject();
56        JSONTokener x = new JSONTokener(string);
57        while (x.more()) {
58            String name = Cookie.unescape(x.nextTo('='));
59            x.next('=');
60            o.put(name, Cookie.unescape(x.nextTo(';')));
61            x.next();
62        }
63        return o;
64    }
65
66
67    /**
68     * Convert a JSONObject into a cookie list. A cookie list is a sequence
69     * of name/value pairs. The names are separated from the values by '='.
70     * The pairs are separated by ';'. The characters '%', '+', '=', and ';'
71     * in the names and values are replaced by "%hh".
72     * @param o A JSONObject
73     * @return A cookie list string
74     * @throws JSONException
75     */
76   
77	public static String toString(JSONObject o) throws JSONException {
78        boolean      b = false;
79        Iterator     keys = o.keys();
80        String       s;
81        StringBuffer sb = new StringBuffer();
82        while (keys.hasNext()) {
83            s = keys.next().toString();
84            if (!o.isNull(s)) {
85                if (b) {
86                    sb.append(';');
87                }
88                sb.append(Cookie.escape(s));
89                sb.append("=");
90                sb.append(Cookie.escape(o.getString(s)));
91                b = true;
92            }
93        }
94        return sb.toString();
95    }
96}