X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=dgbuilder%2Fpublic%2Futil%2Fjs%2FvalidateNodeXml.js;fp=dgbuilder%2Fpublic%2Futil%2Fjs%2FvalidateNodeXml.js;h=2291d865ee86a9f8d9bb15db6854c97366862f02;hb=d1569975bb18f4359fac18aa98f55b69c248a3ad;hp=0000000000000000000000000000000000000000;hpb=a016ea661ff5767a3539734c4c07ef974a6e4614;p=ccsdk%2Fdistribution.git diff --git a/dgbuilder/public/util/js/validateNodeXml.js b/dgbuilder/public/util/js/validateNodeXml.js new file mode 100644 index 00000000..2291d865 --- /dev/null +++ b/dgbuilder/public/util/js/validateNodeXml.js @@ -0,0 +1,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 attribute '" + attrs[i].nodeName + "' is not set.Element#" + elementCount); + resp = false; + } + } + var childNodes = xmlNode.childNodes; + for(var k=0;kXML validation: SUCCESS","success"); + }else{ + console.log("Errors found. "); + RED.notify("XML validation: FAILED","error"); + } + }catch(e){ + console.log("error:" +e); + RED.notify("XML validation: 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 = ""; + + if(xmlStr.indexOf(endTag) != -1){ + resp=false; + showMsg("Error: End tag </" + startTag + "> must not be included.","red"); + console.log("End tag " + endTag + " must not be included."); + return resp; + } + try{ + //var xmlTopStr = "\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. show errors","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="
"; + for(var i=0;errList != null && i"; + } + htmlStr += "
Error List
"; + //var prevHtml = $("#tab-info").html(); + //htmlStr += ""; + //$('#show-errors-div').html(htmlStr); + //$("#tab-info").html(prevHtml + htmlStr); + + //$('
').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 +}