[DMAAP-48] Initial code import
[dmaap/datarouter.git] / datarouter-prov / src / main / java / org / json / JSONWriter.java
diff --git a/datarouter-prov/src/main/java/org/json/JSONWriter.java b/datarouter-prov/src/main/java/org/json/JSONWriter.java
new file mode 100644 (file)
index 0000000..a9b0bab
--- /dev/null
@@ -0,0 +1,349 @@
+/*******************************************************************************\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
+import java.io.IOException;\r
+import java.io.Writer;\r
+\r
+/*\r
+Copyright (c) 2006 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
+ * JSONWriter provides a quick and convenient way of producing JSON text.\r
+ * The texts produced strictly conform to JSON syntax rules. No whitespace is\r
+ * added, so the results are ready for transmission or storage. Each instance of\r
+ * JSONWriter can produce one JSON text.\r
+ * <p>\r
+ * A JSONWriter instance provides a <code>value</code> method for appending\r
+ * values to the\r
+ * text, and a <code>key</code>\r
+ * method for adding keys before values in objects. There are <code>array</code>\r
+ * and <code>endArray</code> methods that make and bound array values, and\r
+ * <code>object</code> and <code>endObject</code> methods which make and bound\r
+ * object values. All of these methods return the JSONWriter instance,\r
+ * permitting a cascade style. For example, <pre>\r
+ * new JSONWriter(myWriter)\r
+ *     .object()\r
+ *         .key("JSON")\r
+ *         .value("Hello, World!")\r
+ *     .endObject();</pre> which writes <pre>\r
+ * {"JSON":"Hello, World!"}</pre>\r
+ * <p>\r
+ * The first method called must be <code>array</code> or <code>object</code>.\r
+ * There are no methods for adding commas or colons. JSONWriter adds them for\r
+ * you. Objects and arrays can be nested up to 20 levels deep.\r
+ * <p>\r
+ * This can sometimes be easier than using a JSONObject to build a string.\r
+ * @author JSON.org\r
+ * @version 2011-11-24\r
+ */\r
+public class JSONWriter {\r
+    private static final int maxdepth = 200;\r
+\r
+    /**\r
+     * The comma flag determines if a comma should be output before the next\r
+     * value.\r
+     */\r
+    private boolean comma;\r
+\r
+    /**\r
+     * The current mode. Values:\r
+     * 'a' (array),\r
+     * 'd' (done),\r
+     * 'i' (initial),\r
+     * 'k' (key),\r
+     * 'o' (object).\r
+     */\r
+    protected char mode;\r
+\r
+    /**\r
+     * The object/array stack.\r
+     */\r
+    private final JSONObject stack[];\r
+\r
+    /**\r
+     * The stack top index. A value of 0 indicates that the stack is empty.\r
+     */\r
+    private int top;\r
+\r
+    /**\r
+     * The writer that will receive the output.\r
+     */\r
+    protected Writer writer;\r
+\r
+    /**\r
+     * Make a fresh JSONWriter. It can be used to build one JSON text.\r
+     */\r
+    public JSONWriter(Writer w) {\r
+        this.comma = false;\r
+        this.mode = 'i';\r
+        this.stack = new JSONObject[maxdepth];\r
+        this.top = 0;\r
+        this.writer = w;\r
+    }\r
+\r
+    /**\r
+     * Append a value.\r
+     * @param string A string value.\r
+     * @return this\r
+     * @throws JSONException If the value is out of sequence.\r
+     */\r
+    private JSONWriter append(String string) throws JSONException {\r
+        if (string == null) {\r
+            throw new JSONException("Null pointer");\r
+        }\r
+        if (this.mode == 'o' || this.mode == 'a') {\r
+            try {\r
+                if (this.comma && this.mode == 'a') {\r
+                    this.writer.write(',');\r
+                }\r
+                this.writer.write(string);\r
+            } catch (IOException e) {\r
+                throw new JSONException(e);\r
+            }\r
+            if (this.mode == 'o') {\r
+                this.mode = 'k';\r
+            }\r
+            this.comma = true;\r
+            return this;\r
+        }\r
+        throw new JSONException("Value out of sequence.");\r
+    }\r
+\r
+    /**\r
+     * Begin appending a new array. All values until the balancing\r
+     * <code>endArray</code> will be appended to this array. The\r
+     * <code>endArray</code> method must be called to mark the array's end.\r
+     * @return this\r
+     * @throws JSONException If the nesting is too deep, or if the object is\r
+     * started in the wrong place (for example as a key or after the end of the\r
+     * outermost array or object).\r
+     */\r
+    public JSONWriter array() throws JSONException {\r
+        if (this.mode == 'i' || this.mode == 'o' || this.mode == 'a') {\r
+            this.push(null);\r
+            this.append("[");\r
+            this.comma = false;\r
+            return this;\r
+        }\r
+        throw new JSONException("Misplaced array.");\r
+    }\r
+\r
+    /**\r
+     * End something.\r
+     * @param mode Mode\r
+     * @param c Closing character\r
+     * @return this\r
+     * @throws JSONException If unbalanced.\r
+     */\r
+    private JSONWriter end(char mode, char c) throws JSONException {\r
+        if (this.mode != mode) {\r
+            throw new JSONException(mode == 'a'\r
+                ? "Misplaced endArray."\r
+                : "Misplaced endObject.");\r
+        }\r
+        this.pop(mode);\r
+        try {\r
+            this.writer.write(c);\r
+        } catch (IOException e) {\r
+            throw new JSONException(e);\r
+        }\r
+        this.comma = true;\r
+        return this;\r
+    }\r
+\r
+    /**\r
+     * End an array. This method most be called to balance calls to\r
+     * <code>array</code>.\r
+     * @return this\r
+     * @throws JSONException If incorrectly nested.\r
+     */\r
+    public JSONWriter endArray() throws JSONException {\r
+        return this.end('a', ']');\r
+    }\r
+\r
+    /**\r
+     * End an object. This method most be called to balance calls to\r
+     * <code>object</code>.\r
+     * @return this\r
+     * @throws JSONException If incorrectly nested.\r
+     */\r
+    public JSONWriter endObject() throws JSONException {\r
+        return this.end('k', '}');\r
+    }\r
+\r
+    /**\r
+     * Append a key. The key will be associated with the next value. In an\r
+     * object, every value must be preceded by a key.\r
+     * @param string A key string.\r
+     * @return this\r
+     * @throws JSONException If the key is out of place. For example, keys\r
+     *  do not belong in arrays or if the key is null.\r
+     */\r
+    public JSONWriter key(String string) throws JSONException {\r
+        if (string == null) {\r
+            throw new JSONException("Null key.");\r
+        }\r
+        if (this.mode == 'k') {\r
+            try {\r
+                this.stack[this.top - 1].putOnce(string, Boolean.TRUE);\r
+                if (this.comma) {\r
+                    this.writer.write(',');\r
+                }\r
+                this.writer.write(JSONObject.quote(string));\r
+                this.writer.write(':');\r
+                this.comma = false;\r
+                this.mode = 'o';\r
+                return this;\r
+            } catch (IOException e) {\r
+                throw new JSONException(e);\r
+            }\r
+        }\r
+        throw new JSONException("Misplaced key.");\r
+    }\r
+\r
+\r
+    /**\r
+     * Begin appending a new object. All keys and values until the balancing\r
+     * <code>endObject</code> will be appended to this object. The\r
+     * <code>endObject</code> method must be called to mark the object's end.\r
+     * @return this\r
+     * @throws JSONException If the nesting is too deep, or if the object is\r
+     * started in the wrong place (for example as a key or after the end of the\r
+     * outermost array or object).\r
+     */\r
+    public JSONWriter object() throws JSONException {\r
+        if (this.mode == 'i') {\r
+            this.mode = 'o';\r
+        }\r
+        if (this.mode == 'o' || this.mode == 'a') {\r
+            this.append("{");\r
+            this.push(new JSONObject());\r
+            this.comma = false;\r
+            return this;\r
+        }\r
+        throw new JSONException("Misplaced object.");\r
+\r
+    }\r
+\r
+\r
+    /**\r
+     * Pop an array or object scope.\r
+     * @param c The scope to close.\r
+     * @throws JSONException If nesting is wrong.\r
+     */\r
+    private void pop(char c) throws JSONException {\r
+        if (this.top <= 0) {\r
+            throw new JSONException("Nesting error.");\r
+        }\r
+        char m = this.stack[this.top - 1] == null ? 'a' : 'k';\r
+        if (m != c) {\r
+            throw new JSONException("Nesting error.");\r
+        }\r
+        this.top -= 1;\r
+        this.mode = this.top == 0\r
+            ? 'd'\r
+            : this.stack[this.top - 1] == null\r
+            ? 'a'\r
+            : 'k';\r
+    }\r
+\r
+    /**\r
+     * Push an array or object scope.\r
+     * @param jo The scope to open.\r
+     * @throws JSONException If nesting is too deep.\r
+     */\r
+    private void push(JSONObject jo) throws JSONException {\r
+        if (this.top >= maxdepth) {\r
+            throw new JSONException("Nesting too deep.");\r
+        }\r
+        this.stack[this.top] = jo;\r
+        this.mode = jo == null ? 'a' : 'k';\r
+        this.top += 1;\r
+    }\r
+\r
+\r
+    /**\r
+     * Append either the value <code>true</code> or the value\r
+     * <code>false</code>.\r
+     * @param b A boolean.\r
+     * @return this\r
+     * @throws JSONException\r
+     */\r
+    public JSONWriter value(boolean b) throws JSONException {\r
+        return this.append(b ? "true" : "false");\r
+    }\r
+\r
+    /**\r
+     * Append a double value.\r
+     * @param d A double.\r
+     * @return this\r
+     * @throws JSONException If the number is not finite.\r
+     */\r
+    public JSONWriter value(double d) throws JSONException {\r
+        return this.value(new Double(d));\r
+    }\r
+\r
+    /**\r
+     * Append a long value.\r
+     * @param l A long.\r
+     * @return this\r
+     * @throws JSONException\r
+     */\r
+    public JSONWriter value(long l) throws JSONException {\r
+        return this.append(Long.toString(l));\r
+    }\r
+\r
+\r
+    /**\r
+     * Append an object value.\r
+     * @param object The object to append. It can be null, or a Boolean, Number,\r
+     *   String, JSONObject, or JSONArray, or an object that implements JSONString.\r
+     * @return this\r
+     * @throws JSONException If the value is out of sequence.\r
+     */\r
+    public JSONWriter value(Object object) throws JSONException {\r
+        return this.append(JSONObject.valueToString(object));\r
+    }\r
+}\r