59fba1c5c364780db1850ab7b53acc7234f6b1f6
[claws.git] / src / plugins / rssyl / libfeed / parser_atom10.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 #include <stdio.h>
25
26 #include "feed.h"
27 #include "feeditem.h"
28 #include "date.h"
29 #include "parser.h"
30 #include "parser_atom10.h"
31
32 void feed_parser_atom10_start(void *data, const gchar *el, const gchar **attr)
33 {
34         FeedParserCtx *ctx = (FeedParserCtx *)data;
35         gchar *a = NULL;
36
37         if( ctx->depth == 1 ) {
38
39                 if( !strcmp(el, "entry") ) {
40                         /* Start of new feed item found.
41                          * Create a new FeedItem, freeing the one we already have, if any. */
42                         if( ctx->curitem != NULL )
43                                 feed_item_free(ctx->curitem);
44                         ctx->curitem = feed_item_new(ctx->feed);
45                         ctx->location = FEED_LOC_ATOM10_ENTRY;
46                 } else if( !strcmp(el, "author") ) {
47                         /* Start of author info for the feed found.
48                          * Set correct location. */
49                         ctx->location = FEED_LOC_ATOM10_AUTHOR;
50                 } else ctx->location = FEED_LOC_ATOM10_NONE;
51
52         } else if( ctx->depth == 2 ) {
53
54                 if( !strcmp(el, "author") ) {
55                         /* Start of author info for current feed item.
56                          * Set correct location. */
57                         ctx->location = FEED_LOC_ATOM10_AUTHOR;
58                 } else if( !strcmp(el, "link") ) {
59                         /* Capture item URL, from the "url" XML attribute. */
60                         if (ctx->curitem && ctx->location == FEED_LOC_ATOM10_ENTRY)
61                   ctx->curitem->url = g_strdup(feed_parser_get_attribute_value(attr, "href"));
62                 } else if( !strcmp(el, "source") ) {
63                         ctx->location = FEED_LOC_ATOM10_SOURCE;
64                 } else ctx->location = FEED_LOC_ATOM10_ENTRY;
65
66                 if( !strcmp(el, "title") ) {
67                         a = feed_parser_get_attribute_value(attr, "type");
68                         if( !a || !strcmp(a, "text") )
69                                 ctx->curitem->title_format = FEED_ITEM_TITLE_TEXT;
70                         else if( !strcmp(a, "html") )
71                                 ctx->curitem->title_format = FEED_ITEM_TITLE_HTML;
72                         else if( !strcmp(a, "xhtml") )
73                                 ctx->curitem->title_format = FEED_ITEM_TITLE_XHTML;
74                         else
75                                 ctx->curitem->title_format = FEED_ITEM_TITLE_UNKNOWN;
76                 } else if (!strcmp(el, "content") ) {
77                         a = feed_parser_get_attribute_value(attr, "type");
78                         if (a && !strcmp(a, "xhtml")) {
79                                 ctx->curitem->xhtml_content = TRUE;
80                                 ctx->location = FEED_LOC_ATOM10_CONTENT;
81                         }
82                 }
83         }
84
85         ctx->depth++;
86 }
87
88 void feed_parser_atom10_end(void *data, const gchar *el)
89 {
90         FeedParserCtx *ctx = (FeedParserCtx *)data;
91         Feed *feed = ctx->feed;
92         gchar *text = NULL;
93
94         if( ctx->str != NULL )
95                 text = ctx->str->str;
96
97         ctx->depth--;
98
99         switch( ctx->depth ) {
100
101                 case 0:
102
103                         if( !strcmp(el, "feed") ) {
104                                 /* We have finished parsing the feed, reverse the list
105                                  * so it's not upside down. */
106                                 feed->items = g_slist_reverse(ctx->feed->items);
107                         }
108
109                         break;
110
111                 case 1:
112
113                         /* decide if we just received </entry>, so we can
114                          * add a complete item to feed */
115                         if( !strcmp(el, "entry") ) {
116
117                                 /* append the complete feed item */
118                                 if( ctx->curitem->id && ctx->curitem->title
119                                                 && ctx->curitem->date_modified ) {
120                                         feed->items = 
121                                                 g_slist_prepend(feed->items, (gpointer)ctx->curitem);
122                                 }
123                                 
124                                 /* since it's in the linked list, lose this pointer */
125                                 ctx->curitem = NULL;
126
127                         } else if( !strcmp(el, "title") ) {     /* so it wasn't end of item */
128                                 FILL(feed->title)
129                         } else if( !strcmp(el, "summary" ) ) {
130                                 FILL(feed->description)
131                         } else if( !strcmp(el, "updated" ) ) {
132                                 feed->date = parseISO8601Date(text);
133                         }
134                         /* FIXME: add more later */
135
136                         break;
137
138                 case 2:
139
140                         if( ctx->curitem == NULL )
141                                 break;
142
143                         switch(ctx->location) {
144
145                                 /* We're in feed/entry */
146                                 case FEED_LOC_ATOM10_ENTRY:
147                                         if( !strcmp(el, "title") ) {
148                                                 FILL(ctx->curitem->title)
149                                         } else if( !strcmp(el, "summary") ) {
150                                                 FILL(ctx->curitem->summary)
151                                         } else if( !strcmp(el, "content") ) {
152                                                 if (!ctx->curitem->xhtml_content)
153                                                         FILL(ctx->curitem->text);
154                                         } else if( !strcmp(el, "id") ) {
155                                                 FILL(ctx->curitem->id);
156                                                 feed_item_set_id_permalink(ctx->curitem, TRUE);
157                                         } else if( !strcmp(el, "published") ) {
158                                                 ctx->curitem->date_published = parseISO8601Date(text);
159                                         } else if( !strcmp(el, "updated") ) {
160                                                 ctx->curitem->date_modified = parseISO8601Date(text);
161                                         }
162
163                                         break;
164
165                                 /* We're in feed/author or about to leave feed/entry/author */
166                                 case FEED_LOC_ATOM10_AUTHOR:
167                                         if( !strcmp(el, "author" ) ) {
168                                                 /* We just finished parsing <author> */
169                                                 ctx->curitem->author = g_strdup_printf("%s%s%s%s%s",
170                                                                 ctx->name ? ctx->name : "",
171                                                                 ctx->name && ctx->mail ? " <" : ctx->mail ? "<" : "",
172                                                                 ctx->mail ? ctx->mail : "",
173                                                                 ctx->mail ? ">" : "",
174                                                                 !ctx->name && !ctx->mail ? "N/A" : "");
175                                                 ctx->location = FEED_LOC_ATOM10_ENTRY;
176                                         } else if( !strcmp(el, "name") ) {
177                                                 FILL(feed->author);
178                                         }
179
180                                         break;
181                         }
182
183                         break;
184
185                 case 3:
186
187                         if( ctx->curitem == NULL )
188                                 break;
189
190                         switch(ctx->location) {
191
192                                 /* We're in feed/entry/author */
193                                 case FEED_LOC_ATOM10_AUTHOR:
194                                         if( !strcmp(el, "name") ) {
195                                                 FILL(ctx->name);
196                                         } else if( !strcmp(el, "email") ) {
197                                                 FILL(ctx->mail);
198                                         }
199
200                                         break;
201
202                                 /* We're in feed/entry/source */
203                                 case FEED_LOC_ATOM10_SOURCE:
204                                         if( !strcmp(el, "title" ) ) {
205                                                 FILL(ctx->curitem->sourcetitle);
206                                         } else if( !strcmp(el, "id" ) ) {
207                                                 FILL(ctx->curitem->sourceid);
208                                         } else if( !strcmp(el, "updated" ) ) {
209                                                 ctx->curitem->sourcedate = parseISO8601Date(text);
210                                         }
211
212                                         break;
213
214                                 case FEED_LOC_ATOM10_CONTENT:
215                                         if (!strcmp(el, "div") && ctx->curitem->xhtml_content)
216                                                 FILL(ctx->curitem->text);
217                                         break;
218
219                                 }
220
221
222                         break;
223         }
224
225         if( ctx->str != NULL ) {
226                 g_string_free(ctx->str, TRUE);
227                 ctx->str = NULL;
228         }
229         ctx->str = NULL;
230 }