]>
Commit | Line | Data |
---|---|---|
27b0c286 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 | |
65571936 | 8 | // Licence: wxWindows licence |
27b0c286 VS |
9 | ///////////////////////////////////////////////////////////////////////////// |
10 | ||
11 | ||
27b0c286 VS |
12 | #ifndef _WX_XML_H_ |
13 | #define _WX_XML_H_ | |
14 | ||
27b0c286 VS |
15 | #include "wx/defs.h" |
16 | ||
17 | #if wxUSE_XML | |
18 | ||
19 | #include "wx/string.h" | |
20 | #include "wx/object.h" | |
21 | #include "wx/list.h" | |
22 | ||
23 | #ifdef WXMAKINGDLL_XML | |
24 | #define WXDLLIMPEXP_XML WXEXPORT | |
25 | #elif defined(WXUSINGDLL) | |
26 | #define WXDLLIMPEXP_XML WXIMPORT | |
27 | #else // not making nor using DLL | |
28 | #define WXDLLIMPEXP_XML | |
29 | #endif | |
30 | ||
b5dbe15d | 31 | class WXDLLIMPEXP_FWD_XML wxXmlNode; |
288b6107 | 32 | class WXDLLIMPEXP_FWD_XML wxXmlAttribute; |
b5dbe15d VS |
33 | class WXDLLIMPEXP_FWD_XML wxXmlDocument; |
34 | class WXDLLIMPEXP_FWD_XML wxXmlIOHandler; | |
35 | class WXDLLIMPEXP_FWD_BASE wxInputStream; | |
36 | class WXDLLIMPEXP_FWD_BASE wxOutputStream; | |
27b0c286 VS |
37 | |
38 | ||
39 | // Represents XML node type. | |
40 | enum wxXmlNodeType | |
41 | { | |
42 | // note: values are synchronized with xmlElementType from libxml | |
43 | wxXML_ELEMENT_NODE = 1, | |
44 | wxXML_ATTRIBUTE_NODE = 2, | |
45 | wxXML_TEXT_NODE = 3, | |
46 | wxXML_CDATA_SECTION_NODE = 4, | |
47 | wxXML_ENTITY_REF_NODE = 5, | |
48 | wxXML_ENTITY_NODE = 6, | |
49 | wxXML_PI_NODE = 7, | |
50 | wxXML_COMMENT_NODE = 8, | |
51 | wxXML_DOCUMENT_NODE = 9, | |
52 | wxXML_DOCUMENT_TYPE_NODE = 10, | |
53 | wxXML_DOCUMENT_FRAG_NODE = 11, | |
54 | wxXML_NOTATION_NODE = 12, | |
55 | wxXML_HTML_DOCUMENT_NODE = 13 | |
56 | }; | |
57 | ||
58 | ||
59 | // Represents node property(ies). | |
60 | // Example: in <img src="hello.gif" id="3"/> "src" is property with value | |
61 | // "hello.gif" and "id" is prop. with value "3". | |
62 | ||
288b6107 | 63 | class WXDLLIMPEXP_XML wxXmlAttribute |
27b0c286 VS |
64 | { |
65 | public: | |
288b6107 VS |
66 | wxXmlAttribute() : m_next(NULL) {} |
67 | wxXmlAttribute(const wxString& name, const wxString& value, | |
68 | wxXmlAttribute *next = NULL) | |
27b0c286 | 69 | : m_name(name), m_value(value), m_next(next) {} |
288b6107 | 70 | virtual ~wxXmlAttribute() {} |
27b0c286 VS |
71 | |
72 | wxString GetName() const { return m_name; } | |
73 | wxString GetValue() const { return m_value; } | |
288b6107 | 74 | wxXmlAttribute *GetNext() const { return m_next; } |
27b0c286 VS |
75 | |
76 | void SetName(const wxString& name) { m_name = name; } | |
77 | void SetValue(const wxString& value) { m_value = value; } | |
288b6107 | 78 | void SetNext(wxXmlAttribute *next) { m_next = next; } |
27b0c286 VS |
79 | |
80 | private: | |
81 | wxString m_name; | |
82 | wxString m_value; | |
288b6107 | 83 | wxXmlAttribute *m_next; |
27b0c286 VS |
84 | }; |
85 | ||
288b6107 VS |
86 | #if WXWIN_COMPATIBILITY_2_8 |
87 | // NB: #define is used instead of typedef so that forward declarations | |
88 | // continue to work | |
89 | #define wxXmlProperty wxXmlAttribute | |
90 | #endif | |
27b0c286 VS |
91 | |
92 | ||
288b6107 VS |
93 | // Represents node in XML document. Node has name and may have content and |
94 | // attributes. Most common node types are wxXML_TEXT_NODE (name and attributes | |
27b0c286 VS |
95 | // are irrelevant) and wxXML_ELEMENT_NODE (e.g. in <title>hi</title> there is |
96 | // element with name="title", irrelevant content and one child (wxXML_TEXT_NODE | |
97 | // with content="hi"). | |
98 | // | |
99 | // If wxUSE_UNICODE is 0, all strings are encoded in the encoding given to Load | |
100 | // (default is UTF-8). | |
101 | ||
102 | class WXDLLIMPEXP_XML wxXmlNode | |
103 | { | |
104 | public: | |
288b6107 | 105 | wxXmlNode() |
6e26d6b7 VS |
106 | : m_attrs(NULL), m_parent(NULL), m_children(NULL), m_next(NULL), |
107 | m_lineNo(-1) | |
108 | { | |
109 | } | |
110 | ||
4c43dd90 JS |
111 | wxXmlNode(wxXmlNode *parent, wxXmlNodeType type, |
112 | const wxString& name, const wxString& content = wxEmptyString, | |
6e26d6b7 VS |
113 | wxXmlAttribute *attrs = NULL, wxXmlNode *next = NULL, |
114 | int lineNo = -1); | |
115 | ||
4c43dd90 | 116 | virtual ~wxXmlNode(); |
27b0c286 VS |
117 | |
118 | // copy ctor & operator=. Note that this does NOT copy syblings | |
119 | // and parent pointer, i.e. m_parent and m_next will be NULL | |
120 | // after using copy ctor and are never unmodified by operator=. | |
288b6107 | 121 | // On the other hand, it DOES copy children and attributes. |
27b0c286 VS |
122 | wxXmlNode(const wxXmlNode& node); |
123 | wxXmlNode& operator=(const wxXmlNode& node); | |
124 | ||
125 | // user-friendly creation: | |
126 | wxXmlNode(wxXmlNodeType type, const wxString& name, | |
6e26d6b7 VS |
127 | const wxString& content = wxEmptyString, |
128 | int lineNo = -1); | |
4c43dd90 | 129 | virtual void AddChild(wxXmlNode *child); |
fa6a8373 | 130 | virtual bool InsertChild(wxXmlNode *child, wxXmlNode *before_node); |
4c43dd90 | 131 | virtual bool RemoveChild(wxXmlNode *child); |
288b6107 VS |
132 | virtual void AddAttribute(const wxString& name, const wxString& value) |
133 | { AddProperty(name, value); } | |
134 | virtual bool DeleteAttribute(const wxString& name) | |
135 | { return DeleteProperty(name); } | |
27b0c286 VS |
136 | |
137 | // access methods: | |
138 | wxXmlNodeType GetType() const { return m_type; } | |
12058e0c VS |
139 | const wxString& GetName() const { return m_name; } |
140 | const wxString& GetContent() const { return m_content; } | |
27b0c286 | 141 | |
538f3830 VS |
142 | bool IsWhitespaceOnly() const; |
143 | int GetDepth(wxXmlNode *grandparent = NULL) const; | |
144 | ||
4c43dd90 JS |
145 | // Gets node content from wxXML_ENTITY_NODE |
146 | // The problem is, <tag>content<tag> is represented as | |
147 | // wxXML_ENTITY_NODE name="tag", content="" | |
148 | // |-- wxXML_TEXT_NODE or | |
149 | // wxXML_CDATA_SECTION_NODE name="" content="content" | |
150 | wxString GetNodeContent() const; | |
151 | ||
27b0c286 VS |
152 | wxXmlNode *GetParent() const { return m_parent; } |
153 | wxXmlNode *GetNext() const { return m_next; } | |
154 | wxXmlNode *GetChildren() const { return m_children; } | |
155 | ||
288b6107 VS |
156 | wxXmlAttribute *GetAttributes() const { return m_attrs; } |
157 | bool GetAttribute(const wxString& attrName, wxString *value) const; | |
158 | wxString GetAttribute(const wxString& attrName, | |
159 | const wxString& defaultVal) const; | |
160 | bool HasAttribute(const wxString& attrName) const; | |
27b0c286 | 161 | |
6e26d6b7 VS |
162 | int GetLineNumber() const { return m_lineNo; } |
163 | ||
27b0c286 VS |
164 | void SetType(wxXmlNodeType type) { m_type = type; } |
165 | void SetName(const wxString& name) { m_name = name; } | |
166 | void SetContent(const wxString& con) { m_content = con; } | |
167 | ||
168 | void SetParent(wxXmlNode *parent) { m_parent = parent; } | |
169 | void SetNext(wxXmlNode *next) { m_next = next; } | |
170 | void SetChildren(wxXmlNode *child) { m_children = child; } | |
171 | ||
288b6107 VS |
172 | void SetAttributes(wxXmlAttribute *attr) { m_attrs = attr; } |
173 | virtual void AddAttribute(wxXmlAttribute *attr) | |
174 | { AddProperty(attr); } | |
175 | ||
176 | #if WXWIN_COMPATIBILITY_2_8 | |
177 | wxDEPRECATED( inline wxXmlAttribute *GetProperties() const ); | |
178 | wxDEPRECATED( inline bool GetPropVal(const wxString& propName, | |
179 | wxString *value) const ); | |
180 | wxDEPRECATED( inline wxString GetPropVal(const wxString& propName, | |
181 | const wxString& defaultVal) const ); | |
182 | wxDEPRECATED( inline bool HasProp(const wxString& propName) const ); | |
183 | ||
184 | wxDEPRECATED( inline void SetProperties(wxXmlAttribute *prop) ); | |
185 | #endif // WXWIN_COMPATIBILITY_2_8 | |
186 | ||
187 | // The following three functions are backward compatibility, but because | |
188 | // they were virtual, we must make it possible to override them. This | |
189 | // is done by calling e.g. AddProperty() from AddAttribute(), so we have | |
190 | // to keep AddProperty() even if 2.8 compatibility is off. To prevent | |
191 | // old code from compiling in that case, we make them private and | |
192 | // non-virtual. (This can be removed when WXWIN_COMPATIBILITY_2_8 is | |
193 | // removed, we'll have just *Attribute versions then.) | |
194 | #if WXWIN_COMPATIBILITY_2_8 | |
195 | wxDEPRECATED_BUT_USED_INTERNALLY( | |
196 | virtual void AddProperty(const wxString& name, const wxString& value) ); | |
197 | wxDEPRECATED_BUT_USED_INTERNALLY( | |
198 | virtual bool DeleteProperty(const wxString& name) ); | |
199 | wxDEPRECATED_BUT_USED_INTERNALLY( | |
200 | virtual void AddProperty(wxXmlAttribute *attr) ); | |
201 | #else | |
202 | private: | |
203 | void AddProperty(const wxString& name, const wxString& value); | |
204 | bool DeleteProperty(const wxString& name); | |
205 | void AddProperty(wxXmlAttribute *attr); | |
206 | #endif // WXWIN_COMPATIBILITY_2_8/!WXWIN_COMPATIBILITY_2_8 | |
27b0c286 VS |
207 | |
208 | private: | |
209 | wxXmlNodeType m_type; | |
210 | wxString m_name; | |
211 | wxString m_content; | |
288b6107 | 212 | wxXmlAttribute *m_attrs; |
27b0c286 | 213 | wxXmlNode *m_parent, *m_children, *m_next; |
6e26d6b7 | 214 | int m_lineNo; // line number in original file, or -1 |
27b0c286 VS |
215 | |
216 | void DoCopy(const wxXmlNode& node); | |
217 | }; | |
218 | ||
288b6107 VS |
219 | #if WXWIN_COMPATIBILITY_2_8 |
220 | inline wxXmlAttribute *wxXmlNode::GetProperties() const | |
221 | { return GetAttributes(); } | |
222 | inline bool wxXmlNode::GetPropVal(const wxString& propName, | |
223 | wxString *value) const | |
224 | { return GetAttribute(propName, value); } | |
225 | inline wxString wxXmlNode::GetPropVal(const wxString& propName, | |
226 | const wxString& defaultVal) const | |
227 | { return GetAttribute(propName, defaultVal); } | |
228 | inline bool wxXmlNode::HasProp(const wxString& propName) const | |
229 | { return HasAttribute(propName); } | |
230 | inline void wxXmlNode::SetProperties(wxXmlAttribute *prop) | |
231 | { SetAttributes(prop); } | |
232 | #endif // WXWIN_COMPATIBILITY_2_8 | |
233 | ||
27b0c286 VS |
234 | |
235 | ||
538f3830 VS |
236 | // special indentation value for wxXmlDocument::Save |
237 | #define wxXML_NO_INDENTATION (-1) | |
27b0c286 | 238 | |
538f3830 VS |
239 | // flags for wxXmlDocument::Load |
240 | enum wxXmlDocumentLoadFlag | |
241 | { | |
242 | wxXMLDOC_NONE = 0, | |
243 | wxXMLDOC_KEEP_WHITESPACE_NODES = 1 | |
244 | }; | |
27b0c286 VS |
245 | |
246 | ||
247 | // This class holds XML data/document as parsed by XML parser. | |
248 | ||
249 | class WXDLLIMPEXP_XML wxXmlDocument : public wxObject | |
250 | { | |
251 | public: | |
252 | wxXmlDocument(); | |
253 | wxXmlDocument(const wxString& filename, | |
254 | const wxString& encoding = wxT("UTF-8")); | |
255 | wxXmlDocument(wxInputStream& stream, | |
256 | const wxString& encoding = wxT("UTF-8")); | |
4c43dd90 | 257 | virtual ~wxXmlDocument() { wxDELETE(m_root); } |
27b0c286 VS |
258 | |
259 | wxXmlDocument(const wxXmlDocument& doc); | |
260 | wxXmlDocument& operator=(const wxXmlDocument& doc); | |
261 | ||
262 | // Parses .xml file and loads data. Returns TRUE on success, FALSE | |
263 | // otherwise. | |
4c43dd90 | 264 | virtual bool Load(const wxString& filename, |
538f3830 | 265 | const wxString& encoding = wxT("UTF-8"), int flags = wxXMLDOC_NONE); |
4c43dd90 | 266 | virtual bool Load(wxInputStream& stream, |
538f3830 | 267 | const wxString& encoding = wxT("UTF-8"), int flags = wxXMLDOC_NONE); |
27b0c286 VS |
268 | |
269 | // Saves document as .xml file. | |
538f3830 VS |
270 | virtual bool Save(const wxString& filename, int indentstep = 1) const; |
271 | virtual bool Save(wxOutputStream& stream, int indentstep = 1) const; | |
27b0c286 VS |
272 | |
273 | bool IsOk() const { return m_root != NULL; } | |
274 | ||
275 | // Returns root node of the document. | |
276 | wxXmlNode *GetRoot() const { return m_root; } | |
277 | ||
278 | // Returns version of document (may be empty). | |
12058e0c | 279 | const wxString& GetVersion() const { return m_version; } |
27b0c286 VS |
280 | // Returns encoding of document (may be empty). |
281 | // Note: this is the encoding original file was saved in, *not* the | |
282 | // encoding of in-memory representation! | |
12058e0c | 283 | const wxString& GetFileEncoding() const { return m_fileEncoding; } |
27b0c286 VS |
284 | |
285 | // Write-access methods: | |
a124f99a | 286 | wxXmlNode *DetachRoot() { wxXmlNode *old=m_root; m_root=NULL; return old; } |
4c43dd90 | 287 | void SetRoot(wxXmlNode *node) { wxDELETE(m_root); m_root = node; } |
27b0c286 VS |
288 | void SetVersion(const wxString& version) { m_version = version; } |
289 | void SetFileEncoding(const wxString& encoding) { m_fileEncoding = encoding; } | |
290 | ||
291 | #if !wxUSE_UNICODE | |
292 | // Returns encoding of in-memory representation of the document | |
293 | // (same as passed to Load or ctor, defaults to UTF-8). | |
294 | // NB: this is meaningless in Unicode build where data are stored as wchar_t* | |
295 | wxString GetEncoding() const { return m_encoding; } | |
296 | void SetEncoding(const wxString& enc) { m_encoding = enc; } | |
297 | #endif | |
298 | ||
299 | private: | |
300 | wxString m_version; | |
301 | wxString m_fileEncoding; | |
302 | #if !wxUSE_UNICODE | |
303 | wxString m_encoding; | |
304 | #endif | |
305 | wxXmlNode *m_root; | |
306 | ||
307 | void DoCopy(const wxXmlDocument& doc); | |
4c43dd90 JS |
308 | |
309 | DECLARE_CLASS(wxXmlDocument) | |
27b0c286 VS |
310 | }; |
311 | ||
312 | #endif // wxUSE_XML | |
313 | ||
314 | #endif // _WX_XML_H_ |