Java IRC bot
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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