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