]> git.saurik.com Git - wxWidgets.git/blame - include/wx/xml/xml.h
Added DetachOldLog to avoid destruction of old log target
[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
31class WXDLLIMPEXP_XML wxXmlNode;
32class WXDLLIMPEXP_XML wxXmlProperty;
33class WXDLLIMPEXP_XML wxXmlDocument;
34class WXDLLIMPEXP_XML wxXmlIOHandler;
35class WXDLLIMPEXP_BASE wxInputStream;
36class WXDLLIMPEXP_BASE wxOutputStream;
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
63class WXDLLIMPEXP_XML wxXmlProperty
64{
65public:
66 wxXmlProperty() : m_next(NULL) {}
67 wxXmlProperty(const wxString& name, const wxString& value,
4c43dd90 68 wxXmlProperty *next = NULL)
27b0c286 69 : m_name(name), m_value(value), m_next(next) {}
4c43dd90 70 virtual ~wxXmlProperty() {}
27b0c286
VS
71
72 wxString GetName() const { return m_name; }
73 wxString GetValue() const { return m_value; }
74 wxXmlProperty *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(wxXmlProperty *next) { m_next = next; }
79
80private:
81 wxString m_name;
82 wxString m_value;
83 wxXmlProperty *m_next;
84};
85
86
87
88// Represents node in XML document. Node has name and may have content
89// and properties. Most common node types are wxXML_TEXT_NODE (name and props
90// are irrelevant) and wxXML_ELEMENT_NODE (e.g. in <title>hi</title> there is
91// element with name="title", irrelevant content and one child (wxXML_TEXT_NODE
92// with content="hi").
93//
94// If wxUSE_UNICODE is 0, all strings are encoded in the encoding given to Load
95// (default is UTF-8).
96
97class WXDLLIMPEXP_XML wxXmlNode
98{
99public:
100 wxXmlNode() : m_properties(NULL), m_parent(NULL),
101 m_children(NULL), m_next(NULL) {}
4c43dd90
JS
102 wxXmlNode(wxXmlNode *parent, wxXmlNodeType type,
103 const wxString& name, const wxString& content = wxEmptyString,
104 wxXmlProperty *props = NULL, wxXmlNode *next = NULL);
105 virtual ~wxXmlNode();
27b0c286
VS
106
107 // copy ctor & operator=. Note that this does NOT copy syblings
108 // and parent pointer, i.e. m_parent and m_next will be NULL
109 // after using copy ctor and are never unmodified by operator=.
110 // On the other hand, it DOES copy children and properties.
111 wxXmlNode(const wxXmlNode& node);
112 wxXmlNode& operator=(const wxXmlNode& node);
113
114 // user-friendly creation:
115 wxXmlNode(wxXmlNodeType type, const wxString& name,
116 const wxString& content = wxEmptyString);
4c43dd90 117 virtual void AddChild(wxXmlNode *child);
fa6a8373 118 virtual bool InsertChild(wxXmlNode *child, wxXmlNode *before_node);
4c43dd90
JS
119 virtual bool RemoveChild(wxXmlNode *child);
120 virtual void AddProperty(const wxString& name, const wxString& value);
121 virtual bool DeleteProperty(const wxString& name);
27b0c286
VS
122
123 // access methods:
124 wxXmlNodeType GetType() const { return m_type; }
12058e0c
VS
125 const wxString& GetName() const { return m_name; }
126 const wxString& GetContent() const { return m_content; }
27b0c286 127
538f3830
VS
128 bool IsWhitespaceOnly() const;
129 int GetDepth(wxXmlNode *grandparent = NULL) const;
130
4c43dd90
JS
131 // Gets node content from wxXML_ENTITY_NODE
132 // The problem is, <tag>content<tag> is represented as
133 // wxXML_ENTITY_NODE name="tag", content=""
134 // |-- wxXML_TEXT_NODE or
135 // wxXML_CDATA_SECTION_NODE name="" content="content"
136 wxString GetNodeContent() const;
137
27b0c286
VS
138 wxXmlNode *GetParent() const { return m_parent; }
139 wxXmlNode *GetNext() const { return m_next; }
140 wxXmlNode *GetChildren() const { return m_children; }
141
142 wxXmlProperty *GetProperties() const { return m_properties; }
143 bool GetPropVal(const wxString& propName, wxString *value) const;
144 wxString GetPropVal(const wxString& propName,
145 const wxString& defaultVal) const;
146 bool HasProp(const wxString& propName) const;
147
148 void SetType(wxXmlNodeType type) { m_type = type; }
149 void SetName(const wxString& name) { m_name = name; }
150 void SetContent(const wxString& con) { m_content = con; }
151
152 void SetParent(wxXmlNode *parent) { m_parent = parent; }
153 void SetNext(wxXmlNode *next) { m_next = next; }
154 void SetChildren(wxXmlNode *child) { m_children = child; }
155
156 void SetProperties(wxXmlProperty *prop) { m_properties = prop; }
4c43dd90 157 virtual void AddProperty(wxXmlProperty *prop);
27b0c286
VS
158
159private:
160 wxXmlNodeType m_type;
161 wxString m_name;
162 wxString m_content;
163 wxXmlProperty *m_properties;
164 wxXmlNode *m_parent, *m_children, *m_next;
165
166 void DoCopy(const wxXmlNode& node);
167};
168
169
170
538f3830
VS
171// special indentation value for wxXmlDocument::Save
172#define wxXML_NO_INDENTATION (-1)
27b0c286 173
538f3830
VS
174// flags for wxXmlDocument::Load
175enum wxXmlDocumentLoadFlag
176{
177 wxXMLDOC_NONE = 0,
178 wxXMLDOC_KEEP_WHITESPACE_NODES = 1
179};
27b0c286
VS
180
181
182// This class holds XML data/document as parsed by XML parser.
183
184class WXDLLIMPEXP_XML wxXmlDocument : public wxObject
185{
186public:
187 wxXmlDocument();
188 wxXmlDocument(const wxString& filename,
189 const wxString& encoding = wxT("UTF-8"));
190 wxXmlDocument(wxInputStream& stream,
191 const wxString& encoding = wxT("UTF-8"));
4c43dd90 192 virtual ~wxXmlDocument() { wxDELETE(m_root); }
27b0c286
VS
193
194 wxXmlDocument(const wxXmlDocument& doc);
195 wxXmlDocument& operator=(const wxXmlDocument& doc);
196
197 // Parses .xml file and loads data. Returns TRUE on success, FALSE
198 // otherwise.
4c43dd90 199 virtual bool Load(const wxString& filename,
538f3830 200 const wxString& encoding = wxT("UTF-8"), int flags = wxXMLDOC_NONE);
4c43dd90 201 virtual bool Load(wxInputStream& stream,
538f3830 202 const wxString& encoding = wxT("UTF-8"), int flags = wxXMLDOC_NONE);
27b0c286
VS
203
204 // Saves document as .xml file.
538f3830
VS
205 virtual bool Save(const wxString& filename, int indentstep = 1) const;
206 virtual bool Save(wxOutputStream& stream, int indentstep = 1) const;
27b0c286
VS
207
208 bool IsOk() const { return m_root != NULL; }
209
210 // Returns root node of the document.
211 wxXmlNode *GetRoot() const { return m_root; }
212
213 // Returns version of document (may be empty).
12058e0c 214 const wxString& GetVersion() const { return m_version; }
27b0c286
VS
215 // Returns encoding of document (may be empty).
216 // Note: this is the encoding original file was saved in, *not* the
217 // encoding of in-memory representation!
12058e0c 218 const wxString& GetFileEncoding() const { return m_fileEncoding; }
27b0c286
VS
219
220 // Write-access methods:
a124f99a 221 wxXmlNode *DetachRoot() { wxXmlNode *old=m_root; m_root=NULL; return old; }
4c43dd90 222 void SetRoot(wxXmlNode *node) { wxDELETE(m_root); m_root = node; }
27b0c286
VS
223 void SetVersion(const wxString& version) { m_version = version; }
224 void SetFileEncoding(const wxString& encoding) { m_fileEncoding = encoding; }
225
226#if !wxUSE_UNICODE
227 // Returns encoding of in-memory representation of the document
228 // (same as passed to Load or ctor, defaults to UTF-8).
229 // NB: this is meaningless in Unicode build where data are stored as wchar_t*
230 wxString GetEncoding() const { return m_encoding; }
231 void SetEncoding(const wxString& enc) { m_encoding = enc; }
232#endif
233
234private:
235 wxString m_version;
236 wxString m_fileEncoding;
237#if !wxUSE_UNICODE
238 wxString m_encoding;
239#endif
240 wxXmlNode *m_root;
241
242 void DoCopy(const wxXmlDocument& doc);
4c43dd90
JS
243
244 DECLARE_CLASS(wxXmlDocument)
27b0c286
VS
245};
246
247#endif // wxUSE_XML
248
249#endif // _WX_XML_H_