Subversion Repositories Applications.papyrus

Rev

Rev 1372 | Go to most recent revision | 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.Parse");
14
dojo.require("dojo.dom");
15
dojo.xml.Parse = function () {
16
	var isIE = ((dojo.render.html.capable) && (dojo.render.html.ie));
17
	function getTagName(node) {
18
		try {
19
			return node.tagName.toLowerCase();
20
		}
21
		catch (e) {
22
			return "";
23
		}
24
	}
25
	function getDojoTagName(node) {
26
		var tagName = getTagName(node);
27
		if (!tagName) {
28
			return "";
29
		}
30
		if ((dojo.widget) && (dojo.widget.tags[tagName])) {
31
			return tagName;
32
		}
33
		var p = tagName.indexOf(":");
34
		if (p >= 0) {
35
			return tagName;
36
		}
37
		if (tagName.substr(0, 5) == "dojo:") {
38
			return tagName;
39
		}
40
		if (dojo.render.html.capable && dojo.render.html.ie && node.scopeName != "HTML") {
41
			return node.scopeName.toLowerCase() + ":" + tagName;
42
		}
43
		if (tagName.substr(0, 4) == "dojo") {
44
			return "dojo:" + tagName.substring(4);
45
		}
46
		var djt = node.getAttribute("dojoType") || node.getAttribute("dojotype");
47
		if (djt) {
48
			if (djt.indexOf(":") < 0) {
49
				djt = "dojo:" + djt;
50
			}
51
			return djt.toLowerCase();
52
		}
53
		djt = node.getAttributeNS && node.getAttributeNS(dojo.dom.dojoml, "type");
54
		if (djt) {
55
			return "dojo:" + djt.toLowerCase();
56
		}
57
		try {
58
			djt = node.getAttribute("dojo:type");
59
		}
60
		catch (e) {
61
		}
62
		if (djt) {
63
			return "dojo:" + djt.toLowerCase();
64
		}
65
		if ((dj_global["djConfig"]) && (!djConfig["ignoreClassNames"])) {
66
			var classes = node.className || node.getAttribute("class");
67
			if ((classes) && (classes.indexOf) && (classes.indexOf("dojo-") != -1)) {
68
				var aclasses = classes.split(" ");
69
				for (var x = 0, c = aclasses.length; x < c; x++) {
70
					if (aclasses[x].slice(0, 5) == "dojo-") {
71
						return "dojo:" + aclasses[x].substr(5).toLowerCase();
72
					}
73
				}
74
			}
75
		}
76
		return "";
77
	}
78
	this.parseElement = function (node, hasParentNodeSet, optimizeForDojoML, thisIdx) {
79
		var tagName = getTagName(node);
80
		if (isIE && tagName.indexOf("/") == 0) {
81
			return null;
82
		}
83
		try {
84
			var attr = node.getAttribute("parseWidgets");
85
			if (attr && attr.toLowerCase() == "false") {
86
				return {};
87
			}
88
		}
89
		catch (e) {
90
		}
91
		var process = true;
92
		if (optimizeForDojoML) {
93
			var dojoTagName = getDojoTagName(node);
94
			tagName = dojoTagName || tagName;
95
			process = Boolean(dojoTagName);
96
		}
97
		var parsedNodeSet = {};
98
		parsedNodeSet[tagName] = [];
99
		var pos = tagName.indexOf(":");
100
		if (pos > 0) {
101
			var ns = tagName.substring(0, pos);
102
			parsedNodeSet["ns"] = ns;
103
			if ((dojo.ns) && (!dojo.ns.allow(ns))) {
104
				process = false;
105
			}
106
		}
107
		if (process) {
108
			var attributeSet = this.parseAttributes(node);
109
			for (var attr in attributeSet) {
110
				if ((!parsedNodeSet[tagName][attr]) || (typeof parsedNodeSet[tagName][attr] != "array")) {
111
					parsedNodeSet[tagName][attr] = [];
112
				}
113
				parsedNodeSet[tagName][attr].push(attributeSet[attr]);
114
			}
115
			parsedNodeSet[tagName].nodeRef = node;
116
			parsedNodeSet.tagName = tagName;
117
			parsedNodeSet.index = thisIdx || 0;
118
		}
119
		var count = 0;
120
		for (var i = 0; i < node.childNodes.length; i++) {
121
			var tcn = node.childNodes.item(i);
122
			switch (tcn.nodeType) {
123
			  case dojo.dom.ELEMENT_NODE:
124
				var ctn = getDojoTagName(tcn) || getTagName(tcn);
125
				if (!parsedNodeSet[ctn]) {
126
					parsedNodeSet[ctn] = [];
127
				}
128
				parsedNodeSet[ctn].push(this.parseElement(tcn, true, optimizeForDojoML, count));
129
				if ((tcn.childNodes.length == 1) && (tcn.childNodes.item(0).nodeType == dojo.dom.TEXT_NODE)) {
130
					parsedNodeSet[ctn][parsedNodeSet[ctn].length - 1].value = tcn.childNodes.item(0).nodeValue;
131
				}
132
				count++;
133
				break;
134
			  case dojo.dom.TEXT_NODE:
135
				if (node.childNodes.length == 1) {
136
					parsedNodeSet[tagName].push({value:node.childNodes.item(0).nodeValue});
137
				}
138
				break;
139
			  default:
140
				break;
141
			}
142
		}
143
		return parsedNodeSet;
144
	};
145
	this.parseAttributes = function (node) {
146
		var parsedAttributeSet = {};
147
		var atts = node.attributes;
148
		var attnode, i = 0;
149
		while ((attnode = atts[i++])) {
150
			if (isIE) {
151
				if (!attnode) {
152
					continue;
153
				}
154
				if ((typeof attnode == "object") && (typeof attnode.nodeValue == "undefined") || (attnode.nodeValue == null) || (attnode.nodeValue == "")) {
155
					continue;
156
				}
157
			}
158
			var nn = attnode.nodeName.split(":");
159
			nn = (nn.length == 2) ? nn[1] : attnode.nodeName;
160
			parsedAttributeSet[nn] = {value:attnode.nodeValue};
161
		}
162
		return parsedAttributeSet;
163
	};
164
};
165