source: trunk/expressoMail1_2/js/xtools.js @ 1503

Revision 1503, 3.6 KB checked in by rodsouza, 15 years ago (diff)

Ticket #622 - Utilizar XML para internacionalização.

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