RSSyl: Strip leading and trailing whitespace from XML tag contents.
[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 <procheader.h>
26
27 #include "feed.h"
28 #include "date.h"
29 #include "parser_rdf.h"
30
31 void feed_parser_rdf_start(void *data, const gchar *el, const gchar **attr)
32 {
33         FeedParserCtx *ctx = (FeedParserCtx *)data;
34
35         if( ctx->depth == 1 ) {
36                 if( !strcmp(el, "channel") ) {
37                         ctx->location = FEED_LOC_RDF_CHANNEL;
38                 } else if( !strcmp(el, "item") ) {
39
40                         if( ctx->curitem != NULL )
41                                 feed_item_free(ctx->curitem);
42
43                         ctx->curitem = feed_item_new(ctx->feed);
44                         ctx->location = FEED_LOC_RDF_ITEM;
45
46                 } else ctx->location = 0;
47         }
48
49         ctx->depth++;
50
51 }
52
53 void feed_parser_rdf_end(void *data, const gchar *el)
54 {
55         FeedParserCtx *ctx = (FeedParserCtx *)data;
56         Feed *feed = ctx->feed;
57         gchar *text = NULL;
58
59         if( ctx->str != NULL )
60                 text = g_strstrip(g_strdup(ctx->str->str));
61         else
62                 text = "";
63
64         ctx->depth--;
65         
66         switch( ctx->depth ) {
67
68                 case 0:
69
70                         if( !strcmp(el, "rdf") ) {
71                                 /* we finished parsing the feed */
72                                 ctx->feed->items = g_slist_reverse(ctx->feed->items);
73                         }
74
75                         break;
76
77                 case 1:
78
79                         /* <item></item> block just ended, so ... */
80                         if( !strcmp(el, "item") ) {
81
82                                 /* add the complete feed item to our feed struct */
83                                 ctx->feed->items = 
84                                         g_slist_prepend(ctx->feed->items, (gpointer)ctx->curitem);
85                                 
86                                 /* since it's in the linked list, lose this pointer */
87                                 ctx->curitem = NULL;
88                         }
89
90                         break;
91
92                 case 2:
93
94                         switch(ctx->location) {
95
96                                 /* We're inside introductory <channel></channel> */
97                                 case FEED_LOC_RDF_CHANNEL:
98                                         if( !strcmp(el, "title") ) {
99                                                 FILL(feed->title)
100                                         } else if( !strcmp(el, "description" ) ) {
101                                                 FILL(feed->description)
102                                         } else if( !strcmp(el, "dc:language") ) {
103                                                 FILL(feed->language)
104                                         } else if( !strcmp(el, "dc:creator") ) {
105                                                 FILL(feed->author)
106                                         } else if( !strcmp(el, "dc:date") ) {
107                                                 feed->date = parseISO8601Date(text);
108                                         } else if( !strcmp(el, "pubDate") ) {
109                                                 feed->date = procheader_date_parse(NULL, text, 0);
110                                         }
111
112                                         break;
113
114                                 /* We're inside an <item></item> */
115                                 case FEED_LOC_RDF_ITEM:
116                                         if( ctx->curitem == NULL ) {
117                                                 break;
118                                         }
119
120                                         /* decide which field did we just get */
121                                         if( !strcmp(el, "title") ) {
122                                                 FILL(ctx->curitem->title)
123                                         } else if( !strcmp(el, "dc:creator") ) {
124                                                 FILL(ctx->curitem->author)
125                                         } else if( !strcmp(el, "description") ) {
126                                                 FILL(ctx->curitem->summary)
127                                         } else if( !strcmp(el, "content:encoded") ) {
128                                                 FILL(ctx->curitem->text)
129                                         } else if( !strcmp(el, "link") ) {
130                                                 FILL(ctx->curitem->url)
131                                         } else if( !strcmp(el, "dc:date") ) {
132                                                 ctx->curitem->date_modified = parseISO8601Date(text);
133                                         } else if( !strcmp(el, "pubDate") ) {
134                                                 ctx->curitem->date_modified = procheader_date_parse(NULL, text, 0);
135                                         }
136
137                                         break;
138                         }
139
140                         break;
141
142         }
143
144         if( ctx->str != NULL ) {
145                 g_free(text);
146                 g_string_free(ctx->str, TRUE);
147                 ctx->str = NULL;
148         }
149 }