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 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
|
// Copyright 2006 The Closure Library Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/**
* @fileoverview
* Implementations of DataNode for wrapping XML data.
*
*/
goog.provide('goog.ds.XmlDataSource');
goog.provide('goog.ds.XmlHttpDataSource');
goog.require('goog.Uri');
goog.require('goog.dom.NodeType');
goog.require('goog.dom.xml');
goog.require('goog.ds.BasicNodeList');
goog.require('goog.ds.DataManager');
goog.require('goog.ds.DataNode');
goog.require('goog.ds.LoadState');
goog.require('goog.ds.logger');
goog.require('goog.log');
goog.require('goog.net.XhrIo');
goog.require('goog.string');
/**
* Data source whose backing is an xml node
*
* @param {Node} node The XML node. Can be null.
* @param {goog.ds.XmlDataSource} parent Parent of XML element. Can be null.
* @param {string=} opt_name The name of this node relative to the parent node.
*
* @extends {goog.ds.DataNode}
* @constructor
*/
// TODO(arv): Use interfaces when available.
goog.ds.XmlDataSource = function(node, parent, opt_name) {
this.parent_ = parent;
this.dataName_ = opt_name || (node ? node.nodeName : '');
this.setNode_(node);
};
/**
* Constant to select XML attributes for getChildNodes
* @type {string}
* @private
*/
goog.ds.XmlDataSource.ATTRIBUTE_SELECTOR_ = '@*';
/**
* Set the current root nodeof the data source.
* Can be an attribute node, text node, or element node
* @param {Node} node The node. Can be null.
*
* @private
*/
goog.ds.XmlDataSource.prototype.setNode_ = function(node) {
this.node_ = node;
if (node != null) {
switch (node.nodeType) {
case goog.dom.NodeType.ATTRIBUTE:
case goog.dom.NodeType.TEXT:
this.value_ = node.nodeValue;
break;
case goog.dom.NodeType.ELEMENT:
if (node.childNodes.length == 1 &&
node.firstChild.nodeType == goog.dom.NodeType.TEXT) {
this.value_ = node.firstChild.nodeValue;
}
}
}
};
/**
* Creates the DataNodeList with the child nodes for this element.
* Allows for only building list as needed.
*
* @private
*/
goog.ds.XmlDataSource.prototype.createChildNodes_ = function() {
if (this.childNodeList_) {
return;
}
var childNodeList = new goog.ds.BasicNodeList();
if (this.node_ != null) {
var childNodes = this.node_.childNodes;
for (var i = 0, childNode; childNode = childNodes[i]; i++) {
if (childNode.nodeType != goog.dom.NodeType.TEXT ||
!goog.ds.XmlDataSource.isEmptyTextNodeValue_(childNode.nodeValue)) {
var newNode =
new goog.ds.XmlDataSource(childNode, this, childNode.nodeName);
childNodeList.add(newNode);
}
}
}
this.childNodeList_ = childNodeList;
};
/**
* Creates the DataNodeList with the attributes for the element
* Allows for only building list as needed.
*
* @private
*/
goog.ds.XmlDataSource.prototype.createAttributes_ = function() {
if (this.attributes_) {
return;
}
var attributes = new goog.ds.BasicNodeList();
if (this.node_ != null && this.node_.attributes != null) {
var atts = this.node_.attributes;
for (var i = 0, att; att = atts[i]; i++) {
var newNode = new goog.ds.XmlDataSource(att, this, att.nodeName);
attributes.add(newNode);
}
}
this.attributes_ = attributes;
};
/**
* Get the value of the node
* @return {Object} The value of the node, or null if no value.
* @override
*/
goog.ds.XmlDataSource.prototype.get = function() {
this.createChildNodes_();
return this.value_;
};
/**
* Set the value of the node
* @param {*} value The new value of the node.
* @override
*/
goog.ds.XmlDataSource.prototype.set = function(value) {
throw new Error('Can\'t set on XmlDataSource yet');
};
/** @override */
goog.ds.XmlDataSource.prototype.getChildNodes = function(opt_selector) {
if (opt_selector &&
opt_selector == goog.ds.XmlDataSource.ATTRIBUTE_SELECTOR_) {
this.createAttributes_();
return this.attributes_;
} else if (
opt_selector == null ||
opt_selector == goog.ds.STR_ALL_CHILDREN_SELECTOR) {
this.createChildNodes_();
return this.childNodeList_;
} else {
throw new Error('Unsupported selector');
}
};
/**
* Gets a named child node of the current node
* @param {string} name The node name.
* @return {goog.ds.DataNode} The child node, or null if
* no node of this name exists.
* @override
*/
goog.ds.XmlDataSource.prototype.getChildNode = function(name) {
if (goog.string.startsWith(name, goog.ds.STR_ATTRIBUTE_START)) {
var att = this.node_.getAttributeNode(name.substring(1));
return att ? new goog.ds.XmlDataSource(att, this) : null;
} else {
return /** @type {goog.ds.DataNode} */ (this.getChildNodes().get(name));
}
};
/**
* Gets the value of a child node
* @param {string} name The node name.
* @return {*} The value of the node, or null if no value or the child node
* doesn't exist.
* @override
*/
goog.ds.XmlDataSource.prototype.getChildNodeValue = function(name) {
if (goog.string.startsWith(name, goog.ds.STR_ATTRIBUTE_START)) {
var node = this.node_.getAttributeNode(name.substring(1));
return node ? node.nodeValue : null;
} else {
var node = this.getChildNode(name);
return node ? node.get() : null;
}
};
/**
* Get the name of the node relative to the parent node
* @return {string} The name of the node.
* @override
*/
goog.ds.XmlDataSource.prototype.getDataName = function() {
return this.dataName_;
};
/**
* Setthe name of the node relative to the parent node
* @param {string} name The name of the node.
* @override
*/
goog.ds.XmlDataSource.prototype.setDataName = function(name) {
this.dataName_ = name;
};
/**
* Gets the a qualified data path to this node
* @return {string} The data path.
* @override
*/
goog.ds.XmlDataSource.prototype.getDataPath = function() {
var parentPath = '';
if (this.parent_) {
parentPath = this.parent_.getDataPath() +
(this.dataName_.indexOf(goog.ds.STR_ARRAY_START) != -1 ?
'' :
goog.ds.STR_PATH_SEPARATOR);
}
return parentPath + this.dataName_;
};
/**
* Load or reload the backing data for this node
* @override
*/
goog.ds.XmlDataSource.prototype.load = function() {
// Nothing to do
};
/**
* Gets the state of the backing data for this node
* @return {goog.ds.LoadState} The state.
* @override
*/
goog.ds.XmlDataSource.prototype.getLoadState = function() {
return this.node_ ? goog.ds.LoadState.LOADED : goog.ds.LoadState.NOT_LOADED;
};
/**
* Check whether a node is an empty text node. Nodes consisting of only white
* space (#x20, #xD, #xA, #x9) can generally be collapsed to a zero length
* text string.
* @param {string} str String to match.
* @return {boolean} True if string equates to empty text node.
* @private
*/
goog.ds.XmlDataSource.isEmptyTextNodeValue_ = function(str) {
return /^[\r\n\t ]*$/.test(str);
};
/**
* Creates an XML document with one empty node.
* Useful for places where you need a node that
* can be queried against.
*
* @return {Document} Document with one empty node.
* @private
*/
goog.ds.XmlDataSource.createChildlessDocument_ = function() {
return goog.dom.xml.createDocument('nothing');
};
/**
* Data source whose backing is an XMLHttpRequest,
*
* A URI of an empty string will mean that no request is made
* and the data source will be a single, empty node.
*
* @param {(string|goog.Uri)} uri URL of the XMLHttpRequest.
* @param {string} name Name of the datasource.
*
* implements goog.ds.XmlHttpDataSource.
* @constructor
* @extends {goog.ds.XmlDataSource}
* @final
*/
goog.ds.XmlHttpDataSource = function(uri, name) {
goog.ds.XmlDataSource.call(this, null, null, name);
if (uri) {
this.uri_ = new goog.Uri(uri);
} else {
this.uri_ = null;
}
};
goog.inherits(goog.ds.XmlHttpDataSource, goog.ds.XmlDataSource);
/**
* Default load state is NOT_LOADED
* @private
*/
goog.ds.XmlHttpDataSource.prototype.loadState_ = goog.ds.LoadState.NOT_LOADED;
/**
* Load or reload the backing data for this node.
* Fires the XMLHttpRequest
* @override
*/
goog.ds.XmlHttpDataSource.prototype.load = function() {
if (this.uri_) {
goog.log.info(
goog.ds.logger, 'Sending XML request for DataSource ' +
this.getDataName() + ' to ' + this.uri_);
this.loadState_ = goog.ds.LoadState.LOADING;
goog.net.XhrIo.send(this.uri_, goog.bind(this.complete_, this));
} else {
this.node_ = goog.ds.XmlDataSource.createChildlessDocument_();
this.loadState_ = goog.ds.LoadState.NOT_LOADED;
}
};
/**
* Gets the state of the backing data for this node
* @return {goog.ds.LoadState} The state.
* @override
*/
goog.ds.XmlHttpDataSource.prototype.getLoadState = function() {
return this.loadState_;
};
/**
* Handles the completion of an XhrIo request. Dispatches to success or load
* based on the result.
* @param {!goog.events.Event} e The XhrIo event object.
* @private
*/
goog.ds.XmlHttpDataSource.prototype.complete_ = function(e) {
var xhr = /** @type {goog.net.XhrIo} */ (e.target);
if (xhr && xhr.isSuccess()) {
this.success_(xhr);
} else {
this.failure_();
}
};
/**
* Success result. Checks whether valid XML was returned
* and sets the XML and loadstate.
*
* @param {!goog.net.XhrIo} xhr The successful XhrIo object.
* @private
*/
goog.ds.XmlHttpDataSource.prototype.success_ = function(xhr) {
goog.log.info(
goog.ds.logger, 'Got data for DataSource ' + this.getDataName());
var xml = xhr.getResponseXml();
// Fix for case where IE returns valid XML as text but
// doesn't parse by default
if (xml && !xml.hasChildNodes() && goog.isObject(xhr.getResponseText())) {
xml = goog.dom.xml.loadXml(xhr.getResponseText());
}
// Failure result
if (!xml || !xml.hasChildNodes()) {
this.loadState_ = goog.ds.LoadState.FAILED;
this.node_ = goog.ds.XmlDataSource.createChildlessDocument_();
} else {
this.loadState_ = goog.ds.LoadState.LOADED;
this.node_ = xml.documentElement;
}
if (this.getDataName()) {
goog.ds.DataManager.getInstance().fireDataChange(this.getDataName());
}
};
/**
* Failure result
*
* @private
*/
goog.ds.XmlHttpDataSource.prototype.failure_ = function() {
goog.log.info(
goog.ds.logger,
'Data retrieve failed for DataSource ' + this.getDataName());
this.loadState_ = goog.ds.LoadState.FAILED;
this.node_ = goog.ds.XmlDataSource.createChildlessDocument_();
if (this.getDataName()) {
goog.ds.DataManager.getInstance().fireDataChange(this.getDataName());
}
};
|