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

1 line
5.8 KiB
JavaScript

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(var b in c){a(c[b]);if(c[b].childNodes){_xmlTraverseAllRec(c[b].childNodes,a)}}}function _turnToXmlRec(w){try{if(w==null){return null}var m=new _treeBuilder(),r=null,z=w.split("<"),u=[],h=null,g=null;for(var o in z){var A=z[o].split(">"),B=A[0].split(" "),l=B[0];if((l.length>0)&&(l[0]!="?")){if(l[0]!="/"){var a=[],s,t=l.split(" ")[0].split(":"),s=(t.length>1)?t[1]:t[0];Object.defineProperty(a,"get",{value:function(){if(arguments.length==1){for(var i in this){if(this[i].name==arguments[0]){return(this[i])}}}else{if(arguments.length==2){for(var i in this){if(this[i].name==arguments[1]&&(arguments[0]=="*"||this[i].namespace==arguments[0])){return(this[i])}}}else{throw ("attributes.get(): Invalid number of parameters")}}}});m.push({name:l,localName:s,getChildElementsByTagName:_getChildElementsByTagName,getElementsByTagNameNS:_getElementsByTagNameNS,getChildElementsByTagNameNS:_getChildElementsByTagNameNS,attributes:a,childNodes:[],nsTable:{}});if(B.length>0){var v=false;for(var p in B){if(B[p]=="/"){m.peek().namespace=m.peek().name==m.peek().localName?m.getNamespace("*"):m.getNamespace(m.peek().name.substring(0,m.peek().name.indexOf(":")));m.peek().textContent="";r=m.pop();v=true;break}var q=B[p].indexOf("=");if(q>0){var b=B[p].substring(0,q);var d=B[p].substring(q+2,B[p].length-1);var c=m.getNamespace("*");if(b=="xmlns"){m.addNamespace("*",d);c=d}else{if(b.startsWith("xmlns:")){m.addNamespace(b.substring(6),d)}else{var f=b.split(":");if(f.length==2){b=f[1];c=m.getNamespace(f[0])}}}var y={name:b,value:d};if(c!=null){y.namespace=c}m.peek().attributes.push(y)}}if(v){continue}}m.peek().namespace=m.peek().name==m.peek().localName?m.getNamespace("*"):m.getNamespace(m.peek().name.substring(0,m.peek().name.indexOf(":")));if(A[1]){m.peek().textContent=A[1]}}else{r=m.pop()}}}}catch(n){return null}return r};