root/trunk/as2/com/jeroenwijering/feeds/ATOMParser.as @ 1

Revision 1, 2.7 kB (checked in by jeroen, 18 months ago)

initial commit of old repository into public one

  • Property svn:executable set to *
Line 
1/**
2* Parses ATOM feeds and returns an indexed array with all elements
3*
4* @author       Jeroen Wijering
5* @version      1.4
6**/
7
8
9import com.jeroenwijering.feeds.AbstractParser;
10import com.jeroenwijering.utils.StringMagic;
11
12
13class com.jeroenwijering.feeds.ATOMParser extends AbstractParser {
14
15
16        /** Contructor **/
17        function ATOMParser() { super(); };
18
19
20        /** build an array with all regular elements **/
21        private function setElements() {
22                elements = new Object();
23                elements["title"] = "title";
24                elements["id"] = "id";
25        };
26
27
28        /** Convert ATOM structure to array **/
29        private function parse(xml:XML):Array {
30                var arr = new Array();
31                var tpl = xml.firstChild.firstChild;
32                var ttl;
33                while(tpl != null) {
34                        if (tpl.nodeName.toLowerCase() == "entry") {
35                                var obj = new Object();
36                                for(var j=0; j<tpl.childNodes.length; j++) {
37                                        var nod:XMLNode = tpl.childNodes[j];
38                                        var nnm = nod.nodeName.toLowerCase();
39                                        if(elements[nnm] != undefined) {
40                                                obj[elements[nnm]]=nod.firstChild.nodeValue;
41                                        } else if(nnm=="link" && nod.attributes.rel=="alternate"){
42                                                obj["link"] =  nod.attributes.href;
43                                        } else if(nnm == "summary") {
44                                                obj["description"] = StringMagic.stripTagsBreaks(
45                                                        nod.firstChild.nodeValue);
46                                        } else if(nnm == "published") {
47                                                obj["date"] = iso2Date(nod.firstChild.nodeValue);
48                                        } else if(nnm == "updated") {
49                                                obj["date"] = iso2Date(nod.firstChild.nodeValue);
50                                        } else if(nnm == "modified") {
51                                                obj["date"] = iso2Date(nod.firstChild.nodeValue);
52                                        } else if(nnm == "category") {
53                                                obj["category"] = nod.attributes.term;
54                                        } else if(nnm == "author") {
55                                                for(var k=0; k< nod.childNodes.length; k++) {
56                                                        if(nod.childNodes[k].nodeName == "name") {
57                                                                obj["author"] =
58                                                                        nod.childNodes[k].firstChild.nodeValue;
59                                                        }
60                                                }
61                                        } else if(nnm=="link" && nod.attributes.rel=="enclosure"){
62                                                var typ = nod.attributes.type.toLowerCase();
63                                                if(mimetypes[typ] != undefined){
64                                                        obj["file"] = nod.attributes.href;
65                                                        obj["type"] = mimetypes[typ];
66                                                        if(obj["file"].substr(0,4) == "rtmp") {
67                                                                obj["type"] = "rtmp";
68                                                        }
69                                                } else if(obj["type"] != undefined && typ == "video/x-flv") {
70                                                        obj["fallback"] = nod.attributes.href;
71                                                }
72                                        } else if (nnm=="link" && nod.attributes.rel=="captions"){
73                                                obj["captions"] = nod.attributes.href;
74                                        } else if (nnm=="link" && nod.attributes.rel=="audio"){
75                                                obj["audio"] = nod.attributes.href;
76                                        } else if (nnm=="link" && nod.attributes.rel=="image"){
77                                                obj["image"] = nod.attributes.href;
78                                        }
79                                }
80                                obj["author"] == undefined ? obj["author"] = ttl: null;
81                                arr.push(obj);
82                        } else if (tpl.nodeName == "title") {
83                                ttl = tpl.firstChild.nodeValue;
84                        }
85                        tpl = tpl.nextSibling;
86                }
87                return arr;
88        };
89
90
91}
Note: See TracBrowser for help on using the browser.