summaryrefslogtreecommitdiffstats
path: root/dgbuilder/public/util/js/validateNodeXml.js
blob: 2291d865ee86a9f8d9bb15db6854c97366862f02 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
function resetStatus(){
	showMsg("","green");
}

function showMsg(msg,color){
     $("#node-validate-result").html(msg);
     $("#node-validate-result")
	 			.css('color', '')
    				.css('color', color);
}

function getAttributeValue(xmlStr,attribute){

	var attrVal=null;
	try{
		if(xmlStr != null){
			var myRe = new RegExp(attribute + "[\s+]?=[\s+]?['\"]([^'\"]+)['\"]","m");
			var myArray = myRe.exec(xmlStr);
			if(myArray != null && myArray[1] != null){
				attrVal=myArray[1];
			}
		}
	}catch(err){
		console.log(err);
	}
	return attrVal;
}


var resp = true;
var processedNode = "";
var errList=[];
var elementCount=0;
function processNode(xmlNode){
	if(xmlNode == null) return;
	if(xmlNode.nodeName != "parsererror" && xmlNode.nodeName != "#text"){
			processedNode = xmlNode.nodeName;
	}
	//console.log("processedNode:" + processedNode);
	switch(xmlNode.nodeType){
		case 1:
			elementCount++;
	       		//ELEMENT_NODE
			//console.log(xmlNode.nodeName);
			//console.dir(xmlNode.nodeName);
			if(xmlNode.nodeName == "parsererror"){
				//var nearNode = xmlNode.previousSibling != null ?xmlNode.previousSibling.nodeName : xmlNode.parentNode.nodeName;
				console.log("Error parsing xml after node " + processedNode);
				var msg = "error parsing XML after element <" + processedNode + "> Element#" + elementCount;
				errList.push(msg);
				resp = false;
				return;
			}	
			processedNode = xmlNode.nodeName;
			var attrs = xmlNode.attributes;
			for(var i=0;i<attrs.length;i++){
				//console.log("Attribute:" + attrs[i].nodeName);
				//console.log("Value:" + attrs[i].value);
				if(attrs[i].nodeName != "value" && attrs[i].value == ""){
					var msg="";
					/*
					var prevSibling = xmlNode.previousSibling;
					if(prevSibling != null && prevSibling != undefined){
						msg = "element &lt;" + xmlNode.nodeName  + "&gt; attribute '" + attrs[i].nodeName + "' is not set. Element#" + elementCount;
					}else{
						msg = "element &lt;" + xmlNode.nodeName  + "&gt; attribute '" + attrs[i].nodeName + "' is not set. Element#" + elementCount;
					}
					*/
					msg = "element &lt;" + xmlNode.nodeName  + "&gt; attribute '" + attrs[i].nodeName + "' is not set. Element#" + elementCount;
					errList.push(msg);
					//console.log("element <" +  xmlNode.nodeName + "> attribute '" + attrs[i].nodeName + "' is not set.Element#" + elementCount);
					resp = false;
				}
			}
			var childNodes = xmlNode.childNodes;
			for(var k=0;k<childNodes.length;k++){
				processNode(childNodes[k]);
			}
			break;
		case 2:
			//ATTRIBUTE_NODE
			//console.log(xmlNode.nodeName);
			break;
		case 3:
			//TEXT_NODE
			//console.log(xmlNode.nodeValue);
			break;
		case 4:
			//CDATA_SECTION_NODE
			console.log("CDATA_SECTION_NODE");
			break;
		case 5:
			//ENTITY_REFERENCE_NODE
			console.log("ENTITY_REFERENCE_NODE");
			break;
		case 6:
			//ENTITY_NODE
			console.log("ENTITY_NODE");
			break;
		case 7:
			//PROCESSING_INSTRUCTION_NODE
			console.log("PROCESSING_INSTRUCTION_NODE");
			break;
		case 8:
			//COMMENT_NODE
			console.log("COMMENT_NODE");
			break;
		case 9:
			//DOCUMENT_NODE
			console.log("DOCUMENT_NODE");
			break;
		case 10:
			//DOCUMENT_TYPE_NODE
			console.log("DOCUMENT_TYPE_NODE");
			break;
		case 11:
			//DOCUMENT_TYPE_NODE
			console.log("DOCUMENT_FRAGMENT_NODE");
			break;
		case 12:
			//NOTATION_NODE
			console.log("DOCUMENT_FRAGMENT_NODE");
			break;
	}
}

function validateFinalXML(xmlStr){

	//console.dir(RED);
	processedNode="";
	resp=true;
	errList=[];
	elementCount=0;
	//console.log("In validateXML xmlStr:" + xmlStr);
	if(xmlStr == null || xmlStr == "") return true;
	xmlStr = xmlStr.trim();
	try{
		var xmlDoc;
         	if (window.DOMParser){
			try{
            			var parser=new DOMParser();
            			xmlDoc=parser.parseFromString(xmlStr,'text/xml');
				//console.log("Not IE");
				var n = xmlDoc.documentElement.nodeName;
				if(n == "html"){
					resp=false;
					console.log("Error parsing");
					return resp;
				}
			}catch(e){
				console.log("Error parsing" +e);
				return false;
			}
         	}else{ 
			try{
				//console.log("IE");
	    			// code for IE
            			xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
            			xmlDoc.async=false;
            			xmlDoc.loadXMLString(xmlStr); 
			}catch(e){
				console.log("Error parsing" +e);
				return false;
			}
        	} 

		//console.dir(xmlDoc);
		processNode(xmlDoc.documentElement);
		
		if(resp){
			console.log("Validation Successful");
			RED.notify("<strong>XML validation</strong>: SUCCESS","success");
		}else{
			console.log("Errors found. ");
			RED.notify("<strong>XML validation</strong>: FAILED","error");
		}
	}catch(e){
		console.log("error:" +e);
		RED.notify("<strong>XML validation</strong>: FAILED","error");
		resp=false;
		return resp;
	}
	return resp;
}

function validateXML(xmlStr){

	//console.dir(RED);
	processedNode="";
	resp=true;
	errList=[];
	elementCount=0;
	//console.log("In validateXML xmlStr:" + xmlStr);
        //var xmlStr = $("#node-input-xml-editor").text();
	if(xmlStr == null || xmlStr == undefined){
        	xmlStr = $("#node-input-xml-editor").text();
	}
	if(xmlStr == undefined) return false;
	//console.dir($("#node-input-xml-editor"));
	//console.log("xmlStr:" +  xmlStr);
	xmlStr = xmlStr.trim();
	var startTag ;
	var endTag ;
	try{
		var re = /^[0-9]+/;
		xmlStr = xmlStr.replace(re,'');
		var regex = /(<)([\w-]+)(.*)?/;
		var match = regex.exec(xmlStr);
		if(match != null){
			if(match[1] != undefined && match[2] != undefined){ 
	   			startTag = match[2];
			}
		}else{
			resp=false;
			showMsg("startTag not found.","red");
			return resp;
		}
	}catch(e){
		console.log(e);
		return false;
	}
	//console.log(xmlStr);	
	if(xmlStr == ""){
		resp=false;
		showMsg("XML not found","red");
		return resp;
	}
	endTag = "</" + startTag + ">";

	if(xmlStr.indexOf(endTag) != -1){
		resp=false;
		showMsg("Error: End tag &lt;/" + startTag + "&gt; must not be included.","red");
		console.log("End tag " + endTag + " must not be included.");
		return resp;
	}
	try{
		//var xmlTopStr = "<?xml version='1.0' encoding='UTF-8'?>\n" ;
		//xmlStr = xmlTopStr + xmlStr;
		//xmlStr = xmlStr.replace(/'/g,"\"");
		xmlStr+= "\n" +  endTag;
		xmlStr = xmlStr.trim();
		//console.log("xmlStr:" + xmlStr);
		var xmlDoc;
         	if (window.DOMParser){
			try{
            			var parser=new DOMParser();
            			xmlDoc=parser.parseFromString(xmlStr,'text/xml');
				//console.log("Not IE");
				var n = xmlDoc.documentElement.nodeName;
				if(n == "html"){
					resp=false;
					showMsg("Error parsing","red");
					console.log("Error parsing");
					return resp;
				}
			}catch(e){
				console.log("Error parsing" +e);
				return false;
			}
         	}else{ 
			try{
				//console.log("IE");
	    			// code for IE
            			xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
            			xmlDoc.async=false;
            			xmlDoc.loadXMLString(xmlStr); 
			}catch(e){
				console.log("Error parsing" +e);
				return false;
			}
        	} 

		//console.dir(xmlDoc);
		processNode(xmlDoc.documentElement);
		if(resp){
			showMsg("Validation Successful","green");
		}else{
			showMsg("Errors found. <a onclick='javascript:showErrors()'>show errors</a>","red");
			console.log("Errors found. ");
		}
	}catch(e){
		console.log("error:" +e);
		showMsg(e,"red");
		resp=false;
		return resp;
	}
	return resp;
}

function showErrors() {
                //var sourceField = event != null ?event.srcElement:event.target;
                //console.dir(sourceField);
                //sourceField.style.backgroundColor="skyblue";
                //var leftVal = event.target.offsetLeft  ; 
                //alert(topVal + ":" + leftVal);
                /*left:leftVal,*/
                //var pos = event.target;
                //var topVal = event.target.offsetTop + topPosition ;
                //var topVal = event.target.offsetTop +75;
		var htmlStr="<div id='error-list-div'><table id='errTable' border='1'><tr><th>Error List</th></tr>";	
		for(var i=0;errList != null && i<errList.length;i++){
			var errSeq = i+1;
			htmlStr += "<tr><td>" + errSeq + ")"  + errList[i] + "</td></tr>"; 
		}
		htmlStr += "</table></div>";
		//var prevHtml = $("#tab-info").html();
		//htmlStr += "<input type='button' value='Hide Errors' onclick=\"closeShowErrors('" + prevHtml + "')\" >";
                //$('#show-errors-div').html(htmlStr);
		//$("#tab-info").html(prevHtml + htmlStr);

		//$('<div></div>').dialog({
		$('#show-errors-dialog').dialog({
                              modal: true,
                              title: "XML Error List ",
                              width: 500,
                              open: function () {
                                $(this).html(htmlStr);
                              },
                              buttons: {
                                Close: function () {
                                  $(this).dialog("close");
                                }
                              }
                            }); // end dialog div
}