993cb22fdfff14adf2810f8f4b5fc7b7eb9956a1
[ardour.git] / libs / pbd3 / pbd / xml++.h
1 /* xml++.h
2  * libxml++ and this file are copyright (C) 2000 by Ari Johnson, and
3  * are covered by the GNU Lesser General Public License, which should be
4  * included with libxml++ as the file COPYING.
5  */
6
7 #include <string>
8 #include <list>
9 #include <map>
10 #include <cstdio>
11 #include <cstdarg>
12
13 #include <libxml/parser.h>
14 #include <libxml/tree.h>
15
16 #ifndef __XML_H
17 #define __XML_H
18
19 using std::string;
20 using std::map;
21 using std::list;
22
23 class XMLTree;
24 class XMLNode;
25 class XMLProperty;
26
27 typedef list<XMLNode *> XMLNodeList;
28 typedef XMLNodeList::iterator XMLNodeIterator;
29 typedef XMLNodeList::const_iterator XMLNodeConstIterator;
30 typedef list<XMLProperty*> XMLPropertyList;
31 typedef XMLPropertyList::iterator XMLPropertyIterator;
32 typedef XMLPropertyList::const_iterator XMLPropertyConstIterator;
33 typedef map<string, XMLProperty*> XMLPropertyMap;
34
35 class XMLTree {
36 private:
37   string _filename;
38   XMLNode *_root;
39   int _compression;
40   bool _initialized;
41
42 public:
43   XMLTree();
44   XMLTree(const string &fn);
45   XMLTree(const XMLTree *);
46   ~XMLTree();
47
48   bool initialized() const { return _initialized; };
49   XMLNode *root() const { return _root; };
50   XMLNode *set_root(XMLNode *n) { return _root = n; };
51
52   const string & filename() const { return _filename; };
53   const string & set_filename(const string &fn) { return _filename = fn; };
54
55   int compression() const { return _compression; };
56   int set_compression(int);
57
58   bool read();
59   bool read(const string &fn) { set_filename(fn); return read(); };
60   bool read_buffer(const string &);
61
62   bool write() const;
63   bool write(const string &fn) { set_filename(fn); return write(); };
64
65   void debug (FILE*) const;
66
67   const string & write_buffer() const;
68 };
69
70 class XMLNode {
71 private:
72   bool _initialized;
73   string _name;
74   bool _is_content;
75   string _content;
76   XMLNodeList _children;
77   XMLPropertyList _proplist;
78   XMLPropertyMap _propmap;
79
80 public:
81   XMLNode(const string &);
82   XMLNode(const string &, const string &);
83   XMLNode(const XMLNode&);
84   ~XMLNode();
85
86   bool initialized() const { return _initialized; };
87   const string name() const { return _name; };
88
89   bool is_content() const { return _is_content; };
90   const string & content() const { return _content; };
91   const string & set_content(const string &);
92   XMLNode *add_content(const string & = string());
93
94   const XMLNodeList & children(const string & = string()) const;
95   XMLNode *add_child(const string &);
96   XMLNode *add_child_copy(const XMLNode&);
97   void     add_child_nocopy (XMLNode&);
98
99   const XMLPropertyList & properties() const { return _proplist; };
100   XMLProperty *property(const string &);
101   const XMLProperty *property(const string &n) const
102         { return ((XMLNode *) this)->property(n); };
103   XMLProperty *add_property(const string &, const string & = string());
104   void remove_property(const string &);
105
106   /** Remove all nodes with the name passed to remove_nodes */
107   void remove_nodes(const string &);
108   /** Remove and delete all nodes with the name passed to remove_nodes */
109   void remove_nodes_and_delete(const string &);
110 };
111
112 class XMLProperty {
113 private:
114   string _name;
115   string _value;
116
117 public:
118   XMLProperty(const string &n, const string &v = string());
119   ~XMLProperty();
120
121   const string & name() const { return _name; };
122   const string & value() const { return _value; };
123   const string & set_value(const string &v) { return _value = v; };
124 };
125
126 #endif /* __XML_H */
127