Add pbd/types_convert.h header for PBD::to_string/string_to specialisations
[ardour.git] / libs / pbd / pbd / xml++.h
1 /*
2     Copyright (C) 2012 Paul Davis
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 __XML_H
21 #define __XML_H
22
23 /* xml++.h
24  * libxml++ and this file are copyright (C) 2000 by Ari Johnson, and
25  * are covered by the GNU Lesser General Public License, which should be
26  * included with libxml++ as the file COPYING.
27  * Modified for Ardour and released under the same terms.
28  */
29
30 #include <string>
31 #include <vector>
32 #include <cstdio>
33 #include <cstdarg>
34
35 #include <libxml/parser.h>
36 #include <libxml/tree.h>
37 #include <boost/shared_ptr.hpp>
38
39 #include <glibmm/ustring.h>
40
41 #include "pbd/string_convert.h"
42 #include "pbd/libpbd_visibility.h"
43
44 class XMLTree;
45 class XMLNode;
46
47 class LIBPBD_API XMLProperty {
48 public:
49         XMLProperty(const std::string& n, const std::string& v = std::string());
50         ~XMLProperty();
51
52         const std::string& name() const { return _name; }
53         const std::string& value() const { return _value; }
54         const std::string& set_value(const std::string& v) { return _value = v; }
55
56 private:
57         std::string _name;
58         std::string _value;
59 };
60
61 typedef std::vector<XMLNode *>                   XMLNodeList;
62 typedef std::vector<boost::shared_ptr<XMLNode> > XMLSharedNodeList;
63 typedef XMLNodeList::iterator                    XMLNodeIterator;
64 typedef XMLNodeList::const_iterator              XMLNodeConstIterator;
65 typedef std::vector<XMLProperty*>                XMLPropertyList;
66 typedef XMLPropertyList::iterator                XMLPropertyIterator;
67 typedef XMLPropertyList::const_iterator          XMLPropertyConstIterator;
68
69 class LIBPBD_API XMLTree {
70 public:
71         XMLTree();
72         XMLTree(const std::string& fn, bool validate = false);
73         XMLTree(const XMLTree*);
74         ~XMLTree();
75
76         XMLNode* root() const         { return _root; }
77         XMLNode* set_root(XMLNode* n) { return _root = n; }
78
79         const std::string& filename() const               { return _filename; }
80         const std::string& set_filename(const std::string& fn) { return _filename = fn; }
81
82         int compression() const { return _compression; }
83         int set_compression(int);
84
85         bool read() { return read_internal(false); }
86         bool read(const std::string& fn) { set_filename(fn); return read_internal(false); }
87         bool read_and_validate() { return read_internal(true); }
88         bool read_and_validate(const std::string& fn) { set_filename(fn); return read_internal(true); }
89         bool read_buffer(const std::string&, bool to_tree_doc = false);
90
91         bool write() const;
92         bool write(const std::string& fn) { set_filename(fn); return write(); }
93
94         void debug (FILE*) const;
95
96         const std::string& write_buffer() const;
97
98         boost::shared_ptr<XMLSharedNodeList> find(const std::string xpath, XMLNode* = 0) const;
99
100 private:
101         bool read_internal(bool validate);
102
103         std::string _filename;
104         XMLNode*    _root;
105         xmlDocPtr   _doc;
106         int         _compression;
107 };
108
109 class LIBPBD_API XMLNode {
110 public:
111         XMLNode(const std::string& name);
112         XMLNode(const std::string& name, const std::string& content);
113         XMLNode(const XMLNode& other);
114         ~XMLNode();
115
116         XMLNode& operator= (const XMLNode& other);
117
118         bool operator== (const XMLNode& other) const;
119         bool operator!= (const XMLNode& other) const;
120
121         const std::string& name() const { return _name; }
122
123         bool          is_content() const { return _is_content; }
124         const std::string& content()    const { return _content; }
125         const std::string& set_content(const std::string&);
126         XMLNode*      add_content(const std::string& s = std::string());
127
128         const XMLNodeList& children(const std::string& str = std::string()) const;
129         XMLNode* child(const char*) const;
130         XMLNode* add_child(const char *);
131         XMLNode* add_child_copy(const XMLNode&);
132         void     add_child_nocopy(XMLNode&);
133
134         std::string attribute_value();
135
136         const XMLPropertyList& properties() const { return _proplist; }
137         XMLProperty const *    property(const char*) const;
138         XMLProperty const *    property(const std::string&) const;
139         XMLProperty *    property(const char*);
140         XMLProperty *    property(const std::string&);
141
142         bool has_property_with_value (const std::string&, const std::string&) const;
143
144         XMLProperty* add_property(const char* name, const std::string& value);
145         XMLProperty* add_property(const char* name, const char* value = "");
146         XMLProperty* add_property(const char* name, const long value);
147
148         bool set_property (const char* name, const std::string& value);
149
150         bool set_property (const char* name, const char* cstr) {
151                 return set_property (name, std::string(cstr));
152         }
153
154         bool set_property (const char* name, const Glib::ustring& ustr)
155         {
156                 return set_property (name, ustr.raw ());
157         }
158
159         template<class T>
160         bool set_property (const char* name, const T& value)
161         {
162                 std::string str;
163                 if (!PBD::to_string<T> (value, str)) {
164                         return false;
165                 }
166                 return set_property(name, str);
167         }
168
169         bool get_property (const char* name, std::string& value) const;
170
171         template <class T>
172         bool get_property (const char* name, T& value) const
173         {
174                 XMLProperty const* const prop = property (name);
175                 if (!prop) {
176                         return false;
177                 }
178
179                 return PBD::string_to<T> (prop->value (), value);
180         }
181
182         void remove_property(const std::string&);
183         void remove_property_recursively(const std::string&);
184
185         /** Remove all nodes with the name passed to remove_nodes */
186         void remove_nodes(const std::string&);
187         /** Remove and delete all nodes with the name passed to remove_nodes */
188         void remove_nodes_and_delete(const std::string&);
189         /** Remove and delete all nodes with property prop matching val */
190         void remove_nodes_and_delete(const std::string& propname, const std::string& val);
191
192         void dump (std::ostream &, std::string p = "") const;
193
194 private:
195         std::string         _name;
196         bool                _is_content;
197         std::string         _content;
198         XMLNodeList         _children;
199         XMLPropertyList     _proplist;
200         mutable XMLNodeList _selected_children;
201
202         void clear_lists ();
203 };
204
205 class LIBPBD_API XMLException: public std::exception {
206 public:
207         explicit XMLException(const std::string msg) : _message(msg) {}
208         virtual ~XMLException() throw() {}
209
210         virtual const char* what() const throw() { return _message.c_str(); }
211
212 private:
213         std::string _message;
214 };
215
216 #endif /* __XML_H */
217