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. */
18 #include <libxml/parser.h>
19 #include <libxml/tree.h>
27 #define PARSE_OPTIONS 0
30 PRSS *prss_parse_doc(xmlDocPtr doc);
32 PRSS *prss_parse_data(const char *xml_data)
34 xmlDocPtr doc = xmlReadMemory(xml_data, strlen(xml_data), "", NULL,
41 return prss_parse_doc(doc);
44 PRSS *prss_parse_file(const char *xml_file)
46 xmlDocPtr doc = xmlReadFile(xml_file, NULL, PARSE_OPTIONS);
52 return prss_parse_doc(doc);
55 void prss_free(PRSS *data)
60 xmlFreeDoc(data->_data);
66 static inline void prss_null(PRSS *p)
68 memset(p, 0, sizeof(PRSS));
70 static inline void prss_null_item(PRSS_Item *i)
72 memset(i, 0, sizeof(PRSS_Item));
75 static inline void read_item(PRSS_Item *res, xmlNodePtr data)
79 res->title = res->link = res->description = NULL;
80 for (; data; data = data->next) {
83 if (data->type != XML_ELEMENT_NODE) {
86 xmlNodePtr child = data->children;
92 name = (const char *)data->name;
93 if (!strcasecmp(name, "title")) {
94 res->title = (char *) child->content;
95 } else if (!strcasecmp(name, "link")) {
96 res->link = (char *) child->content;
97 } else if (!strcasecmp(name, "description")) {
98 res->description = (char *) child->content;
99 } else if (!strcasecmp(name, "category")) {
100 res->category = (char *) child->content;
101 } else if (!strcasecmp(name, "pubDate")) {
102 res->pubdate = (char *) child->content;
103 } else if (!strcasecmp(name, "guid")) {
104 res->guid = (char *) child->content;
108 static inline void read_element(PRSS *res, xmlNodePtr n)
112 if (n->type != XML_ELEMENT_NODE) {
115 xmlNodePtr child = n->children;
121 name = (const char *)n->name;
122 if (!strcasecmp(name, "title")) {
123 res->title = (char *) child->content;
124 } else if (!strcasecmp(name, "link")) {
125 res->link = (char *) child->content;
126 } else if (!strcasecmp(name, "description")) {
127 res->description = (char *) child->content;
128 } else if (!strcasecmp(name, "language")) {
129 res->language = (char *) child->content;
130 } else if (!strcasecmp(name, "pubDate")) {
131 res->pubdate = (char *) child->content;
132 } else if (!strcasecmp(name, "lastBuildDate")) {
133 res->lastbuilddate = (char *) child->content;
134 } else if (!strcasecmp(name, "generator")) {
135 res->generator = (char *) child->content;
136 } else if (!strcasecmp(name, "docs")) {
137 res->docs = (char *) child->content;
138 } else if (!strcasecmp(name, "managingEditor")) {
139 res->managingeditor = (char *) child->content;
140 } else if (!strcasecmp(name, "webMaster")) {
141 res->webmaster = (char *) child->content;
142 } else if (!strcasecmp(name, "copyright")) {
143 res->copyright = (char *) child->content;
144 } else if (!strcasecmp(name, "ttl")) {
145 res->ttl = (char *) child->content;
146 } else if (!strcasecmp(name, "item")) {
147 read_item(&res->items[res->item_count++], n->children);
151 static inline int parse_rss_2_0(PRSS *res, xmlNodePtr root)
153 xmlNodePtr channel = root->children;
155 while (channel && (channel->type != XML_ELEMENT_NODE
156 || strcmp((const char *) channel->name, "channel"))) {
157 channel = channel->next;
166 for (n = channel->children; n; n = n->next) {
167 if (n->type == XML_ELEMENT_NODE &&
168 !strcmp((const char *) n->name, "item")) {
173 res->version = strdup("2.0");
174 res->items = malloc(items * sizeof(PRSS_Item));
177 for (n = channel->children; n; n = n->next) {
178 read_element(res, n);
183 static inline int parse_rss_1_0(PRSS *res, xmlNodePtr root)
188 for (n = root->children; n; n = n->next) {
189 if (n->type == XML_ELEMENT_NODE) {
190 if (!strcmp((const char *) n->name, "item")) {
192 } else if (!strcmp((const char *) n->name, "channel")) {
195 for (i = n->children; i; i = i->next) {
196 read_element(res, i);
202 res->version = strdup("1.0");
203 res->items = malloc(items * sizeof(PRSS_Item));
206 for (n = root->children; n; n = n->next) {
207 if (n->type == XML_ELEMENT_NODE &&
208 !strcmp((const char *) n->name, "item")) {
209 read_item(&res->items[res->item_count++], n->children);
215 static inline int parse_rss_0_9x(PRSS *res, xmlNodePtr root)
218 return parse_rss_2_0(res, root);
221 PRSS *prss_parse_doc(xmlDocPtr doc)
223 /* FIXME: doc shouldn't be freed after failure when called explicitly from
226 xmlNodePtr root = xmlDocGetRootElement(doc);
227 PRSS *result = malloc(sizeof(PRSS));
232 if (root->type == XML_ELEMENT_NODE) {
233 if (!strcmp((const char *) root->name, "RDF")) {
235 if (!parse_rss_1_0(result, root)) {
241 } else if (!strcmp((const char *) root->name, "rss")) {
242 // RSS 2.0 or <1.0 document
243 if (!parse_rss_2_0(result, root)) {