Subversion Repositories Applications.papyrus

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

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