f0e8f70d7fc0413f1aa02c63177b25212dc64576.svn-base 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. if (!dojo._hasResource["dojox.data.OpmlStore"]) { // _hasResource checks added
  2. // by build. Do not use
  3. // _hasResource directly in
  4. // your code.
  5. dojo._hasResource["dojox.data.OpmlStore"] = true;
  6. dojo.provide("dojox.data.OpmlStore");
  7. dojo.require("dojo.data.util.filter");
  8. dojo.require("dojo.data.util.simpleFetch");
  9. dojo.declare("dojox.data.OpmlStore", null, {
  10. /*
  11. * summary: The OpmlStore implements the dojo.data.api.Read API.
  12. */
  13. /*
  14. * examples: var opmlStore = new
  15. * dojo.data.OpmlStore({url:"geography.xml"}); var opmlStore = new
  16. * dojo.data.OpmlStore({url:"http://example.com/geography.xml"});
  17. */
  18. constructor : function(/* Object */keywordParameters) {
  19. // summary: constructor
  20. // keywordParameters: {url: String, label: String} Where label is
  21. // optional and configures what should be used as the return from
  22. // getLabel()
  23. this._xmlData = null;
  24. this._arrayOfTopLevelItems = [];
  25. this._arrayOfAllItems = [];
  26. this._metadataNodes = null;
  27. this._loadFinished = false;
  28. this.url = keywordParameters.url;
  29. this._opmlData = keywordParameters.data; // XML DOM Document
  30. if (keywordParameters.label) {
  31. this.label = keywordParameters.label;
  32. }
  33. this._loadInProgress = false; // Got to track the initial load to
  34. // prevent duelling loads of the
  35. // dataset.
  36. this._queuedFetches = [];
  37. this._identityMap = {};
  38. this._identCount = 0;
  39. this._idProp = "_I";
  40. },
  41. label : "text",
  42. url : "",
  43. _assertIsItem : function(/* item */item) {
  44. if (!this.isItem(item)) {
  45. throw new Error("dojo.data.OpmlStore: a function was passed an item argument that was not an item");
  46. }
  47. },
  48. _assertIsAttribute : function(/* item || String */attribute) {
  49. // summary:
  50. // This function tests whether the item passed in is indeed a valid
  51. // 'attribute' like type for the store.
  52. // attribute:
  53. // The attribute to test for being contained by the store.
  54. if (!dojo.isString(attribute)) {
  55. throw new Error("dojox.data.OpmlStore: a function was passed an attribute argument that was not an attribute object nor an attribute name string");
  56. }
  57. },
  58. _removeChildNodesThatAreNotElementNodes : function(/* node */node, /* boolean */
  59. recursive) {
  60. var childNodes = node.childNodes;
  61. if (childNodes.length === 0) {
  62. return;
  63. }
  64. var nodesToRemove = [];
  65. var i, childNode;
  66. for (i = 0; i < childNodes.length; ++i) {
  67. childNode = childNodes[i];
  68. if (childNode.nodeType != 1) {
  69. nodesToRemove.push(childNode);
  70. }
  71. }
  72. for (i = 0; i < nodesToRemove.length; ++i) {
  73. childNode = nodesToRemove[i];
  74. node.removeChild(childNode);
  75. }
  76. if (recursive) {
  77. for (i = 0; i < childNodes.length; ++i) {
  78. childNode = childNodes[i];
  79. this._removeChildNodesThatAreNotElementNodes(childNode,
  80. recursive);
  81. }
  82. }
  83. },
  84. _processRawXmlTree : function(/* xmlDoc */rawXmlTree) {
  85. this._loadFinished = true;
  86. this._xmlData = rawXmlTree;
  87. var headNodes = rawXmlTree.getElementsByTagName('head');
  88. var headNode = headNodes[0];
  89. if (headNode) {
  90. this._removeChildNodesThatAreNotElementNodes(headNode);
  91. this._metadataNodes = headNode.childNodes;
  92. }
  93. var bodyNodes = rawXmlTree.getElementsByTagName('body');
  94. var bodyNode = bodyNodes[0];
  95. if (bodyNode) {
  96. this._removeChildNodesThatAreNotElementNodes(bodyNode, true);
  97. var bodyChildNodes = bodyNodes[0].childNodes;
  98. for (var i = 0; i < bodyChildNodes.length; ++i) {
  99. var node = bodyChildNodes[i];
  100. if (node.tagName == 'outline') {
  101. this._identityMap[this._identCount] = node;
  102. this._identCount++;
  103. this._arrayOfTopLevelItems.push(node);
  104. this._arrayOfAllItems.push(node);
  105. this._checkChildNodes(node);
  106. }
  107. }
  108. }
  109. },
  110. _checkChildNodes : function(node /* Node */) {
  111. // summary:
  112. // Internal function to recurse over all child nodes from the store
  113. // and add them
  114. // As non-toplevel items
  115. // description:
  116. // Internal function to recurse over all child nodes from the store
  117. // and add them
  118. // As non-toplevel items
  119. //
  120. // node:
  121. // The child node to walk.
  122. if (node.firstChild) {
  123. for (var i = 0; i < node.childNodes.length; i++) {
  124. var child = node.childNodes[i];
  125. if (child.tagName == 'outline') {
  126. this._identityMap[this._identCount] = child;
  127. this._identCount++;
  128. this._arrayOfAllItems.push(child);
  129. this._checkChildNodes(child);
  130. }
  131. }
  132. }
  133. },
  134. _getItemsArray : function(/* object? */queryOptions) {
  135. // summary:
  136. // Internal function to determine which list of items to search
  137. // over.
  138. // queryOptions: The query options parameter, if any.
  139. if (queryOptions && queryOptions.deep) {
  140. return this._arrayOfAllItems;
  141. }
  142. return this._arrayOfTopLevelItems;
  143. },
  144. /***********************************************************************
  145. * dojo.data.api.Read API
  146. **********************************************************************/
  147. getValue : function( /* item */item,
  148. /* attribute || attribute-name-string */attribute,
  149. /* value? */defaultValue) {
  150. // summary:
  151. // See dojo.data.api.Read.getValue()
  152. this._assertIsItem(item);
  153. this._assertIsAttribute(attribute);
  154. if (attribute == 'children') {
  155. return (item.firstChild || defaultValue); // Object
  156. } else {
  157. var value = item.getAttribute(attribute);
  158. return (value !== undefined) ? value : defaultValue; // Object
  159. }
  160. },
  161. getValues : function(/* item */item,
  162. /* attribute || attribute-name-string */attribute) {
  163. // summary:
  164. // See dojo.data.api.Read.getValues()
  165. this._assertIsItem(item);
  166. this._assertIsAttribute(attribute);
  167. var array = [];
  168. if (attribute == 'children') {
  169. for (var i = 0; i < item.childNodes.length; ++i) {
  170. array.push(item.childNodes[i]);
  171. }
  172. } else if (item.getAttribute(attribute) !== null) {
  173. array.push(item.getAttribute(attribute));
  174. }
  175. return array; // Array
  176. },
  177. getAttributes : function(/* item */item) {
  178. // summary:
  179. // See dojo.data.api.Read.getAttributes()
  180. this._assertIsItem(item);
  181. var attributes = [];
  182. var xmlNode = item;
  183. var xmlAttributes = xmlNode.attributes;
  184. for (var i = 0; i < xmlAttributes.length; ++i) {
  185. var xmlAttribute = xmlAttributes.item(i);
  186. attributes.push(xmlAttribute.nodeName);
  187. }
  188. if (xmlNode.childNodes.length > 0) {
  189. attributes.push('children');
  190. }
  191. return attributes; // Array
  192. },
  193. hasAttribute : function( /* item */item,
  194. /* attribute || attribute-name-string */attribute) {
  195. // summary:
  196. // See dojo.data.api.Read.hasAttribute()
  197. return (this.getValues(item, attribute).length > 0); // Boolean
  198. },
  199. containsValue : function(/* item */item,
  200. /* attribute || attribute-name-string */attribute,
  201. /* anything */value) {
  202. // summary:
  203. // See dojo.data.api.Read.containsValue()
  204. var regexp = undefined;
  205. if (typeof value === "string") {
  206. regexp = dojo.data.util.filter.patternToRegExp(value, false);
  207. }
  208. return this._containsValue(item, attribute, value, regexp); // boolean.
  209. },
  210. _containsValue : function( /* item */item,
  211. /* attribute || attribute-name-string */attribute,
  212. /* anything */value,
  213. /* RegExp? */regexp) {
  214. // summary:
  215. // Internal function for looking at the values contained by the
  216. // item.
  217. // description:
  218. // Internal function for looking at the values contained by the
  219. // item. This
  220. // function allows for denoting if the comparison should be case
  221. // sensitive for
  222. // strings or not (for handling filtering cases where string case
  223. // should not matter)
  224. //
  225. // item:
  226. // The data item to examine for attribute values.
  227. // attribute:
  228. // The attribute to inspect.
  229. // value:
  230. // The value to match.
  231. // regexp:
  232. // Optional regular expression generated off value if value was of
  233. // string type to handle wildcarding.
  234. // If present and attribute values are string, then it can be used
  235. // for comparison instead of 'value'
  236. var values = this.getValues(item, attribute);
  237. for (var i = 0; i < values.length; ++i) {
  238. var possibleValue = values[i];
  239. if (typeof possibleValue === "string" && regexp) {
  240. return (possibleValue.match(regexp) !== null);
  241. } else {
  242. // Non-string matching.
  243. if (value === possibleValue) {
  244. return true; // Boolean
  245. }
  246. }
  247. }
  248. return false; // Boolean
  249. },
  250. isItem : function(/* anything */something) {
  251. // summary:
  252. // See dojo.data.api.Read.isItem()
  253. // description:
  254. // Four things are verified to ensure that "something" is an item:
  255. // something can not be null, the nodeType must be an XML Element,
  256. // the tagName must be "outline", and the node must be a member of
  257. // XML document for this datastore.
  258. return (something && something.nodeType == 1
  259. && something.tagName == 'outline' && something.ownerDocument === this._xmlData); // Boolean
  260. },
  261. isItemLoaded : function(/* anything */something) {
  262. // summary:
  263. // See dojo.data.api.Read.isItemLoaded()
  264. // OpmlStore loads every item, so if it's an item, then it's loaded.
  265. return this.isItem(something); // Boolean
  266. },
  267. loadItem : function(/* item */item) {
  268. // summary:
  269. // See dojo.data.api.Read.loadItem()
  270. // description:
  271. // The OpmlStore always loads all items, so if it's an item, then
  272. // it's loaded.
  273. // From the dojo.data.api.Read.loadItem docs:
  274. // If a call to isItemLoaded() returns true before loadItem() is
  275. // even called,
  276. // then loadItem() need not do any work at all and will not even
  277. // invoke the callback handlers.
  278. },
  279. getLabel : function(/* item */item) {
  280. // summary:
  281. // See dojo.data.api.Read.getLabel()
  282. if (this.isItem(item)) {
  283. return this.getValue(item, this.label); // String
  284. }
  285. return undefined; // undefined
  286. },
  287. getLabelAttributes : function(/* item */item) {
  288. // summary:
  289. // See dojo.data.api.Read.getLabelAttributes()
  290. return [this.label]; // array
  291. },
  292. // The dojo.data.api.Read.fetch() function is implemented as
  293. // a mixin from dojo.data.util.simpleFetch.
  294. // That mixin requires us to define _fetchItems().
  295. _fetchItems : function( /* Object */keywordArgs,
  296. /* Function */findCallback,
  297. /* Function */errorCallback) {
  298. // summary:
  299. // See dojo.data.util.simpleFetch.fetch()
  300. var self = this;
  301. var filter = function(requestArgs, arrayOfItems) {
  302. var items = null;
  303. if (requestArgs.query) {
  304. items = [];
  305. var ignoreCase = requestArgs.queryOptions
  306. ? requestArgs.queryOptions.ignoreCase
  307. : false;
  308. // See if there are any string values that can be regexp
  309. // parsed first to avoid multiple regexp gens on the
  310. // same value for each item examined. Much more efficient.
  311. var regexpList = {};
  312. for (var key in requestArgs.query) {
  313. var value = requestArgs.query[key];
  314. if (typeof value === "string") {
  315. regexpList[key] = dojo.data.util.filter
  316. .patternToRegExp(value, ignoreCase);
  317. }
  318. }
  319. for (var i = 0; i < arrayOfItems.length; ++i) {
  320. var match = true;
  321. var candidateItem = arrayOfItems[i];
  322. for (var key in requestArgs.query) {
  323. var value = requestArgs.query[key];
  324. if (!self._containsValue(candidateItem, key, value,
  325. regexpList[key])) {
  326. match = false;
  327. }
  328. }
  329. if (match) {
  330. items.push(candidateItem);
  331. }
  332. }
  333. } else {
  334. // We want a copy to pass back in case the parent wishes to
  335. // sort the array. We shouldn't allow resort
  336. // of the internal list so that multiple callers can get
  337. // lists and sort without affecting each other.
  338. if (arrayOfItems.length > 0) {
  339. items = arrayOfItems.slice(0, arrayOfItems.length);
  340. }
  341. }
  342. findCallback(items, requestArgs);
  343. };
  344. if (this._loadFinished) {
  345. filter(keywordArgs, this
  346. ._getItemsArray(keywordArgs.queryOptions));
  347. } else {
  348. // If fetches come in before the loading has finished, but while
  349. // a load is in progress, we have to defer the fetching to be
  350. // invoked in the callback.
  351. if (this._loadInProgress) {
  352. this._queuedFetches.push({
  353. args : keywordArgs,
  354. filter : filter
  355. });
  356. } else {
  357. if (this.url !== "") {
  358. this._loadInProgress = true;
  359. var getArgs = {
  360. url : self.url,
  361. handleAs : "xml"
  362. };
  363. var getHandler = dojo.xhrGet(getArgs);
  364. getHandler.addCallback(function(data) {
  365. self._processRawXmlTree(data);
  366. filter(
  367. keywordArgs,
  368. self
  369. ._getItemsArray(keywordArgs.queryOptions));
  370. self._handleQueuedFetches();
  371. });
  372. getHandler.addErrback(function(error) {
  373. throw error;
  374. });
  375. } else if (this._opmlData) {
  376. this._processRawXmlTree(this._opmlData);
  377. this._opmlData = null;
  378. filter(keywordArgs,
  379. this._getItemsArray(keywordArgs.queryOptions));
  380. } else {
  381. throw new Error("dojox.data.OpmlStore: No OPML source data was provided as either URL or XML data input.");
  382. }
  383. }
  384. }
  385. },
  386. getFeatures : function() {
  387. // summary: See dojo.data.api.Read.getFeatures()
  388. var features = {
  389. 'dojo.data.api.Read' : true,
  390. 'dojo.data.api.Identity' : true
  391. };
  392. return features; // Object
  393. },
  394. /***********************************************************************
  395. * dojo.data.api.Identity API
  396. **********************************************************************/
  397. getIdentity : function(/* item */item) {
  398. // summary:
  399. // See dojo.data.api.Identity.getIdentity()
  400. if (this.isItem(item)) {
  401. // No ther way to do this other than O(n) without
  402. // complete rework of how the tree stores nodes.
  403. for (var i in this._identityMap) {
  404. if (this._identityMap[i] === item) {
  405. return i;
  406. }
  407. }
  408. }
  409. return null; // null
  410. },
  411. fetchItemByIdentity : function(/* Object */keywordArgs) {
  412. // summary:
  413. // See dojo.data.api.Identity.fetchItemByIdentity()
  414. // Hasn't loaded yet, we have to trigger the load.
  415. if (!this._loadFinished) {
  416. var self = this;
  417. if (this.url !== "") {
  418. // If fetches come in before the loading has finished, but
  419. // while
  420. // a load is in progress, we have to defer the fetching to
  421. // be
  422. // invoked in the callback.
  423. if (this._loadInProgress) {
  424. this._queuedFetches.push({
  425. args : keywordArgs
  426. });
  427. } else {
  428. this._loadInProgress = true;
  429. var getArgs = {
  430. url : self.url,
  431. handleAs : "xml"
  432. };
  433. var getHandler = dojo.xhrGet(getArgs);
  434. getHandler.addCallback(function(data) {
  435. var scope = keywordArgs.scope
  436. ? keywordArgs.scope
  437. : dojo.global;
  438. try {
  439. self._processRawXmlTree(data);
  440. var item = self._identityMap[keywordArgs.identity];
  441. if (!self.isItem(item)) {
  442. item = null;
  443. }
  444. if (keywordArgs.onItem) {
  445. keywordArgs.onItem.call(scope, item);
  446. }
  447. self._handleQueuedFetches();
  448. } catch (error) {
  449. if (keywordArgs.onError) {
  450. keywordArgs.onError.call(scope, error);
  451. }
  452. }
  453. });
  454. getHandler.addErrback(function(error) {
  455. this._loadInProgress = false;
  456. if (keywordArgs.onError) {
  457. var scope = keywordArgs.scope
  458. ? keywordArgs.scope
  459. : dojo.global;
  460. keywordArgs.onError.call(scope, error);
  461. }
  462. });
  463. }
  464. } else if (this._opmlData) {
  465. this._processRawXmlTree(this._opmlData);
  466. this._opmlData = null;
  467. var item = this._identityMap[keywordArgs.identity];
  468. if (!self.isItem(item)) {
  469. item = null;
  470. }
  471. if (keywordArgs.onItem) {
  472. var scope = keywordArgs.scope
  473. ? keywordArgs.scope
  474. : dojo.global;
  475. keywordArgs.onItem.call(scope, item);
  476. }
  477. }
  478. } else {
  479. // Already loaded. We can just look it up and call back.
  480. var item = this._identityMap[keywordArgs.identity];
  481. if (!this.isItem(item)) {
  482. item = null;
  483. }
  484. if (keywordArgs.onItem) {
  485. var scope = keywordArgs.scope
  486. ? keywordArgs.scope
  487. : dojo.global;
  488. keywordArgs.onItem.call(scope, item);
  489. }
  490. }
  491. },
  492. getIdentityAttributes : function(/* item */item) {
  493. // summary:
  494. // See dojo.data.api.Identity.getIdentifierAttributes()
  495. // Identity isn't a public attribute in the item, it's the node
  496. // count.
  497. // So, return null.
  498. return null;
  499. },
  500. _handleQueuedFetches : function() {
  501. // summary:
  502. // Internal function to execute delayed request in the store.
  503. // Execute any deferred fetches now.
  504. if (this._queuedFetches.length > 0) {
  505. for (var i = 0; i < this._queuedFetches.length; i++) {
  506. var fData = this._queuedFetches[i];
  507. var delayedQuery = fData.args;
  508. var delayedFilter = fData.filter;
  509. if (delayedFilter) {
  510. delayedFilter(delayedQuery,
  511. this._getItemsArray(delayedQuery.queryOptions));
  512. } else {
  513. this.fetchItemByIdentity(delayedQuery);
  514. }
  515. }
  516. this._queuedFetches = [];
  517. }
  518. },
  519. close : function(
  520. /* dojo.data.api.Request || keywordArgs || null */request) {
  521. // summary:
  522. // See dojo.data.api.Read.close()
  523. }
  524. });
  525. // Mix in the simple fetch implementation to this class.
  526. dojo.extend(dojox.data.OpmlStore, dojo.data.util.simpleFetch);
  527. }