2 Copyright (C) 2012-2014 Carl Hetherington <cth@carlh.net>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (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
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include <boost/filesystem.hpp>
23 #include <boost/algorithm/string.hpp>
24 #include <libxml++/libxml++.h>
28 using namespace boost;
36 cxml::Node::Node (xmlpp::Node* node)
43 cxml::Node::name () const
46 return _node->get_name ();
49 shared_ptr<cxml::Node>
50 cxml::Node::node_child (string name) const
52 list<shared_ptr<cxml::Node> > n = node_children (name);
54 throw cxml::Error ("duplicate XML tag " + name);
55 } else if (n.empty ()) {
56 throw cxml::Error ("missing XML tag " + name + " in " + _node->get_name());
62 shared_ptr<cxml::Node>
63 cxml::Node::optional_node_child (string name) const
65 list<shared_ptr<cxml::Node> > n = node_children (name);
67 throw cxml::Error ("duplicate XML tag " + name);
68 } else if (n.empty ()) {
69 return shared_ptr<cxml::Node> ();
75 list<shared_ptr<cxml::Node> >
76 cxml::Node::node_children (string name) const
78 /* XXX: using find / get_path should work here, but I can't follow
82 xmlpp::Node::NodeList c = _node->get_children ();
84 list<shared_ptr<cxml::Node> > n;
85 for (xmlpp::Node::NodeList::iterator i = c.begin (); i != c.end(); ++i) {
86 if ((*i)->get_name() == name) {
87 n.push_back (shared_ptr<Node> (new Node (*i)));
91 _taken.push_back (name);
96 cxml::Node::string_child (string c) const
98 return node_child(c)->content ();
102 cxml::Node::optional_string_child (string c) const
104 list<shared_ptr<Node> > nodes = node_children (c);
105 if (nodes.size() > 1) {
106 throw cxml::Error ("duplicate XML tag " + c);
109 if (nodes.empty ()) {
110 return optional<string> ();
113 return nodes.front()->content();
117 cxml::Node::bool_child (string c) const
119 string const s = string_child (c);
120 return (s == "1" || s == "yes");
124 cxml::Node::optional_bool_child (string c) const
126 optional<string> s = optional_string_child (c);
128 return optional<bool> ();
131 return (s.get() == "1" || s.get() == "yes");
135 cxml::Node::ignore_child (string name) const
137 _taken.push_back (name);
141 cxml::Node::string_attribute (string name) const
143 xmlpp::Element const * e = dynamic_cast<const xmlpp::Element *> (_node);
145 throw cxml::Error ("missing attribute");
148 xmlpp::Attribute* a = e->get_attribute (name);
150 throw cxml::Error ("missing attribute");
153 return a->get_value ();
157 cxml::Node::optional_string_attribute (string name) const
159 xmlpp::Element const * e = dynamic_cast<const xmlpp::Element *> (_node);
161 return optional<string> ();
164 xmlpp::Attribute* a = e->get_attribute (name);
166 return optional<string> ();
169 return string (a->get_value ());
173 cxml::Node::bool_attribute (string name) const
175 string const s = string_attribute (name);
176 return (s == "1" || s == "yes");
180 cxml::Node::optional_bool_attribute (string name) const
182 optional<string> s = optional_string_attribute (name);
184 return optional<bool> ();
187 return (s.get() == "1" || s.get() == "yes");
191 cxml::Node::done () const
193 xmlpp::Node::NodeList c = _node->get_children ();
194 for (xmlpp::Node::NodeList::iterator i = c.begin(); i != c.end(); ++i) {
195 if (dynamic_cast<xmlpp::Element *> (*i) && find (_taken.begin(), _taken.end(), (*i)->get_name()) == _taken.end ()) {
196 throw cxml::Error ("unexpected XML node " + (*i)->get_name());
202 cxml::Node::content () const
206 xmlpp::Node::NodeList c = _node->get_children ();
207 for (xmlpp::Node::NodeList::const_iterator i = c.begin(); i != c.end(); ++i) {
208 xmlpp::ContentNode const * v = dynamic_cast<xmlpp::ContentNode const *> (*i);
210 content += v->get_content ();
218 cxml::Node::namespace_uri () const
220 return _node->get_namespace_uri ();
224 cxml::Node::namespace_prefix () const
226 return _node->get_namespace_prefix ();
229 cxml::Document::Document (string root_name)
230 : _root_name (root_name)
232 _parser = new xmlpp::DomParser;
235 cxml::Document::Document (string root_name, boost::filesystem::path file)
236 : _root_name (root_name)
238 _parser = new xmlpp::DomParser ();
242 cxml::Document::~Document ()
248 cxml::Document::read_file (filesystem::path file)
250 if (!filesystem::exists (file)) {
251 throw cxml::Error ("XML file does not exist");
254 _parser->parse_file (file.string ());
259 cxml::Document::read_stream (istream& stream)
261 _parser->parse_stream (stream);
266 cxml::Document::take_root_node ()
269 throw cxml::Error ("could not parse XML");
272 _node = _parser->get_document()->get_root_node ();
273 if (_node->get_name() != _root_name) {
274 throw cxml::Error ("unrecognised root node");