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