2 * Copyright (C) 2006 Andrej Kacian <andrej@kacian.sk>
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.
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.
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.
26 #include <procheader.h>
32 #include "parser_atom10.h"
36 FEED_LOC_ATOM10_ENTRY,
37 FEED_LOC_ATOM10_AUTHOR,
38 FEED_LOC_ATOM10_SOURCE,
39 FEED_LOC_ATOM10_CONTENT
40 } FeedAtom10Locations;
42 void feed_parser_atom10_start(void *data, const gchar *el, const gchar **attr)
44 FeedParserCtx *ctx = (FeedParserCtx *)data;
47 if( ctx->depth == 1 ) {
49 if( !strcmp(el, "entry") ) {
50 /* Start of new feed item found.
51 * Create a new FeedItem, freeing the one we already have, if any. */
52 if( ctx->curitem != NULL )
53 feed_item_free(ctx->curitem);
54 ctx->curitem = feed_item_new(ctx->feed);
55 ctx->location = FEED_LOC_ATOM10_ENTRY;
56 } else if( !strcmp(el, "author") ) {
57 /* Start of author info for the feed found.
58 * Set correct location. */
59 ctx->location = FEED_LOC_ATOM10_AUTHOR;
60 } else if( !strcmp(el, "link") ) {
61 if (!feed_parser_get_attribute_value(attr, "rel")) {
62 /* Link tag for the feed */
63 g_free(ctx->feed->link);
65 g_strdup(feed_parser_get_attribute_value(attr, "href"));
67 } else ctx->location = FEED_LOC_ATOM10_NONE;
69 } else if( ctx->depth == 2 ) {
71 /* Make sure we are in one of known locations within the XML structure.
72 * This condition should never be true on a valid Atom feed. */
73 if (ctx->location != FEED_LOC_ATOM10_AUTHOR &&
74 ctx->location != FEED_LOC_ATOM10_ENTRY) {
79 if( !strcmp(el, "author") ) {
80 /* Start of author info for current feed item.
81 * Set correct location. */
82 ctx->location = FEED_LOC_ATOM10_AUTHOR;
83 } else if( !strcmp(el, "link") ) {
84 /* Capture item URL, from the "url" XML attribute. */
85 if (ctx->curitem && ctx->location == FEED_LOC_ATOM10_ENTRY)
86 ctx->curitem->url = g_strdup(feed_parser_get_attribute_value(attr, "href"));
87 } else if( !strcmp(el, "source") ) {
88 ctx->location = FEED_LOC_ATOM10_SOURCE;
89 } else ctx->location = FEED_LOC_ATOM10_ENTRY;
91 if( !strcmp(el, "title") && ctx->curitem != NULL) {
92 a = feed_parser_get_attribute_value(attr, "type");
93 if( !a || !strcmp(a, "text") )
94 ctx->curitem->title_format = FEED_ITEM_TITLE_TEXT;
95 else if( !strcmp(a, "html") )
96 ctx->curitem->title_format = FEED_ITEM_TITLE_HTML;
97 else if( !strcmp(a, "xhtml") )
98 ctx->curitem->title_format = FEED_ITEM_TITLE_XHTML;
100 ctx->curitem->title_format = FEED_ITEM_TITLE_UNKNOWN;
101 } else if (!strcmp(el, "content") && ctx->curitem != NULL) {
102 ctx->location = FEED_LOC_ATOM10_CONTENT;
103 a = feed_parser_get_attribute_value(attr, "type");
104 if (a && !strcmp(a, "xhtml")) {
105 ctx->curitem->xhtml_content = TRUE;
106 ctx->xhtml_str = g_string_new(NULL);
109 } else if (ctx->depth >= 3) {
110 if (ctx->curitem->xhtml_content
111 && ctx->location == FEED_LOC_ATOM10_CONTENT) {
113 GString *txt = ctx->xhtml_str;
114 g_string_append_c(txt, '<');
115 g_string_append(txt, el);
117 for (i = 0; attr[i] != NULL && attr[i+1] != NULL; i += 2) {
118 g_string_append_printf(txt, " %s='%s'", attr[i], attr[i+1]);
120 g_string_append_c(txt, '>');
128 void feed_parser_atom10_end(void *data, const gchar *el)
130 FeedParserCtx *ctx = (FeedParserCtx *)data;
131 Feed *feed = ctx->feed;
132 gchar *text = NULL, *tmp;
134 if( ctx->str != NULL )
135 text = g_strstrip(g_strdup(ctx->str->str));
139 switch( ctx->depth ) {
147 if( !strcmp(el, "feed") ) {
148 /* We have finished parsing the feed, reverse the list
149 * so it's not upside down. */
150 feed->items = g_slist_reverse(ctx->feed->items);
157 /* decide if we just received </entry>, so we can
158 * add a complete item to feed */
159 if( !strcmp(el, "entry") ) {
161 /* Fix up URL, if it is relative */
162 if (ctx->curitem->url != NULL &&
163 !strstr(ctx->curitem->url, "://") &&
164 ctx->feed->link != NULL) {
165 tmp = g_strconcat(ctx->feed->link,
166 (ctx->curitem->url[0] == '/' ? "" : "/"),
167 ctx->curitem->url, NULL);
168 feed_item_set_url(ctx->curitem, tmp);
172 /* append the complete feed item */
173 if( ctx->curitem->id && ctx->curitem->title
174 && ctx->curitem->date_modified ) {
176 g_slist_prepend(feed->items, (gpointer)ctx->curitem);
179 /* since it's in the linked list, lose this pointer */
182 } else if( !strcmp(el, "title") ) { /* so it wasn't end of item */
184 } else if( !strcmp(el, "summary" ) ) {
185 FILL(feed->description)
186 } else if( !strcmp(el, "updated" ) ) {
187 feed->date = procheader_date_parse(NULL, text, 0);
189 /* FIXME: add more later */
195 if( ctx->curitem == NULL )
198 switch(ctx->location) {
200 /* We're in feed/entry */
201 case FEED_LOC_ATOM10_ENTRY:
202 if( !strcmp(el, "title") ) {
203 FILL(ctx->curitem->title)
204 } else if( !strcmp(el, "summary") ) {
205 FILL(ctx->curitem->summary)
206 } else if( !strcmp(el, "id") ) {
207 FILL(ctx->curitem->id)
208 feed_item_set_id_permalink(ctx->curitem, TRUE);
209 } else if( !strcmp(el, "published") ) {
210 ctx->curitem->date_published = procheader_date_parse(NULL, text, 0);
211 } else if( !strcmp(el, "updated") ) {
212 ctx->curitem->date_modified = procheader_date_parse(NULL, text, 0);
217 /* We're in feed/author or about to leave feed/entry/author */
218 case FEED_LOC_ATOM10_AUTHOR:
219 if( !strcmp(el, "author" ) ) {
220 /* We just finished parsing <author> */
221 ctx->curitem->author = g_strdup_printf("%s%s%s%s%s",
222 ctx->name ? ctx->name : "",
223 ctx->name && ctx->mail ? " <" : ctx->mail ? "<" : "",
224 ctx->mail ? ctx->mail : "",
225 ctx->mail ? ">" : "",
226 !ctx->name && !ctx->mail ? "N/A" : "");
227 ctx->location = FEED_LOC_ATOM10_ENTRY;
228 } else if( !strcmp(el, "name") ) {
234 case FEED_LOC_ATOM10_CONTENT:
235 if( !strcmp(el, "content") ) {
236 if (ctx->curitem->xhtml_content) {
237 /* Just in case the <content> tag itself also has some
238 * content of its own, not just the <div> it should,
239 * let's append it to the end. */
240 g_string_append(ctx->xhtml_str, text);
241 ctx->curitem->text = g_string_free(ctx->xhtml_str, FALSE);
242 ctx->xhtml_str = NULL;
244 FILL(ctx->curitem->text)
246 ctx->location = FEED_LOC_ATOM10_ENTRY;
255 if( ctx->curitem == NULL )
258 switch(ctx->location) {
260 /* We're in feed/entry/author */
261 case FEED_LOC_ATOM10_AUTHOR:
262 if( !strcmp(el, "name") ) {
264 } else if( !strcmp(el, "email") ) {
270 /* We're in feed/entry/source */
271 case FEED_LOC_ATOM10_SOURCE:
272 if( !strcmp(el, "title" ) ) {
273 FILL(ctx->curitem->sourcetitle)
274 } else if( !strcmp(el, "id" ) ) {
275 FILL(ctx->curitem->sourceid)
276 } else if( !strcmp(el, "updated" ) ) {
277 ctx->curitem->sourcedate = procheader_date_parse(NULL, text, 0);
282 case FEED_LOC_ATOM10_CONTENT:
283 if (ctx->curitem->xhtml_content) {
284 g_string_append(ctx->xhtml_str, text);
285 g_string_append_printf(ctx->xhtml_str, "</%s>", el);
295 if (ctx->location == FEED_LOC_ATOM10_CONTENT
296 && ctx->curitem->xhtml_content) {
297 g_string_append(ctx->xhtml_str, text);
298 g_string_append_printf(ctx->xhtml_str, "</%s>", el);
303 if( ctx->str != NULL ) {
305 g_string_free(ctx->str, TRUE);