77770f93b0fb7ad943de5863cc72a151d79db518
[libcxml.git] / src / cxml.cc
1 /*
2     Copyright (C) 2012-2016 Carl Hetherington <cth@carlh.net>
3
4     This file is part of libcxml.
5
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.
10
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.
15
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/>.
18
19 */
20
21 #include <boost/filesystem.hpp>
22 #include <boost/algorithm/string.hpp>
23 #include <libxml++/libxml++.h>
24 #include "cxml.h"
25
26 using std::string;
27 using std::list;
28 using boost::shared_ptr;
29 using boost::optional;
30
31 cxml::Node::Node ()
32         : _node (0)
33 {
34
35 }
36
37 cxml::Node::Node (xmlpp::Node* node)
38         : _node (node)
39 {
40
41 }
42
43 string
44 cxml::Node::name () const
45 {
46         assert (_node);
47         return _node->get_name ();
48 }
49
50 shared_ptr<cxml::Node>
51 cxml::Node::node_child (string name) const
52 {
53         list<shared_ptr<cxml::Node> > n = node_children (name);
54         if (n.size() > 1) {
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());
58         }
59
60         return n.front ();
61 }
62
63 shared_ptr<cxml::Node>
64 cxml::Node::optional_node_child (string name) const
65 {
66         list<shared_ptr<cxml::Node> > n = node_children (name);
67         if (n.size() > 1) {
68                 throw cxml::Error ("duplicate XML tag " + name);
69         } else if (n.empty ()) {
70                 return shared_ptr<cxml::Node> ();
71         }
72
73         return n.front ();
74 }
75
76 list<shared_ptr<cxml::Node> >
77 cxml::Node::node_children () const
78 {
79         xmlpp::Node::NodeList c = _node->get_children ();
80
81         list<shared_ptr<cxml::Node> > n;
82         for (xmlpp::Node::NodeList::iterator i = c.begin (); i != c.end(); ++i) {
83                 n.push_back (shared_ptr<Node> (new Node (*i)));
84         }
85
86         return n;
87 }
88
89 list<shared_ptr<cxml::Node> >
90 cxml::Node::node_children (string name) const
91 {
92         /* XXX: using find / get_path should work here, but I can't follow
93            how get_path works.
94         */
95
96         xmlpp::Node::NodeList c = _node->get_children ();
97
98         list<shared_ptr<cxml::Node> > n;
99         for (xmlpp::Node::NodeList::iterator i = c.begin (); i != c.end(); ++i) {
100                 if ((*i)->get_name() == name) {
101                         n.push_back (shared_ptr<Node> (new Node (*i)));
102                 }
103         }
104
105         _taken.push_back (name);
106         return n;
107 }
108
109 string
110 cxml::Node::string_child (string c) const
111 {
112         return node_child(c)->content ();
113 }
114
115 optional<string>
116 cxml::Node::optional_string_child (string c) const
117 {
118         list<shared_ptr<Node> > nodes = node_children (c);
119         if (nodes.size() > 1) {
120                 throw cxml::Error ("duplicate XML tag " + c);
121         }
122
123         if (nodes.empty ()) {
124                 return optional<string> ();
125         }
126
127         return nodes.front()->content();
128 }
129
130 bool
131 cxml::Node::bool_child (string c) const
132 {
133         string const s = string_child (c);
134         return (s == "1" || s == "yes" || s == "True");
135 }
136
137 optional<bool>
138 cxml::Node::optional_bool_child (string c) const
139 {
140         optional<string> s = optional_string_child (c);
141         if (!s) {
142                 return optional<bool> ();
143         }
144
145         return (s.get() == "1" || s.get() == "yes" || s.get() == "True");
146 }
147
148 void
149 cxml::Node::ignore_child (string name) const
150 {
151         _taken.push_back (name);
152 }
153
154 string
155 cxml::Node::string_attribute (string name) const
156 {
157         xmlpp::Element const * e = dynamic_cast<const xmlpp::Element *> (_node);
158         if (!e) {
159                 throw cxml::Error ("missing attribute " + name);
160         }
161
162         xmlpp::Attribute* a = e->get_attribute (name);
163         if (!a) {
164                 throw cxml::Error ("missing attribute " + name);
165         }
166
167         return a->get_value ();
168 }
169
170 optional<string>
171 cxml::Node::optional_string_attribute (string name) const
172 {
173         xmlpp::Element const * e = dynamic_cast<const xmlpp::Element *> (_node);
174         if (!e) {
175                 return optional<string> ();
176         }
177
178         xmlpp::Attribute* a = e->get_attribute (name);
179         if (!a) {
180                 return optional<string> ();
181         }
182
183         return string (a->get_value ());
184 }
185
186 bool
187 cxml::Node::bool_attribute (string name) const
188 {
189         string const s = string_attribute (name);
190         return (s == "1" || s == "yes");
191 }
192
193 optional<bool>
194 cxml::Node::optional_bool_attribute (string name) const
195 {
196         optional<string> s = optional_string_attribute (name);
197         if (!s) {
198                 return optional<bool> ();
199         }
200
201         return (s.get() == "1" || s.get() == "yes");
202 }
203
204 void
205 cxml::Node::done () const
206 {
207         xmlpp::Node::NodeList c = _node->get_children ();
208         for (xmlpp::Node::NodeList::iterator i = c.begin(); i != c.end(); ++i) {
209                 if (dynamic_cast<xmlpp::Element *> (*i) && find (_taken.begin(), _taken.end(), (*i)->get_name()) == _taken.end ()) {
210                         throw cxml::Error ("unexpected XML node " + (*i)->get_name());
211                 }
212         }
213 }
214
215 string
216 cxml::Node::content () const
217 {
218         string content;
219
220         xmlpp::Node::NodeList c = _node->get_children ();
221         for (xmlpp::Node::NodeList::const_iterator i = c.begin(); i != c.end(); ++i) {
222                 xmlpp::ContentNode const * v = dynamic_cast<xmlpp::ContentNode const *> (*i);
223                 if (v) {
224                         content += v->get_content ();
225                 }
226         }
227
228         return content;
229 }
230
231 string
232 cxml::Node::namespace_uri () const
233 {
234         return _node->get_namespace_uri ();
235 }
236
237 string
238 cxml::Node::namespace_prefix () const
239 {
240         return _node->get_namespace_prefix ();
241 }
242
243 cxml::Document::Document (string root_name)
244         : _root_name (root_name)
245 {
246         _parser = new xmlpp::DomParser;
247 }
248
249 cxml::Document::Document (string root_name, boost::filesystem::path file)
250         : _root_name (root_name)
251 {
252         _parser = new xmlpp::DomParser ();
253         read_file (file);
254 }
255
256 cxml::Document::Document ()
257 {
258         _parser = new xmlpp::DomParser ();
259 }
260
261 cxml::Document::~Document ()
262 {
263         delete _parser;
264 }
265
266 void
267 cxml::Document::read_file (boost::filesystem::path file)
268 {
269         if (!boost::filesystem::exists (file)) {
270                 throw cxml::Error ("XML file " + file.string() + " does not exist");
271         }
272
273         _parser->parse_file (file.string ());
274         take_root_node ();
275 }
276
277 void
278 cxml::Document::read_string (string s)
279 {
280         _parser->parse_memory (s);
281         take_root_node ();
282 }
283
284 void
285 cxml::Document::take_root_node ()
286 {
287         if (!_parser) {
288                 throw cxml::Error ("could not parse XML");
289         }
290
291         _node = _parser->get_document()->get_root_node ();
292         if (!_root_name.empty() && _node->get_name() != _root_name) {
293                 throw cxml::Error ("unrecognised root node " + _node->get_name() + " (expecting " + _root_name + ")");
294         } else if (_root_name.empty ()) {
295                 _root_name = _node->get_name ();
296         }
297 }