Renaming openecomp to onap
[aai/model-loader.git] / src / main / java / org / onap / aai / modelloader / util / JsonXmlConverter.java
1 /**\r
2  * ============LICENSE_START=======================================================\r
3  * org.onap.aai\r
4  * ================================================================================\r
5  * Copyright © 2017 AT&T Intellectual Property.\r
6  * Copyright © 2017 Amdocs\r
7  * All rights reserved.\r
8  * ================================================================================\r
9  * Licensed under the Apache License, Version 2.0 (the "License");\r
10  * you may not use this file except in compliance with the License.\r
11  * You may obtain a copy of the License at\r
12  *\r
13  *     http://www.apache.org/licenses/LICENSE-2.0\r
14  *\r
15  * Unless required by applicable law or agreed to in writing, software\r
16  * distributed under the License is distributed on an "AS IS" BASIS,\r
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
18  * See the License for the specific language governing permissions and\r
19  * limitations under the License.\r
20  * ============LICENSE_END=========================================================\r
21  *\r
22  * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
23  */\r
24 package org.onap.aai.modelloader.util;\r
25 \r
26 import org.json.JSONArray;\r
27 import org.json.JSONException;\r
28 import org.json.JSONObject;\r
29 import org.json.XML;\r
30 \r
31 public class JsonXmlConverter {\r
32 \r
33   /**\r
34    * Determines whether or not the supplied text string represents a valid\r
35    * JSON structure or not.\r
36    * \r
37    * @param text - The text to be evaluated.\r
38    * \r
39    * @return - true if the string represents a valid JSON object,\r
40    *           false, otherwise.\r
41    */\r
42   public static boolean isValidJson(String text) {\r
43     try {\r
44       new JSONObject(text);\r
45     } catch (JSONException ex) {\r
46       try {\r
47         new JSONArray(text);\r
48       } catch (JSONException ex1) {\r
49         return false;\r
50       }\r
51     }\r
52 \r
53     return true;\r
54   }\r
55 \r
56   /**\r
57    * Takes a text string representing a valid JSON structure and converts it to\r
58    * an equivalent XML string.\r
59    * \r
60    * @param jsonText - The JSON string to convert to XML.\r
61    * \r
62    * @return - An XML string representation of the supplied JSON string.\r
63    */\r
64   public static String convertJsonToXml(String jsonText) {\r
65     JSONObject jsonObj = new JSONObject(jsonText);\r
66     String xmlText = XML.toString(jsonObj);\r
67     return xmlText;\r
68   }\r
69 \r
70   /**\r
71    * Takes a text string representing a valid XML structure and converts it to\r
72    * an equivalent JSON string.\r
73    * \r
74    * @param xmlText - The XML string to convert to JSON.\r
75    * \r
76    * @return - A JSON string representation of the supplied XML string.\r
77    */\r
78   public static String convertXmlToJson(String xmlText) {\r
79     JSONObject jsonObj = XML.toJSONObject(xmlText);\r
80     return jsonObj.toString();\r
81   }\r
82 }\r