7 #include <boost/shared_ptr.hpp>
8 #include <boost/optional.hpp>
9 #include <boost/lexical_cast.hpp>
10 #include <boost/filesystem.hpp>
11 #include <boost/algorithm/string/erase.hpp>
21 /** @brief An error */
22 class Error : public std::exception
25 /** Construct an Error exception.
26 * @param message Error message.
28 Error (std::string const & message) : _message (message) {}
30 /** Error destructor */
33 /** @return error message. Caller must not free the returned
36 char const * what () const throw () {
37 return _message.c_str ();
45 /** @brief A wrapper for a xmlpp::Node which simplifies parsing */
51 /** Construct a Node from an xmlpp::Node. This class will
52 * not destroy the xmlpp::Node.
53 * @param node xmlpp::Node.
55 Node (xmlpp::Node* node);
57 std::string name () const;
59 /* A set of methods which look up a child of this node by
60 * its name, and return its contents as some type or other.
62 * If, for example, this object has been created with
63 * a node named "Fred", we might have the following XML:
69 * string_child ("Jim") would return "42"
70 * number_child<int64_t> ("Jim") would return 42.
73 * The methods not marked "optional" will throw an exception
74 * if the child node is not present. The "optional" methods
75 * will return an empty boost::optional<> in that case.
77 * All methods will also throw an exception if there is more
78 * than one of the specified child node.
81 std::string string_child (std::string c) const;
82 boost::optional<std::string> optional_string_child (std::string) const;
84 bool bool_child (std::string) const;
85 boost::optional<bool> optional_bool_child (std::string) const;
88 T number_child (std::string c) const
90 std::string s = string_child (c);
91 boost::erase_all (s, " ");
92 return boost::lexical_cast<T> (s);
96 boost::optional<T> optional_number_child (std::string c) const
98 boost::optional<std::string> s = optional_string_child (c);
100 return boost::optional<T> ();
103 std::string t = s.get ();
104 boost::erase_all (t, " ");
105 return boost::optional<T> (boost::lexical_cast<T> (t));
108 /** This will mark a child as to be ignored when calling done() */
109 void ignore_child (std::string) const;
111 /** Check whether all children of this Node have been looked up
112 * or passed to ignore_child(). If not, an exception is thrown.
116 /* These methods look for an attribute of this node, in the
117 * same way as the child methods do.
120 std::string string_attribute (std::string) const;
121 boost::optional<std::string> optional_string_attribute (std::string) const;
123 bool bool_attribute (std::string) const;
124 boost::optional<bool> optional_bool_attribute (std::string) const;
127 T number_attribute (std::string c) const
129 std::string s = string_attribute (c);
130 boost::erase_all (s, " ");
131 return boost::lexical_cast<T> (s);
135 boost::optional<T> optional_number_attribute (std::string c) const
137 boost::optional<std::string> s = optional_string_attribute (c);
139 return boost::optional<T> ();
142 std::string t = s.get ();
143 boost::erase_all (t, " ");
144 return boost::optional<T> (boost::lexical_cast<T> (t));
147 /** @return The content of this node */
148 std::string content () const;
150 /** @return namespace URI of this node */
151 std::string namespace_uri () const;
153 /** @return namespace prefix of this node */
154 std::string namespace_prefix () const;
156 boost::shared_ptr<Node> node_child (std::string) const;
157 boost::shared_ptr<Node> optional_node_child (std::string) const;
159 std::list<boost::shared_ptr<Node> > node_children (std::string) const;
161 xmlpp::Node* node () const {
169 mutable std::list<Glib::ustring> _taken;
172 typedef boost::shared_ptr<cxml::Node> NodePtr;
174 class Document : public Node
177 Document (std::string root_name);
179 void read_file (boost::filesystem::path);
180 void read_stream (std::istream &);
182 virtual ~Document ();
185 void take_root_node ();
187 xmlpp::DomParser* _parser;
188 std::string _root_name;