author | ivanl |
Wed, 29 Jul 2009 10:14:37 +0100 | |
changeset 15 | 5e64ec5b1dfe |
parent 13 | 3a1db8573f1e |
child 16 | 6544cc5386ce |
permissions | -rw-r--r-- |
0 | 1 |
/////////////////////////////////////////////////////////////////////////////// |
2 |
// The FeedUpdateBroker class implements a simple RSS fetcher and parser. |
|
3 |
// Adapted from WRTKit RssReader example |
|
4 |
||
5 |
// Constructor. |
|
6 |
function FeedUpdateBroker() { |
|
7 |
this.httpReq = null; |
|
8 |
this.feedAddress = null; |
|
9 |
this.callback = null; |
|
10 |
this.ignoreContent = false; |
|
11 |
this.cancelled = false; |
|
12 |
this.responseParser = this.handleRssResponse; |
|
13 |
this.startFromItem = 0; |
|
14 |
this.maxItems = 0; |
|
15 |
} |
|
16 |
||
17 |
// Fetches a feed from the specified URL and calls the callback when the feed |
|
18 |
// has been fetched and parsed, or if the process results in an error. |
|
19 |
FeedUpdateBroker.prototype.doFetchFeed = function(){ |
|
20 |
// create new XML HTTP request |
|
21 |
this.httpReq = new Ajax(); |
|
22 |
||
23 |
// set callback |
|
24 |
var self = this; |
|
25 |
this.httpReq.onreadystatechange = function() { self.readyStateChanged(); }; |
|
26 |
||
27 |
// initiate the request |
|
3 | 28 |
this.httpReq.open("GET", nocache(this.feedAddress), true); |
0 | 29 |
this.httpReq.send(null); |
30 |
} |
|
31 |
||
32 |
// has been fetched and parsed, or if the process results in an error. |
|
33 |
FeedUpdateBroker.prototype.fetchFeed = function(feedURL, callback) { |
|
34 |
// remember callback |
|
35 |
this.callback = callback; |
|
36 |
this.feedAddress = feedURL; |
|
37 |
this.doFetchFeed(); |
|
38 |
} |
|
39 |
||
40 |
// Callback for ready-state change events in the XML HTTP request. |
|
41 |
FeedUpdateBroker.prototype.readyStateChanged = function() { |
|
42 |
// complete request? |
|
43 |
if (this.httpReq.readyState == 4) { |
|
44 |
// attempt to get response status |
|
45 |
var responseStatus = null; |
|
46 |
try { |
|
47 |
responseStatus = this.httpReq.status; |
|
48 |
} catch (noStatusException) {} |
|
49 |
||
50 |
// are we being prompted for login? |
|
51 |
var text = this.httpReq.responseText; |
|
52 |
if ( isLoginPrompt (text) ) { |
|
53 |
var self = this; |
|
54 |
login(function(){self.doFetchFeed();}); |
|
55 |
return; |
|
56 |
} |
|
57 |
||
58 |
// handle the response and call the registered callback object |
|
59 |
var response = this.httpReq.responseXML; |
|
60 |
if (response == null) { |
|
61 |
// if the content type is not set correctly, we get the response as text |
|
62 |
var xmlparser = new DOMParser(); |
|
63 |
response = xmlparser.parseFromString(this.httpReq.responseText, "text/xml"); |
|
64 |
} |
|
65 |
this.callback.feedUpdateCompleted(this.handleResponse(responseStatus, response)); |
|
66 |
} |
|
67 |
} |
|
68 |
||
69 |
// Handles a completed response. |
|
70 |
FeedUpdateBroker.prototype.handleResponse = function(responseStatus, xmlDoc){ |
|
71 |
if (this.responseParser == null) { |
|
72 |
return this.handleRssResponse(responseStatus, xmlDoc); |
|
73 |
} |
|
74 |
else { |
|
75 |
return this.responseParser.call(this, this, responseStatus, xmlDoc); |
|
76 |
} |
|
77 |
} |
|
78 |
||
79 |
||
80 |
FeedUpdateBroker.prototype.handleRssResponse = function(broker, responseStatus, xmlDoc){ |
|
81 |
if ( this.cancelled ) { |
|
82 |
return { status: "cancelled" }; |
|
83 |
} |
|
84 |
if (responseStatus == 200 && xmlDoc != null) { |
|
85 |
// node ref for iterating |
|
86 |
var node; |
|
87 |
||
88 |
// get last modified time - default to current time |
|
89 |
var lastModified = new Date().getTime(); |
|
90 |
var channelElements = xmlDoc.getElementsByTagName("channel"); |
|
91 |
if (channelElements.length > 0) { |
|
92 |
node = channelElements[0].firstChild; |
|
93 |
while (node != null) { |
|
94 |
if (node.nodeType == Node.ELEMENT_NODE) { |
|
95 |
if (node.nodeName == "pubDate" || |
|
96 |
node.nodeName == "lastBuildDate" || |
|
97 |
node.nodeName == "dc:date") { |
|
13 | 98 |
lastModified = getTextOfNode(node); |
0 | 99 |
break; |
100 |
} |
|
101 |
} |
|
102 |
node = node.nextSibling; |
|
103 |
} |
|
104 |
} |
|
105 |
||
106 |
// init feed items array |
|
107 |
var items = []; |
|
108 |
||
109 |
// we got the feed XML so now we'll parse it |
|
110 |
var itemElements = xmlDoc.getElementsByTagName("item"); |
|
111 |
||
112 |
for (var i = this.startFromItem; i < itemElements.length; i++) { |
|
113 |
if ( this.maxItems > 0 && this.maxItems < i ) { |
|
114 |
break; |
|
115 |
} |
|
116 |
// iterate through child nodes of this item and gather |
|
117 |
// all the data we need for a feed item |
|
118 |
var title = null; |
|
119 |
var date = null; |
|
120 |
var description = null; |
|
121 |
var url = null; |
|
122 |
var author = null; |
|
123 |
node = itemElements[i].firstChild; |
|
124 |
while (node != null) { |
|
125 |
if (node.nodeType == Node.ELEMENT_NODE) { |
|
126 |
if (node.nodeName == "title") { |
|
127 |
// item title |
|
13 | 128 |
title = getTextOfNode(node); |
0 | 129 |
} else if (node.nodeName == "pubDate" || node.nodeName == "dc:date") { |
130 |
// item publishing date |
|
13 | 131 |
date = getTextOfNode(node); |
0 | 132 |
} else if (node.nodeName == "description" && !this.ignoreContent ) { |
133 |
// item description |
|
13 | 134 |
description = getTextOfNode(node); |
0 | 135 |
} else if (node.nodeName == "link") { |
136 |
// link URL |
|
13 | 137 |
url = getTextOfNode(node); |
0 | 138 |
} else if (node.nodeName == "dc:creator" ) { |
13 | 139 |
author = getTextOfNode(node); |
0 | 140 |
} |
141 |
} |
|
142 |
node = node.nextSibling; |
|
143 |
} |
|
144 |
||
145 |
// create the item and add to the items array |
|
146 |
items.push({ title: title, date: date, description: description, url: url, author: author }); |
|
147 |
} |
|
148 |
||
149 |
// update was completed successfully |
|
150 |
return { status: "ok", lastModified: lastModified, items: items }; |
|
151 |
} else { |
|
152 |
// update failed |
|
153 |
return { status: "error" }; |
|
154 |
} |
|
155 |
} |
|
156 |
||
157 |
// Returns the text of a node. |
|
13 | 158 |
function getTextOfNode(node) { |
0 | 159 |
var buf = ""; |
6 | 160 |
// iterate through all child elements and collect all text to the buffer |
161 |
var child = node.firstChild; |
|
162 |
while (child != null) { |
|
163 |
if (child.nodeType == Node.TEXT_NODE || child.nodeType == Node.CDATA_SECTION_NODE) { |
|
164 |
// append text to buffer |
|
165 |
if (buf != "") { |
|
166 |
buf += " "; |
|
167 |
} |
|
15 | 168 |
buf += child.nodeValue; |
6 | 169 |
} |
170 |
child = child.nextSibling; |
|
171 |
} |
|
4 | 172 |
|
10
07ac2f6a36a9
1.0rc10 Fixes annoyances and buglets in reading forums and blog.
ivanl
parents:
6
diff
changeset
|
173 |
return buf; |
0 | 174 |
} |
175 |
||
176 |
FeedUpdateBroker.prototype.cancel = function() { |
|
177 |
this.cancelled = true; |
|
178 |
this.httpReq.abort(); |
|
179 |
} |