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

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

Ticket #986 - Importacao do modulo trophy integrado ao expresso.

  • 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                                         xslProc.setParameter(null, i, params[i]);
93                        }
94
95                        fragment = xslProc.transformToFragment(pXML, document);
96
97                        // para retornar valor igual ao ie.
98                        var aux = document.createElement("div");
99                        aux.appendChild(fragment);
100                        fragment = aux.innerHTML;
101                }
102                else
103                {
104                        var xslTemplate = new ActiveXObject("MSXML2.XSLTemplate");
105                        xslTemplate.stylesheet = pXSL;
106
107                        var xslProc = xslTemplate.createProcessor();
108                        xslProc.input = pXML;
109
110                        if ( (arguments.length == 3) && (typeof arguments[2] == 'object') )
111                        {
112                                var params = arguments[2];
113                                for (var i in params )
114                                         xslProc.addParameter(i, params[i], '');
115                        }
116
117                        xslProc.transform();
118                        fragment = xslProc.output;
119                }
120                return fragment;
121        }
122
123        function _xml()
124        {
125                var a = false;
126                if ( document.implementation.createDocument )
127                        a = document.implementation.createDocument("", "", null);
128                else if ( ActiveXObject )
129                        a = new ActiveXObject("Msxml2.DOMDocument");
130
131                if ( arguments.length == 1 && typeof arguments[0] == 'string' )
132                        a.appendChild(a.createElement(arguments[0]));
133                //with ( a )
134                //      appendChild(createProcessingInstruction("xml", "version='1.0'"));
135
136                return a;
137        }
138
139        function xtools()
140        {
141                var _argv = arguments;
142                this._PATH = ( _argv.length > 0 ) ? _argv[0] : '';
143
144                if ( this._PATH != '' && this._PATH.lastIndexOf('/') != (this._PATH.length - 1) )
145                        this._PATH += '/';
146        }
147
148        xtools.prototype.convert        = _convert;
149        xtools.prototype.load           = _load;
150        xtools.prototype.parse          = _parse;
151        xtools.prototype.xml            = _xml;
152       
153        window.xtools = xtools;
154}
155)();
Note: See TracBrowser for help on using the repository browser.