Repository navigation
Expand file tree
/
Copy pathxparser.js
More file actions
211 lines (174 loc) · 6.48 KB
/
Copy pathxparser.js
File metadata and controls
211 lines (174 loc) · 6.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
var xparser = {};
//feedType Enum
xparser.feedType = {
rss : 1,
atom : 2
};
xparser.getFeed = function (sUrl, fpCallBack, oCallBackScope) {
var oReq = zXmlHttp.createRequest();
oReq.onreadystatechange = function () {
if (oReq.readyState == 4) {
if (oReq.status == 200 || oReq.status == 304) {
var oFeed = null;
var oXmlDom = zXmlDom.createDocument();
oXmlDom.loadXML(oReq.responseText);
var oRootNode = oXmlDom.documentElement;
//Get the name of the document element.
var sRootName;
if (oRootNode.nodeName.indexOf(":") > -1)
sRootName = oRootNode.nodeName.split(":")[1];
else
sRootName = oRootNode.nodeName;
switch (sRootName.toLowerCase()) {
case "feed": //It's Atom. Create the object.
oFeed = new xparser.AtomFeed(
oRootNode,
fpCallBack,
oCallBackScope
);
break;
case "rss": //It's RSS
//Check the version.
if (parseInt(oRootNode.getAttribute("version")) < 2)
throw new Error("XParser Error! RSS feed version " +
"is not supported"
);
oFeed = new xparser.RssFeed(
oRootNode,
fpCallBack,
oCallBackScope
);
break;
default: //The feed isn't supported.
throw new Error("XParser Error: The supplied feed " +
"is currently not supported."
);
break;
}
} else { //The HTTP Status code isn't what we wanted; throw an error.
throw new Error("XParser Error: XMLHttpRequest failed. " +
"HTTP Status: " + oReq.status
);
}
}
};
oReq.open("GET", sUrl, true);
oReq.send(null);
};
xparser.FeedNode = function (oNode) {
this.value = (oNode && (oNode.text || oNode.getText())) || null;
};
xparser.BaseFeed = function (iFeedType, fpCallBack, oCallBackScope) {
this.type = iFeedType || null;
this.title = null;
this.link = null;
this.description = null;
this.copyright = null;
this.generator = null;
this.modified = null;
this.author = null;
this.items = [];
this.callBack =
(typeof fpCallBack == "function") ? fpCallBack : function () {};
this.callBackScope =
(typeof oCallBackScope == "object") ? oCallBackScope : this;
};
xparser.BaseFeed.prototype = {
parse : function (oContextNode, oElements, oNamespaces ) {
//Loop through the keys
for (var sProperty in oElements) {
//Create FeedNode objects with the node
//returned from the XPath evaluation
this[sProperty] = new xparser.FeedNode(
zXPath.selectSingleNode(
oContextNode,
oElements[sProperty],
oNamespaces
)
);
}
}
};
xparser.BaseItem = function () {
this.title = null;
this.author = null;
this.link = null;
this.description = null;
this.date = null;
};
xparser.BaseItem.prototype = {
parse : function (oContextNode, oElements, oNamespaces ) {
for (var prop in oElements) {
this[prop] = new xparser.FeedNode(zXPath.selectSingleNode(oContextNode, oElements[prop], oNamespaces));
}
}
};
xparser.RssFeed = function (oRootNode, fpCallBack, oCallBackScope) {
xparser.BaseFeed.apply(this,
[xparser.feedType.rss, fpCallBack, oCallBackScope]);
var oChannelNode = zXPath.selectSingleNode(oRootNode, "channel");
var oElements = {
title : "title",
link : "link",
description : "description",
copyright : "copyright",
generator : "generator",
modified : "lastbuilddate",
author : "managingeditor"
};
this.parse(oChannelNode, oElements, []);
var cItems = zXPath.selectNodes(oChannelNode, "item");
for (var i = 0, oItem; oItem = cItems[i]; i++) {
this.items.push(new xparser.RssItem(oItem));
}
this.callBack.apply(this.callBackScope, [this]);
};
xparser.RssFeed.prototype = new xparser.BaseFeed();
xparser.RssItem = function (oItemNode) {
xparser.BaseItem.apply(this, []);
var oElements = {
title : "title",
link : "link",
description : "description",
date : "pubdate",
author : "author"
};
this.parse(oItemNode, oElements, []);
};
xparser.RssItem.prototype = new xparser.BaseItem();
xparser.AtomFeed = function (oRootNode, fpCallBack, oCallBackScope) {
xparser.BaseFeed.apply(this,
[xparser.feedType.atom, fpCallBack, oCallBackScope]
);
var oNamespaces = {
atom : oRootNode.namespaceURI
};
var oElements = {
title: "atom:title",
link: "atom:link/@href",
description: "atom:tagline",
copyright: "atom:copyright",
generator: "atom:generator",
modified: "atom:modified",
author: "atom:author"
};
this.parse(oRootNode, oElements, oNamespaces);
var cEntries = zXPath.selectNodes(oRootNode, "atom:entry", oNamespaces);
for (var i = 0, oEntry; oEntry = cEntries[i]; i++) {
this.items.push(new xparser.AtomItem(oEntry, oNamespaces));
}
this.callBack.apply(this.callBackScope, [this]);
};
xparser.AtomFeed.prototype = new xparser.BaseFeed();
xparser.AtomItem = function (oEntryNode, oNamespaces) {
xparser.BaseItem.apply(this, []);
var oElements = {
title : "atom:title",
link : "atom:link/@href",
description : "atom:content",
date : "atom:issued",
author : "atom:author"
};
this.parse(oEntryNode, oElements, oNamespaces);
};
xparser.AtomItem.prototype = new xparser.BaseItem();