|
| 1 | +// Copyright (c) Glyn Matthews 2011. |
| 2 | +// Distributed under the Boost Software License, Version 1.0. |
| 3 | +// (See accompanying file LICENSE_1_0.txt or copy at |
| 4 | +// http://www.boost.org/LICENSE_1_0.txt) |
| 5 | + |
| 6 | + |
| 7 | +#include "atom.hpp" |
| 8 | +#include "../rapidxml/rapidxml.hpp" |
| 9 | +#include <stdexcept> |
| 10 | +#include <cassert> |
| 11 | + |
| 12 | + |
| 13 | +namespace boost { |
| 14 | +namespace network { |
| 15 | +namespace atom { |
| 16 | +feed::feed(const http::client::response &response) { |
| 17 | + std::string response_body = body(response); |
| 18 | + rapidxml::xml_document<> doc; |
| 19 | + doc.parse<0>(const_cast<char *>(response_body.c_str())); |
| 20 | + |
| 21 | + rapidxml::xml_node<> *feed = doc.first_node("feed"); |
| 22 | + if (!feed) { |
| 23 | + throw std::runtime_error("Invalid atom feed."); |
| 24 | + } |
| 25 | + |
| 26 | + rapidxml::xml_node<> *title = feed->first_node("title"); |
| 27 | + if (title) { |
| 28 | + title_ = title->first_node()->value(); |
| 29 | + } |
| 30 | + |
| 31 | + rapidxml::xml_node<> *subtitle = feed->first_node("subtitle"); |
| 32 | + if (subtitle) { |
| 33 | + subtitle_ = subtitle->first_node()->value(); |
| 34 | + } |
| 35 | + |
| 36 | + rapidxml::xml_node<> *id = feed->first_node("id"); |
| 37 | + if (id) { |
| 38 | + id_ = id->first_node()->value(); |
| 39 | + } |
| 40 | + |
| 41 | + rapidxml::xml_node<> *updated = feed->first_node("updated"); |
| 42 | + if (updated) { |
| 43 | + updated_ = updated->first_node()->value(); |
| 44 | + } |
| 45 | + |
| 46 | + rapidxml::xml_node<> *author = feed->first_node("author"); |
| 47 | + if (author) { |
| 48 | + rapidxml::xml_node<> *name = author->first_node("name"); |
| 49 | + rapidxml::xml_node<> *email = author->first_node("email"); |
| 50 | + if (name && email) { |
| 51 | + author_ = atom::author(name->first_node()->value(), email->first_node()->value()); |
| 52 | + } |
| 53 | + else if (name) { |
| 54 | + author_ = atom::author(name->first_node()->value()); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + rapidxml::xml_node<> *entry = feed->first_node("entry"); |
| 59 | + while (entry) { |
| 60 | + entries_.push_back(atom::entry()); |
| 61 | + |
| 62 | + rapidxml::xml_node<> *title = entry->first_node("title"); |
| 63 | + if (title) { |
| 64 | + entries_.back().set_title(title->first_node()->value()); |
| 65 | + } |
| 66 | + |
| 67 | + rapidxml::xml_node<> *id = entry->first_node("id"); |
| 68 | + if (id) { |
| 69 | + entries_.back().set_id(id->first_node()->value()); |
| 70 | + } |
| 71 | + |
| 72 | + rapidxml::xml_node<> *published = entry->first_node("published"); |
| 73 | + if (published) { |
| 74 | + entries_.back().set_published(published->first_node()->value()); |
| 75 | + } |
| 76 | + |
| 77 | + rapidxml::xml_node<> *updated = entry->first_node("updated"); |
| 78 | + if (updated) { |
| 79 | + entries_.back().set_updated(updated->first_node()->value()); |
| 80 | + } |
| 81 | + |
| 82 | + rapidxml::xml_node<> *summary = entry->first_node("summary"); |
| 83 | + if (summary) { |
| 84 | + entries_.back().set_summary(summary->first_node()->value()); |
| 85 | + } |
| 86 | + |
| 87 | + rapidxml::xml_node<> *content = entry->first_node("content"); |
| 88 | + if (content) { |
| 89 | + entries_.back().set_content(content->first_node()->value()); |
| 90 | + } |
| 91 | + |
| 92 | + entry = entry->next_sibling(); |
| 93 | + } |
| 94 | +} |
| 95 | +} // namespace atom |
| 96 | +} // namespace network |
| 97 | +} // namespace boost |
0 commit comments