[5036] | 1 | (function() |
---|
| 2 | { |
---|
| 3 | var _FILES = []; |
---|
| 4 | |
---|
| 5 | function _convert(pString) |
---|
| 6 | { |
---|
| 7 | if ( typeof pString != 'string' ) |
---|
| 8 | return false; |
---|
| 9 | |
---|
| 10 | if ( window.ActiveXObject ) |
---|
| 11 | { |
---|
| 12 | var _xmlDoc = new ActiveXObject("Microsoft.XMLDOM"); |
---|
| 13 | _xmlDoc.async = "false"; |
---|
| 14 | _xmlDoc.loadXML(pString); |
---|
| 15 | } |
---|
| 16 | else |
---|
| 17 | { |
---|
| 18 | var parser = new DOMParser(); |
---|
| 19 | var _xmlDoc = parser.parseFromString(pString, "text/xml"); |
---|
| 20 | } |
---|
| 21 | |
---|
| 22 | return _xmlDoc; |
---|
| 23 | } |
---|
| 24 | |
---|
| 25 | function _load(pFile) |
---|
| 26 | { |
---|
| 27 | if( pFile.indexOf("/") == -1 ) |
---|
| 28 | pFile = this._PATH + 'xsl/' + pFile; |
---|
| 29 | |
---|
| 30 | if ( !(_FILES[pFile]) ) |
---|
| 31 | { |
---|
| 32 | var _data = null; |
---|
| 33 | |
---|
| 34 | if ( document.implementation.createDocument ) |
---|
| 35 | { |
---|
| 36 | var xmlHttp = new window.XMLHttpRequest(); |
---|
| 37 | xmlHttp.open("GET",pFile + '?' + Date.parse(new Date),false ); |
---|
| 38 | xmlHttp.send(null); |
---|
| 39 | |
---|
| 40 | _data = xmlHttp.responseXML.documentElement; |
---|
| 41 | } |
---|
| 42 | else |
---|
| 43 | { |
---|
| 44 | _data = new ActiveXObject( "Msxml2.FreeThreadedDOMDocument" ); |
---|
| 45 | _data.async = false; |
---|
| 46 | _data.load( pFile + '?' + Date.parse(new Date) ); |
---|
| 47 | } |
---|
| 48 | |
---|
| 49 | _FILES[pFile] = _data; |
---|
| 50 | } |
---|
| 51 | return _FILES[pFile]; |
---|
| 52 | } |
---|
| 53 | |
---|
| 54 | function _parse() |
---|
| 55 | { |
---|
| 56 | if ( arguments.length == 1 ) |
---|
| 57 | { |
---|
| 58 | pXML = _xml('root'); |
---|
| 59 | pXSL = arguments[0]; |
---|
| 60 | } |
---|
| 61 | else |
---|
| 62 | { |
---|
| 63 | pXML = arguments[0]; |
---|
| 64 | pXSL = arguments[1]; |
---|
| 65 | } |
---|
| 66 | switch ( typeof pXML ) |
---|
| 67 | { |
---|
| 68 | case 'object' : |
---|
| 69 | break; |
---|
| 70 | case 'string' : |
---|
| 71 | if ( pXML.indexOf('<') == 0 ) |
---|
| 72 | pXML = _convert(pXML); |
---|
| 73 | else |
---|
| 74 | pXML = _load.call(this, pXML); |
---|
| 75 | break; |
---|
| 76 | default : |
---|
| 77 | return {'error':'invalid xml'} |
---|
| 78 | } |
---|
| 79 | switch ( typeof pXSL ) |
---|
| 80 | { |
---|
| 81 | case 'object' : |
---|
| 82 | break; |
---|
| 83 | case 'string' : |
---|
| 84 | pXSL = _load.call(this, pXSL); |
---|
| 85 | break; |
---|
| 86 | default : |
---|
| 87 | return {'error':'invalid xsl'} |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | var fragment = null; |
---|
| 91 | if ( window.XSLTProcessor ) |
---|
| 92 | { |
---|
| 93 | var xslProc = new XSLTProcessor(); |
---|
| 94 | xslProc.importStylesheet(pXSL); |
---|
| 95 | |
---|
| 96 | if ( (arguments.length == 3) && (typeof arguments[2] == 'object') ) |
---|
| 97 | { |
---|
| 98 | var params = arguments[2]; |
---|
| 99 | for (var i in params ) |
---|
| 100 | if ( params[ i ] && params[ i ].constructor != Function ) |
---|
| 101 | xslProc.setParameter(null, String( i ), String( params[i] ) ); |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | fragment = xslProc.transformToFragment(pXML, document); |
---|
| 105 | |
---|
| 106 | var aux = document.createElement("div"); |
---|
| 107 | aux.appendChild(fragment); |
---|
| 108 | fragment = aux.innerHTML; |
---|
| 109 | } |
---|
| 110 | else |
---|
| 111 | { |
---|
| 112 | var xslTemplate = new ActiveXObject("MSXML2.XSLTemplate"); |
---|
| 113 | xslTemplate.stylesheet = pXSL; |
---|
| 114 | |
---|
| 115 | var xslProc = xslTemplate.createProcessor(); |
---|
| 116 | xslProc.input = pXML; |
---|
| 117 | |
---|
| 118 | if ( (arguments.length == 3) && (typeof arguments[2] == 'object') ) |
---|
| 119 | { |
---|
| 120 | var params = arguments[2]; |
---|
| 121 | for (var i in params ) |
---|
| 122 | if ( params[ i ] && params[ i ].constructor != Function ) |
---|
| 123 | { |
---|
| 124 | xslProc.addParameter( String( i ), String( params[i] ), ''); |
---|
| 125 | } |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | xslProc.transform(); |
---|
| 129 | fragment = xslProc.output; |
---|
| 130 | } |
---|
| 131 | return fragment; |
---|
| 132 | } |
---|
| 133 | |
---|
| 134 | function _xml() |
---|
| 135 | { |
---|
| 136 | var a = false; |
---|
| 137 | if ( document.implementation.createDocument ) |
---|
| 138 | a = document.implementation.createDocument("", "", null); |
---|
| 139 | else if ( ActiveXObject ) |
---|
| 140 | a = new ActiveXObject("Msxml2.DOMDocument"); |
---|
| 141 | |
---|
| 142 | if ( arguments.length == 1 && typeof arguments[0] == 'string' ) |
---|
| 143 | a.appendChild(a.createElement(arguments[0])); |
---|
| 144 | //with ( a ) |
---|
| 145 | // appendChild(createProcessingInstruction("xml", "version='1.0'")); |
---|
| 146 | |
---|
| 147 | return a; |
---|
| 148 | } |
---|
| 149 | |
---|
| 150 | function xtools() |
---|
| 151 | { |
---|
| 152 | var _argv = arguments; |
---|
| 153 | this._PATH = ( _argv.length > 0 ) ? _argv[0] : ''; |
---|
| 154 | |
---|
| 155 | if ( this._PATH != '' && this._PATH.lastIndexOf('/') != (this._PATH.length - 1) ) |
---|
| 156 | this._PATH += '/'; |
---|
| 157 | } |
---|
| 158 | |
---|
| 159 | xtools.prototype.convert = _convert; |
---|
| 160 | xtools.prototype.load = _load; |
---|
| 161 | xtools.prototype.parse = _parse; |
---|
| 162 | xtools.prototype.xml = _xml; |
---|
| 163 | |
---|
| 164 | window.xtools = xtools; |
---|
| 165 | } |
---|
| 166 | )(); |
---|