/src/main/resources/org/apache/struts2/static/datasource/datasource-debug.js

http://struts2yuiplugin.googlecode.com/ · JavaScript · 3017 lines · 2092 code · 150 blank · 775 comment · 214 complexity · 5980c6ed5e3c258b42dd91a9e11a0d84 MD5 · raw file

Large files are truncated click here to view the full file

  1. /*
  2. Copyright (c) 2009, Yahoo! Inc. All rights reserved.
  3. Code licensed under the BSD License:
  4. http://developer.yahoo.net/yui/license.txt
  5. version: 2.7.0
  6. */
  7. (function () {
  8. var lang = YAHOO.lang,
  9. util = YAHOO.util,
  10. Ev = util.Event;
  11. /**
  12. * The DataSource utility provides a common configurable interface for widgets to
  13. * access a variety of data, from JavaScript arrays to online database servers.
  14. *
  15. * @module datasource
  16. * @requires yahoo, event
  17. * @optional json, get, connection
  18. * @title DataSource Utility
  19. */
  20. /****************************************************************************/
  21. /****************************************************************************/
  22. /****************************************************************************/
  23. /**
  24. * Base class for the YUI DataSource utility.
  25. *
  26. * @namespace YAHOO.util
  27. * @class YAHOO.util.DataSourceBase
  28. * @constructor
  29. * @param oLiveData {HTMLElement} Pointer to live data.
  30. * @param oConfigs {object} (optional) Object literal of configuration values.
  31. */
  32. util.DataSourceBase = function(oLiveData, oConfigs) {
  33. if(oLiveData === null || oLiveData === undefined) {
  34. YAHOO.log("Could not instantiate DataSource due to invalid live database",
  35. "error", this.toString());
  36. return;
  37. }
  38. this.liveData = oLiveData;
  39. this._oQueue = {interval:null, conn:null, requests:[]};
  40. this.responseSchema = {};
  41. // Set any config params passed in to override defaults
  42. if(oConfigs && (oConfigs.constructor == Object)) {
  43. for(var sConfig in oConfigs) {
  44. if(sConfig) {
  45. this[sConfig] = oConfigs[sConfig];
  46. }
  47. }
  48. }
  49. // Validate and initialize public configs
  50. var maxCacheEntries = this.maxCacheEntries;
  51. if(!lang.isNumber(maxCacheEntries) || (maxCacheEntries < 0)) {
  52. maxCacheEntries = 0;
  53. }
  54. // Initialize interval tracker
  55. this._aIntervals = [];
  56. /////////////////////////////////////////////////////////////////////////////
  57. //
  58. // Custom Events
  59. //
  60. /////////////////////////////////////////////////////////////////////////////
  61. /**
  62. * Fired when a request is made to the local cache.
  63. *
  64. * @event cacheRequestEvent
  65. * @param oArgs.request {Object} The request object.
  66. * @param oArgs.callback {Object} The callback object.
  67. * @param oArgs.caller {Object} (deprecated) Use callback.scope.
  68. */
  69. this.createEvent("cacheRequestEvent");
  70. /**
  71. * Fired when data is retrieved from the local cache.
  72. *
  73. * @event cacheResponseEvent
  74. * @param oArgs.request {Object} The request object.
  75. * @param oArgs.response {Object} The response object.
  76. * @param oArgs.callback {Object} The callback object.
  77. * @param oArgs.caller {Object} (deprecated) Use callback.scope.
  78. */
  79. this.createEvent("cacheResponseEvent");
  80. /**
  81. * Fired when a request is sent to the live data source.
  82. *
  83. * @event requestEvent
  84. * @param oArgs.request {Object} The request object.
  85. * @param oArgs.callback {Object} The callback object.
  86. * @param oArgs.tId {Number} Transaction ID.
  87. * @param oArgs.caller {Object} (deprecated) Use callback.scope.
  88. */
  89. this.createEvent("requestEvent");
  90. /**
  91. * Fired when live data source sends response.
  92. *
  93. * @event responseEvent
  94. * @param oArgs.request {Object} The request object.
  95. * @param oArgs.response {Object} The raw response object.
  96. * @param oArgs.callback {Object} The callback object.
  97. * @param oArgs.tId {Number} Transaction ID.
  98. * @param oArgs.caller {Object} (deprecated) Use callback.scope.
  99. */
  100. this.createEvent("responseEvent");
  101. /**
  102. * Fired when response is parsed.
  103. *
  104. * @event responseParseEvent
  105. * @param oArgs.request {Object} The request object.
  106. * @param oArgs.response {Object} The parsed response object.
  107. * @param oArgs.callback {Object} The callback object.
  108. * @param oArgs.caller {Object} (deprecated) Use callback.scope.
  109. */
  110. this.createEvent("responseParseEvent");
  111. /**
  112. * Fired when response is cached.
  113. *
  114. * @event responseCacheEvent
  115. * @param oArgs.request {Object} The request object.
  116. * @param oArgs.response {Object} The parsed response object.
  117. * @param oArgs.callback {Object} The callback object.
  118. * @param oArgs.caller {Object} (deprecated) Use callback.scope.
  119. */
  120. this.createEvent("responseCacheEvent");
  121. /**
  122. * Fired when an error is encountered with the live data source.
  123. *
  124. * @event dataErrorEvent
  125. * @param oArgs.request {Object} The request object.
  126. * @param oArgs.callback {Object} The callback object.
  127. * @param oArgs.caller {Object} (deprecated) Use callback.scope.
  128. * @param oArgs.message {String} The error message.
  129. */
  130. this.createEvent("dataErrorEvent");
  131. /**
  132. * Fired when the local cache is flushed.
  133. *
  134. * @event cacheFlushEvent
  135. */
  136. this.createEvent("cacheFlushEvent");
  137. var DS = util.DataSourceBase;
  138. this._sName = "DataSource instance" + DS._nIndex;
  139. DS._nIndex++;
  140. YAHOO.log("DataSource initialized", "info", this.toString());
  141. };
  142. var DS = util.DataSourceBase;
  143. lang.augmentObject(DS, {
  144. /////////////////////////////////////////////////////////////////////////////
  145. //
  146. // DataSourceBase public constants
  147. //
  148. /////////////////////////////////////////////////////////////////////////////
  149. /**
  150. * Type is unknown.
  151. *
  152. * @property TYPE_UNKNOWN
  153. * @type Number
  154. * @final
  155. * @default -1
  156. */
  157. TYPE_UNKNOWN : -1,
  158. /**
  159. * Type is a JavaScript Array.
  160. *
  161. * @property TYPE_JSARRAY
  162. * @type Number
  163. * @final
  164. * @default 0
  165. */
  166. TYPE_JSARRAY : 0,
  167. /**
  168. * Type is a JavaScript Function.
  169. *
  170. * @property TYPE_JSFUNCTION
  171. * @type Number
  172. * @final
  173. * @default 1
  174. */
  175. TYPE_JSFUNCTION : 1,
  176. /**
  177. * Type is hosted on a server via an XHR connection.
  178. *
  179. * @property TYPE_XHR
  180. * @type Number
  181. * @final
  182. * @default 2
  183. */
  184. TYPE_XHR : 2,
  185. /**
  186. * Type is JSON.
  187. *
  188. * @property TYPE_JSON
  189. * @type Number
  190. * @final
  191. * @default 3
  192. */
  193. TYPE_JSON : 3,
  194. /**
  195. * Type is XML.
  196. *
  197. * @property TYPE_XML
  198. * @type Number
  199. * @final
  200. * @default 4
  201. */
  202. TYPE_XML : 4,
  203. /**
  204. * Type is plain text.
  205. *
  206. * @property TYPE_TEXT
  207. * @type Number
  208. * @final
  209. * @default 5
  210. */
  211. TYPE_TEXT : 5,
  212. /**
  213. * Type is an HTML TABLE element. Data is parsed out of TR elements from all TBODY elements.
  214. *
  215. * @property TYPE_HTMLTABLE
  216. * @type Number
  217. * @final
  218. * @default 6
  219. */
  220. TYPE_HTMLTABLE : 6,
  221. /**
  222. * Type is hosted on a server via a dynamic script node.
  223. *
  224. * @property TYPE_SCRIPTNODE
  225. * @type Number
  226. * @final
  227. * @default 7
  228. */
  229. TYPE_SCRIPTNODE : 7,
  230. /**
  231. * Type is local.
  232. *
  233. * @property TYPE_LOCAL
  234. * @type Number
  235. * @final
  236. * @default 8
  237. */
  238. TYPE_LOCAL : 8,
  239. /**
  240. * Error message for invalid dataresponses.
  241. *
  242. * @property ERROR_DATAINVALID
  243. * @type String
  244. * @final
  245. * @default "Invalid data"
  246. */
  247. ERROR_DATAINVALID : "Invalid data",
  248. /**
  249. * Error message for null data responses.
  250. *
  251. * @property ERROR_DATANULL
  252. * @type String
  253. * @final
  254. * @default "Null data"
  255. */
  256. ERROR_DATANULL : "Null data",
  257. /////////////////////////////////////////////////////////////////////////////
  258. //
  259. // DataSourceBase private static properties
  260. //
  261. /////////////////////////////////////////////////////////////////////////////
  262. /**
  263. * Internal class variable to index multiple DataSource instances.
  264. *
  265. * @property DataSourceBase._nIndex
  266. * @type Number
  267. * @private
  268. * @static
  269. */
  270. _nIndex : 0,
  271. /**
  272. * Internal class variable to assign unique transaction IDs.
  273. *
  274. * @property DataSourceBase._nTransactionId
  275. * @type Number
  276. * @private
  277. * @static
  278. */
  279. _nTransactionId : 0,
  280. /////////////////////////////////////////////////////////////////////////////
  281. //
  282. // DataSourceBase public static methods
  283. //
  284. /////////////////////////////////////////////////////////////////////////////
  285. /**
  286. * Executes a configured callback. For object literal callbacks, the third
  287. * param determines whether to execute the success handler or failure handler.
  288. *
  289. * @method issueCallback
  290. * @param callback {Function|Object} the callback to execute
  291. * @param params {Array} params to be passed to the callback method
  292. * @param error {Boolean} whether an error occurred
  293. * @param scope {Object} the scope from which to execute the callback
  294. * (deprecated - use an object literal callback)
  295. * @static
  296. */
  297. issueCallback : function (callback,params,error,scope) {
  298. if (lang.isFunction(callback)) {
  299. callback.apply(scope, params);
  300. } else if (lang.isObject(callback)) {
  301. scope = callback.scope || scope || window;
  302. var callbackFunc = callback.success;
  303. if (error) {
  304. callbackFunc = callback.failure;
  305. }
  306. if (callbackFunc) {
  307. callbackFunc.apply(scope, params.concat([callback.argument]));
  308. }
  309. }
  310. },
  311. /**
  312. * Converts data to type String.
  313. *
  314. * @method DataSourceBase.parseString
  315. * @param oData {String | Number | Boolean | Date | Array | Object} Data to parse.
  316. * The special values null and undefined will return null.
  317. * @return {String} A string, or null.
  318. * @static
  319. */
  320. parseString : function(oData) {
  321. // Special case null and undefined
  322. if(!lang.isValue(oData)) {
  323. return null;
  324. }
  325. //Convert to string
  326. var string = oData + "";
  327. // Validate
  328. if(lang.isString(string)) {
  329. return string;
  330. }
  331. else {
  332. YAHOO.log("Could not convert data " + lang.dump(oData) + " to type String", "warn", this.toString());
  333. return null;
  334. }
  335. },
  336. /**
  337. * Converts data to type Number.
  338. *
  339. * @method DataSourceBase.parseNumber
  340. * @param oData {String | Number | Boolean} Data to convert. Note, the following
  341. * values return as null: null, undefined, NaN, "".
  342. * @return {Number} A number, or null.
  343. * @static
  344. */
  345. parseNumber : function(oData) {
  346. if(!lang.isValue(oData) || (oData === "")) {
  347. return null;
  348. }
  349. //Convert to number
  350. var number = oData * 1;
  351. // Validate
  352. if(lang.isNumber(number)) {
  353. return number;
  354. }
  355. else {
  356. YAHOO.log("Could not convert data " + lang.dump(oData) + " to type Number", "warn", this.toString());
  357. return null;
  358. }
  359. },
  360. // Backward compatibility
  361. convertNumber : function(oData) {
  362. YAHOO.log("The method YAHOO.util.DataSourceBase.convertNumber() has been" +
  363. " deprecated in favor of YAHOO.util.DataSourceBase.parseNumber()", "warn",
  364. this.toString());
  365. return DS.parseNumber(oData);
  366. },
  367. /**
  368. * Converts data to type Date.
  369. *
  370. * @method DataSourceBase.parseDate
  371. * @param oData {Date | String | Number} Data to convert.
  372. * @return {Date} A Date instance.
  373. * @static
  374. */
  375. parseDate : function(oData) {
  376. var date = null;
  377. //Convert to date
  378. if(!(oData instanceof Date)) {
  379. date = new Date(oData);
  380. }
  381. else {
  382. return oData;
  383. }
  384. // Validate
  385. if(date instanceof Date) {
  386. return date;
  387. }
  388. else {
  389. YAHOO.log("Could not convert data " + lang.dump(oData) + " to type Date", "warn", this.toString());
  390. return null;
  391. }
  392. },
  393. // Backward compatibility
  394. convertDate : function(oData) {
  395. YAHOO.log("The method YAHOO.util.DataSourceBase.convertDate() has been" +
  396. " deprecated in favor of YAHOO.util.DataSourceBase.parseDate()", "warn",
  397. this.toString());
  398. return DS.parseDate(oData);
  399. }
  400. });
  401. // Done in separate step so referenced functions are defined.
  402. /**
  403. * Data parsing functions.
  404. * @property DataSource.Parser
  405. * @type Object
  406. * @static
  407. */
  408. DS.Parser = {
  409. string : DS.parseString,
  410. number : DS.parseNumber,
  411. date : DS.parseDate
  412. };
  413. // Prototype properties and methods
  414. DS.prototype = {
  415. /////////////////////////////////////////////////////////////////////////////
  416. //
  417. // DataSourceBase private properties
  418. //
  419. /////////////////////////////////////////////////////////////////////////////
  420. /**
  421. * Name of DataSource instance.
  422. *
  423. * @property _sName
  424. * @type String
  425. * @private
  426. */
  427. _sName : null,
  428. /**
  429. * Local cache of data result object literals indexed chronologically.
  430. *
  431. * @property _aCache
  432. * @type Object[]
  433. * @private
  434. */
  435. _aCache : null,
  436. /**
  437. * Local queue of request connections, enabled if queue needs to be managed.
  438. *
  439. * @property _oQueue
  440. * @type Object
  441. * @private
  442. */
  443. _oQueue : null,
  444. /**
  445. * Array of polling interval IDs that have been enabled, needed to clear all intervals.
  446. *
  447. * @property _aIntervals
  448. * @type Array
  449. * @private
  450. */
  451. _aIntervals : null,
  452. /////////////////////////////////////////////////////////////////////////////
  453. //
  454. // DataSourceBase public properties
  455. //
  456. /////////////////////////////////////////////////////////////////////////////
  457. /**
  458. * Max size of the local cache. Set to 0 to turn off caching. Caching is
  459. * useful to reduce the number of server connections. Recommended only for data
  460. * sources that return comprehensive results for queries or when stale data is
  461. * not an issue.
  462. *
  463. * @property maxCacheEntries
  464. * @type Number
  465. * @default 0
  466. */
  467. maxCacheEntries : 0,
  468. /**
  469. * Pointer to live database.
  470. *
  471. * @property liveData
  472. * @type Object
  473. */
  474. liveData : null,
  475. /**
  476. * Where the live data is held:
  477. *
  478. * <dl>
  479. * <dt>TYPE_UNKNOWN</dt>
  480. * <dt>TYPE_LOCAL</dt>
  481. * <dt>TYPE_XHR</dt>
  482. * <dt>TYPE_SCRIPTNODE</dt>
  483. * <dt>TYPE_JSFUNCTION</dt>
  484. * </dl>
  485. *
  486. * @property dataType
  487. * @type Number
  488. * @default YAHOO.util.DataSourceBase.TYPE_UNKNOWN
  489. *
  490. */
  491. dataType : DS.TYPE_UNKNOWN,
  492. /**
  493. * Format of response:
  494. *
  495. * <dl>
  496. * <dt>TYPE_UNKNOWN</dt>
  497. * <dt>TYPE_JSARRAY</dt>
  498. * <dt>TYPE_JSON</dt>
  499. * <dt>TYPE_XML</dt>
  500. * <dt>TYPE_TEXT</dt>
  501. * <dt>TYPE_HTMLTABLE</dt>
  502. * </dl>
  503. *
  504. * @property responseType
  505. * @type Number
  506. * @default YAHOO.util.DataSourceBase.TYPE_UNKNOWN
  507. */
  508. responseType : DS.TYPE_UNKNOWN,
  509. /**
  510. * Response schema object literal takes a combination of the following properties:
  511. *
  512. * <dl>
  513. * <dt>resultsList</dt> <dd>Pointer to array of tabular data</dd>
  514. * <dt>resultNode</dt> <dd>Pointer to node name of row data (XML data only)</dd>
  515. * <dt>recordDelim</dt> <dd>Record delimiter (text data only)</dd>
  516. * <dt>fieldDelim</dt> <dd>Field delimiter (text data only)</dd>
  517. * <dt>fields</dt> <dd>Array of field names (aka keys), or array of object literals
  518. * such as: {key:"fieldname",parser:YAHOO.util.DataSourceBase.parseDate}</dd>
  519. * <dt>metaFields</dt> <dd>Object literal of keys to include in the oParsedResponse.meta collection</dd>
  520. * <dt>metaNode</dt> <dd>Name of the node under which to search for meta information in XML response data</dd>
  521. * </dl>
  522. *
  523. * @property responseSchema
  524. * @type Object
  525. */
  526. responseSchema : null,
  527. /**
  528. * Additional arguments passed to the JSON parse routine. The JSON string
  529. * is the assumed first argument (where applicable). This property is not
  530. * set by default, but the parse methods will use it if present.
  531. *
  532. * @property parseJSONArgs
  533. * @type {MIXED|Array} If an Array, contents are used as individual arguments.
  534. * Otherwise, value is used as an additional argument.
  535. */
  536. // property intentionally undefined
  537. /////////////////////////////////////////////////////////////////////////////
  538. //
  539. // DataSourceBase public methods
  540. //
  541. /////////////////////////////////////////////////////////////////////////////
  542. /**
  543. * Public accessor to the unique name of the DataSource instance.
  544. *
  545. * @method toString
  546. * @return {String} Unique name of the DataSource instance.
  547. */
  548. toString : function() {
  549. return this._sName;
  550. },
  551. /**
  552. * Overridable method passes request to cache and returns cached response if any,
  553. * refreshing the hit in the cache as the newest item. Returns null if there is
  554. * no cache hit.
  555. *
  556. * @method getCachedResponse
  557. * @param oRequest {Object} Request object.
  558. * @param oCallback {Object} Callback object.
  559. * @param oCaller {Object} (deprecated) Use callback object.
  560. * @return {Object} Cached response object or null.
  561. */
  562. getCachedResponse : function(oRequest, oCallback, oCaller) {
  563. var aCache = this._aCache;
  564. // If cache is enabled...
  565. if(this.maxCacheEntries > 0) {
  566. // Initialize local cache
  567. if(!aCache) {
  568. this._aCache = [];
  569. YAHOO.log("Cache initialized", "info", this.toString());
  570. }
  571. // Look in local cache
  572. else {
  573. var nCacheLength = aCache.length;
  574. if(nCacheLength > 0) {
  575. var oResponse = null;
  576. this.fireEvent("cacheRequestEvent", {request:oRequest,callback:oCallback,caller:oCaller});
  577. // Loop through each cached element
  578. for(var i = nCacheLength-1; i >= 0; i--) {
  579. var oCacheElem = aCache[i];
  580. // Defer cache hit logic to a public overridable method
  581. if(this.isCacheHit(oRequest,oCacheElem.request)) {
  582. // The cache returned a hit!
  583. // Grab the cached response
  584. oResponse = oCacheElem.response;
  585. this.fireEvent("cacheResponseEvent", {request:oRequest,response:oResponse,callback:oCallback,caller:oCaller});
  586. // Refresh the position of the cache hit
  587. if(i < nCacheLength-1) {
  588. // Remove element from its original location
  589. aCache.splice(i,1);
  590. // Add as newest
  591. this.addToCache(oRequest, oResponse);
  592. YAHOO.log("Refreshed cache position of the response for \"" + oRequest + "\"", "info", this.toString());
  593. }
  594. // Add a cache flag
  595. oResponse.cached = true;
  596. break;
  597. }
  598. }
  599. YAHOO.log("The cached response for \"" + lang.dump(oRequest) +
  600. "\" is " + lang.dump(oResponse), "info", this.toString());
  601. return oResponse;
  602. }
  603. }
  604. }
  605. else if(aCache) {
  606. this._aCache = null;
  607. YAHOO.log("Cache destroyed", "info", this.toString());
  608. }
  609. return null;
  610. },
  611. /**
  612. * Default overridable method matches given request to given cached request.
  613. * Returns true if is a hit, returns false otherwise. Implementers should
  614. * override this method to customize the cache-matching algorithm.
  615. *
  616. * @method isCacheHit
  617. * @param oRequest {Object} Request object.
  618. * @param oCachedRequest {Object} Cached request object.
  619. * @return {Boolean} True if given request matches cached request, false otherwise.
  620. */
  621. isCacheHit : function(oRequest, oCachedRequest) {
  622. return (oRequest === oCachedRequest);
  623. },
  624. /**
  625. * Adds a new item to the cache. If cache is full, evicts the stalest item
  626. * before adding the new item.
  627. *
  628. * @method addToCache
  629. * @param oRequest {Object} Request object.
  630. * @param oResponse {Object} Response object to cache.
  631. */
  632. addToCache : function(oRequest, oResponse) {
  633. var aCache = this._aCache;
  634. if(!aCache) {
  635. return;
  636. }
  637. // If the cache is full, make room by removing stalest element (index=0)
  638. while(aCache.length >= this.maxCacheEntries) {
  639. aCache.shift();
  640. }
  641. // Add to cache in the newest position, at the end of the array
  642. var oCacheElem = {request:oRequest,response:oResponse};
  643. aCache[aCache.length] = oCacheElem;
  644. this.fireEvent("responseCacheEvent", {request:oRequest,response:oResponse});
  645. YAHOO.log("Cached the response for \"" + oRequest + "\"", "info", this.toString());
  646. },
  647. /**
  648. * Flushes cache.
  649. *
  650. * @method flushCache
  651. */
  652. flushCache : function() {
  653. if(this._aCache) {
  654. this._aCache = [];
  655. this.fireEvent("cacheFlushEvent");
  656. YAHOO.log("Flushed the cache", "info", this.toString());
  657. }
  658. },
  659. /**
  660. * Sets up a polling mechanism to send requests at set intervals and forward
  661. * responses to given callback.
  662. *
  663. * @method setInterval
  664. * @param nMsec {Number} Length of interval in milliseconds.
  665. * @param oRequest {Object} Request object.
  666. * @param oCallback {Function} Handler function to receive the response.
  667. * @param oCaller {Object} (deprecated) Use oCallback.scope.
  668. * @return {Number} Interval ID.
  669. */
  670. setInterval : function(nMsec, oRequest, oCallback, oCaller) {
  671. if(lang.isNumber(nMsec) && (nMsec >= 0)) {
  672. YAHOO.log("Enabling polling to live data for \"" + oRequest + "\" at interval " + nMsec, "info", this.toString());
  673. var oSelf = this;
  674. var nId = setInterval(function() {
  675. oSelf.makeConnection(oRequest, oCallback, oCaller);
  676. }, nMsec);
  677. this._aIntervals.push(nId);
  678. return nId;
  679. }
  680. else {
  681. YAHOO.log("Could not enable polling to live data for \"" + oRequest + "\" at interval " + nMsec, "info", this.toString());
  682. }
  683. },
  684. /**
  685. * Disables polling mechanism associated with the given interval ID.
  686. *
  687. * @method clearInterval
  688. * @param nId {Number} Interval ID.
  689. */
  690. clearInterval : function(nId) {
  691. // Remove from tracker if there
  692. var tracker = this._aIntervals || [];
  693. for(var i=tracker.length-1; i>-1; i--) {
  694. if(tracker[i] === nId) {
  695. tracker.splice(i,1);
  696. clearInterval(nId);
  697. }
  698. }
  699. },
  700. /**
  701. * Disables all known polling intervals.
  702. *
  703. * @method clearAllIntervals
  704. */
  705. clearAllIntervals : function() {
  706. var tracker = this._aIntervals || [];
  707. for(var i=tracker.length-1; i>-1; i--) {
  708. clearInterval(tracker[i]);
  709. }
  710. tracker = [];
  711. },
  712. /**
  713. * First looks for cached response, then sends request to live data. The
  714. * following arguments are passed to the callback function:
  715. * <dl>
  716. * <dt><code>oRequest</code></dt>
  717. * <dd>The same value that was passed in as the first argument to sendRequest.</dd>
  718. * <dt><code>oParsedResponse</code></dt>
  719. * <dd>An object literal containing the following properties:
  720. * <dl>
  721. * <dt><code>tId</code></dt>
  722. * <dd>Unique transaction ID number.</dd>
  723. * <dt><code>results</code></dt>
  724. * <dd>Schema-parsed data results.</dd>
  725. * <dt><code>error</code></dt>
  726. * <dd>True in cases of data error.</dd>
  727. * <dt><code>cached</code></dt>
  728. * <dd>True when response is returned from DataSource cache.</dd>
  729. * <dt><code>meta</code></dt>
  730. * <dd>Schema-parsed meta data.</dd>
  731. * </dl>
  732. * <dt><code>oPayload</code></dt>
  733. * <dd>The same value as was passed in as <code>argument</code> in the oCallback object literal.</dd>
  734. * </dl>
  735. *
  736. * @method sendRequest
  737. * @param oRequest {Object} Request object.
  738. * @param oCallback {Object} An object literal with the following properties:
  739. * <dl>
  740. * <dt><code>success</code></dt>
  741. * <dd>The function to call when the data is ready.</dd>
  742. * <dt><code>failure</code></dt>
  743. * <dd>The function to call upon a response failure condition.</dd>
  744. * <dt><code>scope</code></dt>
  745. * <dd>The object to serve as the scope for the success and failure handlers.</dd>
  746. * <dt><code>argument</code></dt>
  747. * <dd>Arbitrary data that will be passed back to the success and failure handlers.</dd>
  748. * </dl>
  749. * @param oCaller {Object} (deprecated) Use oCallback.scope.
  750. * @return {Number} Transaction ID, or null if response found in cache.
  751. */
  752. sendRequest : function(oRequest, oCallback, oCaller) {
  753. // First look in cache
  754. var oCachedResponse = this.getCachedResponse(oRequest, oCallback, oCaller);
  755. if(oCachedResponse) {
  756. DS.issueCallback(oCallback,[oRequest,oCachedResponse],false,oCaller);
  757. return null;
  758. }
  759. // Not in cache, so forward request to live data
  760. YAHOO.log("Making connection to live data for \"" + oRequest + "\"", "info", this.toString());
  761. return this.makeConnection(oRequest, oCallback, oCaller);
  762. },
  763. /**
  764. * Overridable default method generates a unique transaction ID and passes
  765. * the live data reference directly to the handleResponse function. This
  766. * method should be implemented by subclasses to achieve more complex behavior
  767. * or to access remote data.
  768. *
  769. * @method makeConnection
  770. * @param oRequest {Object} Request object.
  771. * @param oCallback {Object} Callback object literal.
  772. * @param oCaller {Object} (deprecated) Use oCallback.scope.
  773. * @return {Number} Transaction ID.
  774. */
  775. makeConnection : function(oRequest, oCallback, oCaller) {
  776. var tId = DS._nTransactionId++;
  777. this.fireEvent("requestEvent", {tId:tId, request:oRequest,callback:oCallback,caller:oCaller});
  778. /* accounts for the following cases:
  779. YAHOO.util.DataSourceBase.TYPE_UNKNOWN
  780. YAHOO.util.DataSourceBase.TYPE_JSARRAY
  781. YAHOO.util.DataSourceBase.TYPE_JSON
  782. YAHOO.util.DataSourceBase.TYPE_HTMLTABLE
  783. YAHOO.util.DataSourceBase.TYPE_XML
  784. YAHOO.util.DataSourceBase.TYPE_TEXT
  785. */
  786. var oRawResponse = this.liveData;
  787. this.handleResponse(oRequest, oRawResponse, oCallback, oCaller, tId);
  788. return tId;
  789. },
  790. /**
  791. * Receives raw data response and type converts to XML, JSON, etc as necessary.
  792. * Forwards oFullResponse to appropriate parsing function to get turned into
  793. * oParsedResponse. Calls doBeforeCallback() and adds oParsedResponse to
  794. * the cache when appropriate before calling issueCallback().
  795. *
  796. * The oParsedResponse object literal has the following properties:
  797. * <dl>
  798. * <dd><dt>tId {Number}</dt> Unique transaction ID</dd>
  799. * <dd><dt>results {Array}</dt> Array of parsed data results</dd>
  800. * <dd><dt>meta {Object}</dt> Object literal of meta values</dd>
  801. * <dd><dt>error {Boolean}</dt> (optional) True if there was an error</dd>
  802. * <dd><dt>cached {Boolean}</dt> (optional) True if response was cached</dd>
  803. * </dl>
  804. *
  805. * @method handleResponse
  806. * @param oRequest {Object} Request object
  807. * @param oRawResponse {Object} The raw response from the live database.
  808. * @param oCallback {Object} Callback object literal.
  809. * @param oCaller {Object} (deprecated) Use oCallback.scope.
  810. * @param tId {Number} Transaction ID.
  811. */
  812. handleResponse : function(oRequest, oRawResponse, oCallback, oCaller, tId) {
  813. this.fireEvent("responseEvent", {tId:tId, request:oRequest, response:oRawResponse,
  814. callback:oCallback, caller:oCaller});
  815. YAHOO.log("Received live data response for \"" + oRequest + "\"", "info", this.toString());
  816. var xhr = (this.dataType == DS.TYPE_XHR) ? true : false;
  817. var oParsedResponse = null;
  818. var oFullResponse = oRawResponse;
  819. // Try to sniff data type if it has not been defined
  820. if(this.responseType === DS.TYPE_UNKNOWN) {
  821. var ctype = (oRawResponse && oRawResponse.getResponseHeader) ? oRawResponse.getResponseHeader["Content-Type"] : null;
  822. if(ctype) {
  823. // xml
  824. if(ctype.indexOf("text/xml") > -1) {
  825. this.responseType = DS.TYPE_XML;
  826. }
  827. else if(ctype.indexOf("application/json") > -1) { // json
  828. this.responseType = DS.TYPE_JSON;
  829. }
  830. else if(ctype.indexOf("text/plain") > -1) { // text
  831. this.responseType = DS.TYPE_TEXT;
  832. }
  833. }
  834. else {
  835. if(YAHOO.lang.isArray(oRawResponse)) { // array
  836. this.responseType = DS.TYPE_JSARRAY;
  837. }
  838. // xml
  839. else if(oRawResponse && oRawResponse.nodeType && oRawResponse.nodeType == 9) {
  840. this.responseType = DS.TYPE_XML;
  841. }
  842. else if(oRawResponse && oRawResponse.nodeName && (oRawResponse.nodeName.toLowerCase() == "table")) { // table
  843. this.responseType = DS.TYPE_HTMLTABLE;
  844. }
  845. else if(YAHOO.lang.isObject(oRawResponse)) { // json
  846. this.responseType = DS.TYPE_JSON;
  847. }
  848. else if(YAHOO.lang.isString(oRawResponse)) { // text
  849. this.responseType = DS.TYPE_TEXT;
  850. }
  851. }
  852. }
  853. switch(this.responseType) {
  854. case DS.TYPE_JSARRAY:
  855. if(xhr && oRawResponse && oRawResponse.responseText) {
  856. oFullResponse = oRawResponse.responseText;
  857. }
  858. try {
  859. // Convert to JS array if it's a string
  860. if(lang.isString(oFullResponse)) {
  861. var parseArgs = [oFullResponse].concat(this.parseJSONArgs);
  862. // Check for YUI JSON Util
  863. if(lang.JSON) {
  864. oFullResponse = lang.JSON.parse.apply(lang.JSON,parseArgs);
  865. }
  866. // Look for JSON parsers using an API similar to json2.js
  867. else if(window.JSON && JSON.parse) {
  868. oFullResponse = JSON.parse.apply(JSON,parseArgs);
  869. }
  870. // Look for JSON parsers using an API similar to json.js
  871. else if(oFullResponse.parseJSON) {
  872. oFullResponse = oFullResponse.parseJSON.apply(oFullResponse,parseArgs.slice(1));
  873. }
  874. // No JSON lib found so parse the string
  875. else {
  876. // Trim leading spaces
  877. while (oFullResponse.length > 0 &&
  878. (oFullResponse.charAt(0) != "{") &&
  879. (oFullResponse.charAt(0) != "[")) {
  880. oFullResponse = oFullResponse.substring(1, oFullResponse.length);
  881. }
  882. if(oFullResponse.length > 0) {
  883. // Strip extraneous stuff at the end
  884. var arrayEnd =
  885. Math.max(oFullResponse.lastIndexOf("]"),oFullResponse.lastIndexOf("}"));
  886. oFullResponse = oFullResponse.substring(0,arrayEnd+1);
  887. // Turn the string into an object literal...
  888. // ...eval is necessary here
  889. oFullResponse = eval("(" + oFullResponse + ")");
  890. }
  891. }
  892. }
  893. }
  894. catch(e1) {
  895. }
  896. oFullResponse = this.doBeforeParseData(oRequest, oFullResponse, oCallback);
  897. oParsedResponse = this.parseArrayData(oRequest, oFullResponse);
  898. break;
  899. case DS.TYPE_JSON:
  900. if(xhr && oRawResponse && oRawResponse.responseText) {
  901. oFullResponse = oRawResponse.responseText;
  902. }
  903. try {
  904. // Convert to JSON object if it's a string
  905. if(lang.isString(oFullResponse)) {
  906. var parseArgs = [oFullResponse].concat(this.parseJSONArgs);
  907. // Check for YUI JSON Util
  908. if(lang.JSON) {
  909. oFullResponse = lang.JSON.parse.apply(lang.JSON,parseArgs);
  910. }
  911. // Look for JSON parsers using an API similar to json2.js
  912. else if(window.JSON && JSON.parse) {
  913. oFullResponse = JSON.parse.apply(JSON,parseArgs);
  914. }
  915. // Look for JSON parsers using an API similar to json.js
  916. else if(oFullResponse.parseJSON) {
  917. oFullResponse = oFullResponse.parseJSON.apply(oFullResponse,parseArgs.slice(1));
  918. }
  919. // No JSON lib found so parse the string
  920. else {
  921. // Trim leading spaces
  922. while (oFullResponse.length > 0 &&
  923. (oFullResponse.charAt(0) != "{") &&
  924. (oFullResponse.charAt(0) != "[")) {
  925. oFullResponse = oFullResponse.substring(1, oFullResponse.length);
  926. }
  927. if(oFullResponse.length > 0) {
  928. // Strip extraneous stuff at the end
  929. var objEnd = Math.max(oFullResponse.lastIndexOf("]"),oFullResponse.lastIndexOf("}"));
  930. oFullResponse = oFullResponse.substring(0,objEnd+1);
  931. // Turn the string into an object literal...
  932. // ...eval is necessary here
  933. oFullResponse = eval("(" + oFullResponse + ")");
  934. }
  935. }
  936. }
  937. }
  938. catch(e) {
  939. }
  940. oFullResponse = this.doBeforeParseData(oRequest, oFullResponse, oCallback);
  941. oParsedResponse = this.parseJSONData(oRequest, oFullResponse);
  942. break;
  943. case DS.TYPE_HTMLTABLE:
  944. if(xhr && oRawResponse.responseText) {
  945. var el = document.createElement('div');
  946. el.innerHTML = oRawResponse.responseText;
  947. oFullResponse = el.getElementsByTagName('table')[0];
  948. }
  949. oFullResponse = this.doBeforeParseData(oRequest, oFullResponse, oCallback);
  950. oParsedResponse = this.parseHTMLTableData(oRequest, oFullResponse);
  951. break;
  952. case DS.TYPE_XML:
  953. if(xhr && oRawResponse.responseXML) {
  954. oFullResponse = oRawResponse.responseXML;
  955. }
  956. oFullResponse = this.doBeforeParseData(oRequest, oFullResponse, oCallback);
  957. oParsedResponse = this.parseXMLData(oRequest, oFullResponse);
  958. break;
  959. case DS.TYPE_TEXT:
  960. if(xhr && lang.isString(oRawResponse.responseText)) {
  961. oFullResponse = oRawResponse.responseText;
  962. }
  963. oFullResponse = this.doBeforeParseData(oRequest, oFullResponse, oCallback);
  964. oParsedResponse = this.parseTextData(oRequest, oFullResponse);
  965. break;
  966. default:
  967. oFullResponse = this.doBeforeParseData(oRequest, oFullResponse, oCallback);
  968. oParsedResponse = this.parseData(oRequest, oFullResponse);
  969. break;
  970. }
  971. // Clean up for consistent signature
  972. oParsedResponse = oParsedResponse || {};
  973. if(!oParsedResponse.results) {
  974. oParsedResponse.results = [];
  975. }
  976. if(!oParsedResponse.meta) {
  977. oParsedResponse.meta = {};
  978. }
  979. // Success
  980. if(oParsedResponse && !oParsedResponse.error) {
  981. // Last chance to touch the raw response or the parsed response
  982. oParsedResponse = this.doBeforeCallback(oRequest, oFullResponse, oParsedResponse, oCallback);
  983. this.fireEvent("responseParseEvent", {request:oRequest,
  984. response:oParsedResponse, callback:oCallback, caller:oCaller});
  985. // Cache the response
  986. this.addToCache(oRequest, oParsedResponse);
  987. }
  988. // Error
  989. else {
  990. // Be sure the error flag is on
  991. oParsedResponse.error = true;
  992. this.fireEvent("dataErrorEvent", {request:oRequest, response: oRawResponse, callback:oCallback,
  993. caller:oCaller, message:DS.ERROR_DATANULL});
  994. YAHOO.log(DS.ERROR_DATANULL, "error", this.toString());
  995. }
  996. // Send the response back to the caller
  997. oParsedResponse.tId = tId;
  998. DS.issueCallback(oCallback,[oRequest,oParsedResponse],oParsedResponse.error,oCaller);
  999. },
  1000. /**
  1001. * Overridable method gives implementers access to the original full response
  1002. * before the data gets parsed. Implementers should take care not to return an
  1003. * unparsable or otherwise invalid response.
  1004. *
  1005. * @method doBeforeParseData
  1006. * @param oRequest {Object} Request object.
  1007. * @param oFullResponse {Object} The full response from the live database.
  1008. * @param oCallback {Object} The callback object.
  1009. * @return {Object} Full response for parsing.
  1010. */
  1011. doBeforeParseData : function(oRequest, oFullResponse, oCallback) {
  1012. return oFullResponse;
  1013. },
  1014. /**
  1015. * Overridable method gives implementers access to the original full response and
  1016. * the parsed response (parsed against the given schema) before the data
  1017. * is added to the cache (if applicable) and then sent back to callback function.
  1018. * This is your chance to access the raw response and/or populate the parsed
  1019. * response with any custom data.
  1020. *
  1021. * @method doBeforeCallback
  1022. * @param oRequest {Object} Request object.
  1023. * @param oFullResponse {Object} The full response from the live database.
  1024. * @param oParsedResponse {Object} The parsed response to return to calling object.
  1025. * @param oCallback {Object} The callback object.
  1026. * @return {Object} Parsed response object.
  1027. */
  1028. doBeforeCallback : function(oRequest, oFullResponse, oParsedResponse, oCallback) {
  1029. return oParsedResponse;
  1030. },
  1031. /**
  1032. * Overridable method parses data of generic RESPONSE_TYPE into a response object.
  1033. *
  1034. * @method parseData
  1035. * @param oRequest {Object} Request object.
  1036. * @param oFullResponse {Object} The full Array from the live database.
  1037. * @return {Object} Parsed response object with the following properties:<br>
  1038. * - results {Array} Array of parsed data results<br>
  1039. * - meta {Object} Object literal of meta values<br>
  1040. * - error {Boolean} (optional) True if there was an error<br>
  1041. */
  1042. parseData : function(oRequest, oFullResponse) {
  1043. if(lang.isValue(oFullResponse)) {
  1044. var oParsedResponse = {results:oFullResponse,meta:{}};
  1045. YAHOO.log("Parsed generic data is " +
  1046. lang.dump(oParsedResponse), "info", this.toString());
  1047. return oParsedResponse;
  1048. }
  1049. YAHOO.log("Generic data could not be parsed: " + lang.dump(oFullResponse),
  1050. "error", this.toString());
  1051. return null;
  1052. },
  1053. /**
  1054. * Overridable method parses Array data into a response object.
  1055. *
  1056. * @method parseArrayData
  1057. * @param oRequest {Object} Request object.
  1058. * @param oFullResponse {Object} The full Array from the live database.
  1059. * @return {Object} Parsed response object with the following properties:<br>
  1060. * - results (Array) Array of parsed data results<br>
  1061. * - error (Boolean) True if there was an error
  1062. */
  1063. parseArrayData : function(oRequest, oFullResponse) {
  1064. if(lang.isArray(oFullResponse)) {
  1065. var results = [],
  1066. i, j,
  1067. rec, field, data;
  1068. // Parse for fields
  1069. if(lang.isArray(this.responseSchema.fields)) {
  1070. var fields = this.responseSchema.fields;
  1071. for (i = fields.length - 1; i >= 0; --i) {
  1072. if (typeof fields[i] !== 'object') {
  1073. fields[i] = { key : fields[i] };
  1074. }
  1075. }
  1076. var parsers = {}, p;
  1077. for (i = fields.length - 1; i >= 0; --i) {
  1078. p = (typeof fields[i].parser === 'function' ?
  1079. fields[i].parser :
  1080. DS.Parser[fields[i].parser+'']) || fields[i].converter;
  1081. if (p) {
  1082. parsers[fields[i].key] = p;
  1083. }
  1084. }
  1085. var arrType = lang.isArray(oFullResponse[0]);
  1086. for(i=oFullResponse.length-1; i>-1; i--) {
  1087. var oResult = {};
  1088. rec = oFullResponse[i];
  1089. if (typeof rec === 'object') {
  1090. for(j=fields.length-1; j>-1; j--) {
  1091. field = fields[j];
  1092. data = arrType ? rec[j] : rec[field.key];
  1093. if (parsers[field.key]) {
  1094. data = parsers[field.key].call(this,data);
  1095. }
  1096. // Safety measure
  1097. if(data === undefined) {
  1098. data = null;
  1099. }
  1100. oResult[field.key] = data;
  1101. }
  1102. }
  1103. else if (lang.isString(rec)) {
  1104. for(j=fields.length-1; j>-1; j--) {
  1105. field = fields[j];
  1106. data = rec;
  1107. if (parsers[field.key]) {
  1108. data = parsers[field.key].call(this,data);
  1109. }
  1110. // Safety measure
  1111. if(data === undefined) {
  1112. data = null;
  1113. }
  1114. oResult[field.key] = data;
  1115. }
  1116. }
  1117. results[i] = oResult;
  1118. }
  1119. }
  1120. // Return entire data set
  1121. else {
  1122. results = oFullResponse;
  1123. }
  1124. var oParsedResponse = {results:results};
  1125. YAHOO.log("Parsed array data is " +
  1126. lang.dump(oParsedResponse), "info", this.toString());
  1127. return oParsedResponse;
  1128. }
  1129. YAHOO.log("Array data could not be parsed: " + lang.dump(oFullResponse),
  1130. "error", this.toString());
  1131. return null;
  1132. },
  1133. /**
  1134. * Overridable method parses plain text data into a response object.
  1135. *
  1136. * @method parseTextData
  1137. * @param oRequest {Object} Request object.
  1138. * @param oFullResponse {Object} The full text response from the live database.
  1139. * @return {Object} Parsed response object with the following properties:<br>
  1140. * - results (Array) Array of parsed data results<br>
  1141. * - error (Boolean) True if there was an error
  1142. */
  1143. parseTextData : function(oRequest, oFullResponse) {
  1144. if(lang.isString(oFullResponse)) {
  1145. if(lang.isString(this.responseSchema.recordDelim) &&
  1146. lang.isString(this.responseSchema.fieldDelim)) {
  1147. var oParsedResponse = {results:[]};
  1148. var recDelim = this.responseSchema.recordDelim;
  1149. var fieldDelim = this.responseSchema.fieldDelim;
  1150. if(oFullResponse.length > 0) {
  1151. // Delete the last line delimiter at the end of the data if it exists
  1152. var newLength = oFullResponse.length-recDelim.length;
  1153. if(oFullResponse.substr(newLength) == recDelim) {
  1154. oFullResponse = oFullResponse.substr(0, newLength);
  1155. }
  1156. if(oFullResponse.length > 0) {
  1157. // Split along record delimiter to get an array of strings
  1158. var recordsarray = oFullResponse.split(recDelim);
  1159. // Cycle through each record
  1160. for(var i = 0, len = recordsarray.length, recIdx = 0; i < len; ++i) {
  1161. var bError = false,
  1162. sRecord = recordsarray[i];
  1163. if (lang.isString(sRecord) && (sRecord.length > 0)) {
  1164. // Split each record along field delimiter to get data
  1165. var fielddataarray = recordsarray[i].split(fieldDelim);
  1166. var oResult = {};
  1167. // Filter for fields data
  1168. if(lang.isArray(this.responseSchema.fields)) {
  1169. var fields = this.responseSchema.fields;
  1170. for(var j=fields.length-1; j>-1; j--) {
  1171. try {
  1172. // Remove quotation marks from edges, if applicable
  1173. var data = fielddataarray[j];
  1174. if (lang.isString(data)) {
  1175. if(data.charAt(0) == "\"") {
  1176. data = data.substr(1);
  1177. }
  1178. if(data.charAt(data.length-1) == "\"") {
  1179. data = data.substr(0,data.length-1);
  1180. }
  1181. var field = fields[j];
  1182. var key = (lang.isValue(field.key)) ? field.key : field;
  1183. // Backward compatibility
  1184. if(!field.parser && field.converter) {
  1185. field.parser = field.converter;
  1186. YAHOO.log("The field property converter has been deprecated" +
  1187. " in favor of parser", "warn", this.toString());
  1188. }
  1189. var parser = (typeof field.parser === 'function') ?
  1190. field.parser :
  1191. DS.Parser[field.parser+''];
  1192. if(parser) {
  1193. data = parser.call(this, data);
  1194. }
  1195. // Safety measure
  1196. if(data === undefined) {
  1197. data = null;
  1198. }
  1199. oResult[key] = data;
  1200. }
  1201. else {
  1202. bError = true;
  1203. }
  1204. }
  1205. catch(e) {
  1206. bError = true;
  1207. }
  1208. }
  1209. }
  1210. // No fields defined so pass along all data as an array
  1211. else {
  1212. oResult = fielddataarray;
  1213. }
  1214. if(!bError) {
  1215. oParsedResponse.results[recIdx++] = oResult;
  1216. }
  1217. }
  1218. }
  1219. }
  1220. }
  1221. YAHOO.log("Parsed text data is " +
  1222. lang.dump(oParsedResponse), "info", this.toString());
  1223. return oParsedResponse;
  1224. }
  1225. }
  1226. YAHOO.log("Text data could not be parsed: " + lang.dump(oFullResponse),
  1227. "error", this.toString());
  1228. return null;
  1229. },
  1230. /**
  1231. * Overridable method parses XML data for one result into an object literal.
  1232. *
  1233. * @method parseXMLResult
  1234. * @param result {XML} XML for one result.
  1235. * @return {Object} Object literal of data for one result.
  1236. */
  1237. parseXMLResult : function(result) {
  1238. var oResult = {},
  1239. schema = this.responseSchema;
  1240. try {
  1241. // Loop through each data field in each result using the schema
  1242. for(var m = schema.fields.length-1; m >= 0 ; m--) {
  1243. var field = schema.fields[m];
  1244. var key = (lang.isValue(field.key)) ? field.key : field;
  1245. var data = null;
  1246. // Values may be held in an attribute...
  1247. var xmlAttr = result.attributes.getNamedItem(key);
  1248. if(xmlAttr) {
  1249. data = xmlAttr.value;
  1250. }
  1251. // ...or in a node
  1252. else {
  1253. var xmlNode = result.getElementsByTagName(key);
  1254. if(xmlNode && xmlNode.item(0)) {
  1255. var item = xmlNode.item(0);
  1256. // For IE, then DOM...
  1257. data = (item) ? ((item.text) ? item.text : (item.textContent) ? item.textContent : null) : null;
  1258. // ...then fallback, but check for multiple child nodes
  1259. if(!data) {
  1260. var datapieces = [];
  1261. for(var j=0, len=item.childNodes.length; j<len; j++) {
  1262. if(item.childNodes[j].nodeValue) {
  1263. datapieces[datapieces.length] = item.childNodes[j].nodeValue;
  1264. }
  1265. }
  1266. if(datapieces.length > 0) {
  1267. data = datapieces.join("");
  1268. }
  1269. }
  1270. }
  1271. }
  1272. // Safety net
  1273. if(data === null) {
  1274. data = "";
  1275. }
  1276. // Backward compatibility
  1277. if(!field.parser && field.converter) {
  1278. field.parser = field.converter;
  1279. YAHOO.log("The field property converter has been deprecated" +
  1280. " in favor of parser", "warn", this.toString());
  1281. }
  1282. var parser = (typeof field.parser === 'function') ?
  1283. field.parser :
  1284. DS.Parser[field.parser+''];
  1285. if(parser) {
  1286. data = parser.call(this, data);
  1287. }
  1288. // Safety measure
  1289. if(data === undefined) {
  1290. data = null;
  1291. }
  1292. oResult[key] = data;
  1293. }
  1294. }
  1295. catch(e) {
  1296. YAHOO.log("Error while parsing XML result: " + e.message);
  1297. }
  1298. return oResult;
  1299. },
  1300. /**
  1301. * Overridable method parses XML data into a response object.
  1302. *
  1303. * @method parseXMLData
  1304. * @param oRequest {Object} Request object.
  1305. * @param oFullResponse {Object} The full XML response from the live database.
  1306. * @return {Object} Parsed response object with the following properties<br>
  1307. * - results (Array) Array of parsed data results<br>
  1308. * - error (Boolean) True if there was an error
  1309. */
  1310. parseXMLData : function(oRequest, oFullResponse) {
  1311. var bError = false,
  1312. schema = this.responseSchema,
  1313. oParsedResponse = {meta:{}},
  1314. xmlList = null,
  1315. metaNode = schema.metaNode,
  1316. metaLocators = schema.metaFields || {},
  1317. i,k,loc,v;
  1318. // In case oFullResponse is something funky
  1319. try {
  1320. xmlList = (schema.resultNode) ?
  1321. oFullResponse.getElementsByTagName(schema.resultNode) :
  1322. null;
  1323. // Pull any meta identified
  1324. metaNode = metaNode ? oFullResponse.getElementsByTagName(metaNode)[0] :
  1325. oFullResponse;
  1326. if (metaNode) {
  1327. for (k in metaLocators) {
  1328. if (lang.hasOwnProperty(metaLocators, k)) {