[CCSDK-28] populated the seed code for dgbuilder
[ccsdk/distribution.git] / dgbuilder / public / util / js / validateNodeXml.js
1 function resetStatus(){
2         showMsg("","green");
3 }
4
5 function showMsg(msg,color){
6      $("#node-validate-result").html(msg);
7      $("#node-validate-result")
8                                 .css('color', '')
9                                 .css('color', color);
10 }
11
12 function getAttributeValue(xmlStr,attribute){
13
14         var attrVal=null;
15         try{
16                 if(xmlStr != null){
17                         var myRe = new RegExp(attribute + "[\s+]?=[\s+]?['\"]([^'\"]+)['\"]","m");
18                         var myArray = myRe.exec(xmlStr);
19                         if(myArray != null && myArray[1] != null){
20                                 attrVal=myArray[1];
21                         }
22                 }
23         }catch(err){
24                 console.log(err);
25         }
26         return attrVal;
27 }
28
29
30 var resp = true;
31 var processedNode = "";
32 var errList=[];
33 var elementCount=0;
34 function processNode(xmlNode){
35         if(xmlNode == null) return;
36         if(xmlNode.nodeName != "parsererror" && xmlNode.nodeName != "#text"){
37                         processedNode = xmlNode.nodeName;
38         }
39         //console.log("processedNode:" + processedNode);
40         switch(xmlNode.nodeType){
41                 case 1:
42                         elementCount++;
43                         //ELEMENT_NODE
44                         //console.log(xmlNode.nodeName);
45                         //console.dir(xmlNode.nodeName);
46                         if(xmlNode.nodeName == "parsererror"){
47                                 //var nearNode = xmlNode.previousSibling != null ?xmlNode.previousSibling.nodeName : xmlNode.parentNode.nodeName;
48                                 console.log("Error parsing xml after node " + processedNode);
49                                 var msg = "error parsing XML after element <" + processedNode + "> Element#" + elementCount;
50                                 errList.push(msg);
51                                 resp = false;
52                                 return;
53                         }       
54                         processedNode = xmlNode.nodeName;
55                         var attrs = xmlNode.attributes;
56                         for(var i=0;i<attrs.length;i++){
57                                 //console.log("Attribute:" + attrs[i].nodeName);
58                                 //console.log("Value:" + attrs[i].value);
59                                 if(attrs[i].nodeName != "value" && attrs[i].value == ""){
60                                         var msg="";
61                                         /*
62                                         var prevSibling = xmlNode.previousSibling;
63                                         if(prevSibling != null && prevSibling != undefined){
64                                                 msg = "element &lt;" + xmlNode.nodeName  + "&gt; attribute '" + attrs[i].nodeName + "' is not set. Element#" + elementCount;
65                                         }else{
66                                                 msg = "element &lt;" + xmlNode.nodeName  + "&gt; attribute '" + attrs[i].nodeName + "' is not set. Element#" + elementCount;
67                                         }
68                                         */
69                                         msg = "element &lt;" + xmlNode.nodeName  + "&gt; attribute '" + attrs[i].nodeName + "' is not set. Element#" + elementCount;
70                                         errList.push(msg);
71                                         //console.log("element <" +  xmlNode.nodeName + "> attribute '" + attrs[i].nodeName + "' is not set.Element#" + elementCount);
72                                         resp = false;
73                                 }
74                         }
75                         var childNodes = xmlNode.childNodes;
76                         for(var k=0;k<childNodes.length;k++){
77                                 processNode(childNodes[k]);
78                         }
79                         break;
80                 case 2:
81                         //ATTRIBUTE_NODE
82                         //console.log(xmlNode.nodeName);
83                         break;
84                 case 3:
85                         //TEXT_NODE
86                         //console.log(xmlNode.nodeValue);
87                         break;
88                 case 4:
89                         //CDATA_SECTION_NODE
90                         console.log("CDATA_SECTION_NODE");
91                         break;
92                 case 5:
93                         //ENTITY_REFERENCE_NODE
94                         console.log("ENTITY_REFERENCE_NODE");
95                         break;
96                 case 6:
97                         //ENTITY_NODE
98                         console.log("ENTITY_NODE");
99                         break;
100                 case 7:
101                         //PROCESSING_INSTRUCTION_NODE
102                         console.log("PROCESSING_INSTRUCTION_NODE");
103                         break;
104                 case 8:
105                         //COMMENT_NODE
106                         console.log("COMMENT_NODE");
107                         break;
108                 case 9:
109                         //DOCUMENT_NODE
110                         console.log("DOCUMENT_NODE");
111                         break;
112                 case 10:
113                         //DOCUMENT_TYPE_NODE
114                         console.log("DOCUMENT_TYPE_NODE");
115                         break;
116                 case 11:
117                         //DOCUMENT_TYPE_NODE
118                         console.log("DOCUMENT_FRAGMENT_NODE");
119                         break;
120                 case 12:
121                         //NOTATION_NODE
122                         console.log("DOCUMENT_FRAGMENT_NODE");
123                         break;
124         }
125 }
126
127 function validateFinalXML(xmlStr){
128
129         //console.dir(RED);
130         processedNode="";
131         resp=true;
132         errList=[];
133         elementCount=0;
134         //console.log("In validateXML xmlStr:" + xmlStr);
135         if(xmlStr == null || xmlStr == "") return true;
136         xmlStr = xmlStr.trim();
137         try{
138                 var xmlDoc;
139                 if (window.DOMParser){
140                         try{
141                                 var parser=new DOMParser();
142                                 xmlDoc=parser.parseFromString(xmlStr,'text/xml');
143                                 //console.log("Not IE");
144                                 var n = xmlDoc.documentElement.nodeName;
145                                 if(n == "html"){
146                                         resp=false;
147                                         console.log("Error parsing");
148                                         return resp;
149                                 }
150                         }catch(e){
151                                 console.log("Error parsing" +e);
152                                 return false;
153                         }
154                 }else{ 
155                         try{
156                                 //console.log("IE");
157                                 // code for IE
158                                 xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
159                                 xmlDoc.async=false;
160                                 xmlDoc.loadXMLString(xmlStr); 
161                         }catch(e){
162                                 console.log("Error parsing" +e);
163                                 return false;
164                         }
165                 } 
166
167                 //console.dir(xmlDoc);
168                 processNode(xmlDoc.documentElement);
169                 
170                 if(resp){
171                         console.log("Validation Successful");
172                         RED.notify("<strong>XML validation</strong>: SUCCESS","success");
173                 }else{
174                         console.log("Errors found. ");
175                         RED.notify("<strong>XML validation</strong>: FAILED","error");
176                 }
177         }catch(e){
178                 console.log("error:" +e);
179                 RED.notify("<strong>XML validation</strong>: FAILED","error");
180                 resp=false;
181                 return resp;
182         }
183         return resp;
184 }
185
186 function validateXML(xmlStr){
187
188         //console.dir(RED);
189         processedNode="";
190         resp=true;
191         errList=[];
192         elementCount=0;
193         //console.log("In validateXML xmlStr:" + xmlStr);
194         //var xmlStr = $("#node-input-xml-editor").text();
195         if(xmlStr == null || xmlStr == undefined){
196                 xmlStr = $("#node-input-xml-editor").text();
197         }
198         if(xmlStr == undefined) return false;
199         //console.dir($("#node-input-xml-editor"));
200         //console.log("xmlStr:" +  xmlStr);
201         xmlStr = xmlStr.trim();
202         var startTag ;
203         var endTag ;
204         try{
205                 var re = /^[0-9]+/;
206                 xmlStr = xmlStr.replace(re,'');
207                 var regex = /(<)([\w-]+)(.*)?/;
208                 var match = regex.exec(xmlStr);
209                 if(match != null){
210                         if(match[1] != undefined && match[2] != undefined){ 
211                                 startTag = match[2];
212                         }
213                 }else{
214                         resp=false;
215                         showMsg("startTag not found.","red");
216                         return resp;
217                 }
218         }catch(e){
219                 console.log(e);
220                 return false;
221         }
222         //console.log(xmlStr);  
223         if(xmlStr == ""){
224                 resp=false;
225                 showMsg("XML not found","red");
226                 return resp;
227         }
228         endTag = "</" + startTag + ">";
229
230         if(xmlStr.indexOf(endTag) != -1){
231                 resp=false;
232                 showMsg("Error: End tag &lt;/" + startTag + "&gt; must not be included.","red");
233                 console.log("End tag " + endTag + " must not be included.");
234                 return resp;
235         }
236         try{
237                 //var xmlTopStr = "<?xml version='1.0' encoding='UTF-8'?>\n" ;
238                 //xmlStr = xmlTopStr + xmlStr;
239                 //xmlStr = xmlStr.replace(/'/g,"\"");
240                 xmlStr+= "\n" +  endTag;
241                 xmlStr = xmlStr.trim();
242                 //console.log("xmlStr:" + xmlStr);
243                 var xmlDoc;
244                 if (window.DOMParser){
245                         try{
246                                 var parser=new DOMParser();
247                                 xmlDoc=parser.parseFromString(xmlStr,'text/xml');
248                                 //console.log("Not IE");
249                                 var n = xmlDoc.documentElement.nodeName;
250                                 if(n == "html"){
251                                         resp=false;
252                                         showMsg("Error parsing","red");
253                                         console.log("Error parsing");
254                                         return resp;
255                                 }
256                         }catch(e){
257                                 console.log("Error parsing" +e);
258                                 return false;
259                         }
260                 }else{ 
261                         try{
262                                 //console.log("IE");
263                                 // code for IE
264                                 xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
265                                 xmlDoc.async=false;
266                                 xmlDoc.loadXMLString(xmlStr); 
267                         }catch(e){
268                                 console.log("Error parsing" +e);
269                                 return false;
270                         }
271                 } 
272
273                 //console.dir(xmlDoc);
274                 processNode(xmlDoc.documentElement);
275                 if(resp){
276                         showMsg("Validation Successful","green");
277                 }else{
278                         showMsg("Errors found. <a onclick='javascript:showErrors()'>show errors</a>","red");
279                         console.log("Errors found. ");
280                 }
281         }catch(e){
282                 console.log("error:" +e);
283                 showMsg(e,"red");
284                 resp=false;
285                 return resp;
286         }
287         return resp;
288 }
289
290 function showErrors() {
291                 //var sourceField = event != null ?event.srcElement:event.target;
292                 //console.dir(sourceField);
293                 //sourceField.style.backgroundColor="skyblue";
294                 //var leftVal = event.target.offsetLeft  ; 
295                 //alert(topVal + ":" + leftVal);
296                 /*left:leftVal,*/
297                 //var pos = event.target;
298                 //var topVal = event.target.offsetTop + topPosition ;
299                 //var topVal = event.target.offsetTop +75;
300                 var htmlStr="<div id='error-list-div'><table id='errTable' border='1'><tr><th>Error List</th></tr>";    
301                 for(var i=0;errList != null && i<errList.length;i++){
302                         var errSeq = i+1;
303                         htmlStr += "<tr><td>" + errSeq + ")"  + errList[i] + "</td></tr>"; 
304                 }
305                 htmlStr += "</table></div>";
306                 //var prevHtml = $("#tab-info").html();
307                 //htmlStr += "<input type='button' value='Hide Errors' onclick=\"closeShowErrors('" + prevHtml + "')\" >";
308                 //$('#show-errors-div').html(htmlStr);
309                 //$("#tab-info").html(prevHtml + htmlStr);
310
311                 //$('<div></div>').dialog({
312                 $('#show-errors-dialog').dialog({
313                               modal: true,
314                               title: "XML Error List ",
315                               width: 500,
316                               open: function () {
317                                 $(this).html(htmlStr);
318                               },
319                               buttons: {
320                                 Close: function () {
321                                   $(this).dialog("close");
322                                 }
323                               }
324                             }); // end dialog div
325 }