| 1 |
dojo.provide("dojox.wire.ml.util"); |
| 2 |
|
| 3 |
dojo.require("dojox.xml.parser"); |
| 4 |
dojo.require("dojox.wire.Wire"); |
| 5 |
|
| 6 |
dojox.wire.ml._getValue = function(source, args){ |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
if(!source){ |
| 23 |
return undefined; |
| 24 |
} |
| 25 |
var property = undefined; |
| 26 |
if(args && source.length >= 9 && source.substring(0, 9) == "arguments"){ |
| 27 |
property = source.substring(9); |
| 28 |
return new dojox.wire.Wire({property: property}).getValue(args); |
| 29 |
} |
| 30 |
var i = source.indexOf('.'); |
| 31 |
if(i >= 0){ |
| 32 |
property = source.substring(i + 1); |
| 33 |
source = source.substring(0, i); |
| 34 |
} |
| 35 |
var object = (dijit.byId(source) || dojo.byId(source) || dojo.getObject(source)); |
| 36 |
if(!object){ |
| 37 |
return undefined; |
| 38 |
} |
| 39 |
if(!property){ |
| 40 |
return object; |
| 41 |
}else{ |
| 42 |
return new dojox.wire.Wire({object: object, property: property}).getValue(); |
| 43 |
} |
| 44 |
}; |
| 45 |
|
| 46 |
dojox.wire.ml._setValue = function(target, value){ |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
if(!target){ |
| 57 |
return; |
| 58 |
} |
| 59 |
var i = target.indexOf('.'); |
| 60 |
if(i < 0){ |
| 61 |
return; |
| 62 |
} |
| 63 |
var object = this._getValue(target.substring(0, i)); |
| 64 |
if(!object){ |
| 65 |
return; |
| 66 |
} |
| 67 |
var property = target.substring(i + 1); |
| 68 |
var wire = new dojox.wire.Wire({object: object, property: property}).setValue(value); |
| 69 |
}; |
| 70 |
|
| 71 |
dojo.declare("dojox.wire.ml.XmlElement", null, { |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
constructor: function(element){ |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
if(dojo.isString(element)){ |
| 83 |
element = this._getDocument().createElement(element); |
| 84 |
} |
| 85 |
this.element = element; |
| 86 |
}, |
| 87 |
getPropertyValue: function(property){ |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
var value = undefined; |
| 101 |
if(!this.element){ |
| 102 |
return value; |
| 103 |
} |
| 104 |
if(!property){ |
| 105 |
return value; |
| 106 |
} |
| 107 |
|
| 108 |
if(property.charAt(0) == '@'){ |
| 109 |
var attribute = property.substring(1); |
| 110 |
value = this.element.getAttribute(attribute); |
| 111 |
}else if(property == "text()"){ |
| 112 |
var text = this.element.firstChild; |
| 113 |
if(text){ |
| 114 |
value = text.nodeValue; |
| 115 |
} |
| 116 |
}else{ |
| 117 |
var elements = []; |
| 118 |
for(var i = 0; i < this.element.childNodes.length; i++){ |
| 119 |
var child = this.element.childNodes[i]; |
| 120 |
if(child.nodeType === 1 && child.nodeName == property){ |
| 121 |
elements.push(new dojox.wire.ml.XmlElement(child)); |
| 122 |
} |
| 123 |
} |
| 124 |
if(elements.length > 0){ |
| 125 |
if(elements.length === 1){ |
| 126 |
value = elements[0]; |
| 127 |
}else{ |
| 128 |
value = elements; |
| 129 |
} |
| 130 |
} |
| 131 |
} |
| 132 |
return value; |
| 133 |
}, |
| 134 |
|
| 135 |
setPropertyValue: function(property, value){ |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
var i; |
| 151 |
var text; |
| 152 |
if(!this.element){ |
| 153 |
return; |
| 154 |
} |
| 155 |
if(!property){ |
| 156 |
return; |
| 157 |
} |
| 158 |
|
| 159 |
if(property.charAt(0) == '@'){ |
| 160 |
var attribute = property.substring(1); |
| 161 |
if(value){ |
| 162 |
this.element.setAttribute(attribute, value); |
| 163 |
}else{ |
| 164 |
this.element.removeAttribute(attribute); |
| 165 |
} |
| 166 |
}else if(property == "text()"){ |
| 167 |
while(this.element.firstChild){ |
| 168 |
this.element.removeChild(this.element.firstChild); |
| 169 |
} |
| 170 |
if(value){ |
| 171 |
text = this._getDocument().createTextNode(value); |
| 172 |
this.element.appendChild(text); |
| 173 |
} |
| 174 |
}else{ |
| 175 |
var nextChild = null; |
| 176 |
var child; |
| 177 |
for(i = this.element.childNodes.length - 1; i >= 0; i--){ |
| 178 |
child = this.element.childNodes[i]; |
| 179 |
if(child.nodeType === 1 && child.nodeName == property){ |
| 180 |
if(!nextChild){ |
| 181 |
nextChild = child.nextSibling; |
| 182 |
} |
| 183 |
this.element.removeChild(child); |
| 184 |
} |
| 185 |
} |
| 186 |
if(value){ |
| 187 |
if(dojo.isArray(value)){ |
| 188 |
for(i in value){ |
| 189 |
var e = value[i]; |
| 190 |
if(e.element){ |
| 191 |
this.element.insertBefore(e.element, nextChild); |
| 192 |
} |
| 193 |
} |
| 194 |
}else if(value instanceof dojox.wire.ml.XmlElement){ |
| 195 |
if(value.element){ |
| 196 |
this.element.insertBefore(value.element, nextChild); |
| 197 |
} |
| 198 |
}else{ |
| 199 |
child = this._getDocument().createElement(property); |
| 200 |
text = this._getDocument().createTextNode(value); |
| 201 |
child.appendChild(text); |
| 202 |
this.element.insertBefore(child, nextChild); |
| 203 |
} |
| 204 |
} |
| 205 |
} |
| 206 |
}, |
| 207 |
|
| 208 |
toString: function(){ |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
var s = ""; |
| 216 |
if(this.element){ |
| 217 |
var text = this.element.firstChild; |
| 218 |
if(text){ |
| 219 |
s = text.nodeValue; |
| 220 |
} |
| 221 |
} |
| 222 |
return s; |
| 223 |
}, |
| 224 |
|
| 225 |
toObject: function(){ |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
if(!this.element){ |
| 234 |
return null; |
| 235 |
} |
| 236 |
var text = ""; |
| 237 |
var obj = {}; |
| 238 |
var elements = 0; |
| 239 |
var i; |
| 240 |
for(i = 0; i < this.element.childNodes.length; i++){ |
| 241 |
var child = this.element.childNodes[i]; |
| 242 |
if(child.nodeType === 1 ){ |
| 243 |
elements++; |
| 244 |
var o = new dojox.wire.ml.XmlElement(child).toObject(); |
| 245 |
var name = child.nodeName; |
| 246 |
var p = obj[name]; |
| 247 |
if(!p){ |
| 248 |
obj[name] = o; |
| 249 |
}else if(dojo.isArray(p)){ |
| 250 |
p.push(o); |
| 251 |
}else{ |
| 252 |
obj[name] = [p, o]; |
| 253 |
} |
| 254 |
}else if(child.nodeType === 3 || |
| 255 |
child.nodeType === 4 ){ |
| 256 |
text += child.nodeValue; |
| 257 |
} |
| 258 |
} |
| 259 |
var attributes = 0; |
| 260 |
if(this.element.nodeType === 1 ){ |
| 261 |
attributes = this.element.attributes.length; |
| 262 |
for(i = 0; i < attributes; i++){ |
| 263 |
var attr = this.element.attributes[i]; |
| 264 |
obj["@" + attr.nodeName] = attr.nodeValue; |
| 265 |
} |
| 266 |
} |
| 267 |
if(elements === 0){ |
| 268 |
if(attributes === 0){ |
| 269 |
|
| 270 |
return text; |
| 271 |
} |
| 272 |
|
| 273 |
obj["text()"] = text; |
| 274 |
} |
| 275 |
|
| 276 |
return obj; |
| 277 |
}, |
| 278 |
|
| 279 |
_getDocument: function(){ |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
if(this.element){ |
| 289 |
return (this.element.nodeType == 9 ? |
| 290 |
this.element : this.element.ownerDocument); |
| 291 |
}else{ |
| 292 |
return dojox.xml.parser.parse(); |
| 293 |
} |
| 294 |
} |
| 295 |
}); |