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