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