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