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