Subversion Repositories Applications.papyrus

Rev

Rev 1422 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1318 alexandre_ 1
/*
2
	Copyright (c) 2004-2006, The Dojo Foundation
3
	All Rights Reserved.
4
 
5
	Licensed under the Academic Free License version 2.1 or above OR the
6
	modified BSD license. For more information on Dojo licensing, see:
7
 
8
		http://dojotoolkit.org/community/licensing.shtml
9
*/
10
 
1422 alexandre_ 11
 
12
 
1318 alexandre_ 13
dojo.provide("dojo.xml.XslTransform");
14
dojo.xml.XslTransform = function (xsltUri) {
15
	dojo.debug("XslTransform is supported by Internet Explorer and Mozilla, with limited support in Opera 9 (no document function support).");
16
	var IS_IE = dojo.render.html.ie;
17
	var ACTIVEX_DOMS = ["Msxml2.DOMDocument.5.0", "Msxml2.DOMDocument.4.0", "Msxml2.DOMDocument.3.0", "MSXML2.DOMDocument", "MSXML.DOMDocument", "Microsoft.XMLDOM"];
18
	var ACTIVEX_FT_DOMS = ["Msxml2.FreeThreadedDOMDocument.5.0", "MSXML2.FreeThreadedDOMDocument.4.0", "MSXML2.FreeThreadedDOMDocument.3.0"];
19
	var ACTIVEX_TEMPLATES = ["Msxml2.XSLTemplate.5.0", "Msxml2.XSLTemplate.4.0", "MSXML2.XSLTemplate.3.0"];
20
	function getActiveXImpl(activeXArray) {
21
		for (var i = 0; i < activeXArray.length; i++) {
22
			try {
23
				var testObj = new ActiveXObject(activeXArray[i]);
24
				if (testObj) {
25
					return activeXArray[i];
26
				}
27
			}
28
			catch (e) {
29
			}
30
		}
31
		dojo.raise("Could not find an ActiveX implementation in:\n\n " + activeXArray);
32
	}
33
	if (xsltUri == null || xsltUri == undefined) {
34
		dojo.raise("You must pass the URI String for the XSL file to be used!");
35
		return false;
36
	}
37
	var xsltDocument = null;
38
	var xsltProcessor = null;
39
	if (IS_IE) {
40
		xsltDocument = new ActiveXObject(getActiveXImpl(ACTIVEX_FT_DOMS));
41
		xsltDocument.async = false;
42
	} else {
43
		xsltProcessor = new XSLTProcessor();
44
		xsltDocument = document.implementation.createDocument("", "", null);
45
		xsltDocument.addEventListener("load", onXslLoad, false);
46
	}
47
	xsltDocument.load(xsltUri);
48
	if (IS_IE) {
49
		var xslt = new ActiveXObject(getActiveXImpl(ACTIVEX_TEMPLATES));
50
		xslt.stylesheet = xsltDocument;
51
		xsltProcessor = xslt.createProcessor();
52
	}
53
	function onXslLoad() {
54
		xsltProcessor.importStylesheet(xsltDocument);
55
	}
56
	function getResultDom(xmlDoc, params) {
57
		if (IS_IE) {
58
			addIeParams(params);
59
			var result = getIeResultDom(xmlDoc);
60
			removeIeParams(params);
61
			return result;
62
		} else {
63
			return getMozillaResultDom(xmlDoc, params);
64
		}
65
	}
66
	function addIeParams(params) {
67
		if (!params) {
68
			return;
69
		}
70
		for (var i = 0; i < params.length; i++) {
71
			xsltProcessor.addParameter(params[i][0], params[i][1]);
72
		}
73
	}
74
	function removeIeParams(params) {
75
		if (!params) {
76
			return;
77
		}
78
		for (var i = 0; i < params.length; i++) {
79
			xsltProcessor.addParameter(params[i][0], "");
80
		}
81
	}
82
	function getIeResultDom(xmlDoc) {
83
		xsltProcessor.input = xmlDoc;
84
		var outDoc = new ActiveXObject(getActiveXImpl(ACTIVEX_DOMS));
85
		outDoc.async = false;
86
		outDoc.validateOnParse = false;
87
		xsltProcessor.output = outDoc;
88
		xsltProcessor.transform();
89
		if (outDoc.parseError.errorCode != 0) {
90
			var err = outDoc.parseError;
91
			dojo.raise("err.errorCode: " + err.errorCode + "\n\nerr.reason: " + err.reason + "\n\nerr.url: " + err.url + "\n\nerr.srcText: " + err.srcText);
92
		}
93
		return outDoc;
94
	}
95
	function getIeResultStr(xmlDoc, params) {
96
		xsltProcessor.input = xmlDoc;
97
		xsltProcessor.transform();
98
		return xsltProcessor.output;
99
	}
100
	function addMozillaParams(params) {
101
		if (!params) {
102
			return;
103
		}
104
		for (var i = 0; i < params.length; i++) {
105
			xsltProcessor.setParameter(null, params[i][0], params[i][1]);
106
		}
107
	}
108
	function getMozillaResultDom(xmlDoc, params) {
109
		addMozillaParams(params);
110
		var resultDoc = xsltProcessor.transformToDocument(xmlDoc);
111
		xsltProcessor.clearParameters();
112
		return resultDoc;
113
	}
114
	function getMozillaResultStr(xmlDoc, params, parentDoc) {
115
		addMozillaParams(params);
116
		var resultDoc = xsltProcessor.transformToFragment(xmlDoc, parentDoc);
117
		var serializer = new XMLSerializer();
118
		xsltProcessor.clearParameters();
119
		return serializer.serializeToString(resultDoc);
120
	}
121
	this.getResultString = function (xmlDoc, params, parentDoc) {
122
		var content = null;
123
		if (IS_IE) {
124
			addIeParams(params);
125
			content = getIeResultStr(xmlDoc, params);
126
			removeIeParams(params);
127
		} else {
128
			content = getMozillaResultStr(xmlDoc, params, parentDoc);
129
		}
130
		return content;
131
	};
132
	this.transformToContentPane = function (xmlDoc, params, contentPane, parentDoc) {
133
		var content = this.getResultString(xmlDoc, params, parentDoc);
134
		contentPane.setContent(content);
135
	};
136
	this.transformToRegion = function (xmlDoc, params, region, parentDoc) {
137
		try {
138
			var content = this.getResultString(xmlDoc, params, parentDoc);
139
			region.innerHTML = content;
140
		}
141
		catch (e) {
142
			dojo.raise(e.message + "\n\n xsltUri: " + xsltUri);
143
		}
144
	};
145
	this.transformToDocument = function (xmlDoc, params) {
146
		return getResultDom(xmlDoc, params);
147
	};
148
	this.transformToWindow = function (xmlDoc, params, windowDoc, parentDoc) {
149
		try {
150
			windowDoc.open();
151
			windowDoc.write(this.getResultString(xmlDoc, params, parentDoc));
152
			windowDoc.close();
153
		}
154
		catch (e) {
155
			dojo.raise(e.message + "\n\n xsltUri: " + xsltUri);
156
		}
157
	};
158
};
159