forked from aessam/ikhbr-SmartArabicNewsAggregator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RDFRSSFeedParser.js
67 lines (55 loc) · 1.87 KB
/
RDFRSSFeedParser.js
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
/**
* Created by aessam on 4/13/14.
*/
var allowedItemsElements = {
"title" : "title", "link" : "link", "description" : "description", "dc:date": "date", "dc:subject" : "category", "dc:language" : "language",
"dwsyn:contentid":"id"};
var ptype = RDFRSSFeedParser.prototype;
function RDFRSSFeedParser() {
this.tree = [];
this.lastOpenedTag = "";
this.lastCreatedItem = [];
this.itemCompeletionCallBack;
}
ptype.setItemCompeletionCallBack = function (clbk){
this.itemCompeletionCallBack = clbk;
}
ptype.getParsedFeed = function() {
return this.tree;
};
ptype.getLastCreatedItem = function() {
return this.lastCreatedItem;
};
ptype.tagOpened = function(node) {
if(node.name.toLowerCase() == "item"){
this.lastCreatedItem = {};
this.tree.push(this.lastCreatedItem);
}
if(allowedItemsElements[node.name.toLowerCase()] != undefined){
this.lastOpenedTag = node.name.toLowerCase();
}
};
ptype.tagClosed = function(node) {
if(node.toLowerCase() == "item"){
if(this.itemCompeletionCallBack){
this.itemCompeletionCallBack(this.lastCreatedItem);
}
this.lastCreatedItem = [];
this.lastOpenedTag = "";
}
if(allowedItemsElements[this.lastOpenedTag] != undefined &&
this.lastOpenedTag==node.toLowerCase()){
this.lastOpenedTag = "";
}
}
ptype.addTextToLastOpenedTag = function(text) {
if(!this.lastCreatedItem[allowedItemsElements[this.lastOpenedTag]])
this.lastCreatedItem[allowedItemsElements[this.lastOpenedTag]] = "";
this.lastCreatedItem[allowedItemsElements[this.lastOpenedTag]] = this.lastCreatedItem[allowedItemsElements[this.lastOpenedTag]] + text;
};
RDFRSSFeedParser.validateAndCreate = function (initNode){
if (initNode.name.toLowerCase() == "rdf:rdf"){
return new RDFRSSFeedParser();
}
}
module.exports = RDFRSSFeedParser;