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 std::stringstream;
31 using boost::shared_ptr;
32 using boost::optional;
40 cxml::Node::Node (xmlpp::Node* node)
47 cxml::Node::name () const
50 return _node->get_name ();
53 shared_ptr<cxml::Node>
54 cxml::Node::node_child (string name) const
56 list<shared_ptr<cxml::Node> > n = node_children (name);
58 throw cxml::Error ("duplicate XML tag " + name);
59 } else if (n.empty ()) {
60 throw cxml::Error ("missing XML tag " + name + " in " + _node->get_name());
66 shared_ptr<cxml::Node>
67 cxml::Node::optional_node_child (string name) const
69 list<shared_ptr<cxml::Node> > n = node_children (name);
71 throw cxml::Error ("duplicate XML tag " + name);
72 } else if (n.empty ()) {
73 return shared_ptr<cxml::Node> ();
79 list<shared_ptr<cxml::Node> >
80 cxml::Node::node_children (string name) const
82 /* XXX: using find / get_path should work here, but I can't follow
86 xmlpp::Node::NodeList c = _node->get_children ();
88 list<shared_ptr<cxml::Node> > n;
89 for (xmlpp::Node::NodeList::iterator i = c.begin (); i != c.end(); ++i) {
90 if ((*i)->get_name() == name) {
91 n.push_back (shared_ptr<Node> (new Node (*i)));
95 _taken.push_back (name);
100 cxml::Node::string_child (string c) const
102 return node_child(c)->content ();
106 cxml::Node::optional_string_child (string c) const
108 list<shared_ptr<Node> > nodes = node_children (c);
109 if (nodes.size() > 1) {
110 throw cxml::Error ("duplicate XML tag " + c);
113 if (nodes.empty ()) {
114 return optional<string> ();
117 return nodes.front()->content();
121 cxml::Node::bool_child (string c) const
123 string const s = string_child (c);
124 return (s == "1" || s == "yes" || s == "True");
128 cxml::Node::optional_bool_child (string c) const
130 optional<string> s = optional_string_child (c);
132 return optional<bool> ();
135 return (s.get() == "1" || s.get() == "yes" || s.get() == "True");
139 cxml::Node::ignore_child (string name) const
141 _taken.push_back (name);
145 cxml::Node::string_attribute (string name) const
147 xmlpp::Element const * e = dynamic_cast<const xmlpp::Element *> (_node);
149 throw cxml::Error ("missing attribute " + name);
152 xmlpp::Attribute* a = e->get_attribute (name);
154 throw cxml::Error ("missing attribute " + name);
157 return a->get_value ();
161 cxml::Node::optional_string_attribute (string name) const
163 xmlpp::Element const * e = dynamic_cast<const xmlpp::Element *> (_node);
165 return optional<string> ();
168 xmlpp::Attribute* a = e->get_attribute (name);
170 return optional<string> ();
173 return string (a->get_value ());
177 cxml::Node::bool_attribute (string name) const
179 string const s = string_attribute (name);
180 return (s == "1" || s == "yes");
184 cxml::Node::optional_bool_attribute (string name) const
186 optional<string> s = optional_string_attribute (name);
188 return optional<bool> ();
191 return (s.get() == "1" || s.get() == "yes");
195 cxml::Node::done () const
197 xmlpp::Node::NodeList c = _node->get_children ();
198 for (xmlpp::Node::NodeList::iterator i = c.begin(); i != c.end(); ++i) {
199 if (dynamic_cast<xmlpp::Element *> (*i) && find (_taken.begin(), _taken.end(), (*i)->get_name()) == _taken.end ()) {
200 throw cxml::Error ("unexpected XML node " + (*i)->get_name());
206 cxml::Node::content () const
210 xmlpp::Node::NodeList c = _node->get_children ();
211 for (xmlpp::Node::NodeList::const_iterator i = c.begin(); i != c.end(); ++i) {
212 xmlpp::ContentNode const * v = dynamic_cast<xmlpp::ContentNode const *> (*i);
214 content += v->get_content ();
222 cxml::Node::namespace_uri () const
224 return _node->get_namespace_uri ();
228 cxml::Node::namespace_prefix () const
230 return _node->get_namespace_prefix ();
233 cxml::Document::Document (string root_name)
234 : _root_name (root_name)
236 _parser = new xmlpp::DomParser;
239 cxml::Document::Document (string root_name, boost::filesystem::path file)
240 : _root_name (root_name)
242 _parser = new xmlpp::DomParser ();
246 cxml::Document::Document ()
248 _parser = new xmlpp::DomParser ();
251 cxml::Document::~Document ()
257 cxml::Document::read_file (boost::filesystem::path file)
259 if (!boost::filesystem::exists (file)) {
260 throw cxml::Error ("XML file " + file.string() + " does not exist");
263 _parser->parse_file (file.string ());
268 cxml::Document::read_stream (istream& stream)
270 _parser->parse_stream (stream);
275 cxml::Document::read_string (string s)
278 _parser->parse_stream (t);
283 cxml::Document::take_root_node ()
286 throw cxml::Error ("could not parse XML");
289 _node = _parser->get_document()->get_root_node ();
290 if (!_root_name.empty() && _node->get_name() != _root_name) {
291 throw cxml::Error ("unrecognised root node " + _node->get_name() + " (expecting " + _root_name + ")");
292 } else if (_root_name.empty ()) {
293 _root_name = _node->get_name ();