2 Copyright (C) 2012-2016 Carl Hetherington <cth@carlh.net>
4 This file is part of libcxml.
6 libcxml is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 libcxml is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with libcxml. If not, see <http://www.gnu.org/licenses/>.
21 #include <boost/filesystem.hpp>
22 #include <boost/algorithm/string.hpp>
23 #include <libxml++/libxml++.h>
28 using boost::shared_ptr;
29 using boost::optional;
37 cxml::Node::Node (xmlpp::Node* node)
44 cxml::Node::name () const
47 return _node->get_name ();
50 shared_ptr<cxml::Node>
51 cxml::Node::node_child (string name) const
53 list<shared_ptr<cxml::Node> > n = node_children (name);
55 throw cxml::Error ("duplicate XML tag " + name);
56 } else if (n.empty ()) {
57 throw cxml::Error ("missing XML tag " + name + " in " + _node->get_name());
63 shared_ptr<cxml::Node>
64 cxml::Node::optional_node_child (string name) const
66 list<shared_ptr<cxml::Node> > n = node_children (name);
68 throw cxml::Error ("duplicate XML tag " + name);
69 } else if (n.empty ()) {
70 return shared_ptr<cxml::Node> ();
76 list<shared_ptr<cxml::Node> >
77 cxml::Node::node_children (string name) const
79 /* XXX: using find / get_path should work here, but I can't follow
83 xmlpp::Node::NodeList c = _node->get_children ();
85 list<shared_ptr<cxml::Node> > n;
86 for (xmlpp::Node::NodeList::iterator i = c.begin (); i != c.end(); ++i) {
87 if ((*i)->get_name() == name) {
88 n.push_back (shared_ptr<Node> (new Node (*i)));
92 _taken.push_back (name);
97 cxml::Node::string_child (string c) const
99 return node_child(c)->content ();
103 cxml::Node::optional_string_child (string c) const
105 list<shared_ptr<Node> > nodes = node_children (c);
106 if (nodes.size() > 1) {
107 throw cxml::Error ("duplicate XML tag " + c);
110 if (nodes.empty ()) {
111 return optional<string> ();
114 return nodes.front()->content();
118 cxml::Node::bool_child (string c) const
120 string const s = string_child (c);
121 return (s == "1" || s == "yes" || s == "True");
125 cxml::Node::optional_bool_child (string c) const
127 optional<string> s = optional_string_child (c);
129 return optional<bool> ();
132 return (s.get() == "1" || s.get() == "yes" || s.get() == "True");
136 cxml::Node::ignore_child (string name) const
138 _taken.push_back (name);
142 cxml::Node::string_attribute (string name) const
144 xmlpp::Element const * e = dynamic_cast<const xmlpp::Element *> (_node);
146 throw cxml::Error ("missing attribute " + name);
149 xmlpp::Attribute* a = e->get_attribute (name);
151 throw cxml::Error ("missing attribute " + name);
154 return a->get_value ();
158 cxml::Node::optional_string_attribute (string name) const
160 xmlpp::Element const * e = dynamic_cast<const xmlpp::Element *> (_node);
162 return optional<string> ();
165 xmlpp::Attribute* a = e->get_attribute (name);
167 return optional<string> ();
170 return string (a->get_value ());
174 cxml::Node::bool_attribute (string name) const
176 string const s = string_attribute (name);
177 return (s == "1" || s == "yes");
181 cxml::Node::optional_bool_attribute (string name) const
183 optional<string> s = optional_string_attribute (name);
185 return optional<bool> ();
188 return (s.get() == "1" || s.get() == "yes");
192 cxml::Node::done () const
194 xmlpp::Node::NodeList c = _node->get_children ();
195 for (xmlpp::Node::NodeList::iterator i = c.begin(); i != c.end(); ++i) {
196 if (dynamic_cast<xmlpp::Element *> (*i) && find (_taken.begin(), _taken.end(), (*i)->get_name()) == _taken.end ()) {
197 throw cxml::Error ("unexpected XML node " + (*i)->get_name());
203 cxml::Node::content () const
207 xmlpp::Node::NodeList c = _node->get_children ();
208 for (xmlpp::Node::NodeList::const_iterator i = c.begin(); i != c.end(); ++i) {
209 xmlpp::ContentNode const * v = dynamic_cast<xmlpp::ContentNode const *> (*i);
211 content += v->get_content ();
219 cxml::Node::namespace_uri () const
221 return _node->get_namespace_uri ();
225 cxml::Node::namespace_prefix () const
227 return _node->get_namespace_prefix ();
230 cxml::Document::Document (string root_name)
231 : _root_name (root_name)
233 _parser = new xmlpp::DomParser;
236 cxml::Document::Document (string root_name, boost::filesystem::path file)
237 : _root_name (root_name)
239 _parser = new xmlpp::DomParser ();
243 cxml::Document::Document ()
245 _parser = new xmlpp::DomParser ();
248 cxml::Document::~Document ()
254 cxml::Document::read_file (boost::filesystem::path file)
256 if (!boost::filesystem::exists (file)) {
257 throw cxml::Error ("XML file " + file.string() + " does not exist");
260 _parser->parse_file (file.string ());
265 cxml::Document::read_string (string s)
267 _parser->parse_memory (s);
272 cxml::Document::take_root_node ()
275 throw cxml::Error ("could not parse XML");
278 _node = _parser->get_document()->get_root_node ();
279 if (!_root_name.empty() && _node->get_name() != _root_name) {
280 throw cxml::Error ("unrecognised root node " + _node->get_name() + " (expecting " + _root_name + ")");
281 } else if (_root_name.empty ()) {
282 _root_name = _node->get_name ();