1318 |
alexandre_ |
1 |
/*
|
|
|
2 |
Copyright (c) 2004-2006, The Dojo Foundation
|
|
|
3 |
All Rights Reserved.
|
|
|
4 |
|
|
|
5 |
Licensed under the Academic Free License version 2.1 or above OR the
|
|
|
6 |
modified BSD license. For more information on Dojo licensing, see:
|
|
|
7 |
|
|
|
8 |
http://dojotoolkit.org/community/licensing.shtml
|
|
|
9 |
*/
|
|
|
10 |
|
|
|
11 |
dojo.provide("dojo.data.OpmlStore");
|
|
|
12 |
dojo.require("dojo.data.core.Read");
|
|
|
13 |
dojo.require("dojo.data.core.Result");
|
|
|
14 |
dojo.require("dojo.lang.assert");
|
|
|
15 |
dojo.require("dojo.json");
|
|
|
16 |
dojo.require("dojo.experimental");
|
|
|
17 |
dojo.experimental("dojo.data.OpmlStore");
|
|
|
18 |
dojo.declare("dojo.data.OpmlStore", dojo.data.core.Read, {initializer:function (keywordParameters) {
|
|
|
19 |
this._arrayOfTopLevelItems = [];
|
|
|
20 |
this._metadataNodes = null;
|
|
|
21 |
this._loadFinished = false;
|
|
|
22 |
this._opmlFileUrl = keywordParameters["url"];
|
|
|
23 |
}, _assertIsItem:function (item) {
|
|
|
24 |
if (!this.isItem(item)) {
|
|
|
25 |
throw new Error("dojo.data.OpmlStore: a function was passed an item argument that was not an item");
|
|
|
26 |
}
|
|
|
27 |
}, _removeChildNodesThatAreNotElementNodes:function (node, recursive) {
|
|
|
28 |
var childNodes = node.childNodes;
|
|
|
29 |
if (childNodes.length == 0) {
|
|
|
30 |
return;
|
|
|
31 |
}
|
|
|
32 |
var nodesToRemove = [];
|
|
|
33 |
var i, childNode;
|
|
|
34 |
for (i = 0; i < childNodes.length; ++i) {
|
|
|
35 |
childNode = childNodes[i];
|
|
|
36 |
if (childNode.nodeType != Node.ELEMENT_NODE) {
|
|
|
37 |
nodesToRemove.push(childNode);
|
|
|
38 |
}
|
|
|
39 |
}
|
|
|
40 |
for (i = 0; i < nodesToRemove.length; ++i) {
|
|
|
41 |
childNode = nodesToRemove[i];
|
|
|
42 |
node.removeChild(childNode);
|
|
|
43 |
}
|
|
|
44 |
if (recursive) {
|
|
|
45 |
for (i = 0; i < childNodes.length; ++i) {
|
|
|
46 |
childNode = childNodes[i];
|
|
|
47 |
this._removeChildNodesThatAreNotElementNodes(childNode, recursive);
|
|
|
48 |
}
|
|
|
49 |
}
|
|
|
50 |
}, _processRawXmlTree:function (rawXmlTree) {
|
|
|
51 |
var headNodes = rawXmlTree.getElementsByTagName("head");
|
|
|
52 |
var headNode = headNodes[0];
|
|
|
53 |
this._removeChildNodesThatAreNotElementNodes(headNode);
|
|
|
54 |
this._metadataNodes = headNode.childNodes;
|
|
|
55 |
var bodyNodes = rawXmlTree.getElementsByTagName("body");
|
|
|
56 |
var bodyNode = bodyNodes[0];
|
|
|
57 |
this._removeChildNodesThatAreNotElementNodes(bodyNode, true);
|
|
|
58 |
var bodyChildNodes = bodyNodes[0].childNodes;
|
|
|
59 |
for (var i = 0; i < bodyChildNodes.length; ++i) {
|
|
|
60 |
var node = bodyChildNodes[i];
|
|
|
61 |
if (node.tagName == "outline") {
|
|
|
62 |
this._arrayOfTopLevelItems.push(node);
|
|
|
63 |
}
|
|
|
64 |
}
|
|
|
65 |
}, get:function (item, attribute, defaultValue) {
|
|
|
66 |
this._assertIsItem(item);
|
|
|
67 |
if (attribute == "children") {
|
|
|
68 |
return (item.firstChild || defaultValue);
|
|
|
69 |
} else {
|
|
|
70 |
var value = item.getAttribute(attribute);
|
|
|
71 |
value = (value != undefined) ? value : defaultValue;
|
|
|
72 |
return value;
|
|
|
73 |
}
|
|
|
74 |
}, getValues:function (item, attribute) {
|
|
|
75 |
this._assertIsItem(item);
|
|
|
76 |
if (attribute == "children") {
|
|
|
77 |
var array = [];
|
|
|
78 |
for (var i = 0; i < item.childNodes.length; ++i) {
|
|
|
79 |
array.push(item.childNodes[i]);
|
|
|
80 |
}
|
|
|
81 |
return array;
|
|
|
82 |
} else {
|
|
|
83 |
return [item.getAttribute(attribute)];
|
|
|
84 |
}
|
|
|
85 |
}, getAttributes:function (item) {
|
|
|
86 |
this._assertIsItem(item);
|
|
|
87 |
var attributes = [];
|
|
|
88 |
var xmlNode = item;
|
|
|
89 |
var xmlAttributes = xmlNode.attributes;
|
|
|
90 |
for (var i = 0; i < xmlAttributes.length; ++i) {
|
|
|
91 |
var xmlAttribute = xmlAttributes.item(i);
|
|
|
92 |
attributes.push(xmlAttribute.nodeName);
|
|
|
93 |
}
|
|
|
94 |
if (xmlNode.childNodes.length > 0) {
|
|
|
95 |
attributes.push("children");
|
|
|
96 |
}
|
|
|
97 |
return attributes;
|
|
|
98 |
}, hasAttribute:function (item, attribute) {
|
|
|
99 |
return (this.getValues(item, attribute).length > 0);
|
|
|
100 |
}, containsValue:function (item, attribute, value) {
|
|
|
101 |
var values = this.getValues(item, attribute);
|
|
|
102 |
for (var i = 0; i < values.length; ++i) {
|
|
|
103 |
var possibleValue = values[i];
|
|
|
104 |
if (value == possibleValue) {
|
|
|
105 |
return true;
|
|
|
106 |
}
|
|
|
107 |
}
|
|
|
108 |
return false;
|
|
|
109 |
}, isItem:function (something) {
|
|
|
110 |
return (something && something.nodeType == Node.ELEMENT_NODE && something.tagName == "outline");
|
|
|
111 |
}, isItemAvailable:function (something) {
|
|
|
112 |
return this.isItem(something);
|
|
|
113 |
}, find:function (keywordArgs) {
|
|
|
114 |
var result = null;
|
|
|
115 |
if (keywordArgs instanceof dojo.data.core.Result) {
|
|
|
116 |
result = keywordArgs;
|
|
|
117 |
result.store = this;
|
|
|
118 |
} else {
|
|
|
119 |
result = new dojo.data.core.Result(keywordArgs, this);
|
|
|
120 |
}
|
|
|
121 |
var self = this;
|
|
|
122 |
var bindHandler = function (type, data, evt) {
|
|
|
123 |
var scope = result.scope || dj_global;
|
|
|
124 |
if (type == "load") {
|
|
|
125 |
self._processRawXmlTree(data);
|
|
|
126 |
if (result.saveResult) {
|
|
|
127 |
result.items = self._arrayOfTopLevelItems;
|
|
|
128 |
}
|
|
|
129 |
if (result.onbegin) {
|
|
|
130 |
result.onbegin.call(scope, result);
|
|
|
131 |
}
|
|
|
132 |
for (var i = 0; i < self._arrayOfTopLevelItems.length; i++) {
|
|
|
133 |
var item = self._arrayOfTopLevelItems[i];
|
|
|
134 |
if (result.onnext && !result._aborted) {
|
|
|
135 |
result.onnext.call(scope, item, result);
|
|
|
136 |
}
|
|
|
137 |
}
|
|
|
138 |
if (result.oncompleted && !result._aborted) {
|
|
|
139 |
result.oncompleted.call(scope, result);
|
|
|
140 |
}
|
|
|
141 |
} else {
|
|
|
142 |
if (type == "error" || type == "timeout") {
|
|
|
143 |
var errorObject = data;
|
|
|
144 |
if (result.onerror) {
|
|
|
145 |
result.onerror.call(scope, data);
|
|
|
146 |
}
|
|
|
147 |
}
|
|
|
148 |
}
|
|
|
149 |
};
|
|
|
150 |
if (!this._loadFinished) {
|
|
|
151 |
if (this._opmlFileUrl) {
|
|
|
152 |
var bindRequest = dojo.io.bind({url:this._opmlFileUrl, handle:bindHandler, mimetype:"text/xml", sync:(result.sync || false)});
|
|
|
153 |
result._abortFunc = bindRequest.abort;
|
|
|
154 |
}
|
|
|
155 |
}
|
|
|
156 |
return result;
|
|
|
157 |
}, getIdentity:function (item) {
|
|
|
158 |
dojo.unimplemented("dojo.data.OpmlStore.getIdentity()");
|
|
|
159 |
return null;
|
|
|
160 |
}, findByIdentity:function (identity) {
|
|
|
161 |
dojo.unimplemented("dojo.data.OpmlStore.findByIdentity()");
|
|
|
162 |
return null;
|
|
|
163 |
}});
|
|
|
164 |
|