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.
20 #ifndef LIBCXML_CXML_H
21 #define LIBCXML_CXML_H
26 #include <boost/shared_ptr.hpp>
27 #include <boost/optional.hpp>
28 #include <boost/filesystem.hpp>
29 #include <boost/algorithm/string/erase.hpp>
39 /** @brief An error */
40 class Error : public std::exception
43 /** Construct an Error exception.
44 * @param message Error message.
46 Error (std::string const & message) : _message (message) {}
48 /** Error destructor */
51 /** @return error message. Caller must not free the returned
54 char const * what () const throw () {
55 return _message.c_str ();
63 /** @brief A wrapper for a xmlpp::Node which simplifies parsing */
69 /** Construct a Node from an xmlpp::Node. This class will
70 * not destroy the xmlpp::Node.
71 * @param node xmlpp::Node.
73 Node (xmlpp::Node* node);
75 std::string name () const;
77 /* A set of methods which look up a child of this node by
78 * its name, and return its contents as some type or other.
80 * If, for example, this object has been created with
81 * a node named "Fred", we might have the following XML:
87 * string_child ("Jim") would return "42"
88 * number_child<int64_t> ("Jim") would return 42.
91 * The methods not marked "optional" will throw an exception
92 * if the child node is not present. The "optional" methods
93 * will return an empty boost::optional<> in that case.
95 * All methods will also throw an exception if there is more
96 * than one of the specified child node.
99 std::string string_child (std::string c) const;
100 boost::optional<std::string> optional_string_child (std::string) const;
102 bool bool_child (std::string) const;
103 boost::optional<bool> optional_bool_child (std::string) const;
106 T number_child (std::string c) const
108 std::string s = string_child (c);
109 boost::erase_all (s, " ");
111 t.imbue (std::locale::classic ());
119 boost::optional<T> optional_number_child (std::string c) const
121 boost::optional<std::string> s = optional_string_child (c);
123 return boost::optional<T> ();
126 std::string t = s.get ();
127 boost::erase_all (t, " ");
129 u.imbue (std::locale::classic ());
136 /** This will mark a child as to be ignored when calling done() */
137 void ignore_child (std::string) const;
139 /** Check whether all children of this Node have been looked up
140 * or passed to ignore_child(). If not, an exception is thrown.
144 /* These methods look for an attribute of this node, in the
145 * same way as the child methods do.
148 std::string string_attribute (std::string) const;
149 boost::optional<std::string> optional_string_attribute (std::string) const;
151 bool bool_attribute (std::string) const;
152 boost::optional<bool> optional_bool_attribute (std::string) const;
155 T number_attribute (std::string c) const
157 std::string s = string_attribute (c);
158 boost::erase_all (s, " ");
160 t.imbue (std::locale::classic ());
168 boost::optional<T> optional_number_attribute (std::string c) const
170 boost::optional<std::string> s = optional_string_attribute (c);
172 return boost::optional<T> ();
175 std::string t = s.get ();
176 boost::erase_all (t, " ");
178 u.imbue (std::locale::classic ());
185 /** @return The content of this node */
186 std::string content () const;
188 /** @return namespace URI of this node */
189 std::string namespace_uri () const;
191 /** @return namespace prefix of this node */
192 std::string namespace_prefix () const;
194 boost::shared_ptr<Node> node_child (std::string) const;
195 boost::shared_ptr<Node> optional_node_child (std::string) const;
197 std::list<boost::shared_ptr<Node> > node_children (std::string) const;
199 xmlpp::Node* node () const {
207 mutable std::list<Glib::ustring> _taken;
210 typedef boost::shared_ptr<cxml::Node> NodePtr;
211 typedef boost::shared_ptr<const cxml::Node> ConstNodePtr;
213 class Document : public Node
217 Document (std::string root_name);
218 Document (std::string root_name, boost::filesystem::path);
220 virtual ~Document ();
222 void read_file (boost::filesystem::path);
223 void read_stream (std::istream &);
224 void read_string (std::string);
226 std::string root_name () const {
231 void take_root_node ();
233 xmlpp::DomParser* _parser;
234 std::string _root_name;