001    package org.apache.tapestry.json;
002    
003    /*
004     Copyright (c) 2002 JSON.org
005    
006     Permission is hereby granted, free of charge, to any person obtaining a copy
007     of this software and associated documentation files (the "Software"), to deal
008     in the Software without restriction, including without limitation the rights
009     to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
010     copies of the Software, and to permit persons to whom the Software is
011     furnished to do so, subject to the following conditions:
012    
013     The above copyright notice and this permission notice shall be included in all
014     copies or substantial portions of the Software.
015    
016     The Software shall be used for Good, not Evil.
017    
018     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
019     IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
020     FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
021     AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
022     LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
023     OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
024     SOFTWARE.
025     */
026    
027    import java.text.ParseException;
028    import java.util.Iterator;
029    
030    /**
031     * Convert a web browser cookie list string to a JSONObject and back.
032     * 
033     * @author JSON.org
034     * @version 0.1
035     */
036    public final class CookieList
037    {
038    
039        /* defeat instantiation */
040        private CookieList() { }
041        
042        /**
043         * Convert a cookie list into a JSONObject. A cookie list is a sequence of
044         * name/value pairs. The names are separated from the values by '='. The
045         * pairs are separated by ';'. The names and the values will be unescaped,
046         * possibly converting '+' and '%' sequences. To add a cookie to a cooklist,
047         * cookielistJSONObject.put(cookieJSONObject.getString("name"),
048         * cookieJSONObject.getString("value"));
049         * 
050         * @param string
051         *            A cookie list string
052         * @return A JSONObject
053         * @throws ParseException
054         */
055        public static JSONObject toJSONObject(String string)
056            throws ParseException
057        {
058            JSONObject o = new JSONObject();
059            JSONTokener x = new JSONTokener(string);
060            while(x.more())
061            {
062                String name = JSONTokener.unescape(x.nextTo('='));
063                x.next('=');
064                o.put(name, JSONTokener.unescape(x.nextTo(';')));
065                x.next();
066            }
067            return o;
068        }
069    
070        /**
071         * Convert a JSONObject into a cookie list. A cookie list is a sequence of
072         * name/value pairs. The names are separated from the values by '='. The
073         * pairs are separated by ';'. The characters '%', '+', '=', and ';' in the
074         * names and values are replaced by "%hh".
075         * 
076         * @param o
077         *            A JSONObject
078         * @return A cookie list string
079         */
080        public static String toString(JSONObject o)
081        {
082            boolean b = false;
083            Iterator keys = o.keys();
084            String s;
085            StringBuffer sb = new StringBuffer();
086            while(keys.hasNext())
087            {
088                s = keys.next().toString();
089                if (!o.isNull(s))
090                {
091                    if (b)
092                    {
093                        sb.append(';');
094                    }
095                    sb.append(Cookie.escape(s));
096                    sb.append("=");
097                    sb.append(Cookie.escape(o.getString(s)));
098                    b = true;
099                }
100            }
101            return sb.toString();
102        }
103    }