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.
21 #include <curl/curl.h>
31 #include "parser_opml.h"
33 static void _opml_parser_start(void *data, const gchar *el, const gchar **attr)
35 OPMLProcessCtx *ctx = (OPMLProcessCtx *)data;
36 gchar *title = NULL, *type = NULL, *url = NULL, *tmp = NULL;
38 if( ctx->body_reached ) {
39 if( ctx->depth >= 2 && !strcmp(el, "outline") ) {
40 title = feed_parser_get_attribute_value(attr, "title");
41 type = feed_parser_get_attribute_value(attr, "type");
42 if( type != NULL && strcmp(type, "folder") ) {
43 url = feed_parser_get_attribute_value(attr, "xmlUrl");
46 if( !strncmp(url, "feed://", 7) )
47 tmp = g_strdup(url+7);
48 else if( !strncmp(url, "feed:", 5) )
49 tmp = g_strdup(url+5);
58 if( ctx->user_function != NULL ) {
59 ctx->user_function(title, url, ctx->depth, ctx->user_data);
64 if( ctx->depth == 1 ) {
65 if( !strcmp(el, "body") ) {
66 ctx->body_reached = TRUE;
73 static void _opml_parser_end(void *data, const gchar *el)
75 OPMLProcessCtx *ctx = (OPMLProcessCtx *)data;
80 void opml_process(gchar *path, OPMLProcessFunc function, gpointer data)
82 OPMLProcessCtx *ctx = NULL;
83 gchar *contents = NULL;
87 /* Initialize our context */
88 ctx = malloc( sizeof(OPMLProcessCtx) );
89 ctx->parser = XML_ParserCreate(NULL);
92 ctx->user_function = function;
93 ctx->body_reached = FALSE;
94 ctx->user_data = data;
96 /* Set expat parser handlers */
97 XML_SetUserData(ctx->parser, (void *)ctx);
98 XML_SetElementHandler(ctx->parser,
101 XML_SetCharacterDataHandler(ctx->parser, libfeed_expat_chparse);
102 XML_SetUnknownEncodingHandler(ctx->parser,
103 feed_parser_unknown_encoding_handler, NULL);
105 g_file_get_contents(path, &contents, NULL, &error);
107 if( error || !contents )
111 lines = g_strsplit(contents, '\n', 0);
114 status = XML_Parse(ctx->parser, lines[i], strlen(lines[i]), FALSE);
115 if( status == XML_STATUS_ERROR ) {
116 err = XML_GetErrorCode(ctx->parser);
117 sprintf(stderr, "\nExpat: --- %s\n\n", XML_ErrorString(err));
122 status = XML_Parse(ctx->parser, contents, strlen(contents), FALSE);
123 err = XML_GetErrorCode(ctx->parser);
124 fprintf(stderr, "\nExpat: --- %s (%s)\n\n", XML_ErrorString(err),
125 (status == XML_STATUS_OK ? "OK" : "NOT OK"));
127 XML_Parse(ctx->parser, "", 0, TRUE);
129 XML_ParserFree(ctx->parser);