3 * Copyright (c) 2007 Mikko Sysikaski <mikko.sysikaski@gmail.com>
4 * Toni Spets <toni.spets@gmail.com>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
20 #include <libxml/parser.h>
21 #include <libxml/tree.h>
24 #define PARSE_OPTIONS 0
27 void prss_parse_doc(PRSS *result, xmlDocPtr doc);
29 void prss_parse_data(void *result, const char *xml_data)
31 PRSS *data = (PRSS*)result;
32 xmlDocPtr doc = xmlReadMemory(xml_data, strlen(xml_data), "", NULL,
39 prss_parse_doc(data, doc);
42 void prss_free(PRSS *data)
47 xmlFreeDoc(data->_data);
52 static inline void prss_null(PRSS *p)
54 memset(p, 0, sizeof(PRSS));
56 static inline void prss_null_item(PRSS_Item *i)
58 memset(i, 0, sizeof(PRSS_Item));
61 static inline void read_item(PRSS_Item *res, xmlNodePtr data)
65 res->title = res->link = res->description = NULL;
66 for (; data; data = data->next) {
70 if (data->type != XML_ELEMENT_NODE) {
73 child = data->children;
79 name = (const char *)data->name;
80 if (!strcasecmp(name, "title")) {
81 res->title = (char *) child->content;
82 } else if (!strcasecmp(name, "link")) {
83 res->link = (char *) child->content;
84 } else if (!strcasecmp(name, "description")) {
85 res->description = (char *) child->content;
86 } else if (!strcasecmp(name, "category")) {
87 res->category = (char *) child->content;
88 } else if (!strcasecmp(name, "pubDate")) {
89 res->pubdate = (char *) child->content;
90 } else if (!strcasecmp(name, "guid")) {
91 res->guid = (char *) child->content;
95 static inline void read_element(PRSS *res, xmlNodePtr n)
100 if (n->type != XML_ELEMENT_NODE) {
109 name = (const char *)n->name;
110 if (!strcasecmp(name, "title")) {
111 res->title = (char *) child->content;
112 } else if (!strcasecmp(name, "link")) {
113 res->link = (char *) child->content;
114 } else if (!strcasecmp(name, "description")) {
115 res->description = (char *) child->content;
116 } else if (!strcasecmp(name, "language")) {
117 res->language = (char *) child->content;
118 } else if (!strcasecmp(name, "pubDate")) {
119 res->pubdate = (char *) child->content;
120 } else if (!strcasecmp(name, "lastBuildDate")) {
121 res->lastbuilddate = (char *) child->content;
122 } else if (!strcasecmp(name, "generator")) {
123 res->generator = (char *) child->content;
124 } else if (!strcasecmp(name, "docs")) {
125 res->docs = (char *) child->content;
126 } else if (!strcasecmp(name, "managingEditor")) {
127 res->managingeditor = (char *) child->content;
128 } else if (!strcasecmp(name, "webMaster")) {
129 res->webmaster = (char *) child->content;
130 } else if (!strcasecmp(name, "copyright")) {
131 res->copyright = (char *) child->content;
132 } else if (!strcasecmp(name, "ttl")) {
133 res->ttl = (char *) child->content;
134 } else if (!strcasecmp(name, "item")) {
135 read_item(&res->items[res->item_count++], n->children);
139 static inline int parse_rss_2_0(PRSS *res, xmlNodePtr root)
141 xmlNodePtr channel = root->children;
145 while (channel && (channel->type != XML_ELEMENT_NODE
146 || strcmp((const char *) channel->name, "channel"))) {
147 channel = channel->next;
153 for (n = channel->children; n; n = n->next) {
154 if (n->type == XML_ELEMENT_NODE &&
155 !strcmp((const char *) n->name, "item")) {
160 res->version = strndup("2.0", text_buffer_size);
161 if (res->items) free(res->items);
162 res->items = malloc(items * sizeof(PRSS_Item));
165 for (n = channel->children; n; n = n->next) {
166 read_element(res, n);
171 static inline int parse_rss_1_0(PRSS *res, xmlNodePtr root)
176 for (n = root->children; n; n = n->next) {
177 if (n->type == XML_ELEMENT_NODE) {
178 if (!strcmp((const char *) n->name, "item")) {
180 } else if (!strcmp((const char *) n->name, "channel")) {
183 for (i = n->children; i; i = i->next) {
184 read_element(res, i);
190 res->version = strndup("1.0", text_buffer_size);
191 if (res->items) free(res->items);
192 res->items = malloc(items * sizeof(PRSS_Item));
195 for (n = root->children; n; n = n->next) {
196 if (n->type == XML_ELEMENT_NODE &&
197 !strcmp((const char *) n->name, "item")) {
198 read_item(&res->items[res->item_count++], n->children);
204 static inline int parse_rss_0_9x(PRSS *res, xmlNodePtr root)
207 return parse_rss_2_0(res, root);
210 void prss_parse_doc(PRSS *result, xmlDocPtr doc)
212 /* FIXME: doc shouldn't be freed after failure when called explicitly from
215 xmlNodePtr root = xmlDocGetRootElement(doc);
220 if (root->type == XML_ELEMENT_NODE) {
221 if (!strcmp((const char *) root->name, "RDF")) {
223 parse_rss_1_0(result, root);
225 } else if (!strcmp((const char *) root->name, "rss")) {
226 // RSS 2.0 or <1.0 document
227 parse_rss_2_0(result, root);