source: sandbox/jabberit_messenger/trophy_expresso/js/xtools.js @ 2670

Revision 2670, 3.2 KB checked in by alexandrecorreia, 14 years ago (diff)

Ticket #986 - Correcao para compatibilizar o carregamento do script para o trunk.

  • Property svn:executable set to *
Line 
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                pFile = this._PATH + 'xsl/' + pFile;
28                if ( !(_FILES[pFile]) )
29                {
30                        var _data = null;
31                        if ( document.implementation.createDocument )
32                                _data = document.implementation.createDocument("", "", null);
33                        else
34                                _data = new ActiveXObject( "Msxml2.FreeThreadedDOMDocument" );
35
36                        _data.async = false;
37                        _data.load(pFile + '?' + Date.parse(new Date));
38                        _FILES[pFile] = _data;
39                }
40                return _FILES[pFile];
41        }
42
43        function _parse()
44        {
45                if ( arguments.length == 1 )
46                {
47                        pXML = _xml('root');
48                        pXSL = arguments[0];
49                }
50                else
51                {
52                        pXML = arguments[0];
53                        pXSL = arguments[1];
54                }
55               
56                switch ( typeof pXML )
57                {
58                        case 'object' :
59                        break;
60                        case 'string' :
61                                if ( pXML.indexOf('<') == 0 )
62                                        pXML = _convert(pXML);
63                                else
64                                        pXML = _load.call(this, pXML);
65                        break;
66                        default :
67                                return { 'error' : 'invalid xml' }
68                }
69               
70                switch ( typeof pXSL )
71                {
72                        case 'object' :
73                        break;
74                        case 'string' :
75                                pXSL = _load.call(this, pXSL);
76                        break;
77                        default :
78                                return {'error':'invalid xsl'}
79                }
80
81                var fragment = null;
82               
83                if ( window.XSLTProcessor )
84                {
85                        var xslProc = new XSLTProcessor();
86                        xslProc.importStylesheet(pXSL);
87
88                        if ( (arguments.length == 3) && (typeof arguments[2] == 'object') )
89                        {
90                                var params = arguments[2];
91                                for (var i in params )
92                                        if ( params[ i ] && params[ i ].constructor != Function )
93                                                xslProc.setParameter(null, i, params[i]);
94                        }
95
96                        fragment = xslProc.transformToFragment(pXML, document);
97
98                        // para retornar valor igual ao ie.
99                        var aux = document.createElement("div");
100                        aux.appendChild(fragment);
101                        fragment = aux.innerHTML;
102                }
103                else
104                {
105                        var xslTemplate = new ActiveXObject("MSXML2.XSLTemplate");
106                        xslTemplate.stylesheet = pXSL;
107
108                        var xslProc = xslTemplate.createProcessor();
109                        xslProc.input = pXML;
110
111                        if ( (arguments.length == 3) && (typeof arguments[2] == 'object') )
112                        {
113                                var params = arguments[2];
114                                for (var i in params )
115                                         xslProc.addParameter(i, params[i], '');
116                        }
117
118                        xslProc.transform();
119                        fragment = xslProc.output;
120                }
121                return fragment;
122        }
123
124        function _xml()
125        {
126                var a = false;
127                if ( document.implementation.createDocument )
128                        a = document.implementation.createDocument("", "", null);
129                else if ( ActiveXObject )
130                        a = new ActiveXObject("Msxml2.DOMDocument");
131
132                if ( arguments.length == 1 && typeof arguments[0] == 'string' )
133                        a.appendChild(a.createElement(arguments[0]));
134                //with ( a )
135                //      appendChild(createProcessingInstruction("xml", "version='1.0'"));
136
137                return a;
138        }
139
140        function xtools()
141        {
142                var _argv = arguments;
143                this._PATH = ( _argv.length > 0 ) ? _argv[0] : '';
144
145                if ( this._PATH != '' && this._PATH.lastIndexOf('/') != (this._PATH.length - 1) )
146                        this._PATH += '/';
147        }
148
149        xtools.prototype.convert        = _convert;
150        xtools.prototype.load           = _load;
151        xtools.prototype.parse          = _parse;
152        xtools.prototype.xml            = _xml;
153       
154        window.xtools = xtools;
155}
156)();
Note: See TracBrowser for help on using the repository browser.