Ardour  9.0-pre0-582-g084a23a80d
xml++.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2016 Paul Davis <paul@linuxaudiosystems.com>
3  * Copyright (C) 2006 Taybin Rutkin <taybin@taybin.com>
4  * Copyright (C) 2008-2009 David Robillard <d@drobilla.net>
5  * Copyright (C) 2008 Hans Baier <hansfbaier@googlemail.com>
6  * Copyright (C) 2009-2011 Carl Hetherington <carl@carlh.net>
7  * Copyright (C) 2015-2017 Robin Gareus <robin@gareus.org>
8  * Copyright (C) 2016 Tim Mayberry <mojofunk@gmail.com>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License along
21  * with this program; if not, write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23  */
24 
25 #ifndef __XML_H
26 #define __XML_H
27 
28 /* xml++.h
29  * libxml++ and this file are copyright (C) 2000 by Ari Johnson, and
30  * are covered by the GNU Lesser General Public License, which should be
31  * included with libxml++ as the file COPYING.
32  * Modified for Ardour and released under the same terms.
33  */
34 
35 #include <cstdarg>
36 #include <cstdio>
37 #include <memory>
38 #include <string>
39 #include <vector>
40 
41 #include <libxml/parser.h>
42 #include <libxml/tree.h>
43 
44 #include <glibmm/ustring.h>
45 
46 #include "pbd/string_convert.h"
47 #include "pbd/libpbd_visibility.h"
48 
49 class XMLTree;
50 class XMLNode;
51 
53 public:
54  XMLProperty(const std::string& n, const std::string& v = std::string());
56 
57  const std::string& name() const { return _name; }
58  const std::string& value() const { return _value; }
59  const std::string& set_value(const std::string& v) { return _value = v; }
60 
61 private:
62  std::string _name;
63  std::string _value;
64 };
65 
66 typedef std::vector<XMLNode *> XMLNodeList;
67 typedef std::vector<std::shared_ptr<XMLNode> > XMLSharedNodeList;
68 typedef XMLNodeList::iterator XMLNodeIterator;
69 typedef XMLNodeList::const_iterator XMLNodeConstIterator;
70 typedef std::vector<XMLProperty*> XMLPropertyList;
71 typedef XMLPropertyList::iterator XMLPropertyIterator;
72 typedef XMLPropertyList::const_iterator XMLPropertyConstIterator;
73 
75 public:
77  XMLTree(const std::string& fn, bool validate = false);
78  XMLTree(const XMLTree*);
80 
81  XMLNode* root() const { return _root; }
82  XMLNode* set_root(XMLNode* n) { return _root = n; }
83 
84  const std::string& filename() const { return _filename; }
85  const std::string& set_filename(const std::string& fn) { return _filename = fn; }
86 
87  int compression() const { return _compression; }
88  int set_compression(int);
89 
90  bool read() { return read_internal(false); }
91  bool read(const std::string& fn) { set_filename(fn); return read_internal(false); }
92  bool read_and_validate() { return read_internal(true); }
93  bool read_and_validate(const std::string& fn) { set_filename(fn); return read_internal(true); }
94  bool read_buffer(char const*, bool to_tree_doc = false);
95 
96  bool write() const;
97  bool write(const std::string& fn) { set_filename(fn); return write(); }
98 
99  void debug (FILE*) const;
100 
101  const std::string& write_buffer() const;
102 
103  std::shared_ptr<XMLSharedNodeList> find(const std::string xpath, XMLNode* = 0) const;
104 
105 private:
106  bool read_internal(bool validate);
107 
108  std::string _filename;
110  xmlDocPtr _doc;
112 };
113 
115 public:
116  XMLNode(const std::string& name);
117  XMLNode(const std::string& name, const std::string& content);
118  XMLNode(const XMLNode& other);
120 
121  XMLNode& operator= (const XMLNode& other);
122 
123  bool operator== (const XMLNode& other) const;
124  bool operator!= (const XMLNode& other) const;
125 
126  const std::string& name() const { return _name; }
127 
128  bool is_content() const { return _is_content; }
129  const std::string& content() const { return _content; }
130  const std::string& set_content(const std::string&);
131  XMLNode* add_content(const std::string& s = std::string());
132 
133  const std::string& child_content() const;
134 
135  const XMLNodeList& children(const std::string& str = std::string()) const;
136  XMLNode* child(const char*) const;
137  XMLNode* add_child(const char *);
140 
141  std::string attribute_value(); //throws XMLException if attribute doesn't exist
142 
143  const XMLPropertyList& properties() const { return _proplist; }
144  XMLProperty const * property(const char*) const;
145  XMLProperty const * property(const std::string&) const;
146  XMLProperty * property(const char*);
147  XMLProperty * property(const std::string&);
148 
149  bool has_property_with_value (const std::string&, const std::string&) const;
150 
151  bool set_property (const char* name, const std::string& value);
152 
153  bool set_property (const char* name, const char* cstr) {
154  return set_property (name, std::string(cstr));
155  }
156 
157  bool set_property (const char* name, const Glib::ustring& ustr)
158  {
159  return set_property (name, ustr.raw ());
160  }
161 
162  template<class T>
163  bool set_property (const char* name, const T& value)
164  {
165  std::string str;
166  if (!PBD::to_string<T> (value, str)) {
167  return false;
168  }
169  return set_property(name, str);
170  }
171 
172  bool get_property (const char* name, std::string& value) const;
173 
174  template <class T>
175  bool get_property (const char* name, T& value) const
176  {
177  XMLProperty const* const prop = property (name);
178  if (!prop) {
179  return false;
180  }
181 
182  return PBD::string_to<T> (prop->value (), value);
183  }
184 
185  void remove_property(const std::string&);
186  void remove_property_recursively(const std::string&);
187 
189  void remove_nodes (const std::string&);
191  void remove_nodes_and_delete (const std::string&);
193  void remove_nodes_and_delete (const std::string& propname, const std::string& val);
195  void remove_node_and_delete (const std::string& n, const std::string& propname, const std::string& val);
196 
197  void dump (std::ostream &, std::string p = "") const;
198 
199 private:
200  std::string _name;
202  std::string _content;
206 
207  void clear_lists ();
208 };
209 
210 class LIBPBD_API XMLException: public std::exception {
211 public:
212  explicit XMLException(const std::string msg) : _message(msg) {}
213  virtual ~XMLException() throw() {}
214 
215  virtual const char* what() const throw() { return _message.c_str(); }
216 
217 private:
218  std::string _message;
219 };
220 
221 #endif /* __XML_H */
222 
XMLException(const std::string msg)
Definition: xml++.h:212
virtual ~XMLException()
Definition: xml++.h:213
virtual const char * what() const
Definition: xml++.h:215
std::string _message
Definition: xml++.h:218
Definition: xml++.h:114
bool has_property_with_value(const std::string &, const std::string &) const
XMLNode(const XMLNode &other)
const std::string & child_content() const
void remove_nodes_and_delete(const std::string &propname, const std::string &val)
const XMLNodeList & children(const std::string &str=std::string()) const
const std::string & name() const
Definition: xml++.h:126
void remove_node_and_delete(const std::string &n, const std::string &propname, const std::string &val)
void clear_lists()
const std::string & content() const
Definition: xml++.h:129
bool set_property(const char *name, const char *cstr)
Definition: xml++.h:153
XMLNode(const std::string &name, const std::string &content)
void remove_property_recursively(const std::string &)
bool set_property(const char *name, const std::string &value)
void dump(std::ostream &, std::string p="") const
XMLNode(const std::string &name)
XMLPropertyList _proplist
Definition: xml++.h:204
bool set_property(const char *name, const T &value)
Definition: xml++.h:163
XMLProperty const * property(const char *) const
void remove_nodes_and_delete(const std::string &)
bool get_property(const char *name, std::string &value) const
bool _is_content
Definition: xml++.h:201
XMLNode * child(const char *) const
const std::string & set_content(const std::string &)
XMLProperty const * property(const std::string &) const
XMLNodeList _children
Definition: xml++.h:203
void remove_property(const std::string &)
void add_child_nocopy(XMLNode &)
XMLNode * add_child_copy(const XMLNode &)
XMLProperty * property(const std::string &)
bool set_property(const char *name, const Glib::ustring &ustr)
Definition: xml++.h:157
void remove_nodes(const std::string &)
XMLProperty * property(const char *)
const XMLPropertyList & properties() const
Definition: xml++.h:143
std::string attribute_value()
XMLNodeList _selected_children
Definition: xml++.h:205
std::string _name
Definition: xml++.h:200
XMLNode * add_content(const std::string &s=std::string())
bool get_property(const char *name, T &value) const
Definition: xml++.h:175
bool is_content() const
Definition: xml++.h:128
std::string _content
Definition: xml++.h:202
XMLNode * add_child(const char *)
const std::string & name() const
Definition: xml++.h:57
const std::string & value() const
Definition: xml++.h:58
std::string _value
Definition: xml++.h:63
XMLProperty(const std::string &n, const std::string &v=std::string())
std::string _name
Definition: xml++.h:62
const std::string & set_value(const std::string &v)
Definition: xml++.h:59
Definition: xml++.h:74
XMLTree(const std::string &fn, bool validate=false)
bool read_and_validate(const std::string &fn)
Definition: xml++.h:93
bool read_buffer(char const *, bool to_tree_doc=false)
bool write(const std::string &fn)
Definition: xml++.h:97
bool write() const
void debug(FILE *) const
XMLTree(const XMLTree *)
XMLNode * _root
Definition: xml++.h:109
int set_compression(int)
const std::string & write_buffer() const
std::string _filename
Definition: xml++.h:108
bool read()
Definition: xml++.h:90
bool read_internal(bool validate)
int _compression
Definition: xml++.h:111
int compression() const
Definition: xml++.h:87
const std::string & set_filename(const std::string &fn)
Definition: xml++.h:85
bool read_and_validate()
Definition: xml++.h:92
std::shared_ptr< XMLSharedNodeList > find(const std::string xpath, XMLNode *=0) const
xmlDocPtr _doc
Definition: xml++.h:110
XMLNode * root() const
Definition: xml++.h:81
const std::string & filename() const
Definition: xml++.h:84
bool read(const std::string &fn)
Definition: xml++.h:91
XMLNode * set_root(XMLNode *n)
Definition: xml++.h:82
GtkImageIconNameData name
Definition: gtkimage.h:6
#define LIBPBD_API
GTKMM_API const Gtk::BuiltinStockID FILE
bool operator==(const ProcessorSelection &a, const ProcessorSelection &b)
std::vector< XMLProperty * > XMLPropertyList
Definition: xml++.h:70
XMLPropertyList::iterator XMLPropertyIterator
Definition: xml++.h:71
std::vector< std::shared_ptr< XMLNode > > XMLSharedNodeList
Definition: xml++.h:67
XMLNodeList::iterator XMLNodeIterator
Definition: xml++.h:68
XMLPropertyList::const_iterator XMLPropertyConstIterator
Definition: xml++.h:72
std::vector< XMLNode * > XMLNodeList
Definition: xml++.h:66
XMLNodeList::const_iterator XMLNodeConstIterator
Definition: xml++.h:69