merge with master and fix 4 conflicts by hand
[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 <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;
44 class XMLProperty;
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
55 class LIBPBD_API XMLTree {
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;
90         XMLNode*    _root;
91         xmlDocPtr   _doc;
92         int         _compression;
93 };
94
95 class LIBPBD_API XMLNode {
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
132         /** Remove all nodes with the name passed to remove_nodes */
133         void remove_nodes(const std::string&);
134         /** Remove and delete all nodes with the name passed to remove_nodes */
135         void remove_nodes_and_delete(const std::string&);
136         /** Remove and delete all nodes with property prop matching val */
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;
143         bool                _is_content;
144         std::string         _content;
145         XMLNodeList         _children;
146         XMLPropertyList     _proplist;
147         XMLPropertyMap      _propmap;
148         mutable XMLNodeList _selected_children;
149
150         void clear_lists ();
151 };
152
153 class LIBPBD_API XMLProperty {
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