2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * ============LICENSE_END=========================================================
24 package org.onap.sdnc.config.generator.tool;
26 import com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
28 import java.io.StringReader;
30 import javax.xml.XMLConstants;
31 import javax.xml.parsers.DocumentBuilder;
32 import javax.xml.parsers.DocumentBuilderFactory;
33 import org.codehaus.jettison.json.JSONArray;
34 import org.codehaus.jettison.json.JSONException;
35 import org.codehaus.jettison.json.JSONObject;
36 import org.onap.sdnc.config.generator.ConfigGeneratorConstant;
37 import org.xml.sax.InputSource;
39 public class CheckDataTool {
41 private static final EELFLogger logger = EELFManager.getInstance().getLogger(CheckDataTool.class);
42 private static final String STR_INPUT_DATA = "Input data: \n";
44 private CheckDataTool() {}
46 public static String checkData(String data) {
47 boolean isJSON = isJSON(data);
49 return ConfigGeneratorConstant.DATA_TYPE_JSON;
52 boolean isXML = isXML(data);
54 return ConfigGeneratorConstant.DATA_TYPE_XML;
57 return ConfigGeneratorConstant.DATA_TYPE_TEXT;
60 public static boolean isJSON(String data) {
63 } catch (JSONException ex) {
64 logger.error(STR_INPUT_DATA + data + "\n is not json object", ex);
67 } catch (JSONException ex1) {
68 logger.error(STR_INPUT_DATA + data + "\n is not json array", ex1);
75 public static boolean isXML(String data) {
77 DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
78 dbFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
79 dbFactory.setFeature("http://xml.org/sax/features/external-general-entities", false);
80 dbFactory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
82 DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
83 dBuilder.parse(new InputSource(new StringReader(data)));
85 } catch (Exception ex) {
86 logger.error(STR_INPUT_DATA + data + "\n is not xml document", ex);