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