]> git.saurik.com Git - wxWidgets.git/blob - include/wx/xml/xml.h
More details about the internal module name changes
[wxWidgets.git] / include / wx / xml / xml.h
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
12 /* ************************************************************************* *
13 * CAUTION! *
14 * *
15 * The API defined in this header *WILL* change in the future and backward *
16 * compatibility will *not* be preserved. If you use these classes in your *
17 * application, it probably won't compile with future wxWindows releases. *
18 * Use on your own risk. *
19 * *
20 * ************************************************************************* */
21
22 #ifndef _WX_XML_H_
23 #define _WX_XML_H_
24
25 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
26 #pragma interface "xml.h"
27 #endif
28
29 #include "wx/defs.h"
30
31 #if wxUSE_XML
32
33 #include "wx/string.h"
34 #include "wx/object.h"
35 #include "wx/list.h"
36
37 #ifdef WXMAKINGDLL_XML
38 #define WXDLLIMPEXP_XML WXEXPORT
39 #elif defined(WXUSINGDLL)
40 #define WXDLLIMPEXP_XML WXIMPORT
41 #else // not making nor using DLL
42 #define WXDLLIMPEXP_XML
43 #endif
44
45 class WXDLLIMPEXP_XML wxXmlNode;
46 class WXDLLIMPEXP_XML wxXmlProperty;
47 class WXDLLIMPEXP_XML wxXmlDocument;
48 class WXDLLIMPEXP_XML wxXmlIOHandler;
49 class WXDLLIMPEXP_BASE wxInputStream;
50 class WXDLLIMPEXP_BASE wxOutputStream;
51
52
53 // Represents XML node type.
54 enum wxXmlNodeType
55 {
56 // note: values are synchronized with xmlElementType from libxml
57 wxXML_ELEMENT_NODE = 1,
58 wxXML_ATTRIBUTE_NODE = 2,
59 wxXML_TEXT_NODE = 3,
60 wxXML_CDATA_SECTION_NODE = 4,
61 wxXML_ENTITY_REF_NODE = 5,
62 wxXML_ENTITY_NODE = 6,
63 wxXML_PI_NODE = 7,
64 wxXML_COMMENT_NODE = 8,
65 wxXML_DOCUMENT_NODE = 9,
66 wxXML_DOCUMENT_TYPE_NODE = 10,
67 wxXML_DOCUMENT_FRAG_NODE = 11,
68 wxXML_NOTATION_NODE = 12,
69 wxXML_HTML_DOCUMENT_NODE = 13
70 };
71
72
73 // Represents node property(ies).
74 // Example: in <img src="hello.gif" id="3"/> "src" is property with value
75 // "hello.gif" and "id" is prop. with value "3".
76
77 class WXDLLIMPEXP_XML wxXmlProperty
78 {
79 public:
80 wxXmlProperty() : m_next(NULL) {}
81 wxXmlProperty(const wxString& name, const wxString& value,
82 wxXmlProperty *next)
83 : m_name(name), m_value(value), m_next(next) {}
84
85 wxString GetName() const { return m_name; }
86 wxString GetValue() const { return m_value; }
87 wxXmlProperty *GetNext() const { return m_next; }
88
89 void SetName(const wxString& name) { m_name = name; }
90 void SetValue(const wxString& value) { m_value = value; }
91 void SetNext(wxXmlProperty *next) { m_next = next; }
92
93 private:
94 wxString m_name;
95 wxString m_value;
96 wxXmlProperty *m_next;
97 };
98
99
100
101 // Represents node in XML document. Node has name and may have content
102 // and properties. Most common node types are wxXML_TEXT_NODE (name and props
103 // are irrelevant) and wxXML_ELEMENT_NODE (e.g. in <title>hi</title> there is
104 // element with name="title", irrelevant content and one child (wxXML_TEXT_NODE
105 // with content="hi").
106 //
107 // If wxUSE_UNICODE is 0, all strings are encoded in the encoding given to Load
108 // (default is UTF-8).
109
110 class WXDLLIMPEXP_XML wxXmlNode
111 {
112 public:
113 wxXmlNode() : m_properties(NULL), m_parent(NULL),
114 m_children(NULL), m_next(NULL) {}
115 wxXmlNode(wxXmlNode *parent,wxXmlNodeType type,
116 const wxString& name, const wxString& content,
117 wxXmlProperty *props, wxXmlNode *next);
118 ~wxXmlNode();
119
120 // copy ctor & operator=. Note that this does NOT copy syblings
121 // and parent pointer, i.e. m_parent and m_next will be NULL
122 // after using copy ctor and are never unmodified by operator=.
123 // On the other hand, it DOES copy children and properties.
124 wxXmlNode(const wxXmlNode& node);
125 wxXmlNode& operator=(const wxXmlNode& node);
126
127 // user-friendly creation:
128 wxXmlNode(wxXmlNodeType type, const wxString& name,
129 const wxString& content = wxEmptyString);
130 void AddChild(wxXmlNode *child);
131 void InsertChild(wxXmlNode *child, wxXmlNode *before_node);
132 bool RemoveChild(wxXmlNode *child);
133 void AddProperty(const wxString& name, const wxString& value);
134 bool DeleteProperty(const wxString& name);
135
136 // access methods:
137 wxXmlNodeType GetType() const { return m_type; }
138 wxString GetName() const { return m_name; }
139 wxString GetContent() const { return m_content; }
140
141 wxXmlNode *GetParent() const { return m_parent; }
142 wxXmlNode *GetNext() const { return m_next; }
143 wxXmlNode *GetChildren() const { return m_children; }
144
145 wxXmlProperty *GetProperties() const { return m_properties; }
146 bool GetPropVal(const wxString& propName, wxString *value) const;
147 wxString GetPropVal(const wxString& propName,
148 const wxString& defaultVal) const;
149 bool HasProp(const wxString& propName) const;
150
151 void SetType(wxXmlNodeType type) { m_type = type; }
152 void SetName(const wxString& name) { m_name = name; }
153 void SetContent(const wxString& con) { m_content = con; }
154
155 void SetParent(wxXmlNode *parent) { m_parent = parent; }
156 void SetNext(wxXmlNode *next) { m_next = next; }
157 void SetChildren(wxXmlNode *child) { m_children = child; }
158
159 void SetProperties(wxXmlProperty *prop) { m_properties = prop; }
160 void AddProperty(wxXmlProperty *prop);
161
162 private:
163 wxXmlNodeType m_type;
164 wxString m_name;
165 wxString m_content;
166 wxXmlProperty *m_properties;
167 wxXmlNode *m_parent, *m_children, *m_next;
168
169 void DoCopy(const wxXmlNode& node);
170 };
171
172
173
174
175
176
177
178 // This class holds XML data/document as parsed by XML parser.
179
180 class WXDLLIMPEXP_XML wxXmlDocument : public wxObject
181 {
182 public:
183 wxXmlDocument();
184 wxXmlDocument(const wxString& filename,
185 const wxString& encoding = wxT("UTF-8"));
186 wxXmlDocument(wxInputStream& stream,
187 const wxString& encoding = wxT("UTF-8"));
188 ~wxXmlDocument() { delete m_root; }
189
190 wxXmlDocument(const wxXmlDocument& doc);
191 wxXmlDocument& operator=(const wxXmlDocument& doc);
192
193 // Parses .xml file and loads data. Returns TRUE on success, FALSE
194 // otherwise.
195 bool Load(const wxString& filename,
196 const wxString& encoding = wxT("UTF-8"));
197 bool Load(wxInputStream& stream,
198 const wxString& encoding = wxT("UTF-8"));
199
200 // Saves document as .xml file.
201 bool Save(const wxString& filename) const;
202 bool Save(wxOutputStream& stream) const;
203
204 bool IsOk() const { return m_root != NULL; }
205
206 // Returns root node of the document.
207 wxXmlNode *GetRoot() const { return m_root; }
208
209 // Returns version of document (may be empty).
210 wxString GetVersion() const { return m_version; }
211 // Returns encoding of document (may be empty).
212 // Note: this is the encoding original file was saved in, *not* the
213 // encoding of in-memory representation!
214 wxString GetFileEncoding() const { return m_fileEncoding; }
215
216 // Write-access methods:
217 void SetRoot(wxXmlNode *node) { delete m_root ; m_root = node; }
218 void SetVersion(const wxString& version) { m_version = version; }
219 void SetFileEncoding(const wxString& encoding) { m_fileEncoding = encoding; }
220
221 #if !wxUSE_UNICODE
222 // Returns encoding of in-memory representation of the document
223 // (same as passed to Load or ctor, defaults to UTF-8).
224 // NB: this is meaningless in Unicode build where data are stored as wchar_t*
225 wxString GetEncoding() const { return m_encoding; }
226 void SetEncoding(const wxString& enc) { m_encoding = enc; }
227 #endif
228
229 private:
230 wxString m_version;
231 wxString m_fileEncoding;
232 #if !wxUSE_UNICODE
233 wxString m_encoding;
234 #endif
235 wxXmlNode *m_root;
236
237 void DoCopy(const wxXmlDocument& doc);
238 };
239
240 #endif // wxUSE_XML
241
242 #endif // _WX_XML_H_