Platform Hardening for DG - Part 8
[appc.git] / appc-directed-graph / dg-loader / provider / src / main / resources / js / dg_xml2json.js
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
26 var dgconverter = new Object();
27
28 dgconverter.getNodeToXml = function (inputNodeSet) {
29     var exportableNodeSet = JSON.parse(inputNodeSet);
30     //uses inputNodeSet if passed otherwise build the latest nodeSet
31
32     //$("#btn-deploy").removeClass("disabled");
33
34     function getDgStartNode(nodeList) {
35         for (var i = 0; i < nodeList.length; i++) {
36             if (nodeList[i].type == 'dgstart' && nodeList[i].wires != null && nodeList[i].wires != undefined) {
37                 return nodeList[i];
38             }
39         }
40         return null;
41     }
42
43
44     function getNode(id) {
45         for (var i = 0; i < exportableNodeSet.length; i++) {
46             if (exportableNodeSet[i].id == id) {
47                 return exportableNodeSet[i];
48             }
49         }
50         return null;
51     }
52
53     function getStartTag(node) {
54         var startTag = "";
55         var xmlStr = "";
56         if (node != null && node.type != 'dgstart') {
57             xmlStr = node.xml;
58             var regex = /(<)([\w-]+)(.*)?/;
59             var match = regex.exec(xmlStr);
60             if (match != null) {
61                 if (match[1] != undefined && match[2] != undefined) {
62                     startTag = match[2];
63                 }
64             } else {
65                 console.log("startTag not found.");
66             }
67         }
68         return startTag;
69     }
70
71     // if (inputNodeSet == null || inputNodeSet == undefined) {
72     //     exportableNodeSet = getCurrentFlowNodeSet();
73     // } else {
74     //     exportableNodeSet = JSON.parse(inputNodeSet);
75     // }
76     var dgstartNode = getDgStartNode(exportableNodeSet);
77
78     var level = 0;
79     var fullXmlStr = "";
80
81     printXml(dgstartNode);
82
83
84     function printXml(node) {
85         var xmlStr = "";
86         var startTag = "";
87         if (node != null && node.type != 'dgstart') {
88             var comments = node.comments;
89             if (comments != null && comments != "") {
90                 //if xml comments field already has the <!-- and --> remove them
91                 comments = comments.replace("<!--", "");
92                 comments = comments.replace("-->", "");
93                 xmlStr = "<!--" + comments + "-->";
94             }
95             xmlStr += node.xml;
96             startTag = getStartTag(node);
97           //special handling for break node
98             if(xmlStr != undefined && xmlStr != null && xmlStr.trim() == "<break>"){
99                 fullXmlStr += "<break/>";
100             }else{    
101                 fullXmlStr +=xmlStr;
102             }
103             /*
104             if(level > 0){
105                 var spacing = Array(level).join("  ");
106                 xmlStr=xmlStr.replace(/\n/g,spacing);
107                 fullXmlStr +=xmlStr;
108
109                 console.log(xmlStr);
110             }else{
111                 fullXmlStr +=xmlStr;
112                 console.log(xmlStr);
113             }
114             */
115         }
116
117         //console.log("startTag:" + startTag);
118
119         var wiredNodes = [];
120         var wiredNodesArr = [];
121         if (node != null && node.wires != null && node.wires[0] != null && node.wires[0] != undefined && node.wires[0].length > 0) {
122             wiredNodes = node.wires[0];
123             //console.log("Before sort");
124             for (var k = 0; wiredNodes != undefined && wiredNodes != null && k < wiredNodes.length; k++) {
125                 wiredNodesArr.push(getNode(wiredNodes[k]));
126             }
127             //console.dir(wiredNodesArr);
128             //sort based on y position
129             wiredNodesArr.sort(function (a, b) {
130                 return a.y - b.y;
131             });
132             //console.log("After sort");
133             //console.dir(wiredNodesArr);
134         }
135
136         for (var k = 0; wiredNodesArr != null && k < wiredNodesArr.length; k++) {
137             level++;
138             var nd = wiredNodesArr[k];
139             printXml(nd);
140         }
141
142         //append end tag
143         if (startTag != "") {
144             if(startTag != "break"){
145                 fullXmlStr += "</" + startTag + ">";
146             }
147             /*
148             if(level >0){
149                 var spacing = Array(level).join("  ");
150                 fullXmlStr += spacing + "</" + startTag + ">";
151                 console.log(spacing + "</" + startTag + ">");
152             }else{
153                 fullXmlStr += "</" + startTag + ">";
154                 console.log("</" + startTag + ">");
155             }
156             */
157         }
158
159         /*if(level>0){
160             level=level-1;
161         }
162         */
163         //console.log("endTag:" + startTag);
164         //console.log("xml:" + fullXmlStr);
165     }
166     //console.log("fullXmlStr:" + fullXmlStr);
167     return fullXmlStr;
168 };
169