static void __mrss_parser_atom_category (nxml_data_t * cur, mrss_category_t ** category) { char *c; mrss_category_t *cat; if (!(cat = calloc (1, sizeof (mrss_category_t)))) return; if (!(c = nxmle_find_attribute (cur, "term", NULL))) { free (cat); return; } cat->element = MRSS_ELEMENT_CATEGORY; cat->allocated = 1; cat->category = c; if ((c = nxmle_find_attribute (cur, "scheme", NULL))) cat->domain = c; if ((c = nxmle_find_attribute (cur, "label", NULL))) cat->label = c; if (!*category) *category = cat; else { mrss_category_t *tmp; tmp = *category; while (tmp->next) tmp = tmp->next; tmp->next = cat; } }
static mrss_error_t __mrss_parser (nxml_t * doc, mrss_t ** ret) { mrss_error_t r = MRSS_ERR_VERSION; nxml_data_t *cur; char *c; if (!(cur = nxmle_root_element (doc, NULL))) return MRSS_ERR_PARSER; if (!strcmp (cur->value, "rss")) { if ((c = nxmle_find_attribute (cur, "version", NULL))) { /* 0.91 VERSION */ if (!strcmp (c, "0.91")) r = __mrss_parser_rss (MRSS_VERSION_0_91, doc, cur->children, ret); /* 0.92 VERSION */ else if (!strcmp (c, "0.92")) r = __mrss_parser_rss (MRSS_VERSION_0_92, doc, cur->children, ret); /* 2.0 VERSION */ else if (!strcmp (c, "2.0")) r = __mrss_parser_rss (MRSS_VERSION_2_0, doc, cur->children, ret); else r = MRSS_ERR_VERSION; free (c); } else r = MRSS_ERR_VERSION; } else if (!strcmp (cur->value, "RDF")) r = __mrss_parser_rss (MRSS_VERSION_1_0, doc, cur->children, ret); else if (!strcmp (cur->value, "feed")) r = __mrss_parser_atom (doc, cur, ret); else r = MRSS_ERR_PARSER; return r; }
static mrss_error_t __mrss_parser_rss (mrss_version_t v, nxml_t * doc, nxml_data_t * cur, mrss_t ** ret) { mrss_t *data; char *c, *attr; if (!(data = (mrss_t *) malloc (sizeof (mrss_t)))) return MRSS_ERR_POSIX; memset (data, 0, sizeof (mrss_t)); data->element = MRSS_ELEMENT_CHANNEL; data->allocated = 1; data->version = v; if (doc->encoding && !(data->encoding = strdup (doc->encoding))) { mrss_free (data); return MRSS_ERR_POSIX; } if (data->version == MRSS_VERSION_1_0) { nxml_data_t *cur_channel = NULL; while (cur) { if (!strcmp (cur->value, "channel")) cur_channel = cur; else if (!strcmp (cur->value, "image")) __mrss_parser_rss_image (doc, cur, data); else if (!strcmp (cur->value, "textinput")) __mrss_parser_rss_textinput (doc, cur, data); else if (!strcmp (cur->value, "item")) __mrss_parser_rss_item (doc, cur, data); cur = cur->next; } cur = cur_channel; } else { while (cur && strcmp (cur->value, "channel")) cur = cur->next; } if (!cur) { mrss_free (data); return MRSS_ERR_PARSER; } if (data->version == MRSS_VERSION_1_0) { if ((attr = nxmle_find_attribute (cur, "about", NULL))) data->about = attr; } for (cur = cur->children; cur; cur = cur->next) { if (cur->type == NXML_TYPE_ELEMENT) { /* title */ if (!strcmp (cur->value, "title") && !data->title && (c = nxmle_get_string (cur, NULL))) data->title = c; /* description */ else if (!strcmp (cur->value, "description") && !data->description && (c = nxmle_get_string (cur, NULL))) data->description = c; /* link */ else if (!strcmp (cur->value, "link") && !data->link && (c = nxmle_get_string (cur, NULL))) data->link = c; /* language */ else if (!strcmp (cur->value, "language") && !data->language && (c = nxmle_get_string (cur, NULL))) data->language = c; /* rating */ else if (!strcmp (cur->value, "rating") && !data->rating && (c = nxmle_get_string (cur, NULL))) data->rating = c; /* copyright */ else if (!strcmp (cur->value, "copyright") && !data->copyright && (c = nxmle_get_string (cur, NULL))) data->copyright = c; /* pubDate */ else if (!strcmp (cur->value, "pubDate") && !data->pubDate && (c = nxmle_get_string (cur, NULL))) data->pubDate = c; /* lastBuildDate */ else if (!strcmp (cur->value, "lastBuildDate") && !data->lastBuildDate && (c = nxmle_get_string (cur, NULL))) data->lastBuildDate = c; /* docs */ else if (!strcmp (cur->value, "docs") && !data->docs && (c = nxmle_get_string (cur, NULL))) data->docs = c; /* managingeditor */ else if (!strcmp (cur->value, "managingeditor") && !data->managingeditor && (c = nxmle_get_string (cur, NULL))) data->managingeditor = c; /* webMaster */ else if (!strcmp (cur->value, "webMaster") && !data->webMaster && (c = nxmle_get_string (cur, NULL))) data->webMaster = c; /* image */ else if (!strcmp (cur->value, "image")) __mrss_parser_rss_image (doc, cur, data); /* textinput */ else if (!strcmp (cur->value, "textinput")) __mrss_parser_rss_textinput (doc, cur, data); /* skipHours */ else if (!strcmp (cur->value, "skipHours")) __mrss_parser_rss_skipHours (doc, cur, data); /* skipDays */ else if (!strcmp (cur->value, "skipDays")) __mrss_parser_rss_skipDays (doc, cur, data); /* item */ else if (!strcmp (cur->value, "item")) __mrss_parser_rss_item (doc, cur, data); /* category */ else if (!strcmp (cur->value, "category") && (c = nxmle_get_string (cur, NULL))) { mrss_category_t *category; if (! (category = (mrss_category_t *) malloc (sizeof (mrss_category_t)))) { mrss_free ((mrss_generic_t *) data); free (c); return MRSS_ERR_POSIX; } memset (category, 0, sizeof (mrss_category_t)); category->element = MRSS_ELEMENT_CATEGORY; category->allocated = 1; category->category = c; if ((attr = nxmle_find_attribute (cur, "domain", NULL))) category->domain = attr; if (!data->category) data->category = category; else { mrss_category_t *tmp; tmp = data->category; while (tmp->next) tmp = tmp->next; tmp->next = category; } } /* enclosure */ else if (!strcmp (cur->value, "cloud") && !data->cloud) { data->cloud = nxmle_get_string (cur, NULL); if (!data->cloud_domain && (attr = nxmle_find_attribute (cur, "domain", NULL))) data->cloud_domain = attr; if (!data->cloud_port && (attr = nxmle_find_attribute (cur, "port", NULL))) data->cloud_port = atoi (attr); if (!data->cloud_registerProcedure && (attr = nxmle_find_attribute (cur, "registerProcedure", NULL))) data->cloud_registerProcedure = attr; if (!data->cloud_protocol && (attr = nxmle_find_attribute (cur, "protocol", NULL))) data->cloud_protocol = attr; } /* generator */ else if (!strcmp (cur->value, "generator") && !data->generator && (c = nxmle_get_string (cur, NULL))) data->generator = c; /* ttl */ else if (!strcmp (cur->value, "ttl") && !data->ttl && (c = nxmle_get_string (cur, NULL))) { data->ttl = atoi (c); free (c); } } } *ret = data; return MRSS_OK; }
static void __mrss_parser_rss_item (nxml_t * doc, nxml_data_t * cur, mrss_t * data) { char *c; char *attr; mrss_item_t *item; if (!(item = (mrss_item_t *) malloc (sizeof (mrss_item_t)))) return; memset (item, 0, sizeof (mrss_item_t)); item->element = MRSS_ELEMENT_ITEM; item->allocated = 1; for (cur = cur->children; cur; cur = cur->next) { if (cur->type == NXML_TYPE_ELEMENT) { /* title */ if (!strcmp (cur->value, "title") && !item->title && (c = nxmle_get_string (cur, NULL))) item->title = c; /* link */ else if (!strcmp (cur->value, "link") && !item->link && (c = nxmle_get_string (cur, NULL))) item->link = c; /* description */ else if (!strcmp (cur->value, "description") && !item->description && (c = nxmle_get_string (cur, NULL))) item->description = c; /* source */ else if (!strcmp (cur->value, "source") && !item->source) { item->source = nxmle_get_string (cur, NULL); if ((attr = nxmle_find_attribute (cur, "url", NULL))) item->source_url = attr; } /* enclosure */ else if (!strcmp (cur->value, "enclosure") && !item->enclosure) { item->enclosure = nxmle_get_string (cur, NULL); if ((attr = nxmle_find_attribute (cur, "url", NULL))) item->enclosure_url = attr; if ((attr = nxmle_find_attribute (cur, "length", NULL))) { item->enclosure_length = atoi (attr); free (attr); } if ((attr = nxmle_find_attribute (cur, "type", NULL))) item->enclosure_type = attr; } /* category */ else if (!strcmp (cur->value, "category") && (c = nxmle_get_string (cur, NULL))) { mrss_category_t *category; if (! (category = (mrss_category_t *) malloc (sizeof (mrss_category_t)))) { free (c); return; } memset (category, 0, sizeof (mrss_category_t)); category->element = MRSS_ELEMENT_CATEGORY; category->allocated = 1; category->category = c; if ((attr = nxmle_find_attribute (cur, "domain", NULL))) category->domain = attr; if (!item->category) item->category = category; else { mrss_category_t *tmp; tmp = item->category; while (tmp->next) tmp = tmp->next; tmp->next = category; } } /* author */ else if (!strcmp (cur->value, "author") && !item->author && (c = nxmle_get_string (cur, NULL))) item->author = c; /* comments */ else if (!strcmp (cur->value, "comments") && !item->comments && (c = nxmle_get_string (cur, NULL))) item->comments = c; /* guid */ else if (!strcmp (cur->value, "guid") && !item->guid && (c = nxmle_get_string (cur, NULL))) { item->guid = c; if ((attr = nxmle_find_attribute (cur, "isPermaLink", NULL))) { if (!strcmp (attr, "false")) item->guid_isPermaLink = 0; else item->guid_isPermaLink = 1; free (attr); } } /* pubDate */ else if (!strcmp (cur->value, "pubDate") && !item->pubDate && (c = nxmle_get_string (cur, NULL))) item->pubDate = c; } } if (!data->item) data->item = item; else { mrss_item_t *tmp; tmp = data->item; while (tmp->next) tmp = tmp->next; tmp->next = item; } }
static mrss_error_t __mrss_parser_atom (nxml_t * doc, nxml_data_t * cur, mrss_t ** ret) { mrss_t *data; char *c = NULL; if (!(data = malloc (sizeof (mrss_t)))) return MRSS_ERR_POSIX; memset (data, 0, sizeof (mrss_t)); data->element = MRSS_ELEMENT_CHANNEL; data->allocated = 1; data->version = MRSS_VERSION_ATOM_1_0; if (doc->encoding && !(data->encoding = strdup (doc->encoding))) { mrss_free (data); return MRSS_ERR_POSIX; } if (!data->language && (c = nxmle_find_attribute (cur, "xml:lang", NULL))) data->language = c; if ((c = nxmle_find_attribute (cur, "version", NULL))) { if (!strcmp (c, "0.3")) data->version = MRSS_VERSION_ATOM_0_3; free (c); } for (cur = cur->children; cur; cur = cur->next) { if (cur->type == NXML_TYPE_ELEMENT) { /* title -> title */ if (!strcmp (cur->value, "title") && !data->title && (c = nxmle_get_string (cur, NULL))) data->title = c; /* subtitle -> description */ else if (!strcmp (cur->value, "subtitle") && data->version == MRSS_VERSION_ATOM_1_0 && !data->description && (c = nxmle_get_string (cur, NULL))) data->description = c; /* tagline -> description (Atom 0.3) */ else if (!strcmp (cur->value, "tagline") && data->version == MRSS_VERSION_ATOM_0_3 && !data->description && (c = nxmle_get_string (cur, NULL))) data->description = c; /* link href -> link */ else if (!strcmp (cur->value, "link") && !data->link && (c = nxmle_find_attribute (cur, "href", NULL))) data->link = c; /* id -> id */ else if (!strcmp (cur->value, "id") && !data->id && (c = nxmle_get_string (cur, NULL))) data->id = c; /* rights -> copyright */ else if (!strcmp (cur->value, "rights") && !data->copyright && (c = nxmle_get_string (cur, NULL))) data->copyright = c; /* updated -> lastBuildDate */ else if (!strcmp (cur->value, "updated") && (c = nxmle_get_string (cur, NULL))) { data->lastBuildDate = __mrss_atom_prepare_date (data, c); free (c); } /* author -> managingeditor */ else if (!strcmp (cur->value, "author")) __mrss_parser_atom_author (cur, &data->managingeditor, &data->managingeditor_email, &data->managingeditor_uri); /* contributor */ else if (!strcmp (cur->value, "contributor")) __mrss_parser_atom_author (cur, &data->contributor, &data->contributor_email, &data->contributor_uri); /* generator -> generator */ else if (!strcmp (cur->value, "generator") && !data->generator && (c = nxmle_get_string (cur, NULL))) { char *attr; data->generator = c; if ((attr = nxmle_find_attribute (cur, "uri", NULL))) data->generator_uri = attr; if ((attr = nxmle_find_attribute (cur, "version", NULL))) data->generator_version = attr; } /* icon -> image_url */ else if (!strcmp (cur->value, "icon") && !data->image_url && (c = nxmle_get_string (cur, NULL))) data->image_url = c; /* logo -> image_logo */ else if (!strcmp (cur->value, "logo") && !data->image_logo && (c = nxmle_get_string (cur, NULL))) data->image_logo = c; /* category */ else if (!strcmp (cur->value, "category")) __mrss_parser_atom_category (cur, &data->category); /* entry -> item */ else if (!strcmp (cur->value, "entry")) __mrss_parser_atom_entry (doc, cur, data); else { mrss_tag_t *tag; if ((tag = __mrss_parse_tag (doc, cur))) __mrss_parse_tag_insert (&data->other_tags, tag); } } } *ret = data; return MRSS_OK; }
static void __mrss_parser_atom_entry (nxml_t * doc, nxml_data_t * cur, mrss_t * data) { char *c; mrss_item_t *item; if (!(item = malloc (sizeof (mrss_item_t)))) return; memset (item, 0, sizeof (mrss_item_t)); item->element = MRSS_ELEMENT_ITEM; item->allocated = 1; for (cur = cur->children; cur; cur = cur->next) { if (cur->type == NXML_TYPE_ELEMENT) { /* title -> title */ if (!item->title && !strcmp (cur->value, "title") && (c = nxmle_get_string (cur, NULL))) item->title = c; /* link href -> link */ else if (!item->link && !strcmp (cur->value, "link") && (c = nxmle_find_attribute (cur, "href", NULL))) item->link = c; /* content -> description */ else if (!item->description && !strcmp (cur->value, "content") && (c = nxmle_get_string (cur, NULL))) item->description = c; /* summary -> description */ else if (!item->description && !strcmp (cur->value, "summary") && (c = nxmle_get_string (cur, NULL))) item->description = c; /* right -> copyright */ else if (!item->copyright && !strcmp (cur->value, "rights") && (c = nxmle_get_string (cur, NULL))) item->copyright = c; /* author structure -> author elements */ else if (!strcmp (cur->value, "author")) __mrss_parser_atom_author (cur, &item->author, &item->author_email, &item->author_uri); /* contributor structure -> contributor elements */ else if (!strcmp (cur->value, "contributor")) __mrss_parser_atom_author (cur, &item->contributor, &item->contributor_email, &item->contributor_uri); /* published -> pubDate */ else if (!item->pubDate && !strcmp (cur->value, "published") && data->version == MRSS_VERSION_ATOM_1_0 && (c = nxmle_get_string (cur, NULL))) { item->pubDate = __mrss_atom_prepare_date (data, c); free (c); } else if (!item->pubDate && !strcmp (cur->value, "updated") && data->version == MRSS_VERSION_ATOM_1_0 && (c = nxmle_get_string (cur, NULL))) { item->pubDate = __mrss_atom_prepare_date (data, c); free (c); } /* issued -> pubDate (Atom 0.3) */ else if (!item->pubDate && !strcmp (cur->value, "issued") && (c = nxmle_get_string (cur, NULL))) { item->pubDate = __mrss_atom_prepare_date (data, c); free (c); } /* id -> guid */ else if (!item->guid && !strcmp (cur->value, "id") && (c = nxmle_get_string (cur, NULL))) item->guid = c; /* categories */ else if (!strcmp (cur->value, "category")) __mrss_parser_atom_category (cur, &item->category); else { mrss_tag_t *tag; if ((tag = __mrss_parse_tag (doc, cur))) __mrss_parse_tag_insert (&item->other_tags, tag); } } } if (!data->item) data->item = item; else { mrss_item_t *tmp = data->item; while (tmp->next) tmp = tmp->next; tmp->next = item; } }