Class |
Line # |
Actions |
|||
---|---|---|---|---|---|
CDL | 47 | 93 | 54 |
1 | package org.json; | |
2 | ||
3 | /* | |
4 | Copyright (c) 2002 JSON.org | |
5 | ||
6 | Permission is hereby granted, free of charge, to any person obtaining a copy | |
7 | of this software and associated documentation files (the "Software"), to deal | |
8 | in the Software without restriction, including without limitation the rights | |
9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
10 | copies of the Software, and to permit persons to whom the Software is | |
11 | furnished to do so, subject to the following conditions: | |
12 | ||
13 | The above copyright notice and this permission notice shall be included in all | |
14 | copies or substantial portions of the Software. | |
15 | ||
16 | The Software shall be used for Good, not Evil. | |
17 | ||
18 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
19 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
20 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
21 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
22 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
23 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
24 | SOFTWARE. | |
25 | */ | |
26 | ||
27 | /** | |
28 | * This provides static methods to convert comma delimited text into a | |
29 | * JSONArray, and to convert a JSONArray into comma delimited text. Comma | |
30 | * delimited text is a very popular format for data interchange. It is | |
31 | * understood by most database, spreadsheet, and organizer programs. | |
32 | * <p> | |
33 | * Each row of text represents a row in a table or a data record. Each row ends | |
34 | * with a NEWLINE character. Each row contains one or more values. Values are | |
35 | * separated by commas. A value can contain any character except for comma, | |
36 | * unless is is wrapped in single quotes or double quotes. | |
37 | * <p> | |
38 | * The first row usually contains the names of the columns. | |
39 | * <p> | |
40 | * A comma delimited list can be converted into a JSONArray of JSONObjects. The | |
41 | * names for the elements in the JSONObjects can be taken from the names in the | |
42 | * first row. | |
43 | * | |
44 | * @author JSON.org | |
45 | * @version 2016-05-01 | |
46 | */ | |
47 | public class CDL | |
48 | { | |
49 | ||
50 | /** | |
51 | * Get the next value. The value can be wrapped in quotes. The value can be | |
52 | * empty. | |
53 | * | |
54 | * @param x | |
55 | * A JSONTokener of the source text. | |
56 | * @return The value string, or null if empty. | |
57 | * @throws JSONException | |
58 | * if the quoted string is badly formed. | |
59 | */ | |
60 | 0 | private static String getValue(JSONTokener x) throws JSONException |
61 | { | |
62 | 0 | char c; |
63 | 0 | char q; |
64 | 0 | StringBuffer sb; |
65 | 0 | do |
66 | { | |
67 | 0 | c = x.next(); |
68 | 0 | } while (c == ' ' || c == '\t'); |
69 | 0 | switch (c) |
70 | { | |
71 | 0 | case 0: |
72 | 0 | return null; |
73 | 0 | case '"': |
74 | 0 | case '\'': |
75 | 0 | q = c; |
76 | 0 | sb = new StringBuffer(); |
77 | 0 | for (;;) |
78 | { | |
79 | 0 | c = x.next(); |
80 | 0 | if (c == q) |
81 | { | |
82 | // Handle escaped double-quote | |
83 | 0 | char nextC = x.next(); |
84 | 0 | if (nextC != '\"') |
85 | { | |
86 | // if our quote was the end of the file, don't step | |
87 | 0 | if (nextC > 0) |
88 | { | |
89 | 0 | x.back(); |
90 | } | |
91 | 0 | break; |
92 | } | |
93 | } | |
94 | 0 | if (c == 0 || c == '\n' || c == '\r') |
95 | { | |
96 | 0 | throw x.syntaxError("Missing close quote '" + q + "'."); |
97 | } | |
98 | 0 | sb.append(c); |
99 | } | |
100 | 0 | return sb.toString(); |
101 | 0 | case ',': |
102 | 0 | x.back(); |
103 | 0 | return ""; |
104 | 0 | default: |
105 | 0 | x.back(); |
106 | 0 | return x.nextTo(','); |
107 | } | |
108 | } | |
109 | ||
110 | /** | |
111 | * Produce a JSONArray of strings from a row of comma delimited values. | |
112 | * | |
113 | * @param x | |
114 | * A JSONTokener of the source text. | |
115 | * @return A JSONArray of strings. | |
116 | * @throws JSONException | |
117 | */ | |
118 | 0 | public static JSONArray rowToJSONArray(JSONTokener x) throws JSONException |
119 | { | |
120 | 0 | JSONArray ja = new JSONArray(); |
121 | 0 | for (;;) |
122 | { | |
123 | 0 | String value = getValue(x); |
124 | 0 | char c = x.next(); |
125 | 0 | if (value == null |
126 | || (ja.length() == 0 && value.length() == 0 && c != ',')) | |
127 | { | |
128 | 0 | return null; |
129 | } | |
130 | 0 | ja.put(value); |
131 | 0 | for (;;) |
132 | { | |
133 | 0 | if (c == ',') |
134 | { | |
135 | 0 | break; |
136 | } | |
137 | 0 | if (c != ' ') |
138 | { | |
139 | 0 | if (c == '\n' || c == '\r' || c == 0) |
140 | { | |
141 | 0 | return ja; |
142 | } | |
143 | 0 | throw x.syntaxError( |
144 | "Bad character '" + c + "' (" + (int) c + ")."); | |
145 | } | |
146 | 0 | c = x.next(); |
147 | } | |
148 | } | |
149 | } | |
150 | ||
151 | /** | |
152 | * Produce a JSONObject from a row of comma delimited text, using a parallel | |
153 | * JSONArray of strings to provides the names of the elements. | |
154 | * | |
155 | * @param names | |
156 | * A JSONArray of names. This is commonly obtained from the first row | |
157 | * of a comma delimited text file using the rowToJSONArray method. | |
158 | * @param x | |
159 | * A JSONTokener of the source text. | |
160 | * @return A JSONObject combining the names and values. | |
161 | * @throws JSONException | |
162 | */ | |
163 | 0 | public static JSONObject rowToJSONObject(JSONArray names, JSONTokener x) |
164 | throws JSONException | |
165 | { | |
166 | 0 | JSONArray ja = rowToJSONArray(x); |
167 | 0 | return ja != null ? ja.toJSONObject(names) : null; |
168 | } | |
169 | ||
170 | /** | |
171 | * Produce a comma delimited text row from a JSONArray. Values containing the | |
172 | * comma character will be quoted. Troublesome characters may be removed. | |
173 | * | |
174 | * @param ja | |
175 | * A JSONArray of strings. | |
176 | * @return A string ending in NEWLINE. | |
177 | */ | |
178 | 0 | public static String rowToString(JSONArray ja) |
179 | { | |
180 | 0 | StringBuilder sb = new StringBuilder(); |
181 | 0 | for (int i = 0; i < ja.length(); i += 1) |
182 | { | |
183 | 0 | if (i > 0) |
184 | { | |
185 | 0 | sb.append(','); |
186 | } | |
187 | 0 | Object object = ja.opt(i); |
188 | 0 | if (object != null) |
189 | { | |
190 | 0 | String string = object.toString(); |
191 | 0 | if (string.length() > 0 && (string.indexOf(',') >= 0 |
192 | || string.indexOf('\n') >= 0 || string.indexOf('\r') >= 0 | |
193 | || string.indexOf(0) >= 0 || string.charAt(0) == '"')) | |
194 | { | |
195 | 0 | sb.append('"'); |
196 | 0 | int length = string.length(); |
197 | 0 | for (int j = 0; j < length; j += 1) |
198 | { | |
199 | 0 | char c = string.charAt(j); |
200 | 0 | if (c >= ' ' && c != '"') |
201 | { | |
202 | 0 | sb.append(c); |
203 | } | |
204 | } | |
205 | 0 | sb.append('"'); |
206 | } | |
207 | else | |
208 | { | |
209 | 0 | sb.append(string); |
210 | } | |
211 | } | |
212 | } | |
213 | 0 | sb.append('\n'); |
214 | 0 | return sb.toString(); |
215 | } | |
216 | ||
217 | /** | |
218 | * Produce a JSONArray of JSONObjects from a comma delimited text string, | |
219 | * using the first row as a source of names. | |
220 | * | |
221 | * @param string | |
222 | * The comma delimited text. | |
223 | * @return A JSONArray of JSONObjects. | |
224 | * @throws JSONException | |
225 | */ | |
226 | 0 | public static JSONArray toJSONArray(String string) throws JSONException |
227 | { | |
228 | 0 | return toJSONArray(new JSONTokener(string)); |
229 | } | |
230 | ||
231 | /** | |
232 | * Produce a JSONArray of JSONObjects from a comma delimited text string, | |
233 | * using the first row as a source of names. | |
234 | * | |
235 | * @param x | |
236 | * The JSONTokener containing the comma delimited text. | |
237 | * @return A JSONArray of JSONObjects. | |
238 | * @throws JSONException | |
239 | */ | |
240 | 0 | public static JSONArray toJSONArray(JSONTokener x) throws JSONException |
241 | { | |
242 | 0 | return toJSONArray(rowToJSONArray(x), x); |
243 | } | |
244 | ||
245 | /** | |
246 | * Produce a JSONArray of JSONObjects from a comma delimited text string using | |
247 | * a supplied JSONArray as the source of element names. | |
248 | * | |
249 | * @param names | |
250 | * A JSONArray of strings. | |
251 | * @param string | |
252 | * The comma delimited text. | |
253 | * @return A JSONArray of JSONObjects. | |
254 | * @throws JSONException | |
255 | */ | |
256 | 0 | public static JSONArray toJSONArray(JSONArray names, String string) |
257 | throws JSONException | |
258 | { | |
259 | 0 | return toJSONArray(names, new JSONTokener(string)); |
260 | } | |
261 | ||
262 | /** | |
263 | * Produce a JSONArray of JSONObjects from a comma delimited text string using | |
264 | * a supplied JSONArray as the source of element names. | |
265 | * | |
266 | * @param names | |
267 | * A JSONArray of strings. | |
268 | * @param x | |
269 | * A JSONTokener of the source text. | |
270 | * @return A JSONArray of JSONObjects. | |
271 | * @throws JSONException | |
272 | */ | |
273 | 0 | public static JSONArray toJSONArray(JSONArray names, JSONTokener x) |
274 | throws JSONException | |
275 | { | |
276 | 0 | if (names == null || names.length() == 0) |
277 | { | |
278 | 0 | return null; |
279 | } | |
280 | 0 | JSONArray ja = new JSONArray(); |
281 | 0 | for (;;) |
282 | { | |
283 | 0 | JSONObject jo = rowToJSONObject(names, x); |
284 | 0 | if (jo == null) |
285 | { | |
286 | 0 | break; |
287 | } | |
288 | 0 | ja.put(jo); |
289 | } | |
290 | 0 | if (ja.length() == 0) |
291 | { | |
292 | 0 | return null; |
293 | } | |
294 | 0 | return ja; |
295 | } | |
296 | ||
297 | /** | |
298 | * Produce a comma delimited text from a JSONArray of JSONObjects. The first | |
299 | * row will be a list of names obtained by inspecting the first JSONObject. | |
300 | * | |
301 | * @param ja | |
302 | * A JSONArray of JSONObjects. | |
303 | * @return A comma delimited text. | |
304 | * @throws JSONException | |
305 | */ | |
306 | 0 | public static String toString(JSONArray ja) throws JSONException |
307 | { | |
308 | 0 | JSONObject jo = ja.optJSONObject(0); |
309 | 0 | if (jo != null) |
310 | { | |
311 | 0 | JSONArray names = jo.names(); |
312 | 0 | if (names != null) |
313 | { | |
314 | 0 | return rowToString(names) + toString(names, ja); |
315 | } | |
316 | } | |
317 | 0 | return null; |
318 | } | |
319 | ||
320 | /** | |
321 | * Produce a comma delimited text from a JSONArray of JSONObjects using a | |
322 | * provided list of names. The list of names is not included in the output. | |
323 | * | |
324 | * @param names | |
325 | * A JSONArray of strings. | |
326 | * @param ja | |
327 | * A JSONArray of JSONObjects. | |
328 | * @return A comma delimited text. | |
329 | * @throws JSONException | |
330 | */ | |
331 | 0 | public static String toString(JSONArray names, JSONArray ja) |
332 | throws JSONException | |
333 | { | |
334 | 0 | if (names == null || names.length() == 0) |
335 | { | |
336 | 0 | return null; |
337 | } | |
338 | 0 | StringBuffer sb = new StringBuffer(); |
339 | 0 | for (int i = 0; i < ja.length(); i += 1) |
340 | { | |
341 | 0 | JSONObject jo = ja.optJSONObject(i); |
342 | 0 | if (jo != null) |
343 | { | |
344 | 0 | sb.append(rowToString(jo.toJSONArray(names))); |
345 | } | |
346 | } | |
347 | 0 | return sb.toString(); |
348 | } | |
349 | } |