[DMAAP-48] Initial code import
[dmaap/datarouter.git] / datarouter-prov / src / main / java / org / json / XML.java
diff --git a/datarouter-prov/src/main/java/org/json/XML.java b/datarouter-prov/src/main/java/org/json/XML.java
new file mode 100644 (file)
index 0000000..33f43e5
--- /dev/null
@@ -0,0 +1,530 @@
+/*******************************************************************************\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
+import java.util.Iterator;\r
+\r
+\r
+/**\r
+ * This provides static methods to convert an XML text into a JSONObject,\r
+ * and to covert a JSONObject into an XML text.\r
+ * @author JSON.org\r
+ * @version 2012-10-26\r
+ */\r
+public class XML {\r
+\r
+    /** The Character '&'. */\r
+    public static final Character AMP   = new Character('&');\r
+\r
+    /** The Character '''. */\r
+    public static final Character APOS  = new Character('\'');\r
+\r
+    /** The Character '!'. */\r
+    public static final Character BANG  = new Character('!');\r
+\r
+    /** The Character '='. */\r
+    public static final Character EQ    = new Character('=');\r
+\r
+    /** The Character '>'. */\r
+    public static final Character GT    = new Character('>');\r
+\r
+    /** The Character '<'. */\r
+    public static final Character LT    = new Character('<');\r
+\r
+    /** The Character '?'. */\r
+    public static final Character QUEST = new Character('?');\r
+\r
+    /** The Character '"'. */\r
+    public static final Character QUOT  = new Character('"');\r
+\r
+    /** The Character '/'. */\r
+    public static final Character SLASH = new Character('/');\r
+\r
+    /**\r
+     * Replace special characters with XML escapes:\r
+     * <pre>\r
+     * &amp; <small>(ampersand)</small> is replaced by &amp;amp;\r
+     * &lt; <small>(less than)</small> is replaced by &amp;lt;\r
+     * &gt; <small>(greater than)</small> is replaced by &amp;gt;\r
+     * &quot; <small>(double quote)</small> is replaced by &amp;quot;\r
+     * </pre>\r
+     * @param string The string to be escaped.\r
+     * @return The escaped string.\r
+     */\r
+    public static String escape(String string) {\r
+        StringBuffer sb = new StringBuffer();\r
+        for (int i = 0, length = string.length(); i < length; i++) {\r
+            char c = string.charAt(i);\r
+            switch (c) {\r
+            case '&':\r
+                sb.append("&amp;");\r
+                break;\r
+            case '<':\r
+                sb.append("&lt;");\r
+                break;\r
+            case '>':\r
+                sb.append("&gt;");\r
+                break;\r
+            case '"':\r
+                sb.append("&quot;");\r
+                break;\r
+            case '\'':\r
+                sb.append("&apos;");\r
+                break;\r
+            default:\r
+                sb.append(c);\r
+            }\r
+        }\r
+        return sb.toString();\r
+    }\r
+\r
+    /**\r
+     * Throw an exception if the string contains whitespace.\r
+     * Whitespace is not allowed in tagNames and attributes.\r
+     * @param string\r
+     * @throws JSONException\r
+     */\r
+    public static void noSpace(String string) throws JSONException {\r
+        int i, length = string.length();\r
+        if (length == 0) {\r
+            throw new JSONException("Empty string.");\r
+        }\r
+        for (i = 0; i < length; i += 1) {\r
+            if (Character.isWhitespace(string.charAt(i))) {\r
+                throw new JSONException("'" + string +\r
+                        "' contains a space character.");\r
+            }\r
+        }\r
+    }\r
+\r
+    /**\r
+     * Scan the content following the named tag, attaching it to the context.\r
+     * @param x       The XMLTokener containing the source string.\r
+     * @param context The JSONObject that will include the new material.\r
+     * @param name    The tag name.\r
+     * @return true if the close tag is processed.\r
+     * @throws JSONException\r
+     */\r
+    private static boolean parse(XMLTokener x, JSONObject context,\r
+                                 String name) throws JSONException {\r
+        char       c;\r
+        int        i;\r
+        JSONObject jsonobject = null;\r
+        String     string;\r
+        String     tagName;\r
+        Object     token;\r
+\r
+// Test for and skip past these forms:\r
+//      <!-- ... -->\r
+//      <!   ...   >\r
+//      <![  ... ]]>\r
+//      <?   ...  ?>\r
+// Report errors for these forms:\r
+//      <>\r
+//      <=\r
+//      <<\r
+\r
+        token = x.nextToken();\r
+\r
+// <!\r
+\r
+        if (token == BANG) {\r
+            c = x.next();\r
+            if (c == '-') {\r
+                if (x.next() == '-') {\r
+                    x.skipPast("-->");\r
+                    return false;\r
+                }\r
+                x.back();\r
+            } else if (c == '[') {\r
+                token = x.nextToken();\r
+                if ("CDATA".equals(token)) {\r
+                    if (x.next() == '[') {\r
+                        string = x.nextCDATA();\r
+                        if (string.length() > 0) {\r
+                            context.accumulate("content", string);\r
+                        }\r
+                        return false;\r
+                    }\r
+                }\r
+                throw x.syntaxError("Expected 'CDATA['");\r
+            }\r
+            i = 1;\r
+            do {\r
+                token = x.nextMeta();\r
+                if (token == null) {\r
+                    throw x.syntaxError("Missing '>' after '<!'.");\r
+                } else if (token == LT) {\r
+                    i += 1;\r
+                } else if (token == GT) {\r
+                    i -= 1;\r
+                }\r
+            } while (i > 0);\r
+            return false;\r
+        } else if (token == QUEST) {\r
+\r
+// <?\r
+\r
+            x.skipPast("?>");\r
+            return false;\r
+        } else if (token == SLASH) {\r
+\r
+// Close tag </\r
+\r
+            token = x.nextToken();\r
+            if (name == null) {\r
+                throw x.syntaxError("Mismatched close tag " + token);\r
+            }\r
+            if (!token.equals(name)) {\r
+                throw x.syntaxError("Mismatched " + name + " and " + token);\r
+            }\r
+            if (x.nextToken() != GT) {\r
+                throw x.syntaxError("Misshaped close tag");\r
+            }\r
+            return true;\r
+\r
+        } else if (token instanceof Character) {\r
+            throw x.syntaxError("Misshaped tag");\r
+\r
+// Open tag <\r
+\r
+        } else {\r
+            tagName = (String)token;\r
+            token = null;\r
+            jsonobject = new JSONObject();\r
+            for (;;) {\r
+                if (token == null) {\r
+                    token = x.nextToken();\r
+                }\r
+\r
+// attribute = value\r
+\r
+                if (token instanceof String) {\r
+                    string = (String)token;\r
+                    token = x.nextToken();\r
+                    if (token == EQ) {\r
+                        token = x.nextToken();\r
+                        if (!(token instanceof String)) {\r
+                            throw x.syntaxError("Missing value");\r
+                        }\r
+                        jsonobject.accumulate(string,\r
+                                XML.stringToValue((String)token));\r
+                        token = null;\r
+                    } else {\r
+                        jsonobject.accumulate(string, "");\r
+                    }\r
+\r
+// Empty tag <.../>\r
+\r
+                } else if (token == SLASH) {\r
+                    if (x.nextToken() != GT) {\r
+                        throw x.syntaxError("Misshaped tag");\r
+                    }\r
+                    if (jsonobject.length() > 0) {\r
+                        context.accumulate(tagName, jsonobject);\r
+                    } else {\r
+                        context.accumulate(tagName, "");\r
+                    }\r
+                    return false;\r
+\r
+// Content, between <...> and </...>\r
+\r
+                } else if (token == GT) {\r
+                    for (;;) {\r
+                        token = x.nextContent();\r
+                        if (token == null) {\r
+                            if (tagName != null) {\r
+                                throw x.syntaxError("Unclosed tag " + tagName);\r
+                            }\r
+                            return false;\r
+                        } else if (token instanceof String) {\r
+                            string = (String)token;\r
+                            if (string.length() > 0) {\r
+                                jsonobject.accumulate("content",\r
+                                        XML.stringToValue(string));\r
+                            }\r
+\r
+// Nested element\r
+\r
+                        } else if (token == LT) {\r
+                            if (parse(x, jsonobject, tagName)) {\r
+                                if (jsonobject.length() == 0) {\r
+                                    context.accumulate(tagName, "");\r
+                                } else if (jsonobject.length() == 1 &&\r
+                                       jsonobject.opt("content") != null) {\r
+                                    context.accumulate(tagName,\r
+                                            jsonobject.opt("content"));\r
+                                } else {\r
+                                    context.accumulate(tagName, jsonobject);\r
+                                }\r
+                                return false;\r
+                            }\r
+                        }\r
+                    }\r
+                } else {\r
+                    throw x.syntaxError("Misshaped tag");\r
+                }\r
+            }\r
+        }\r
+    }\r
+\r
+\r
+    /**\r
+     * Try to convert a string into a number, boolean, or null. If the string\r
+     * can't be converted, return the string. This is much less ambitious than\r
+     * JSONObject.stringToValue, especially because it does not attempt to\r
+     * convert plus forms, octal forms, hex forms, or E forms lacking decimal\r
+     * points.\r
+     * @param string A String.\r
+     * @return A simple JSON value.\r
+     */\r
+    public static Object stringToValue(String string) {\r
+        if ("".equals(string)) {\r
+            return string;\r
+        }\r
+        if ("true".equalsIgnoreCase(string)) {\r
+            return Boolean.TRUE;\r
+        }\r
+        if ("false".equalsIgnoreCase(string)) {\r
+            return Boolean.FALSE;\r
+        }\r
+        if ("null".equalsIgnoreCase(string)) {\r
+            return JSONObject.NULL;\r
+        }\r
+        if ("0".equals(string)) {\r
+            return new Integer(0);\r
+        }\r
+\r
+// If it might be a number, try converting it. If that doesn't work,\r
+// return the string.\r
+\r
+        try {\r
+            char initial = string.charAt(0);\r
+            boolean negative = false;\r
+            if (initial == '-') {\r
+                initial = string.charAt(1);\r
+                negative = true;\r
+            }\r
+            if (initial == '0' && string.charAt(negative ? 2 : 1) == '0') {\r
+                return string;\r
+            }\r
+            if ((initial >= '0' && initial <= '9')) {\r
+                if (string.indexOf('.') >= 0) {\r
+                    return Double.valueOf(string);\r
+                } else if (string.indexOf('e') < 0 && string.indexOf('E') < 0) {\r
+                    Long myLong = new Long(string);\r
+                    if (myLong.longValue() == myLong.intValue()) {\r
+                        return new Integer(myLong.intValue());\r
+                    } else {\r
+                        return myLong;\r
+                    }\r
+                }\r
+            }\r
+        }  catch (Exception ignore) {\r
+        }\r
+        return string;\r
+    }\r
+\r
+\r
+    /**\r
+     * Convert a well-formed (but not necessarily valid) XML string into a\r
+     * JSONObject. Some information may be lost in this transformation\r
+     * because JSON is a data format and XML is a document format. XML uses\r
+     * elements, attributes, and content text, while JSON uses unordered\r
+     * collections of name/value pairs and arrays of values. JSON does not\r
+     * does not like to distinguish between elements and attributes.\r
+     * Sequences of similar elements are represented as JSONArrays. Content\r
+     * text may be placed in a "content" member. Comments, prologs, DTDs, and\r
+     * <code>&lt;[ [ ]]></code> are ignored.\r
+     * @param string The source string.\r
+     * @return A JSONObject containing the structured data from the XML string.\r
+     * @throws JSONException\r
+     */\r
+    public static JSONObject toJSONObject(String string) throws JSONException {\r
+        JSONObject jo = new JSONObject();\r
+        XMLTokener x = new XMLTokener(string);\r
+        while (x.more() && x.skipPast("<")) {\r
+            parse(x, jo, null);\r
+        }\r
+        return jo;\r
+    }\r
+\r
+\r
+    /**\r
+     * Convert a JSONObject into a well-formed, element-normal XML string.\r
+     * @param object A JSONObject.\r
+     * @return  A string.\r
+     * @throws  JSONException\r
+     */\r
+    public static String toString(Object object) throws JSONException {\r
+        return toString(object, null);\r
+    }\r
+\r
+\r
+    /**\r
+     * Convert a JSONObject into a well-formed, element-normal XML string.\r
+     * @param object A JSONObject.\r
+     * @param tagName The optional name of the enclosing tag.\r
+     * @return A string.\r
+     * @throws JSONException\r
+     */\r
+    public static String toString(Object object, String tagName)\r
+            throws JSONException {\r
+        StringBuffer sb = new StringBuffer();\r
+        int          i;\r
+        JSONArray    ja;\r
+        JSONObject   jo;\r
+        String       key;\r
+        Iterator<String> keys;\r
+        int          length;\r
+        String       string;\r
+        Object       value;\r
+        if (object instanceof JSONObject) {\r
+\r
+// Emit <tagName>\r
+\r
+            if (tagName != null) {\r
+                sb.append('<');\r
+                sb.append(tagName);\r
+                sb.append('>');\r
+            }\r
+\r
+// Loop thru the keys.\r
+\r
+            jo = (JSONObject)object;\r
+            keys = jo.keys();\r
+            while (keys.hasNext()) {\r
+                key = keys.next().toString();\r
+                value = jo.opt(key);\r
+                if (value == null) {\r
+                    value = "";\r
+                }\r
+                if (value instanceof String) {\r
+                    string = (String)value;\r
+                } else {\r
+                    string = null;\r
+                }\r
+\r
+// Emit content in body\r
+\r
+                if ("content".equals(key)) {\r
+                    if (value instanceof JSONArray) {\r
+                        ja = (JSONArray)value;\r
+                        length = ja.length();\r
+                        for (i = 0; i < length; i += 1) {\r
+                            if (i > 0) {\r
+                                sb.append('\n');\r
+                            }\r
+                            sb.append(escape(ja.get(i).toString()));\r
+                        }\r
+                    } else {\r
+                        sb.append(escape(value.toString()));\r
+                    }\r
+\r
+// Emit an array of similar keys\r
+\r
+                } else if (value instanceof JSONArray) {\r
+                    ja = (JSONArray)value;\r
+                    length = ja.length();\r
+                    for (i = 0; i < length; i += 1) {\r
+                        value = ja.get(i);\r
+                        if (value instanceof JSONArray) {\r
+                            sb.append('<');\r
+                            sb.append(key);\r
+                            sb.append('>');\r
+                            sb.append(toString(value));\r
+                            sb.append("</");\r
+                            sb.append(key);\r
+                            sb.append('>');\r
+                        } else {\r
+                            sb.append(toString(value, key));\r
+                        }\r
+                    }\r
+                } else if ("".equals(value)) {\r
+                    sb.append('<');\r
+                    sb.append(key);\r
+                    sb.append("/>");\r
+\r
+// Emit a new tag <k>\r
+\r
+                } else {\r
+                    sb.append(toString(value, key));\r
+                }\r
+            }\r
+            if (tagName != null) {\r
+\r
+// Emit the </tagname> close tag\r
+\r
+                sb.append("</");\r
+                sb.append(tagName);\r
+                sb.append('>');\r
+            }\r
+            return sb.toString();\r
+\r
+// XML does not have good support for arrays. If an array appears in a place\r
+// where XML is lacking, synthesize an <array> element.\r
+\r
+        } else {\r
+            if (object.getClass().isArray()) {\r
+                object = new JSONArray(object);\r
+            }\r
+            if (object instanceof JSONArray) {\r
+                ja = (JSONArray)object;\r
+                length = ja.length();\r
+                for (i = 0; i < length; i += 1) {\r
+                    sb.append(toString(ja.opt(i), tagName == null ? "array" : tagName));\r
+                }\r
+                return sb.toString();\r
+            } else {\r
+                string = (object == null) ? "null" : escape(object.toString());\r
+                return (tagName == null) ? "\"" + string + "\"" :\r
+                    (string.length() == 0) ? "<" + tagName + "/>" :\r
+                    "<" + tagName + ">" + string + "</" + tagName + ">";\r
+            }\r
+        }\r
+    }\r
+}\r