--- /dev/null
+/*******************************************************************************\r
+ * ============LICENSE_START==================================================\r
+ * * org.onap.dmaap\r
+ * * ===========================================================================\r
+ * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
+ * * ===========================================================================\r
+ * * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * * you may not use this file except in compliance with the License.\r
+ * * You may obtain a copy of the License at\r
+ * * \r
+ * * http://www.apache.org/licenses/LICENSE-2.0\r
+ * * \r
+ * * Unless required by applicable law or agreed to in writing, software\r
+ * * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * * See the License for the specific language governing permissions and\r
+ * * limitations under the License.\r
+ * * ============LICENSE_END====================================================\r
+ * *\r
+ * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
+ * *\r
+ ******************************************************************************/\r
+package org.json;\r
+\r
+/*\r
+Copyright (c) 2002 JSON.org\r
+\r
+Permission is hereby granted, free of charge, to any person obtaining a copy\r
+of this software and associated documentation files (the "Software"), to deal\r
+in the Software without restriction, including without limitation the rights\r
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\r
+copies of the Software, and to permit persons to whom the Software is\r
+furnished to do so, subject to the following conditions:\r
+\r
+The above copyright notice and this permission notice shall be included in all\r
+copies or substantial portions of the Software.\r
+\r
+The Software shall be used for Good, not Evil.\r
+\r
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\r
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\r
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\r
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\r
+SOFTWARE.\r
+*/\r
+\r
+/**\r
+ * This provides static methods to convert comma delimited text into a\r
+ * JSONArray, and to covert a JSONArray into comma delimited text. Comma\r
+ * delimited text is a very popular format for data interchange. It is\r
+ * understood by most database, spreadsheet, and organizer programs.\r
+ * <p>\r
+ * Each row of text represents a row in a table or a data record. Each row\r
+ * ends with a NEWLINE character. Each row contains one or more values.\r
+ * Values are separated by commas. A value can contain any character except\r
+ * for comma, unless is is wrapped in single quotes or double quotes.\r
+ * <p>\r
+ * The first row usually contains the names of the columns.\r
+ * <p>\r
+ * A comma delimited list can be converted into a JSONArray of JSONObjects.\r
+ * The names for the elements in the JSONObjects can be taken from the names\r
+ * in the first row.\r
+ * @author JSON.org\r
+ * @version 2012-11-13\r
+ */\r
+public class CDL {\r
+\r
+ /**\r
+ * Get the next value. The value can be wrapped in quotes. The value can\r
+ * be empty.\r
+ * @param x A JSONTokener of the source text.\r
+ * @return The value string, or null if empty.\r
+ * @throws JSONException if the quoted string is badly formed.\r
+ */\r
+ private static String getValue(JSONTokener x) throws JSONException {\r
+ char c;\r
+ char q;\r
+ StringBuffer sb;\r
+ do {\r
+ c = x.next();\r
+ } while (c == ' ' || c == '\t');\r
+ switch (c) {\r
+ case 0:\r
+ return null;\r
+ case '"':\r
+ case '\'':\r
+ q = c;\r
+ sb = new StringBuffer();\r
+ for (;;) {\r
+ c = x.next();\r
+ if (c == q) {\r
+ break;\r
+ }\r
+ if (c == 0 || c == '\n' || c == '\r') {\r
+ throw x.syntaxError("Missing close quote '" + q + "'.");\r
+ }\r
+ sb.append(c);\r
+ }\r
+ return sb.toString();\r
+ case ',':\r
+ x.back();\r
+ return "";\r
+ default:\r
+ x.back();\r
+ return x.nextTo(',');\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Produce a JSONArray of strings from a row of comma delimited values.\r
+ * @param x A JSONTokener of the source text.\r
+ * @return A JSONArray of strings.\r
+ * @throws JSONException\r
+ */\r
+ public static JSONArray rowToJSONArray(JSONTokener x) throws JSONException {\r
+ JSONArray ja = new JSONArray();\r
+ for (;;) {\r
+ String value = getValue(x);\r
+ char c = x.next();\r
+ if (value == null ||\r
+ (ja.length() == 0 && value.length() == 0 && c != ',')) {\r
+ return null;\r
+ }\r
+ ja.put(value);\r
+ for (;;) {\r
+ if (c == ',') {\r
+ break;\r
+ }\r
+ if (c != ' ') {\r
+ if (c == '\n' || c == '\r' || c == 0) {\r
+ return ja;\r
+ }\r
+ throw x.syntaxError("Bad character '" + c + "' (" +\r
+ (int)c + ").");\r
+ }\r
+ c = x.next();\r
+ }\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Produce a JSONObject from a row of comma delimited text, using a\r
+ * parallel JSONArray of strings to provides the names of the elements.\r
+ * @param names A JSONArray of names. This is commonly obtained from the\r
+ * first row of a comma delimited text file using the rowToJSONArray\r
+ * method.\r
+ * @param x A JSONTokener of the source text.\r
+ * @return A JSONObject combining the names and values.\r
+ * @throws JSONException\r
+ */\r
+ public static JSONObject rowToJSONObject(JSONArray names, JSONTokener x)\r
+ throws JSONException {\r
+ JSONArray ja = rowToJSONArray(x);\r
+ return ja != null ? ja.toJSONObject(names) : null;\r
+ }\r
+\r
+ /**\r
+ * Produce a comma delimited text row from a JSONArray. Values containing\r
+ * the comma character will be quoted. Troublesome characters may be\r
+ * removed.\r
+ * @param ja A JSONArray of strings.\r
+ * @return A string ending in NEWLINE.\r
+ */\r
+ public static String rowToString(JSONArray ja) {\r
+ StringBuffer sb = new StringBuffer();\r
+ for (int i = 0; i < ja.length(); i += 1) {\r
+ if (i > 0) {\r
+ sb.append(',');\r
+ }\r
+ Object object = ja.opt(i);\r
+ if (object != null) {\r
+ String string = object.toString();\r
+ if (string.length() > 0 && (string.indexOf(',') >= 0 ||\r
+ string.indexOf('\n') >= 0 || string.indexOf('\r') >= 0 ||\r
+ string.indexOf(0) >= 0 || string.charAt(0) == '"')) {\r
+ sb.append('"');\r
+ int length = string.length();\r
+ for (int j = 0; j < length; j += 1) {\r
+ char c = string.charAt(j);\r
+ if (c >= ' ' && c != '"') {\r
+ sb.append(c);\r
+ }\r
+ }\r
+ sb.append('"');\r
+ } else {\r
+ sb.append(string);\r
+ }\r
+ }\r
+ }\r
+ sb.append('\n');\r
+ return sb.toString();\r
+ }\r
+\r
+ /**\r
+ * Produce a JSONArray of JSONObjects from a comma delimited text string,\r
+ * using the first row as a source of names.\r
+ * @param string The comma delimited text.\r
+ * @return A JSONArray of JSONObjects.\r
+ * @throws JSONException\r
+ */\r
+ public static JSONArray toJSONArray(String string) throws JSONException {\r
+ return toJSONArray(new JSONTokener(string));\r
+ }\r
+\r
+ /**\r
+ * Produce a JSONArray of JSONObjects from a comma delimited text string,\r
+ * using the first row as a source of names.\r
+ * @param x The JSONTokener containing the comma delimited text.\r
+ * @return A JSONArray of JSONObjects.\r
+ * @throws JSONException\r
+ */\r
+ public static JSONArray toJSONArray(JSONTokener x) throws JSONException {\r
+ return toJSONArray(rowToJSONArray(x), x);\r
+ }\r
+\r
+ /**\r
+ * Produce a JSONArray of JSONObjects from a comma delimited text string\r
+ * using a supplied JSONArray as the source of element names.\r
+ * @param names A JSONArray of strings.\r
+ * @param string The comma delimited text.\r
+ * @return A JSONArray of JSONObjects.\r
+ * @throws JSONException\r
+ */\r
+ public static JSONArray toJSONArray(JSONArray names, String string)\r
+ throws JSONException {\r
+ return toJSONArray(names, new JSONTokener(string));\r
+ }\r
+\r
+ /**\r
+ * Produce a JSONArray of JSONObjects from a comma delimited text string\r
+ * using a supplied JSONArray as the source of element names.\r
+ * @param names A JSONArray of strings.\r
+ * @param x A JSONTokener of the source text.\r
+ * @return A JSONArray of JSONObjects.\r
+ * @throws JSONException\r
+ */\r
+ public static JSONArray toJSONArray(JSONArray names, JSONTokener x)\r
+ throws JSONException {\r
+ if (names == null || names.length() == 0) {\r
+ return null;\r
+ }\r
+ JSONArray ja = new JSONArray();\r
+ for (;;) {\r
+ JSONObject jo = rowToJSONObject(names, x);\r
+ if (jo == null) {\r
+ break;\r
+ }\r
+ ja.put(jo);\r
+ }\r
+ if (ja.length() == 0) {\r
+ return null;\r
+ }\r
+ return ja;\r
+ }\r
+\r
+\r
+ /**\r
+ * Produce a comma delimited text from a JSONArray of JSONObjects. The\r
+ * first row will be a list of names obtained by inspecting the first\r
+ * JSONObject.\r
+ * @param ja A JSONArray of JSONObjects.\r
+ * @return A comma delimited text.\r
+ * @throws JSONException\r
+ */\r
+ public static String toString(JSONArray ja) throws JSONException {\r
+ JSONObject jo = ja.optJSONObject(0);\r
+ if (jo != null) {\r
+ JSONArray names = jo.names();\r
+ if (names != null) {\r
+ return rowToString(names) + toString(names, ja);\r
+ }\r
+ }\r
+ return null;\r
+ }\r
+\r
+ /**\r
+ * Produce a comma delimited text from a JSONArray of JSONObjects using\r
+ * a provided list of names. The list of names is not included in the\r
+ * output.\r
+ * @param names A JSONArray of strings.\r
+ * @param ja A JSONArray of JSONObjects.\r
+ * @return A comma delimited text.\r
+ * @throws JSONException\r
+ */\r
+ public static String toString(JSONArray names, JSONArray ja)\r
+ throws JSONException {\r
+ if (names == null || names.length() == 0) {\r
+ return null;\r
+ }\r
+ StringBuffer sb = new StringBuffer();\r
+ for (int i = 0; i < ja.length(); i += 1) {\r
+ JSONObject jo = ja.optJSONObject(i);\r
+ if (jo != null) {\r
+ sb.append(rowToString(jo.toJSONArray(names)));\r
+ }\r
+ }\r
+ return sb.toString();\r
+ }\r
+}\r