]> git.saurik.com Git - wxWidgets.git/blob - include/wx/xml/xml.h
missing commit
[wxWidgets.git] / include / wx / xml / xml.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/xml/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
12 #ifndef _WX_XML_H_
13 #define _WX_XML_H_
14
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 #include "wx/versioninfo.h"
23
24 #ifdef WXMAKINGDLL_XML
25 #define WXDLLIMPEXP_XML WXEXPORT
26 #elif defined(WXUSINGDLL)
27 #define WXDLLIMPEXP_XML WXIMPORT
28 #else // not making nor using DLL
29 #define WXDLLIMPEXP_XML
30 #endif
31
32 class WXDLLIMPEXP_FWD_XML wxXmlNode;
33 class WXDLLIMPEXP_FWD_XML wxXmlAttribute;
34 class WXDLLIMPEXP_FWD_XML wxXmlDocument;
35 class WXDLLIMPEXP_FWD_XML wxXmlIOHandler;
36 class WXDLLIMPEXP_FWD_BASE wxInputStream;
37 class WXDLLIMPEXP_FWD_BASE wxOutputStream;
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
63 class WXDLLIMPEXP_XML wxXmlAttribute
64 {
65 public:
66 wxXmlAttribute() : m_next(NULL) {}
67 wxXmlAttribute(const wxString& name, const wxString& value,
68 wxXmlAttribute *next = NULL)
69 : m_name(name), m_value(value), m_next(next) {}
70 virtual ~wxXmlAttribute() {}
71
72 const wxString& GetName() const { return m_name; }
73 const wxString& GetValue() const { return m_value; }
74 wxXmlAttribute *GetNext() const { return m_next; }
75
76 void SetName(const wxString& name) { m_name = name; }
77 void SetValue(const wxString& value) { m_value = value; }
78 void SetNext(wxXmlAttribute *next) { m_next = next; }
79
80 private:
81 wxString m_name;
82 wxString m_value;
83 wxXmlAttribute *m_next;
84 };
85
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
91
92
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
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:
105 wxXmlNode()
106 : m_attrs(NULL), m_parent(NULL), m_children(NULL), m_next(NULL),
107 m_lineNo(-1), m_noConversion(false)
108 {
109 }
110
111 wxXmlNode(wxXmlNode *parent, wxXmlNodeType type,
112 const wxString& name, const wxString& content = wxEmptyString,
113 wxXmlAttribute *attrs = NULL, wxXmlNode *next = NULL,
114 int lineNo = -1);
115
116 virtual ~wxXmlNode();
117
118 // copy ctor & operator=. Note that this does NOT copy siblings
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=.
121 // On the other hand, it DOES copy children and attributes.
122 wxXmlNode(const wxXmlNode& node);
123 wxXmlNode& operator=(const wxXmlNode& node);
124
125 // user-friendly creation:
126 wxXmlNode(wxXmlNodeType type, const wxString& name,
127 const wxString& content = wxEmptyString,
128 int lineNo = -1);
129 virtual void AddChild(wxXmlNode *child);
130 virtual bool InsertChild(wxXmlNode *child, wxXmlNode *followingNode);
131 virtual bool InsertChildAfter(wxXmlNode *child, wxXmlNode *precedingNode);
132 virtual bool RemoveChild(wxXmlNode *child);
133 virtual void AddAttribute(const wxString& name, const wxString& value);
134 virtual bool DeleteAttribute(const wxString& name);
135
136 // access methods:
137 wxXmlNodeType GetType() const { return m_type; }
138 const wxString& GetName() const { return m_name; }
139 const wxString& GetContent() const { return m_content; }
140
141 bool IsWhitespaceOnly() const;
142 int GetDepth(wxXmlNode *grandparent = NULL) const;
143
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
151 wxXmlNode *GetParent() const { return m_parent; }
152 wxXmlNode *GetNext() const { return m_next; }
153 wxXmlNode *GetChildren() const { return m_children; }
154
155 wxXmlAttribute *GetAttributes() const { return m_attrs; }
156 bool GetAttribute(const wxString& attrName, wxString *value) const;
157 wxString GetAttribute(const wxString& attrName,
158 const wxString& defaultVal = wxEmptyString) const;
159 bool HasAttribute(const wxString& attrName) const;
160
161 int GetLineNumber() const { return m_lineNo; }
162
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
171 void SetAttributes(wxXmlAttribute *attr) { m_attrs = attr; }
172 virtual void AddAttribute(wxXmlAttribute *attr);
173
174 // If true, don't do encoding conversion to improve efficiency - node content is ACII text
175 bool GetNoConversion() const { return m_noConversion; }
176 void SetNoConversion(bool noconversion) { m_noConversion = noconversion; }
177
178 #if WXWIN_COMPATIBILITY_2_8
179 wxDEPRECATED( inline wxXmlAttribute *GetProperties() const );
180 wxDEPRECATED( inline bool GetPropVal(const wxString& propName,
181 wxString *value) const );
182 wxDEPRECATED( inline wxString GetPropVal(const wxString& propName,
183 const wxString& defaultVal) const );
184 wxDEPRECATED( inline bool HasProp(const wxString& propName) const );
185
186 wxDEPRECATED( inline void SetProperties(wxXmlAttribute *prop) );
187 #endif // WXWIN_COMPATIBILITY_2_8
188
189 // The following three functions are backward compatibility, but because
190 // they were virtual, we must make it possible to override them. This
191 // is done by calling e.g. AddProperty() from AddAttribute(), so we have
192 // to keep AddProperty() even if 2.8 compatibility is off. To prevent
193 // old code from compiling in that case, we make them private and
194 // non-virtual. (This can be removed when WXWIN_COMPATIBILITY_2_8 is
195 // removed, we'll have just *Attribute versions then.)
196 #if WXWIN_COMPATIBILITY_2_8
197 wxDEPRECATED_BUT_USED_INTERNALLY(
198 virtual void AddProperty(const wxString& name, const wxString& value) );
199 wxDEPRECATED_BUT_USED_INTERNALLY(
200 virtual bool DeleteProperty(const wxString& name) );
201 wxDEPRECATED_BUT_USED_INTERNALLY(
202 virtual void AddProperty(wxXmlAttribute *attr) );
203 #else
204 private:
205 void AddProperty(const wxString& name, const wxString& value);
206 bool DeleteProperty(const wxString& name);
207 void AddProperty(wxXmlAttribute *attr);
208 #endif // WXWIN_COMPATIBILITY_2_8/!WXWIN_COMPATIBILITY_2_8
209
210 private:
211 wxXmlNodeType m_type;
212 wxString m_name;
213 wxString m_content;
214 wxXmlAttribute *m_attrs;
215 wxXmlNode *m_parent, *m_children, *m_next;
216 int m_lineNo; // line number in original file, or -1
217 bool m_noConversion; // don't do encoding conversion - node is plain text
218
219 void DoFree();
220 void DoCopy(const wxXmlNode& node);
221 };
222
223 #if WXWIN_COMPATIBILITY_2_8
224 inline wxXmlAttribute *wxXmlNode::GetProperties() const
225 { return GetAttributes(); }
226 inline bool wxXmlNode::GetPropVal(const wxString& propName,
227 wxString *value) const
228 { return GetAttribute(propName, value); }
229 inline wxString wxXmlNode::GetPropVal(const wxString& propName,
230 const wxString& defaultVal) const
231 { return GetAttribute(propName, defaultVal); }
232 inline bool wxXmlNode::HasProp(const wxString& propName) const
233 { return HasAttribute(propName); }
234 inline void wxXmlNode::SetProperties(wxXmlAttribute *prop)
235 { SetAttributes(prop); }
236 #endif // WXWIN_COMPATIBILITY_2_8
237
238
239
240 // special indentation value for wxXmlDocument::Save
241 #define wxXML_NO_INDENTATION (-1)
242
243 // flags for wxXmlDocument::Load
244 enum wxXmlDocumentLoadFlag
245 {
246 wxXMLDOC_NONE = 0,
247 wxXMLDOC_KEEP_WHITESPACE_NODES = 1
248 };
249
250
251 // This class holds XML data/document as parsed by XML parser.
252
253 class WXDLLIMPEXP_XML wxXmlDocument : public wxObject
254 {
255 public:
256 wxXmlDocument();
257 wxXmlDocument(const wxString& filename,
258 const wxString& encoding = wxT("UTF-8"));
259 wxXmlDocument(wxInputStream& stream,
260 const wxString& encoding = wxT("UTF-8"));
261 virtual ~wxXmlDocument() { wxDELETE(m_docNode); }
262
263 wxXmlDocument(const wxXmlDocument& doc);
264 wxXmlDocument& operator=(const wxXmlDocument& doc);
265
266 // Parses .xml file and loads data. Returns TRUE on success, FALSE
267 // otherwise.
268 virtual bool Load(const wxString& filename,
269 const wxString& encoding = wxT("UTF-8"), int flags = wxXMLDOC_NONE);
270 virtual bool Load(wxInputStream& stream,
271 const wxString& encoding = wxT("UTF-8"), int flags = wxXMLDOC_NONE);
272
273 // Saves document as .xml file.
274 virtual bool Save(const wxString& filename, int indentstep = 2) const;
275 virtual bool Save(wxOutputStream& stream, int indentstep = 2) const;
276
277 bool IsOk() const { return GetRoot() != NULL; }
278
279 // Returns root node of the document.
280 wxXmlNode *GetRoot() const;
281 // Returns the document node.
282 wxXmlNode *GetDocumentNode() const { return m_docNode; }
283
284
285 // Returns version of document (may be empty).
286 const wxString& GetVersion() const { return m_version; }
287 // Returns encoding of document (may be empty).
288 // Note: this is the encoding original file was saved in, *not* the
289 // encoding of in-memory representation!
290 const wxString& GetFileEncoding() const { return m_fileEncoding; }
291
292 // Write-access methods:
293 wxXmlNode *DetachDocumentNode() { wxXmlNode *old=m_docNode; m_docNode=NULL; return old; }
294 void SetDocumentNode(wxXmlNode *node) { wxDELETE(m_docNode); m_docNode = node; }
295 wxXmlNode *DetachRoot();
296 void SetRoot(wxXmlNode *node);
297 void SetVersion(const wxString& version) { m_version = version; }
298 void SetFileEncoding(const wxString& encoding) { m_fileEncoding = encoding; }
299 void AppendToProlog(wxXmlNode *node);
300
301 #if !wxUSE_UNICODE
302 // Returns encoding of in-memory representation of the document
303 // (same as passed to Load or ctor, defaults to UTF-8).
304 // NB: this is meaningless in Unicode build where data are stored as wchar_t*
305 wxString GetEncoding() const { return m_encoding; }
306 void SetEncoding(const wxString& enc) { m_encoding = enc; }
307 #endif
308
309 static wxVersionInfo GetLibraryVersionInfo();
310
311 private:
312 wxString m_version;
313 wxString m_fileEncoding;
314 #if !wxUSE_UNICODE
315 wxString m_encoding;
316 #endif
317 wxXmlNode *m_docNode;
318
319 void DoCopy(const wxXmlDocument& doc);
320
321 DECLARE_CLASS(wxXmlDocument)
322 };
323
324 #endif // wxUSE_XML
325
326 #endif // _WX_XML_H_