dc424c56e4c6279afe5a6f1c8a12c7dd1c4767d4
[claws.git] / src / plugins / rssyl / libfeed / parser_rdf.c
1 /*
2  * Copyright (C) 2006 Andrej Kacian <andrej@kacian.sk>
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public
15  * License along with this program; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19 #define __USE_GNU
20
21 #include <glib.h>
22 #include <expat.h>
23 #include <string.h>
24
25 #include "feed.h"
26 #include "date.h"
27 #include "parser_rdf.h"
28
29 void feed_parser_rdf_start(void *data, const gchar *el, const gchar **attr)
30 {
31         FeedParserCtx *ctx = (FeedParserCtx *)data;
32
33         if( ctx->depth == 1 ) {
34                 if( !strcmp(el, "channel") ) {
35                         ctx->location = FEED_LOC_RDF_CHANNEL;
36                 } else if( !strcmp(el, "item") ) {
37
38                         if( ctx->curitem != NULL )
39                                 feed_item_free(ctx->curitem);
40
41                         ctx->curitem = feed_item_new(ctx->feed);
42                         ctx->location = FEED_LOC_RDF_ITEM;
43
44                 } else ctx->location = 0;
45         }
46
47         ctx->depth++;
48
49 }
50
51 void feed_parser_rdf_end(void *data, const gchar *el)
52 {
53         FeedParserCtx *ctx = (FeedParserCtx *)data;
54         Feed *feed = ctx->feed;
55         gchar *text = NULL;
56
57         if( ctx->str != NULL )
58                 text = ctx->str->str;
59
60         ctx->depth--;
61         
62         switch( ctx->depth ) {
63
64                 case 0:
65
66                         if( !strcmp(el, "rdf") ) {
67                                 /* we finished parsing the feed */
68                                 ctx->feed->items = g_slist_reverse(ctx->feed->items);
69                         }
70
71                         break;
72
73                 case 1:
74
75                         /* <item></item> block just ended, so ... */
76                         if( !strcmp(el, "item") ) {
77
78                                 /* add the complete feed item to our feed struct */
79                                 ctx->feed->items = 
80                                         g_slist_prepend(ctx->feed->items, (gpointer)ctx->curitem);
81                                 
82                                 /* since it's in the linked list, lose this pointer */
83                                 ctx->curitem = NULL;
84                         }
85
86                         break;
87
88                 case 2:
89
90                         switch(ctx->location) {
91
92                                 /* We're inside introductory <channel></channel> */
93                                 case FEED_LOC_RDF_CHANNEL:
94                                         if( !strcmp(el, "title") ) {
95                                                 FILL(feed->title)
96                                         } else if( !strcmp(el, "description" ) ) {
97                                                 FILL(feed->description)
98                                         } else if( !strcmp(el, "dc:language") ) {
99                                                 FILL(feed->language)
100                                         } else if( !strcmp(el, "dc:creator") ) {
101                                                 FILL(feed->author)
102                                         } else if( !strcmp(el, "dc:date") ) {
103                                                 feed->date = parseISO8601Date(text);
104                                         } else if( !strcmp(el, "pubDate") ) {
105                                                 feed->date = parseRFC822Date(text);
106                                         }
107
108                                         break;
109
110                                 /* We're inside an <item></item> */
111                                 case FEED_LOC_RDF_ITEM:
112                                         if( ctx->curitem == NULL ) {
113                                                 break;
114                                         }
115
116                                         /* decide which field did we just get */
117                                         if( !strcmp(el, "title") ) {
118                                                 FILL(ctx->curitem->title)
119                                         } else if( !strcmp(el, "dc:creator") ) {
120                                                 FILL(ctx->curitem->author)
121                                         } else if( !strcmp(el, "description") ) {
122                                                 FILL(ctx->curitem->summary)
123                                         } else if( !strcmp(el, "content:encoded") ) {
124                                                 FILL(ctx->curitem->text)
125                                         } else if( !strcmp(el, "link") ) {
126                                                 FILL(ctx->curitem->url)
127                                         } else if( !strcmp(el, "dc:date") ) {
128                                                 ctx->curitem->date_modified = parseISO8601Date(text);
129                                         } else if( !strcmp(el, "pubDate") ) {
130                                                 ctx->curitem->date_modified = parseRFC822Date(text);
131                                         }
132
133                                         break;
134                         }
135
136                         break;
137
138         }
139
140         if( ctx->str != NULL ) {
141                 g_string_free(ctx->str, TRUE);
142                 ctx->str = NULL;
143         }
144 }