2 Copyright (C) 2012-2019 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 #ifndef LIBCXML_CXML_H
22 #define LIBCXML_CXML_H
24 #include <boost/shared_ptr.hpp>
25 #include <boost/optional.hpp>
26 #include <boost/filesystem.hpp>
27 #include <boost/algorithm/string/erase.hpp>
32 /* Hack for OS X compile failure; see https://bugs.launchpad.net/hugin/+bug/910160 */
44 /** @brief An error */
45 class Error : public std::exception
48 /** Construct an Error exception.
49 * @param message Error message.
51 Error (std::string const & message) : _message (message) {}
53 /** Error destructor */
56 /** @return error message. Caller must not free the returned
59 char const * what () const throw () {
60 return _message.c_str ();
68 /** A sort-of version of boost::lexical_cast that does uses the "C"
69 * locale (i.e. no thousands separators and a . for the decimal separator).
71 template <typename P, typename Q>
75 /* We can't write a generic version of raw_convert; all required
76 versions must be specialised.
78 BOOST_STATIC_ASSERT (sizeof(Q) == 0);
83 raw_convert (std::string v);
87 raw_convert (std::string v);
91 raw_convert (std::string v);
95 raw_convert (std::string v);
99 raw_convert (std::string v);
103 raw_convert (std::string v);
105 /** @brief A wrapper for a xmlpp::Node which simplifies parsing */
111 /** Construct a Node from an xmlpp::Node. This class will
112 * not destroy the xmlpp::Node.
113 * @param node xmlpp::Node.
115 Node (xmlpp::Node* node);
117 std::string name () const;
119 /* A set of methods which look up a child of this node by
120 * its name, and return its contents as some type or other.
122 * If, for example, this object has been created with
123 * a node named "Fred", we might have the following XML:
129 * string_child ("Jim") would return "42"
130 * number_child<int64_t> ("Jim") would return 42.
133 * The methods not marked "optional" will throw an exception
134 * if the child node is not present. The "optional" methods
135 * will return an empty boost::optional<> in that case.
137 * All methods will also throw an exception if there is more
138 * than one of the specified child node.
141 std::string string_child (std::string c) const;
142 boost::optional<std::string> optional_string_child (std::string) const;
144 bool bool_child (std::string) const;
145 boost::optional<bool> optional_bool_child (std::string) const;
148 T number_child (std::string c) const
150 std::string s = string_child (c);
151 boost::erase_all (s, " ");
152 return raw_convert<T> (s);
156 boost::optional<T> optional_number_child (std::string c) const
158 boost::optional<std::string> s = optional_string_child (c);
160 return boost::optional<T> ();
163 std::string t = s.get ();
164 boost::erase_all (t, " ");
165 return raw_convert<T> (t);
168 /** This will mark a child as to be ignored when calling done() */
169 void ignore_child (std::string) const;
171 /** Check whether all children of this Node have been looked up
172 * or passed to ignore_child(). If not, an exception is thrown.
176 /* These methods look for an attribute of this node, in the
177 * same way as the child methods do.
180 std::string string_attribute (std::string) const;
181 boost::optional<std::string> optional_string_attribute (std::string) const;
183 bool bool_attribute (std::string) const;
184 boost::optional<bool> optional_bool_attribute (std::string) const;
187 T number_attribute (std::string c) const
189 std::string s = string_attribute (c);
190 boost::erase_all (s, " ");
191 return raw_convert<T> (s);
195 boost::optional<T> optional_number_attribute (std::string c) const
197 boost::optional<std::string> s = optional_string_attribute (c);
199 return boost::optional<T> ();
202 std::string t = s.get ();
203 boost::erase_all (t, " ");
204 return raw_convert<T> (t);
207 /** @return The text content of this node (excluding comments or CDATA) */
208 std::string content () const;
210 /** @return namespace URI of this node */
211 std::string namespace_uri () const;
213 /** @return namespace prefix of this node */
214 std::string namespace_prefix () const;
216 boost::shared_ptr<Node> node_child (std::string) const;
217 boost::shared_ptr<Node> optional_node_child (std::string) const;
219 std::list<boost::shared_ptr<Node> > node_children () const;
220 std::list<boost::shared_ptr<Node> > node_children (std::string) const;
222 xmlpp::Node* node () const {
230 mutable std::list<std::string> _taken;
233 typedef boost::shared_ptr<cxml::Node> NodePtr;
234 typedef boost::shared_ptr<const cxml::Node> ConstNodePtr;
236 class Document : public Node
240 Document (std::string root_name);
241 Document (std::string root_name, boost::filesystem::path);
243 virtual ~Document ();
245 void read_file (boost::filesystem::path);
246 void read_string (std::string);
248 std::string root_name () const {
253 void take_root_node ();
255 xmlpp::DomParser* _parser;
256 std::string _root_name;