290a449869128382604ad51c550bd79d0f4a06a8
[ardour.git] / libs / pbd / 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 #include <boost/shared_ptr.hpp>
16
17 #ifndef __XML_H
18 #define __XML_H
19
20 using std::string;
21 using std::map;
22 using std::list;
23
24 class XMLTree;
25 class XMLNode;
26 class XMLProperty;
27
28 typedef list<XMLNode *> XMLNodeList;
29 typedef list<boost::shared_ptr<XMLNode> > XMLSharedNodeList;
30 typedef XMLNodeList::iterator XMLNodeIterator;
31 typedef XMLNodeList::const_iterator XMLNodeConstIterator;
32 typedef list<XMLProperty*> XMLPropertyList;
33 typedef XMLPropertyList::iterator XMLPropertyIterator;
34 typedef XMLPropertyList::const_iterator XMLPropertyConstIterator;
35 typedef map<string, XMLProperty*> XMLPropertyMap;
36
37 class XMLTree {
38 private:
39   string _filename;
40   XMLNode *_root;
41   int _compression;
42   bool read_internal(bool validate);
43   
44 public:
45   XMLTree();
46   XMLTree(const string &fn, bool validate = false);
47   XMLTree(const XMLTree *);
48   ~XMLTree();
49
50   XMLNode *root() const { return _root; };
51   XMLNode *set_root(XMLNode *n) { return _root = n; };
52
53   const string & filename() const { return _filename; };
54   const string & set_filename(const string &fn) { return _filename = fn; };
55
56   int compression() const { return _compression; };
57   int set_compression(int);
58
59   bool read() { return read_internal(false); };
60   bool read(const string &fn) { set_filename(fn); return read_internal(false); };
61   bool read_and_validate() { return read_internal(true); };
62   bool read_and_validate(const string &fn) { set_filename(fn); return read_internal(true); };
63   bool read_buffer(const string &);
64
65   bool write() const;
66   bool write(const string &fn) { set_filename(fn); return write(); };
67
68   void debug (FILE*) const;
69
70   const string & write_buffer() const;
71 };
72
73 class XMLNode {
74 private:
75   string _name;
76   bool _is_content;
77   string _content;
78   XMLNodeList _children;
79   XMLPropertyList _proplist;
80   XMLPropertyMap _propmap;
81
82 public:
83   XMLNode(const string& name);
84   XMLNode(const string& name, const string& content);
85   XMLNode(const XMLNode& other);
86   ~XMLNode();
87
88   const string name() const { return _name; };
89
90   bool is_content() const { return _is_content; };
91   const string & content() const { return _content; };
92   const string & set_content (const string &);
93   XMLNode *add_content(const string & = string());
94
95   const XMLNodeList & children (const string& str = string()) const;
96   XMLNode *add_child (const char *);
97   XMLNode *add_child_copy (const XMLNode&);
98   XMLNode *child (const char*) const;
99   void add_child_nocopy (XMLNode&);
100   
101   boost::shared_ptr<XMLSharedNodeList> find(const std::string xpath) const;
102   std::string attribute_value();
103   
104   const XMLPropertyList & properties() const { return _proplist; };
105   XMLProperty *property(const char * );
106   XMLProperty *property(const std::string&);
107   const XMLProperty *property(const char * n) const
108         { return ((XMLNode *) this)->property(n); };
109   const XMLProperty *property(const std::string& ns) const
110         { return ((XMLNode *) this)->property(ns); };
111   XMLProperty *add_property(const char *name, const string& value);
112   XMLProperty *add_property(const char *name, const char *value = "");
113   XMLProperty *add_property(const char *name, const long value);
114
115   void remove_property(const string &);
116
117   /** Remove all nodes with the name passed to remove_nodes */
118   void remove_nodes(const string &);
119   /** Remove and delete all nodes with the name passed to remove_nodes */
120   void remove_nodes_and_delete(const string &);
121   /** Remove and delete all nodes with property prop matching val */
122   void remove_nodes_and_delete(const string& propname, const string& val);
123 };
124
125 class XMLProperty {
126 private:
127   string _name;
128   string _value;
129
130 public:
131   XMLProperty(const string &n, const string &v = string());
132   ~XMLProperty();
133
134   const string & name() const { return _name; };
135   const string & value() const { return _value; };
136   const string & set_value(const string &v) { return _value = v; };
137 };
138
139 class XMLException: public std::exception
140 {
141 public:
142   explicit XMLException(const string message)
143   : message_(message)
144   {
145   }
146   
147   virtual ~XMLException() throw() {};
148
149   virtual const char* what() const throw() { return message_.c_str(); }
150   virtual void Raise() const { throw *this; }
151   virtual exception * Clone() const { return new exception(*this); }
152
153 private:
154   string message_;
155 };
156
157 #endif /* __XML_H */
158