First part of onap rename
[appc.git] / appc-config / appc-config-generator / provider / src / main / java / org / onap / sdnc / config / generator / tool / CheckDataTool.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APP-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property.  All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdnc.config.generator.tool;
22
23 import java.io.StringReader;
24
25 import javax.xml.parsers.DocumentBuilder;
26 import javax.xml.parsers.DocumentBuilderFactory;
27
28 import org.codehaus.jettison.json.JSONArray;
29 import org.codehaus.jettison.json.JSONException;
30 import org.codehaus.jettison.json.JSONObject;
31 import org.openecomp.sdnc.config.generator.ConfigGeneratorConstant;
32 import org.w3c.dom.Document;
33 import org.xml.sax.InputSource;
34
35 public class CheckDataTool {
36
37     public static String checkData(String data){
38         boolean isJSON = isJSON(data);
39         if(isJSON){
40             return ConfigGeneratorConstant.DATA_TYPE_JSON;
41         }
42
43         boolean isXML = isXML(data);
44         if(isXML){
45             return ConfigGeneratorConstant.DATA_TYPE_XML;
46         }
47
48         return ConfigGeneratorConstant.DATA_TYPE_TEXT;
49     }
50
51     public static boolean isJSON(String data) {
52         try {
53             new JSONObject(data);
54         } catch (JSONException ex) {
55             try {
56                 new JSONArray(data);
57             } catch (JSONException ex1) {
58                 return false;
59             }
60         }
61         return true;
62
63         //        try {
64         //            final ObjectMapper mapper = new ObjectMapper();
65         //            mapper.readTree(data);
66         //            return true;
67         //         } catch (IOException e) {
68         //            return false;
69         //         }
70     }
71
72     public static boolean isXML(String data) {
73         try {
74             DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
75             DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
76             Document doc = dBuilder.parse(new InputSource(new StringReader(data)));
77             return true;
78         } catch (Exception ex) {
79             return false;
80         }
81
82     }
83
84 }