appc-config-generator-provider sonar fixes part 2
[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 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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.sdnc.config.generator.tool;
26
27 import com.att.eelf.configuration.EELFLogger;
28 import com.att.eelf.configuration.EELFManager;
29 import java.io.StringReader;
30 import javax.xml.parsers.DocumentBuilder;
31 import javax.xml.parsers.DocumentBuilderFactory;
32 import org.codehaus.jettison.json.JSONArray;
33 import org.codehaus.jettison.json.JSONException;
34 import org.codehaus.jettison.json.JSONObject;
35 import org.onap.sdnc.config.generator.ConfigGeneratorConstant;
36 import org.xml.sax.InputSource;
37
38 public class CheckDataTool {
39
40     private static final EELFLogger logger = EELFManager.getInstance().getLogger(CheckDataTool.class);
41     private static final String STR_INPUT_DATA = "Input data: \n";
42
43     private CheckDataTool() {}
44
45     public static String checkData(String data) {
46         boolean isJSON = isJSON(data);
47         if (isJSON) {
48             return ConfigGeneratorConstant.DATA_TYPE_JSON;
49         }
50
51         boolean isXML = isXML(data);
52         if (isXML) {
53             return ConfigGeneratorConstant.DATA_TYPE_XML;
54         }
55
56         return ConfigGeneratorConstant.DATA_TYPE_TEXT;
57     }
58
59     public static boolean isJSON(String data) {
60         try {
61             new JSONObject(data);
62         } catch (JSONException ex) {
63             logger.error(STR_INPUT_DATA + data + "\n is not json object", ex);
64             try {
65                 new JSONArray(data);
66             } catch (JSONException ex1) {
67                 logger.error(STR_INPUT_DATA + data + "\n is not json array", ex1);
68                 return false;
69             }
70         }
71         return true;
72     }
73
74     public static boolean isXML(String data) {
75         try {
76             DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
77             DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
78             dBuilder.parse(new InputSource(new StringReader(data)));
79             return true;
80         } catch (Exception ex) {
81             logger.error(STR_INPUT_DATA + data + "\n is not xml document", ex);
82             return false;
83         }
84     }
85 }