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.

CDL.java 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. /**
  22. * This provides static methods to convert comma delimited text into a
  23. * JSONArray, and to covert a JSONArray into comma delimited text. Comma
  24. * delimited text is a very popular format for data interchange. It is
  25. * understood by most database, spreadsheet, and organizer programs.
  26. * <p>
  27. * Each row of text represents a row in a table or a data record. Each row
  28. * ends with a NEWLINE character. Each row contains one or more values.
  29. * Values are separated by commas. A value can contain any character except
  30. * for comma, unless is is wrapped in single quotes or double quotes.
  31. * <p>
  32. * The first row usually contains the names of the columns.
  33. * <p>
  34. * A comma delimited list can be converted into a JSONArray of JSONObjects.
  35. * The names for the elements in the JSONObjects can be taken from the names
  36. * in the first row.
  37. * @author JSON.org
  38. * @version 2008-09-18
  39. */
  40. public class CDL {
  41. /**
  42. * Get the next value. The value can be wrapped in quotes. The value can
  43. * be empty.
  44. * @param x A JSONTokener of the source text.
  45. * @return The value string, or null if empty.
  46. * @throws JSONException if the quoted string is badly formed.
  47. */
  48. private static String getValue(JSONTokener x) throws JSONException {
  49. char c;
  50. do {
  51. c = x.next();
  52. } while (c == ' ' || c == '\t');
  53. switch (c) {
  54. case 0:
  55. return null;
  56. case '"':
  57. case '\'':
  58. return x.nextString(c);
  59. case ',':
  60. x.back();
  61. return "";
  62. default:
  63. x.back();
  64. return x.nextTo(',');
  65. }
  66. }
  67. /**
  68. * Produce a JSONArray of strings from a row of comma delimited values.
  69. * @param x A JSONTokener of the source text.
  70. * @return A JSONArray of strings.
  71. * @throws JSONException
  72. */
  73. public static JSONArray rowToJSONArray(JSONTokener x) throws JSONException {
  74. JSONArray ja = new JSONArray();
  75. for (;;) {
  76. String value = getValue(x);
  77. if (value == null || (ja.length() == 0 && value.length() == 0)) {
  78. return null;
  79. }
  80. ja.put(value);
  81. for (;;) {
  82. char c = x.next();
  83. if (c == ',') {
  84. break;
  85. }
  86. if (c != ' ') {
  87. if (c == '\n' || c == '\r' || c == 0) {
  88. return ja;
  89. }
  90. throw x.syntaxError("Bad character '" + c + "' (" +
  91. (int)c + ").");
  92. }
  93. }
  94. }
  95. }
  96. /**
  97. * Produce a JSONObject from a row of comma delimited text, using a
  98. * parallel JSONArray of strings to provides the names of the elements.
  99. * @param names A JSONArray of names. This is commonly obtained from the
  100. * first row of a comma delimited text file using the rowToJSONArray
  101. * method.
  102. * @param x A JSONTokener of the source text.
  103. * @return A JSONObject combining the names and values.
  104. * @throws JSONException
  105. */
  106. public static JSONObject rowToJSONObject(JSONArray names, JSONTokener x)
  107. throws JSONException {
  108. JSONArray ja = rowToJSONArray(x);
  109. return ja != null ? ja.toJSONObject(names) : null;
  110. }
  111. /**
  112. * Produce a JSONArray of JSONObjects from a comma delimited text string,
  113. * using the first row as a source of names.
  114. * @param string The comma delimited text.
  115. * @return A JSONArray of JSONObjects.
  116. * @throws JSONException
  117. */
  118. public static JSONArray toJSONArray(String string) throws JSONException {
  119. return toJSONArray(new JSONTokener(string));
  120. }
  121. /**
  122. * Produce a JSONArray of JSONObjects from a comma delimited text string,
  123. * using the first row as a source of names.
  124. * @param x The JSONTokener containing the comma delimited text.
  125. * @return A JSONArray of JSONObjects.
  126. * @throws JSONException
  127. */
  128. public static JSONArray toJSONArray(JSONTokener x) throws JSONException {
  129. return toJSONArray(rowToJSONArray(x), x);
  130. }
  131. /**
  132. * Produce a JSONArray of JSONObjects from a comma delimited text string
  133. * using a supplied JSONArray as the source of element names.
  134. * @param names A JSONArray of strings.
  135. * @param string The comma delimited text.
  136. * @return A JSONArray of JSONObjects.
  137. * @throws JSONException
  138. */
  139. public static JSONArray toJSONArray(JSONArray names, String string)
  140. throws JSONException {
  141. return toJSONArray(names, new JSONTokener(string));
  142. }
  143. /**
  144. * Produce a JSONArray of JSONObjects from a comma delimited text string
  145. * using a supplied JSONArray as the source of element names.
  146. * @param names A JSONArray of strings.
  147. * @param x A JSONTokener of the source text.
  148. * @return A JSONArray of JSONObjects.
  149. * @throws JSONException
  150. */
  151. public static JSONArray toJSONArray(JSONArray names, JSONTokener x)
  152. throws JSONException {
  153. if (names == null || names.length() == 0) {
  154. return null;
  155. }
  156. JSONArray ja = new JSONArray();
  157. for (;;) {
  158. JSONObject jo = rowToJSONObject(names, x);
  159. if (jo == null) {
  160. break;
  161. }
  162. ja.put(jo);
  163. }
  164. if (ja.length() == 0) {
  165. return null;
  166. }
  167. return ja;
  168. }
  169. /**
  170. * Produce a comma delimited text row from a JSONArray. Values containing
  171. * the comma character will be quoted.
  172. * @param ja A JSONArray of strings.
  173. * @return A string ending in NEWLINE.
  174. */
  175. public static String rowToString(JSONArray ja) {
  176. StringBuffer sb = new StringBuffer();
  177. for (int i = 0; i < ja.length(); i += 1) {
  178. if (i > 0) {
  179. sb.append(',');
  180. }
  181. Object o = ja.opt(i);
  182. if (o != null) {
  183. String s = o.toString();
  184. if (s.indexOf(',') >= 0) {
  185. if (s.indexOf('"') >= 0) {
  186. sb.append('\'');
  187. sb.append(s);
  188. sb.append('\'');
  189. } else {
  190. sb.append('"');
  191. sb.append(s);
  192. sb.append('"');
  193. }
  194. } else {
  195. sb.append(s);
  196. }
  197. }
  198. }
  199. sb.append('\n');
  200. return sb.toString();
  201. }
  202. /**
  203. * Produce a comma delimited text from a JSONArray of JSONObjects. The
  204. * first row will be a list of names obtained by inspecting the first
  205. * JSONObject.
  206. * @param ja A JSONArray of JSONObjects.
  207. * @return A comma delimited text.
  208. * @throws JSONException
  209. */
  210. public static String toString(JSONArray ja) throws JSONException {
  211. JSONObject jo = ja.optJSONObject(0);
  212. if (jo != null) {
  213. JSONArray names = jo.names();
  214. if (names != null) {
  215. return rowToString(names) + toString(names, ja);
  216. }
  217. }
  218. return null;
  219. }
  220. /**
  221. * Produce a comma delimited text from a JSONArray of JSONObjects using
  222. * a provided list of names. The list of names is not included in the
  223. * output.
  224. * @param names A JSONArray of strings.
  225. * @param ja A JSONArray of JSONObjects.
  226. * @return A comma delimited text.
  227. * @throws JSONException
  228. */
  229. public static String toString(JSONArray names, JSONArray ja)
  230. throws JSONException {
  231. if (names == null || names.length() == 0) {
  232. return null;
  233. }
  234. StringBuffer sb = new StringBuffer();
  235. for (int i = 0; i < ja.length(); i += 1) {
  236. JSONObject jo = ja.optJSONObject(i);
  237. if (jo != null) {
  238. sb.append(rowToString(jo.toJSONArray(names)));
  239. }
  240. }
  241. return sb.toString();
  242. }
  243. }