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.openecomp.sdnc.dg.loader;
23 import java.util.ArrayList;
24 import java.util.List;
26 import javax.script.Invocable;
27 import javax.script.ScriptEngine;
28 import javax.script.ScriptEngineManager;
30 import org.apache.commons.io.FileUtils;
31 import org.apache.commons.io.IOUtils;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
36 public class DGXMLGenerator {
37 private final static Logger logger = LoggerFactory.getLogger(DGXMLGenerator.class);
39 public static String STRING_ENCODING = "utf-8";
40 public static String JS_INTERFACE_DG_CONVERTOR = "dgconverter";
41 public static String JS_METHOD_GET_NODE_TO_XML = "getNodeToXml";
42 public static String GENERATOR_TEMPLATE_FILE = "js/dg_xml2json.js";
44 public void generateXMLFromJSON(String jsonPath, String xmlpath, String propertyPath) throws Exception {
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;
53 // engine.eval(new FileReader(DGXMLGenerator.class.getClassLoader().getResource(GENERATOR_TEMPLATE_FILE).getPath()));
54 String js = IOUtils.toString(DGXMLGenerator.class.getClassLoader().getResourceAsStream(GENERATOR_TEMPLATE_FILE),STRING_ENCODING);
57 Object dgconverter = engine.get(JS_INTERFACE_DG_CONVERTOR);
59 List<File> files = new ArrayList<File>();
60 if(dgconverter != null){
61 File jsonPathFile = new File(jsonPath);
62 if(jsonPathFile.isDirectory()){
63 String[] extensions = new String[] { "json", "JSON" };
64 files = (List<File>) FileUtils.listFiles(jsonPathFile, extensions, true);
65 }else if(jsonPathFile.isFile()){
66 files.add(jsonPathFile);
68 throw new Exception("Failed to get the nature of the JSON path :"+ jsonPath);
71 logger.info("JSON Files identified "+ files.size());
74 boolean isXmlPathDeleted = FileUtils.deleteQuietly(new File(xmlpath));
75 logger.info("Cleaning old DG XML under : " + xmlpath + ", delete status :" + isXmlPathDeleted);
77 for (File file : files) {
78 String dgJson = FileUtils.readFileToString(file,STRING_ENCODING);
79 logger.info("Generating XML from :" + file.getName());
80 String xmlFileName = xmlpath +"/"+file.getName().replace(".json", ".xml");
82 Object dgXMl = inv.invokeMethod(dgconverter, JS_METHOD_GET_NODE_TO_XML, dgJson);
85 File xmlFile = new File(xmlFileName);
86 FileUtils.writeStringToFile(xmlFile, dgXMl.toString(), STRING_ENCODING);
87 logger.info("Generated XML File under :" + xmlFile.getCanonicalPath());
92 logger.info("No JSON Files to generate XML");
95 logger.error("Couldn't get Java Script Engine..");
97 }catch (Exception e) {
98 logger.error("Failed to generate generateXMLFromJSON :"+e.getMessage());
103 public static void main(String[] args) {
105 DGXMLGenerator application = new DGXMLGenerator();
106 String jsonPath = null;
107 String xmlPath = null;
108 String propertyPath = null;
109 // Generate, GenerateLoad, GenerateLoadActivate
110 //args = new String[]{"src/main/resources/json","src/test/resources/xml"};
111 logger.info("DGXML Conversion Started with arguments :"+ args[0] +":"+ args[1]);
112 if(args != null && args.length >= 2){
116 throw new Exception("Sufficient inputs are missing <jsonPath> <xmlPath>");
119 application.generateXMLFromJSON(jsonPath, xmlPath, propertyPath);
120 logger.info("DGXML Conversion Completed...");
121 } catch (Exception e) {
122 logger.error("Failed in DG XML Generation :"+e.getMessage());