2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.sdnc.dg.loader;
24 import java.util.ArrayList;
25 import java.util.List;
26 import javax.script.Invocable;
27 import javax.script.ScriptEngine;
28 import javax.script.ScriptEngineManager;
29 import org.apache.commons.io.FileUtils;
30 import org.apache.commons.io.IOUtils;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
35 public class DGXMLGenerator {
36 private final static Logger logger = LoggerFactory.getLogger(DGXMLGenerator.class);
38 public static String STRING_ENCODING = "utf-8";
39 public static String JS_INTERFACE_DG_CONVERTOR = "dgconverter";
40 public static String JS_METHOD_GET_NODE_TO_XML = "getNodeToXml";
41 public static String GENERATOR_TEMPLATE_FILE = "js/dg_xml2json.js";
43 public void generateXMLFromJSON(String jsonPath, String xmlpath, String propertyPath)
46 ScriptEngineManager manager = new ScriptEngineManager();
47 ScriptEngine engine = manager.getEngineByName("JavaScript");
48 if (!(engine instanceof Invocable)) {
49 logger.error("Invoking methods is not supported.");
52 Invocable inv = (Invocable) engine;
54 // FileReader(DGXMLGenerator.class.getClassLoader().getResource(GENERATOR_TEMPLATE_FILE).getPath()));
55 String js = IOUtils.toString(DGXMLGenerator.class.getClassLoader()
56 .getResourceAsStream(GENERATOR_TEMPLATE_FILE), STRING_ENCODING);
59 Object dgconverter = engine.get(JS_INTERFACE_DG_CONVERTOR);
61 List<File> files = new ArrayList<File>();
62 if (dgconverter != null) {
63 File jsonPathFile = new File(jsonPath);
64 if (jsonPathFile.isDirectory()) {
65 String[] extensions = new String[] {"json", "JSON"};
66 files = (List<File>) FileUtils.listFiles(jsonPathFile, extensions, true);
67 } else if (jsonPathFile.isFile()) {
68 files.add(jsonPathFile);
70 throw new Exception("Failed to get the nature of the JSON path :" + jsonPath);
73 logger.info("JSON Files identified " + files.size());
75 if (files.size() > 0) {
76 boolean isXmlPathDeleted = FileUtils.deleteQuietly(new File(xmlpath));
77 logger.info("Cleaning old DG XML under : " + xmlpath + ", delete status :"
80 for (File file : files) {
81 String dgJson = FileUtils.readFileToString(file, STRING_ENCODING);
82 logger.info("Generating XML from :" + file.getName());
84 xmlpath + "/" + file.getName().replace(".json", ".xml");
87 inv.invokeMethod(dgconverter, JS_METHOD_GET_NODE_TO_XML, dgJson);
90 File xmlFile = new File(xmlFileName);
91 FileUtils.writeStringToFile(xmlFile, dgXMl.toString(), STRING_ENCODING);
92 logger.info("Generated XML File under :" + xmlFile.getCanonicalPath());
97 logger.info("No JSON Files to generate XML");
100 logger.error("Couldn't get Java Script Engine..");
102 } catch (Exception e) {
103 logger.error("Failed to generate generateXMLFromJSON :" + e.getMessage());
108 public static void main(String[] args) {
110 DGXMLGenerator application = new DGXMLGenerator();
111 String jsonPath = null;
112 String xmlPath = null;
113 String propertyPath = null;
114 // Generate, GenerateLoad, GenerateLoadActivate
115 // args = new String[]{"src/main/resources/json","src/test/resources/xml"};
116 logger.info("DGXML Conversion Started with arguments :" + args[0] + ":" + args[1]);
117 if (args != null && args.length >= 2) {
121 throw new Exception("Sufficient inputs are missing <jsonPath> <xmlPath>");
124 application.generateXMLFromJSON(jsonPath, xmlPath, propertyPath);
125 logger.info("DGXML Conversion Completed...");
126 } catch (Exception e) {
127 logger.error("Failed in DG XML Generation :" + e.getMessage());