Subversion Repositories Applications.papyrus

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2150 mathias 1
if(!dojo._hasResource["dojox.wire.XmlWire"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
2
dojo._hasResource["dojox.wire.XmlWire"] = true;
3
dojo.provide("dojox.wire.XmlWire");
4
 
5
dojo.require("dojox.data.dom");
6
dojo.require("dojox.wire.Wire");
7
 
8
dojo.declare("dojox.wire.XmlWire", dojox.wire.Wire, {
9
	//	summary:
10
	//		A Wire for XML nodes or values (element, attribute and text)
11
	//	description:
12
	//		This class accesses XML nodes or value with a simplified XPath
13
	//		specified to 'path' property.
14
	//		The root object for this class must be an DOM document or element
15
	//		node.
16
	//		"@name" accesses to an attribute value of an element and "text()"
17
	//		accesses to a text value of an element.
18
	//		The hierarchy of the elements from the root node can be specified
19
	//		with slash-separated list, such as "a/b/@c", which specifies
20
	//		the value of an attribute named "c" of an element named "b" as
21
	//		a child of another element named "a" of a child of the root node.
22
 
23
	_wireClass: "dojox.wire.XmlWire",
24
 
25
	constructor: function(/*Object*/args){
26
		//	summary:
27
		//		Initialize properties
28
		//	description:
29
		//		'args' is just mixed in with no further processing.
30
		//	args:
31
		//		Arguments to initialize properties
32
		//		path:
33
		//			A simplified XPath to an attribute, a text or elements
34
	},
35
	_getValue: function(/*Node*/object){
36
		//	summary:
37
		//		Return an attribute value, a text value or an array of elements
38
		//	description:
39
		//		This method first uses a root node passed in 'object' argument
40
		//		and 'path' property to identify an attribute, a text or
41
		//		elements.
42
		//		If 'path' starts with a slash (absolute), the first path
43
		//		segment is ignored assuming it point to the root node.
44
		//		(That is, "/a/b/@c" and "b/@c" against a root node access
45
		//		the same attribute value, assuming the root node is an element
46
		//		with a tag name, "a".)
47
		//	object:
48
		//		A root node
49
		//	returns:
50
		//		A value found, otherwise 'undefined'
51
		if(!object || !this.path){
52
			return object; //Node
53
		}
54
 
55
		var node = object;
56
		var path = this.path;
57
		if(path.charAt(0) == '/'){ // absolute
58
			// skip the first expression (supposed to select the top node)
59
			var i = path.indexOf('/', 1);
60
			path = path.substring(i + 1);
61
		}
62
		var list = path.split('/');
63
		var last = list.length - 1;
64
		for(var i = 0; i < last; i++){
65
			node = this._getChildNode(node, list[i]);
66
			if(!node){
67
				return undefined; //undefined
68
			}
69
		}
70
		var value = this._getNodeValue(node, list[last]);
71
		return value; //String||Array
72
	},
73
 
74
	_setValue: function(/*Node*/object, /*String*/value){
75
		//	summary:
76
		//		Set an attribute value or a child text value to an element
77
		//	description:
78
		//		This method first uses a root node passed in 'object' argument
79
		//		and 'path' property to identify an attribute, a text or
80
		//		elements.
81
		//		If an intermediate element does not exist, it creates
82
		//		an element of the tag name in the 'path' segment as a child
83
		//		node of the current node.
84
		//		Finally, 'value' argument is set to an attribute or a text
85
		//		(a child node) of the leaf element.
86
		//	object:
87
		//		A root node
88
		//	value:
89
		//		A value to set
90
		if(!this.path){
91
			return object; //Node
92
		}
93
 
94
		var node = object;
95
		var doc = this._getDocument(node);
96
		var path = this.path;
97
		if(path.charAt(0) == '/'){ // absolute
98
			var i = path.indexOf('/', 1);
99
			if(!node){
100
				var name = path.substring(1, i);
101
				node = doc.createElement(name);
102
				object = node; // to be returned as a new object
103
			}
104
			// skip the first expression (supposed to select the top node)
105
			path = path.substring(i + 1);
106
		}else{
107
			if(!node){
108
				return undefined; //undefined
109
			}
110
		}
111
 
112
		var list = path.split('/');
113
		var last = list.length - 1;
114
		for(var i = 0; i < last; i++){
115
			var child = this._getChildNode(node, list[i]);
116
			if(!child){
117
				child = doc.createElement(list[i]);
118
				node.appendChild(child);
119
			}
120
			node = child;
121
		}
122
		this._setNodeValue(node, list[last], value);
123
		return object; //Node
124
	},
125
 
126
	_getNodeValue: function(/*Node*/node, /*String*/exp){
127
		//	summary:
128
		//		Return an attribute value, a text value or an array of elements
129
		//	description:
130
		//		If 'exp' starts with '@', an attribute value of the specified
131
		//		attribute is returned.
132
		//		If 'exp' is "text()", a child text value is returned.
133
		//		Otherwise, an array of child elements, the tag name of which
134
		//		match 'exp', is returned.
135
		//	node:
136
		//		A node
137
		//	exp:
138
		//		An expression for attribute, text or elements
139
		//	returns:
140
		//		A value found, otherwise 'undefined'
141
		var value = undefined;
142
		if(exp.charAt(0) == '@'){
143
			var attribute = exp.substring(1);
144
			value = node.getAttribute(attribute);
145
		}else if(exp == "text()"){
146
			var text = node.firstChild;
147
			if(text){
148
				value = text.nodeValue;
149
			}
150
		}else{ // assume elements
151
			value = [];
152
			for(var i = 0; i < node.childNodes.length; i++){
153
				var child = node.childNodes[i];
154
				if(child.nodeType === 1 /* ELEMENT_NODE */ && child.nodeName == exp){
155
					value.push(child);
156
				}
157
			}
158
		}
159
		return value; //String||Array
160
	},
161
 
162
	_setNodeValue: function(/*Node*/node, /*String*/exp, /*String*/value){
163
		//	summary:
164
		//		Set an attribute value or a child text value to an element
165
		//	description:
166
		//		If 'exp' starts with '@', 'value' is set to the specified
167
		//		attribute.
168
		//		If 'exp' is "text()", 'value' is set to a child text.
169
		//	node:
170
		//		A node
171
		//	exp:
172
		//		An expression for attribute or text
173
		//	value:
174
		//		A value to set
175
		if(exp.charAt(0) == '@'){
176
			var attribute = exp.substring(1);
177
			if(value){
178
				node.setAttribute(attribute, value);
179
			}else{
180
				node.removeAttribute(attribute);
181
			}
182
		}else if(exp == "text()"){
183
			while(node.firstChild){
184
				node.removeChild(node.firstChild);
185
			}
186
			if(value){
187
				var text = this._getDocument(node).createTextNode(value);
188
				node.appendChild(text);
189
			}
190
		}
191
		// else not supported
192
	},
193
 
194
	_getChildNode: function(/*Node*/node, /*String*/name){
195
		//	summary:
196
		//		Return a child node
197
		//	description:
198
		//		A child element of the tag name specified with 'name' is
199
		//		returned.
200
		//		If 'name' ends with an array index, it is used to pick up
201
		//		the corresponding element from multiple child elements.
202
		//	node:
203
		//		A parent node
204
		//	name:
205
		//		A tag name
206
		//	returns:
207
		//		A child node
208
		var index = 1;
209
		var i1 = name.indexOf('[');
210
		if(i1 >= 0){
211
			var i2 = name.indexOf(']');
212
			index = name.substring(i1 + 1, i2);
213
			name = name.substring(0, i1);
214
		}
215
		var count = 1;
216
		for(var i = 0; i < node.childNodes.length; i++){
217
			var child = node.childNodes[i];
218
			if(child.nodeType === 1 /* ELEMENT_NODE */ && child.nodeName == name){
219
				if(count == index){
220
					return child; //Node
221
				}
222
				count++;
223
			}
224
		}
225
		return null; //null
226
	},
227
 
228
	_getDocument: function(/*Node*/node){
229
		//	summary:
230
		//		Return a DOM document
231
		//	description:
232
		//		If 'node' is specified, a DOM document of the node is returned.
233
		//		Otherwise, a DOM document is created.
234
		//	returns:
235
		//		A DOM document
236
		if(node){
237
			return (node.nodeType == 9 /* DOCUMENT_NODE */ ? node : node.ownerDocument); //Document
238
		}else{
239
			return dojox.data.dom.createDocument(); //Document
240
		}
241
	}
242
});
243
 
244
}