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