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