Add Document::read_string().
[libcxml.git] / src / cxml.h
1 /*
2     Copyright (C) 2012-2014 Carl Hetherington <cth@carlh.net>
3
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.
8
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.
13
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.
17
18 */
19
20 #ifndef LIBCXML_CXML_H
21 #define LIBCXML_CXML_H
22
23 #include <string>
24 #include <list>
25 #include <stdint.h>
26 #include <boost/shared_ptr.hpp>
27 #include <boost/optional.hpp>
28 #include <boost/filesystem.hpp>
29 #include <boost/algorithm/string/erase.hpp>
30 #include <glibmm.h>
31
32 namespace xmlpp {
33         class Node;
34         class DomParser;
35 }
36
37 namespace cxml {
38
39 /** @brief An error */
40 class Error : public std::exception
41 {
42 public:
43         /** Construct an Error exception.
44          *  @param message Error message.
45          */
46         Error (std::string const & message) : _message (message) {}
47
48         /** Error destructor */
49         ~Error () throw () {}
50
51         /** @return error message.  Caller must not free the returned
52          *  value.
53          */
54         char const * what () const throw () {
55                 return _message.c_str ();
56         }
57
58 private:
59         /** error message */
60         std::string _message;
61 };
62
63 /** @brief A wrapper for a xmlpp::Node which simplifies parsing */
64 class Node
65 {
66 public:
67         Node ();
68         
69         /** Construct a Node from an xmlpp::Node.  This class will
70          *  not destroy the xmlpp::Node.
71          *  @param node xmlpp::Node.
72          */
73         Node (xmlpp::Node* node);
74
75         std::string name () const;
76
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.
79          *
80          * If, for example, this object has been created with
81          * a node named "Fred", we might have the following XML:
82          *
83          * <Fred>
84          *   <Jim>42</Jim>
85          * </Fred>
86          *
87          * string_child ("Jim") would return "42"
88          * number_child<int64_t> ("Jim") would return 42.
89          * ...and so on.
90          *
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.
94          *
95          * All methods will also throw an exception if there is more
96          * than one of the specified child node.
97          */
98
99         std::string string_child (std::string c) const;
100         boost::optional<std::string> optional_string_child (std::string) const;
101
102         bool bool_child (std::string) const;
103         boost::optional<bool> optional_bool_child (std::string) const;
104
105         template <class T>
106         T number_child (std::string c) const
107         {
108                 std::string s = string_child (c);
109                 boost::erase_all (s, " ");
110                 std::stringstream t;
111                 t.imbue (std::locale::classic ());
112                 t << s;
113                 T n;
114                 t >> n;
115                 return n;
116         }
117
118         template <class T>
119         boost::optional<T> optional_number_child (std::string c) const
120         {
121                 boost::optional<std::string> s = optional_string_child (c);
122                 if (!s) {
123                         return boost::optional<T> ();
124                 }
125
126                 std::string t = s.get ();
127                 boost::erase_all (t, " ");
128                 std::stringstream u;
129                 u.imbue (std::locale::classic ());
130                 u << t;
131                 T n;
132                 u >> n;
133                 return n;
134         }
135                 
136         /** This will mark a child as to be ignored when calling done() */
137         void ignore_child (std::string) const;
138
139         /** Check whether all children of this Node have been looked up
140          *  or passed to ignore_child().  If not, an exception is thrown.
141          */
142         void done () const;
143
144         /* These methods look for an attribute of this node, in the
145          * same way as the child methods do.
146          */
147
148         std::string string_attribute (std::string) const;
149         boost::optional<std::string> optional_string_attribute (std::string) const;
150
151         bool bool_attribute (std::string) const;
152         boost::optional<bool> optional_bool_attribute (std::string) const;
153
154         template <class T>
155         T number_attribute (std::string c) const
156         {
157                 std::string s = string_attribute (c);
158                 boost::erase_all (s, " ");
159                 std::stringstream t;
160                 t.imbue (std::locale::classic ());
161                 t << s;
162                 T n;
163                 t >> n;
164                 return n;
165         }
166
167         template <class T>
168         boost::optional<T> optional_number_attribute (std::string c) const
169         {
170                 boost::optional<std::string> s = optional_string_attribute (c);
171                 if (!s) {
172                         return boost::optional<T> ();
173                 }
174
175                 std::string t = s.get ();
176                 boost::erase_all (t, " ");
177                 std::stringstream u;
178                 u.imbue (std::locale::classic ());
179                 u << t;
180                 T n;
181                 u >> n;
182                 return n;
183         }
184
185         /** @return The content of this node */
186         std::string content () const;
187
188         /** @return namespace URI of this node */
189         std::string namespace_uri () const;
190
191         /** @return namespace prefix of this node */
192         std::string namespace_prefix () const;
193
194         boost::shared_ptr<Node> node_child (std::string) const;
195         boost::shared_ptr<Node> optional_node_child (std::string) const;
196
197         std::list<boost::shared_ptr<Node> > node_children (std::string) const;
198
199         xmlpp::Node* node () const {
200                 return _node;
201         }
202         
203 protected:
204         xmlpp::Node* _node;
205         
206 private:
207         mutable std::list<Glib::ustring> _taken;
208 };
209
210 typedef boost::shared_ptr<cxml::Node> NodePtr;
211 typedef boost::shared_ptr<const cxml::Node> ConstNodePtr;
212
213 class Document : public Node
214 {
215 public:
216         Document ();
217         Document (std::string root_name);
218         Document (std::string root_name, boost::filesystem::path);
219
220         virtual ~Document ();
221
222         void read_file (boost::filesystem::path);
223         void read_stream (std::istream &);
224         void read_string (std::string);
225         
226         std::string root_name () const {
227                 return _root_name;
228         }
229                
230 private:
231         void take_root_node ();
232         
233         xmlpp::DomParser* _parser;
234         std::string _root_name;
235 };
236
237 }
238
239 #endif