/*
 * XML Processor
 * Copyright (c) 2009 Ryan Morr (ryanmorr.com)
 * Licensed under the MIT license.
 */

XMLProcessor=function(xml,xslt){if(xml){this.loadXML(xml);if(xslt){this.loadXSLT(xslt)}}};XMLProcessor.MSXML=["Msxml2.DOMDocument.6.0","Msxml2.DOMDocument.5.0","Msxml2.DOMDocument.4.0","Msxml2.DOMDocument.3.0","MSXML2.DOMDocument","Microsoft.XMLDOM"];XMLProcessor.prototype={getXML:function(){return this.xml},getXSLT:function(){return this.xslt},loadXML:function(url){var doc=this.createDocument();this.xml=this.loadDocument(doc,url);return this.xml},loadXSLT:function(url){var doc=this.createDocument();this.xslt=this.loadDocument(doc,url);return this.xslt},decode:function(str){str=str.replace(/&amp;/g,'&');str=str.replace(/&lt;/g,'<');str=str.replace(/&gt;/g,'>');str=str.replace(/&quot;/g,'"');str=str.replace(/&#039;/g,'\'');return str},encode:function(str){str=str.replace(/&/g,'&amp;');str=str.replace(/</g,'&lt;');str=str.replace(/>/g,'&gt;');str=str.replace(/"/g,'&quot;');str=str.replace(/'/g,'&apos;');return str},serialize:function(encode){if(window.XMLSerializer){serializer=new XMLSerializer();str=serializer.serializeToString(this.xml);return encode?this.encode(str):str}else if(typeof(this.xml.xml||this.xml.outerHTML)=='string'){var str=this.xml.xml||this.xml.outerHTML;return encode?this.encode(str):str}},parse:function(str){str=this.decode(str);if(window.DOMParser){var parser=new DOMParser();this.xml=parser.parseFromString(str,"text/xml")}else if(window.ActiveXObject){this.xml=new ActiveXObject("Microsoft.XMLDOM");this.xml.async=false;this.xml.loadXML(str)}return this.xml},createDocument:function(){var doc;if(document.implementation&&document.implementation.createDocument){doc=document.implementation.createDocument("","",null)}else if(window.ActiveXObject){for(var i=0;i<XMLProcessor.MSXML.length;i++){try{doc=new ActiveXObject(XMLProcessor.MSXML[i])}catch(e){}}}return doc},loadDocument:function(doc,url){if(doc&&typeof doc.load!='undefined'){doc.async=false;doc.load(url);return doc}else{var request=new XMLHttpRequest();request.open("GET",url,false);request.send("");return request.responseXML}},transform:function(target){if(window.XSLTProcessor){var processor=new XSLTProcessor();processor.importStylesheet(this.xslt);var doc=processor.transformToFragment(this.xml,document);target.appendChild(doc)}else{target.innerHTML=this.xml.transformNode(this.xslt)}},find:function(query){if(typeof this.xml.selectNodes!='undefined'){return this.xml.selectNodes(query)}else if(document.implementation.hasFeature('XPath','3.0')){var nodes=[];var resolver=this.xml.createNSResolver(this.xml.documentElement);var items=this.xml.evaluate(query,this.xml,resolver,XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,null);for(var i=0;i<items.snapshotLength;i++){nodes[i]=items.snapshotItem(i)}return nodes}else{return[]}}}