JAL-3438 spotless for 2.11.2.0
[jalview.git] / src / org / json / CDL.java
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   private static String getValue(JSONTokener x) throws JSONException
61   {
62     char c;
63     char q;
64     StringBuffer sb;
65     do
66     {
67       c = x.next();
68     } while (c == ' ' || c == '\t');
69     switch (c)
70     {
71     case 0:
72       return null;
73     case '"':
74     case '\'':
75       q = c;
76       sb = new StringBuffer();
77       for (;;)
78       {
79         c = x.next();
80         if (c == q)
81         {
82           // Handle escaped double-quote
83           char nextC = x.next();
84           if (nextC != '\"')
85           {
86             // if our quote was the end of the file, don't step
87             if (nextC > 0)
88             {
89               x.back();
90             }
91             break;
92           }
93         }
94         if (c == 0 || c == '\n' || c == '\r')
95         {
96           throw x.syntaxError("Missing close quote '" + q + "'.");
97         }
98         sb.append(c);
99       }
100       return sb.toString();
101     case ',':
102       x.back();
103       return "";
104     default:
105       x.back();
106       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   public static JSONArray rowToJSONArray(JSONTokener x) throws JSONException
119   {
120     JSONArray ja = new JSONArray();
121     for (;;)
122     {
123       String value = getValue(x);
124       char c = x.next();
125       if (value == null
126               || (ja.length() == 0 && value.length() == 0 && c != ','))
127       {
128         return null;
129       }
130       ja.put(value);
131       for (;;)
132       {
133         if (c == ',')
134         {
135           break;
136         }
137         if (c != ' ')
138         {
139           if (c == '\n' || c == '\r' || c == 0)
140           {
141             return ja;
142           }
143           throw x.syntaxError(
144                   "Bad character '" + c + "' (" + (int) c + ").");
145         }
146         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   public static JSONObject rowToJSONObject(JSONArray names, JSONTokener x)
164           throws JSONException
165   {
166     JSONArray ja = rowToJSONArray(x);
167     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   public static String rowToString(JSONArray ja)
179   {
180     StringBuilder sb = new StringBuilder();
181     for (int i = 0; i < ja.length(); i += 1)
182     {
183       if (i > 0)
184       {
185         sb.append(',');
186       }
187       Object object = ja.opt(i);
188       if (object != null)
189       {
190         String string = object.toString();
191         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           sb.append('"');
196           int length = string.length();
197           for (int j = 0; j < length; j += 1)
198           {
199             char c = string.charAt(j);
200             if (c >= ' ' && c != '"')
201             {
202               sb.append(c);
203             }
204           }
205           sb.append('"');
206         }
207         else
208         {
209           sb.append(string);
210         }
211       }
212     }
213     sb.append('\n');
214     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   public static JSONArray toJSONArray(String string) throws JSONException
227   {
228     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   public static JSONArray toJSONArray(JSONTokener x) throws JSONException
241   {
242     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   public static JSONArray toJSONArray(JSONArray names, String string)
257           throws JSONException
258   {
259     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   public static JSONArray toJSONArray(JSONArray names, JSONTokener x)
274           throws JSONException
275   {
276     if (names == null || names.length() == 0)
277     {
278       return null;
279     }
280     JSONArray ja = new JSONArray();
281     for (;;)
282     {
283       JSONObject jo = rowToJSONObject(names, x);
284       if (jo == null)
285       {
286         break;
287       }
288       ja.put(jo);
289     }
290     if (ja.length() == 0)
291     {
292       return null;
293     }
294     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   public static String toString(JSONArray ja) throws JSONException
307   {
308     JSONObject jo = ja.optJSONObject(0);
309     if (jo != null)
310     {
311       JSONArray names = jo.names();
312       if (names != null)
313       {
314         return rowToString(names) + toString(names, ja);
315       }
316     }
317     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   public static String toString(JSONArray names, JSONArray ja)
332           throws JSONException
333   {
334     if (names == null || names.length() == 0)
335     {
336       return null;
337     }
338     StringBuffer sb = new StringBuffer();
339     for (int i = 0; i < ja.length(); i += 1)
340     {
341       JSONObject jo = ja.optJSONObject(i);
342       if (jo != null)
343       {
344         sb.append(rowToString(jo.toJSONArray(names)));
345       }
346     }
347     return sb.toString();
348   }
349 }