Subversion Repositories Applications.papyrus

Rev

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