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