]>
Commit | Line | Data |
---|---|---|
56d2f750 VS |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: xml.h | |
3 | // Purpose: wxXmlDocument - XML parser & data holder class | |
4 | // Author: Vaclav Slavik | |
5 | // Created: 2000/03/05 | |
6 | // RCS-ID: $Id$ | |
7 | // Copyright: (c) 2000 Vaclav Slavik | |
8 | // Licence: wxWindows licence | |
9 | ///////////////////////////////////////////////////////////////////////////// | |
10 | ||
11 | #ifndef _WX_XML_H_ | |
12 | #define _WX_XML_H_ | |
13 | ||
14 | #ifdef __GNUG__ | |
15 | #pragma interface "xml.h" | |
16 | #endif | |
17 | ||
18 | #include "wx/defs.h" | |
19 | #include "wx/string.h" | |
20 | #include "wx/object.h" | |
21 | #include "wx/list.h" | |
22 | ||
ea89ec17 RD |
23 | #ifdef WXXMLISDLL |
24 | #define WXXMLDLLEXPORT WXDLLEXPORT | |
25 | #else | |
26 | #define WXXMLDLLEXPORT | |
27 | #endif | |
56d2f750 | 28 | |
ea89ec17 RD |
29 | class WXXMLDLLEXPORT wxXmlNode; |
30 | class WXXMLDLLEXPORT wxXmlProperty; | |
31 | class WXXMLDLLEXPORT wxXmlDocument; | |
32 | class WXXMLDLLEXPORT wxXmlIOHandler; | |
1ad83f6b VS |
33 | class WXDLLEXPORT wxInputStream; |
34 | class WXDLLEXPORT wxOutputStream; | |
56d2f750 VS |
35 | |
36 | ||
37 | // Represents XML node type. | |
38 | enum wxXmlNodeType | |
39 | { | |
40 | // note: values are synchronized with xmlElementType from libxml | |
41 | wxXML_ELEMENT_NODE = 1, | |
42 | wxXML_ATTRIBUTE_NODE = 2, | |
43 | wxXML_TEXT_NODE = 3, | |
44 | wxXML_CDATA_SECTION_NODE = 4, | |
45 | wxXML_ENTITY_REF_NODE = 5, | |
46 | wxXML_ENTITY_NODE = 6, | |
47 | wxXML_PI_NODE = 7, | |
48 | wxXML_COMMENT_NODE = 8, | |
49 | wxXML_DOCUMENT_NODE = 9, | |
50 | wxXML_DOCUMENT_TYPE_NODE = 10, | |
51 | wxXML_DOCUMENT_FRAG_NODE = 11, | |
52 | wxXML_NOTATION_NODE = 12, | |
53 | wxXML_HTML_DOCUMENT_NODE = 13 | |
54 | }; | |
55 | ||
56 | ||
57 | // Types of XML files: | |
58 | ||
59 | enum wxXmlIOType | |
60 | { | |
eb8671f2 VS |
61 | wxXML_IO_AUTO = 0, // detect it automatically |
62 | wxXML_IO_EXPAT, // use Expat to load from text/xml document | |
63 | wxXML_IO_TEXT_OUTPUT, // generic saver into text/xml | |
64 | wxXML_IO_BIN, // save in binary uncompressed proprietary format | |
65 | wxXML_IO_BINZ // svae in binary zlib-compressed proprietary format | |
56d2f750 VS |
66 | }; |
67 | ||
68 | ||
69 | // Represents node property(ies). | |
70 | // Example: in <img src="hello.gif" id="3"/> "src" is property with value | |
71 | // "hello.gif" and "id" is prop. with value "3". | |
72 | ||
ea89ec17 | 73 | class WXXMLDLLEXPORT wxXmlProperty |
56d2f750 | 74 | { |
eb8671f2 VS |
75 | public: |
76 | wxXmlProperty() : m_next(NULL) {} | |
ea89ec17 | 77 | wxXmlProperty(const wxString& name, const wxString& value, |
eb8671f2 VS |
78 | wxXmlProperty *next) |
79 | : m_name(name), m_value(value), m_next(next) {} | |
eb8671f2 VS |
80 | |
81 | wxString GetName() const { return m_name; } | |
82 | wxString GetValue() const { return m_value; } | |
83 | wxXmlProperty *GetNext() const { return m_next; } | |
84 | ||
85 | void SetName(const wxString& name) { m_name = name; } | |
86 | void SetValue(const wxString& value) { m_value = value; } | |
87 | void SetNext(wxXmlProperty *next) { m_next = next; } | |
88 | ||
89 | private: | |
90 | wxString m_name; | |
91 | wxString m_value; | |
92 | wxXmlProperty *m_next; | |
56d2f750 VS |
93 | }; |
94 | ||
95 | ||
96 | ||
97 | // Represents node in XML document. Node has name and may have content | |
98 | // and properties. Most common node types are wxXML_TEXT_NODE (name and props | |
99 | // are irrelevant) and wxXML_ELEMENT_NODE (e.g. in <title>hi</title> there is | |
100 | // element with name="title", irrelevant content and one child (wxXML_TEXT_NODE | |
101 | // with content="hi"). | |
eb8671f2 | 102 | // |
cc30b233 VS |
103 | // If wxUSE_UNICODE is 0, all strings are encoded in the encoding given to Load |
104 | // (default is UTF-8). | |
56d2f750 | 105 | |
ea89ec17 | 106 | class WXXMLDLLEXPORT wxXmlNode |
56d2f750 | 107 | { |
eb8671f2 | 108 | public: |
ea89ec17 | 109 | wxXmlNode() : m_properties(NULL), m_parent(NULL), |
eb8671f2 | 110 | m_children(NULL), m_next(NULL) {} |
ea89ec17 | 111 | wxXmlNode(wxXmlNode *parent,wxXmlNodeType type, |
eb8671f2 VS |
112 | const wxString& name, const wxString& content, |
113 | wxXmlProperty *props, wxXmlNode *next); | |
2f0bac12 | 114 | ~wxXmlNode(); |
eb8671f2 VS |
115 | |
116 | // copy ctor & operator=. Note that this does NOT copy syblings | |
117 | // and parent pointer, i.e. m_parent and m_next will be NULL | |
118 | // after using copy ctor and are never unmodified by operator=. | |
119 | // On the other hand, it DOES copy children and properties. | |
120 | wxXmlNode(const wxXmlNode& node); | |
121 | wxXmlNode& operator=(const wxXmlNode& node); | |
122 | ||
123 | // user-friendly creation: | |
ea89ec17 | 124 | wxXmlNode(wxXmlNodeType type, const wxString& name, |
eb8671f2 VS |
125 | const wxString& content = wxEmptyString); |
126 | void AddChild(wxXmlNode *child); | |
127 | void InsertChild(wxXmlNode *child, wxXmlNode *before_node); | |
128 | bool RemoveChild(wxXmlNode *child); | |
129 | void AddProperty(const wxString& name, const wxString& value); | |
130 | bool DeleteProperty(const wxString& name); | |
131 | ||
132 | // access methods: | |
133 | wxXmlNodeType GetType() const { return m_type; } | |
134 | wxString GetName() const { return m_name; } | |
135 | wxString GetContent() const { return m_content; } | |
136 | ||
137 | wxXmlNode *GetParent() const { return m_parent; } | |
138 | wxXmlNode *GetNext() const { return m_next; } | |
139 | wxXmlNode *GetChildren() const { return m_children; } | |
140 | ||
141 | wxXmlProperty *GetProperties() const { return m_properties; } | |
142 | bool GetPropVal(const wxString& propName, wxString *value) const; | |
ea89ec17 | 143 | wxString GetPropVal(const wxString& propName, |
eb8671f2 VS |
144 | const wxString& defaultVal) const; |
145 | bool HasProp(const wxString& propName) const; | |
146 | ||
147 | void SetType(wxXmlNodeType type) { m_type = type; } | |
148 | void SetName(const wxString& name) { m_name = name; } | |
149 | void SetContent(const wxString& con) { m_content = con; } | |
150 | ||
151 | void SetParent(wxXmlNode *parent) { m_parent = parent; } | |
152 | void SetNext(wxXmlNode *next) { m_next = next; } | |
153 | void SetChildren(wxXmlNode *child) { m_children = child; } | |
154 | ||
155 | void SetProperties(wxXmlProperty *prop) { m_properties = prop; } | |
156 | void AddProperty(wxXmlProperty *prop); | |
157 | ||
158 | private: | |
159 | wxXmlNodeType m_type; | |
160 | wxString m_name; | |
161 | wxString m_content; | |
162 | wxXmlProperty *m_properties; | |
163 | wxXmlNode *m_parent, *m_children, *m_next; | |
164 | ||
165 | void DoCopy(const wxXmlNode& node); | |
56d2f750 VS |
166 | }; |
167 | ||
168 | ||
169 | ||
170 | ||
171 | ||
172 | ||
173 | ||
cc30b233 | 174 | // This class holds XML data/document as parsed by XML parser. |
56d2f750 | 175 | |
ea89ec17 | 176 | class WXXMLDLLEXPORT wxXmlDocument : public wxObject |
56d2f750 | 177 | { |
eb8671f2 VS |
178 | public: |
179 | wxXmlDocument() : wxObject(), m_version(wxT("1.0")), m_root(NULL) {} | |
ea89ec17 | 180 | wxXmlDocument(const wxString& filename, |
cc30b233 VS |
181 | wxXmlIOType io_type = wxXML_IO_AUTO, |
182 | const wxString& encoding = wxT("UTF-8")); | |
ea89ec17 | 183 | wxXmlDocument(wxInputStream& stream, |
cc30b233 VS |
184 | wxXmlIOType io_type = wxXML_IO_AUTO, |
185 | const wxString& encoding = wxT("UTF-8")); | |
eb8671f2 VS |
186 | ~wxXmlDocument() { delete m_root; } |
187 | ||
188 | wxXmlDocument(const wxXmlDocument& doc); | |
189 | wxXmlDocument& operator=(const wxXmlDocument& doc); | |
190 | ||
191 | // Parses .xml file and loads data. Returns TRUE on success, FALSE | |
ea89ec17 | 192 | // otherwise. |
cc30b233 VS |
193 | bool Load(const wxString& filename, |
194 | wxXmlIOType io_type = wxXML_IO_AUTO, | |
195 | const wxString& encoding = wxT("UTF-8")); | |
196 | bool Load(wxInputStream& stream, | |
197 | wxXmlIOType io_type = wxXML_IO_AUTO, | |
198 | const wxString& encoding = wxT("UTF-8")); | |
eb8671f2 VS |
199 | |
200 | // Saves document as .xml file. | |
ea89ec17 | 201 | bool Save(const wxString& filename, |
eb8671f2 | 202 | wxXmlIOType io_type = wxXML_IO_TEXT_OUTPUT) const; |
ea89ec17 | 203 | bool Save(wxOutputStream& stream, |
eb8671f2 VS |
204 | wxXmlIOType io_type = wxXML_IO_TEXT_OUTPUT) const; |
205 | ||
206 | bool IsOk() const { return m_root != NULL; } | |
207 | ||
208 | // Returns root node of the document. | |
209 | wxXmlNode *GetRoot() const { return m_root; } | |
210 | ||
211 | // Returns version of document (may be empty). | |
212 | wxString GetVersion() const { return m_version; } | |
213 | // Returns encoding of document (may be empty). | |
214 | // Note: this is the encoding original fail was saved in, *not* the | |
cc30b233 VS |
215 | // encoding of in-memory representation! |
216 | wxString GetFileEncoding() const { return m_fileEncoding; } | |
eb8671f2 VS |
217 | |
218 | // Write-access methods: | |
219 | void SetRoot(wxXmlNode *node) { delete m_root ; m_root = node; } | |
220 | void SetVersion(const wxString& version) { m_version = version; } | |
cc30b233 VS |
221 | void SetFileEncoding(const wxString& encoding) { m_fileEncoding = encoding; } |
222 | ||
223 | #if !wxUSE_UNICODE | |
224 | // Returns encoding of in-memory representation of the document | |
225 | // (same as passed to Load or ctor, defaults to UTF-8). | |
226 | // NB: this is meaningless in Unicode build where data are stored as wchar_t* | |
227 | wxString GetEncoding() const { return m_encoding; } | |
228 | #endif | |
eb8671f2 VS |
229 | |
230 | static void AddHandler(wxXmlIOHandler *handler); | |
231 | static void CleanUpHandlers(); | |
232 | static void InitStandardHandlers(); | |
233 | ||
234 | protected: | |
235 | static wxList *sm_handlers; | |
236 | ||
237 | private: | |
cc30b233 VS |
238 | wxString m_version; |
239 | wxString m_fileEncoding; | |
240 | #if !wxUSE_UNICODE | |
241 | wxString m_encoding; | |
242 | #endif | |
eb8671f2 VS |
243 | wxXmlNode *m_root; |
244 | ||
245 | void DoCopy(const wxXmlDocument& doc); | |
56d2f750 VS |
246 | }; |
247 | ||
248 | ||
249 | ||
250 | // wxXmlIOHandler takes care of loading and/or saving XML data. | |
251 | // see xmlio.h for available handlers | |
252 | ||
ea89ec17 | 253 | class WXXMLDLLEXPORT wxXmlIOHandler : public wxObject |
56d2f750 VS |
254 | { |
255 | public: | |
256 | wxXmlIOHandler() {} | |
ea89ec17 | 257 | |
56d2f750 VS |
258 | virtual wxXmlIOType GetType() = 0; |
259 | virtual bool CanLoad(wxInputStream& stream) = 0; | |
260 | virtual bool CanSave() = 0; | |
ea89ec17 | 261 | |
cc30b233 VS |
262 | virtual bool Load(wxInputStream& stream, wxXmlDocument& doc, |
263 | const wxString& encoding) = 0; | |
56d2f750 VS |
264 | virtual bool Save(wxOutputStream& stream, const wxXmlDocument& doc) = 0; |
265 | }; | |
266 | ||
267 | ||
268 | ||
ea89ec17 | 269 | void wxXmlInitXmlModule(); |
56d2f750 VS |
270 | |
271 | #endif // _WX_XML_H_ |