]> git.saurik.com Git - wxWidgets.git/blame - include/wx/xml/xml.h
fix typo in last commit
[wxWidgets.git] / include / wx / xml / xml.h
CommitLineData
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 31class WXDLLIMPEXP_FWD_XML wxXmlNode;
288b6107 32class WXDLLIMPEXP_FWD_XML wxXmlAttribute;
b5dbe15d
VS
33class WXDLLIMPEXP_FWD_XML wxXmlDocument;
34class WXDLLIMPEXP_FWD_XML wxXmlIOHandler;
35class WXDLLIMPEXP_FWD_BASE wxInputStream;
36class WXDLLIMPEXP_FWD_BASE wxOutputStream;
27b0c286
VS
37
38
39// Represents XML node type.
40enum 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 63class WXDLLIMPEXP_XML wxXmlAttribute
27b0c286
VS
64{
65public:
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
80private:
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
102class WXDLLIMPEXP_XML wxXmlNode
103{
104public:
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);
5e05df3c 130 virtual bool InsertChild(wxXmlNode *child, wxXmlNode *followingNode);
43a302f2 131 virtual bool InsertChildAfter(wxXmlNode *child, wxXmlNode *precedingNode);
4c43dd90 132 virtual bool RemoveChild(wxXmlNode *child);
e13ce4a3
VZ
133 virtual void AddAttribute(const wxString& name, const wxString& value);
134 virtual bool DeleteAttribute(const wxString& name);
27b0c286
VS
135
136 // access methods:
137 wxXmlNodeType GetType() const { return m_type; }
12058e0c
VS
138 const wxString& GetName() const { return m_name; }
139 const wxString& GetContent() const { return m_content; }
27b0c286 140
538f3830
VS
141 bool IsWhitespaceOnly() const;
142 int GetDepth(wxXmlNode *grandparent = NULL) const;
143
4c43dd90
JS
144 // Gets node content from wxXML_ENTITY_NODE
145 // The problem is, <tag>content<tag> is represented as
146 // wxXML_ENTITY_NODE name="tag", content=""
147 // |-- wxXML_TEXT_NODE or
148 // wxXML_CDATA_SECTION_NODE name="" content="content"
149 wxString GetNodeContent() const;
150
27b0c286
VS
151 wxXmlNode *GetParent() const { return m_parent; }
152 wxXmlNode *GetNext() const { return m_next; }
153 wxXmlNode *GetChildren() const { return m_children; }
154
288b6107
VS
155 wxXmlAttribute *GetAttributes() const { return m_attrs; }
156 bool GetAttribute(const wxString& attrName, wxString *value) const;
157 wxString GetAttribute(const wxString& attrName,
b5cc5cbd 158 const wxString& defaultVal = wxEmptyString) const;
288b6107 159 bool HasAttribute(const wxString& attrName) const;
27b0c286 160
6e26d6b7
VS
161 int GetLineNumber() const { return m_lineNo; }
162
27b0c286
VS
163 void SetType(wxXmlNodeType type) { m_type = type; }
164 void SetName(const wxString& name) { m_name = name; }
165 void SetContent(const wxString& con) { m_content = con; }
166
167 void SetParent(wxXmlNode *parent) { m_parent = parent; }
168 void SetNext(wxXmlNode *next) { m_next = next; }
169 void SetChildren(wxXmlNode *child) { m_children = child; }
170
288b6107 171 void SetAttributes(wxXmlAttribute *attr) { m_attrs = attr; }
e13ce4a3 172 virtual void AddAttribute(wxXmlAttribute *attr);
288b6107
VS
173
174#if WXWIN_COMPATIBILITY_2_8
175 wxDEPRECATED( inline wxXmlAttribute *GetProperties() const );
176 wxDEPRECATED( inline bool GetPropVal(const wxString& propName,
177 wxString *value) const );
178 wxDEPRECATED( inline wxString GetPropVal(const wxString& propName,
179 const wxString& defaultVal) const );
180 wxDEPRECATED( inline bool HasProp(const wxString& propName) const );
181
182 wxDEPRECATED( inline void SetProperties(wxXmlAttribute *prop) );
183#endif // WXWIN_COMPATIBILITY_2_8
184
185 // The following three functions are backward compatibility, but because
186 // they were virtual, we must make it possible to override them. This
187 // is done by calling e.g. AddProperty() from AddAttribute(), so we have
188 // to keep AddProperty() even if 2.8 compatibility is off. To prevent
189 // old code from compiling in that case, we make them private and
190 // non-virtual. (This can be removed when WXWIN_COMPATIBILITY_2_8 is
191 // removed, we'll have just *Attribute versions then.)
192#if WXWIN_COMPATIBILITY_2_8
193 wxDEPRECATED_BUT_USED_INTERNALLY(
194 virtual void AddProperty(const wxString& name, const wxString& value) );
195 wxDEPRECATED_BUT_USED_INTERNALLY(
196 virtual bool DeleteProperty(const wxString& name) );
197 wxDEPRECATED_BUT_USED_INTERNALLY(
198 virtual void AddProperty(wxXmlAttribute *attr) );
199#else
200private:
201 void AddProperty(const wxString& name, const wxString& value);
202 bool DeleteProperty(const wxString& name);
203 void AddProperty(wxXmlAttribute *attr);
204#endif // WXWIN_COMPATIBILITY_2_8/!WXWIN_COMPATIBILITY_2_8
27b0c286
VS
205
206private:
207 wxXmlNodeType m_type;
208 wxString m_name;
209 wxString m_content;
288b6107 210 wxXmlAttribute *m_attrs;
27b0c286 211 wxXmlNode *m_parent, *m_children, *m_next;
b5cc5cbd 212 int m_lineNo; // line number in original file, or -1
27b0c286
VS
213
214 void DoCopy(const wxXmlNode& node);
215};
216
288b6107
VS
217#if WXWIN_COMPATIBILITY_2_8
218inline wxXmlAttribute *wxXmlNode::GetProperties() const
219 { return GetAttributes(); }
220inline bool wxXmlNode::GetPropVal(const wxString& propName,
221 wxString *value) const
222 { return GetAttribute(propName, value); }
223inline wxString wxXmlNode::GetPropVal(const wxString& propName,
224 const wxString& defaultVal) const
225 { return GetAttribute(propName, defaultVal); }
226inline bool wxXmlNode::HasProp(const wxString& propName) const
227 { return HasAttribute(propName); }
228inline void wxXmlNode::SetProperties(wxXmlAttribute *prop)
229 { SetAttributes(prop); }
230#endif // WXWIN_COMPATIBILITY_2_8
231
27b0c286
VS
232
233
538f3830
VS
234// special indentation value for wxXmlDocument::Save
235#define wxXML_NO_INDENTATION (-1)
27b0c286 236
538f3830
VS
237// flags for wxXmlDocument::Load
238enum wxXmlDocumentLoadFlag
239{
240 wxXMLDOC_NONE = 0,
241 wxXMLDOC_KEEP_WHITESPACE_NODES = 1
242};
27b0c286
VS
243
244
245// This class holds XML data/document as parsed by XML parser.
246
247class WXDLLIMPEXP_XML wxXmlDocument : public wxObject
248{
249public:
250 wxXmlDocument();
251 wxXmlDocument(const wxString& filename,
252 const wxString& encoding = wxT("UTF-8"));
253 wxXmlDocument(wxInputStream& stream,
254 const wxString& encoding = wxT("UTF-8"));
4c43dd90 255 virtual ~wxXmlDocument() { wxDELETE(m_root); }
27b0c286
VS
256
257 wxXmlDocument(const wxXmlDocument& doc);
258 wxXmlDocument& operator=(const wxXmlDocument& doc);
259
260 // Parses .xml file and loads data. Returns TRUE on success, FALSE
261 // otherwise.
4c43dd90 262 virtual bool Load(const wxString& filename,
538f3830 263 const wxString& encoding = wxT("UTF-8"), int flags = wxXMLDOC_NONE);
4c43dd90 264 virtual bool Load(wxInputStream& stream,
538f3830 265 const wxString& encoding = wxT("UTF-8"), int flags = wxXMLDOC_NONE);
b5cc5cbd 266
27b0c286 267 // Saves document as .xml file.
538f3830
VS
268 virtual bool Save(const wxString& filename, int indentstep = 1) const;
269 virtual bool Save(wxOutputStream& stream, int indentstep = 1) const;
27b0c286
VS
270
271 bool IsOk() const { return m_root != NULL; }
272
273 // Returns root node of the document.
274 wxXmlNode *GetRoot() const { return m_root; }
275
276 // Returns version of document (may be empty).
12058e0c 277 const wxString& GetVersion() const { return m_version; }
27b0c286
VS
278 // Returns encoding of document (may be empty).
279 // Note: this is the encoding original file was saved in, *not* the
280 // encoding of in-memory representation!
12058e0c 281 const wxString& GetFileEncoding() const { return m_fileEncoding; }
27b0c286
VS
282
283 // Write-access methods:
a124f99a 284 wxXmlNode *DetachRoot() { wxXmlNode *old=m_root; m_root=NULL; return old; }
4c43dd90 285 void SetRoot(wxXmlNode *node) { wxDELETE(m_root); m_root = node; }
27b0c286
VS
286 void SetVersion(const wxString& version) { m_version = version; }
287 void SetFileEncoding(const wxString& encoding) { m_fileEncoding = encoding; }
288
289#if !wxUSE_UNICODE
290 // Returns encoding of in-memory representation of the document
291 // (same as passed to Load or ctor, defaults to UTF-8).
292 // NB: this is meaningless in Unicode build where data are stored as wchar_t*
293 wxString GetEncoding() const { return m_encoding; }
294 void SetEncoding(const wxString& enc) { m_encoding = enc; }
295#endif
296
297private:
298 wxString m_version;
299 wxString m_fileEncoding;
300#if !wxUSE_UNICODE
301 wxString m_encoding;
302#endif
303 wxXmlNode *m_root;
304
305 void DoCopy(const wxXmlDocument& doc);
4c43dd90
JS
306
307 DECLARE_CLASS(wxXmlDocument)
27b0c286
VS
308};
309
310#endif // wxUSE_XML
311
312#endif // _WX_XML_H_