ardour
xml++.h
Go to the documentation of this file.
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 <list>
32 #include <map>
33 #include <cstdio>
34 #include <cstdarg>
35 
36 #include <libxml/parser.h>
37 #include <libxml/tree.h>
38 #include <boost/shared_ptr.hpp>
39 
40 #include "pbd/libpbd_visibility.h"
41 
42 class XMLTree;
43 class XMLNode;
45 
46 typedef std::list<XMLNode *> XMLNodeList;
47 typedef std::list<boost::shared_ptr<XMLNode> > XMLSharedNodeList;
48 typedef XMLNodeList::iterator XMLNodeIterator;
49 typedef XMLNodeList::const_iterator XMLNodeConstIterator;
50 typedef std::list<XMLProperty*> XMLPropertyList;
51 typedef XMLPropertyList::iterator XMLPropertyIterator;
52 typedef XMLPropertyList::const_iterator XMLPropertyConstIterator;
53 typedef std::map<std::string, XMLProperty*> XMLPropertyMap;
54 
56 public:
57  XMLTree();
58  XMLTree(const std::string& fn, bool validate = false);
59  XMLTree(const XMLTree*);
60  ~XMLTree();
61 
62  XMLNode* root() const { return _root; }
63  XMLNode* set_root(XMLNode* n) { return _root = n; }
64 
65  const std::string& filename() const { return _filename; }
66  const std::string& set_filename(const std::string& fn) { return _filename = fn; }
67 
68  int compression() const { return _compression; }
69  int set_compression(int);
70 
71  bool read() { return read_internal(false); }
72  bool read(const std::string& fn) { set_filename(fn); return read_internal(false); }
73  bool read_and_validate() { return read_internal(true); }
74  bool read_and_validate(const std::string& fn) { set_filename(fn); return read_internal(true); }
75  bool read_buffer(const std::string&);
76 
77  bool write() const;
78  bool write(const std::string& fn) { set_filename(fn); return write(); }
79 
80  void debug (FILE*) const;
81 
82  const std::string& write_buffer() const;
83 
84  boost::shared_ptr<XMLSharedNodeList> find(const std::string xpath, XMLNode* = 0) const;
85 
86 private:
87  bool read_internal(bool validate);
88 
89  std::string _filename;
91  xmlDocPtr _doc;
93 };
94 
96 public:
97  XMLNode(const std::string& name);
98  XMLNode(const std::string& name, const std::string& content);
99  XMLNode(const XMLNode& other);
100  ~XMLNode();
101 
102  XMLNode& operator= (const XMLNode& other);
103 
104  const std::string& name() const { return _name; }
105 
106  bool is_content() const { return _is_content; }
107  const std::string& content() const { return _content; }
108  const std::string& set_content(const std::string&);
109  XMLNode* add_content(const std::string& s = std::string());
110 
111  const XMLNodeList& children(const std::string& str = std::string()) const;
112  XMLNode* child(const char*) const;
113  XMLNode* add_child(const char *);
114  XMLNode* add_child_copy(const XMLNode&);
115  void add_child_nocopy(XMLNode&);
116 
117  std::string attribute_value();
118 
119  const XMLPropertyList& properties() const { return _proplist; }
120  XMLProperty* property(const char*);
121  XMLProperty* property(const std::string&);
122  const XMLProperty* property(const char* n) const { return const_cast<XMLNode*>(this)->property(n); }
123  const XMLProperty* property(const std::string& n) const { return const_cast<XMLNode*>(this)->property(n); }
124 
125  XMLProperty* add_property(const char* name, const std::string& value);
126  XMLProperty* add_property(const char* name, const char* value = "");
127  XMLProperty* add_property(const char* name, const long value);
128 
129  void remove_property(const std::string&);
130  void remove_property_recursively(const std::string&);
131 
133  void remove_nodes(const std::string&);
135  void remove_nodes_and_delete(const std::string&);
137  void remove_nodes_and_delete(const std::string& propname, const std::string& val);
138 
139  void dump (std::ostream &, std::string p = "") const;
140 
141 private:
142  std::string _name;
144  std::string _content;
149 
150  void clear_lists ();
151 };
152 
154 public:
155  XMLProperty(const std::string& n, const std::string& v = std::string());
156  ~XMLProperty();
157 
158  const std::string& name() const { return _name; }
159  const std::string& value() const { return _value; }
160  const std::string& set_value(const std::string& v) { return _value = v; }
161 
162 private:
163  std::string _name;
164  std::string _value;
165 };
166 
167 class LIBPBD_API XMLException: public std::exception {
168 public:
169  explicit XMLException(const std::string msg) : _message(msg) {}
170  virtual ~XMLException() throw() {}
171 
172  virtual const char* what() const throw() { return _message.c_str(); }
173 
174 private:
175  std::string _message;
176 };
177 
178 #endif /* __XML_H */
179 
XMLPropertyList::const_iterator XMLPropertyConstIterator
Definition: xml++.h:52
virtual ~XMLException()
Definition: xml++.h:170
XMLNodeList::iterator XMLNodeIterator
Definition: xml++.h:48
std::list< boost::shared_ptr< XMLNode > > XMLSharedNodeList
Definition: xml++.h:47
const XMLPropertyList & properties() const
Definition: xml++.h:119
const std::string & content() const
Definition: xml++.h:107
const std::string & value() const
Definition: xml++.h:159
#define LIBPBD_API
bool _is_content
Definition: xml++.h:143
const XMLProperty * property(const std::string &n) const
Definition: xml++.h:123
XMLNodeList _children
Definition: xml++.h:145
xmlDocPtr _doc
Definition: xml++.h:91
int compression() const
Definition: xml++.h:68
const std::string & name() const
Definition: xml++.h:104
const std::string & name() const
Definition: xml++.h:158
bool read_and_validate(const std::string &fn)
Definition: xml++.h:74
std::list< XMLProperty * > XMLPropertyList
Definition: xml++.h:50
std::string _content
Definition: xml++.h:144
int _compression
Definition: xml++.h:92
std::string _filename
Definition: xml++.h:89
XMLNodeList _selected_children
Definition: xml++.h:148
std::map< std::string, XMLProperty * > XMLPropertyMap
Definition: xml++.h:53
bool read_and_validate()
Definition: xml++.h:73
bool write(const std::string &fn)
Definition: xml++.h:78
Definition: xml++.h:55
std::list< XMLNode * > XMLNodeList
Definition: xml++.h:44
virtual const char * what() const
Definition: xml++.h:172
const XMLProperty * property(const char *n) const
Definition: xml++.h:122
std::string _message
Definition: xml++.h:175
XMLNode * set_root(XMLNode *n)
Definition: xml++.h:63
XMLNode * root() const
Definition: xml++.h:62
XMLNode * _root
Definition: xml++.h:90
bool read()
Definition: xml++.h:71
const std::string & filename() const
Definition: xml++.h:65
RouteGroup::RouteGroup(Session &s, const string &n) add_property(_relative)
Definition: route_group.cc:101
bool read(const std::string &fn)
Definition: xml++.h:72
const std::string & set_filename(const std::string &fn)
Definition: xml++.h:66
std::string _value
Definition: xml++.h:164
XMLPropertyMap _propmap
Definition: xml++.h:147
std::string _name
Definition: xml++.h:142
const char * name
Definition: xml++.h:95
XMLPropertyList _proplist
Definition: xml++.h:146
XMLException(const std::string msg)
Definition: xml++.h:169
bool is_content() const
Definition: xml++.h:106
XMLPropertyList::iterator XMLPropertyIterator
Definition: xml++.h:51
std::string _name
Definition: xml++.h:163
const std::string & set_value(const std::string &v)
Definition: xml++.h:160
XMLNodeList::const_iterator XMLNodeConstIterator
Definition: xml++.h:49