1 /*******************************************************************************
\r
2 * ============LICENSE_START==================================================
\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
11 * * http://www.apache.org/licenses/LICENSE-2.0
\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
20 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
\r
22 ******************************************************************************/
\r
29 * Get the next value. The value can be wrapped in quotes. The value can
\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
35 private static String getValue(JSONTokener x) throws JSONException {
\r
41 } while (c == ' ' || c == '\t');
\r
48 sb = new StringBuffer();
\r
54 if (c == 0 || c == '\n' || c == '\r') {
\r
55 throw x.syntaxError("Missing close quote '" + q + "'.");
\r
59 return sb.toString();
\r
65 return x.nextTo(',');
\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
75 public static JSONArray rowToJSONArray(JSONTokener x) throws JSONException {
\r
76 JSONArray ja = new JSONArray();
\r
78 String value = getValue(x);
\r
80 if (value == null ||
\r
81 (ja.length() == 0 && value.length() == 0 && c != ',')) {
\r
90 if (c == '\n' || c == '\r' || c == 0) {
\r
93 throw x.syntaxError("Bad character '" + c + "' (" +
\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
107 * @param x A JSONTokener of the source text.
\r
108 * @return A JSONObject combining the names and values.
\r
109 * @throws JSONException
\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
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
121 * @param ja A JSONArray of strings.
\r
122 * @return A string ending in NEWLINE.
\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
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
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
151 return sb.toString();
\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
161 public static JSONArray toJSONArray(String string) throws JSONException {
\r
162 return toJSONArray(new JSONTokener(string));
\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
172 public static JSONArray toJSONArray(JSONTokener x) throws JSONException {
\r
173 return toJSONArray(rowToJSONArray(x), x);
\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
184 public static JSONArray toJSONArray(JSONArray names, String string)
\r
185 throws JSONException {
\r
186 return toJSONArray(names, new JSONTokener(string));
\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
197 public static JSONArray toJSONArray(JSONArray names, JSONTokener x)
\r
198 throws JSONException {
\r
199 if (names == null || names.length() == 0) {
\r
202 JSONArray ja = new JSONArray();
\r
204 JSONObject jo = rowToJSONObject(names, x);
\r
210 if (ja.length() == 0) {
\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
221 * @param ja A JSONArray of JSONObjects.
\r
222 * @return A comma delimited text.
\r
223 * @throws JSONException
\r
225 public static String toString(JSONArray ja) throws JSONException {
\r
226 JSONObject jo = ja.optJSONObject(0);
\r
228 JSONArray names = jo.names();
\r
229 if (names != null) {
\r
230 return rowToString(names) + toString(names, ja);
\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
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
245 public static String toString(JSONArray names, JSONArray ja)
\r
246 throws JSONException {
\r
247 if (names == null || names.length() == 0) {
\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
254 sb.append(rowToString(jo.toJSONArray(names)));
\r
257 return sb.toString();
\r