MeshCentral/agents/modules_meshcore_min/amt-xml.min.js

1 line
5.7 KiB
JavaScript
Raw Normal View History

2019-02-07 23:06:33 -05:00
try{Object.defineProperty(Array.prototype,"peek",{value:function(){return(this.length>0?this[this.length-1]:undefined)}})}catch(e){}module.exports.ParseWsman=function(j){try{if(!j.childNodes){j=_turnToXml(j)}var g={Header:{}},d=j.getElementsByTagName("Header")[0],h;if(!d){d=j.getElementsByTagName("a:Header")[0]}if(!d){return null}for(var f=0;f<d.childNodes.length;f++){var b=d.childNodes[f];g.Header[b.localName]=b.textContent}var a=j.getElementsByTagName("Body")[0];if(!a){a=j.getElementsByTagName("a:Body")[0]}if(!a){return null}if(a.childNodes.length>0){h=a.childNodes[0].localName;if(h.indexOf("_OUTPUT")==h.length-7){h=h.substring(0,h.length-7)}g.Header.Method=h;g.Body=_ParseWsmanRec(a.childNodes[0])}return g}catch(c){console.error("Unable to parse XML: "+j,c);return null}};function _ParseWsmanRec(g){var c,h={};for(var d=0;d<g.childNodes.length;d++){var a=g.childNodes[d];if((a.childElementCount==null)||(a.childElementCount==0)){c=a.textContent}else{c=_ParseWsmanRec(a)}if(c=="true"){c=true}if(c=="false"){c=false}if((parseInt(c)+"")===c){c=parseInt(c)}var b=c;if((a.attributes!=null)&&(a.attributes.length>0)){b={Value:c};for(var f=0;f<a.attributes.length;f++){b["@"+a.attributes[f].name]=a.attributes[f].value}}if(h[a.localName] instanceof Array){h[a.localName].push(b)}else{if(h[a.localName]==null){h[a.localName]=b}else{h[a.localName]=[h[a.localName],b]}}}return h}function _PutObjToBodyXml(g,d){if(!g||d==null){return""}var b=obj.GetNameFromUrl(g);var f="<r:"+b+' xmlns:r="'+g+'">';for(var c in d){if(!d.hasOwnProperty(c)||c.indexOf("__")===0||c.indexOf("@")===0){continue}if(d[c]==null||typeof d[c]==="function"){continue}if(typeof d[c]==="object"&&d[c]["ReferenceParameters"]){f+="<r:"+c+"><a:Address>"+d[c].Address+"</a:Address><a:ReferenceParameters><w:ResourceURI>"+d[c]["ReferenceParameters"]["ResourceURI"]+"</w:ResourceURI><w:SelectorSet>";var h=d[c]["ReferenceParameters"]["SelectorSet"]["Selector"];if(Array.isArray(h)){for(var a=0;a<h.length;a++){f+="<w:Selector"+_ObjectToXmlAttributes(h[a])+">"+h[a]["Value"]+"</w:Selector>"}}else{f+="<w:Selector"+_ObjectToXmlAttributes(h)+">"+h.Value+"</w:Selector>"}f+="</w:SelectorSet></a:ReferenceParameters></r:"+c+">"}else{if(Array.isArray(d[c])){for(var a=0;a<d[c].length;a++){f+="<r:"+c+">"+d[c][a].toString()+"</r:"+c+">"}}else{f+="<r:"+c+">"+d[c].toString()+"</r:"+c+">"}}}f+="</r:"+b+">";return f}function _treeBuilder(){this.tree=[];this.push=function(a){this.tree.push(a)};this.pop=function(){var a=this.tree.pop();if(this.tree.length>0){var b=this.tree.peek();b.childNodes.push(a);b.childElementCount=b.childNodes.length}return(a)};this.peek=function(){return(this.tree.peek())};this.addNamespace=function(g,d){this.tree.peek().nsTable[g]=d;if(this.tree.peek().attributes.length>0){for(var c=0;c<this.tree.peek().attributes;++c){var b=this.tree.peek().attributes[c];if(g=="*"&&b.name==b.localName){b.namespace=d}else{if(g!="*"&&b.name!=b.localName){var f=b.name.split(":")[0];if(f==g){b.namespace=d}}}}}};this.getNamespace=function(b){for(var a=this.tree.length-1;a>=0;--a){if(this.tree[a].nsTable[b]!=null){return(this.tree[a].nsTable[b])}}return null}}function _turnToXml(a){if(a==null){return null}return({childNodes:[_turnToXmlRec(a)],getElementsByTagName:_getElementsByTagName,getChildElementsByTagName:_getChildElementsByTagName,getElementsByTagNameNS:_getElementsByTagNameNS})}function _getElementsByTagNameNS(b,a){var c=[];_xmlTraverseAllRec(this.childNodes,function(d){if(d.localName==a&&(d.namespace==b||b=="*")){c.push(d)}});return c}function _getElementsByTagName(a){var b=[];_xmlTraverseAllRec(this.childNodes,function(c){if(c.localName==a){b.push(c)}});return b}function _getChildElementsByTagName(a){var c=[];if(this.childNodes!=null){for(var b in this.childNodes){if(this.childNodes[b].localName==a){c.push(this.childNodes[b])}}}return(c)}function _getChildElementsByTagNameNS(c,a){var d=[];if(this.childNodes!=null){for(var b in this.childNodes){if(this.childNodes[b].localName==a&&(c=="*"||this.childNodes[b].namespace==c)){d.push(this.childNodes[b])}}}return(d)}function _xmlTraverseAllRec(c,a){for