]> git.saurik.com Git - wxWidgets.git/blob - contrib/src/xml/xml.cpp
added wxHtmlHelpController::GetHelpData and removed SetBookBasePath (you can use...
[wxWidgets.git] / contrib / src / xml / xml.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: xml.cpp
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 #ifdef __GNUG__
12 #pragma implementation "xml.h"
13 #pragma implementation "xmlio.h"
14 #endif
15
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23
24 #include "wx/wfstream.h"
25 #include "wx/datstrm.h"
26 #include "wx/zstream.h"
27 #include "wx/log.h"
28 #include "wx/intl.h"
29
30 #include "wx/xml/xml.h"
31 #include "wx/xml/xmlio.h"
32
33
34
35 wxXmlNode::wxXmlNode(wxXmlNode *parent,wxXmlNodeType type,
36 const wxString& name, const wxString& content,
37 wxXmlProperty *props, wxXmlNode *next)
38 : m_type(type), m_name(name), m_content(content),
39 m_properties(props), m_parent(parent),
40 m_children(NULL), m_next(next)
41 {
42 if (m_parent)
43 {
44 if (m_parent->m_children)
45 {
46 m_next = m_parent->m_children;
47 m_parent->m_children = this;
48 }
49 else
50 m_parent->m_children = this;
51 }
52 }
53
54
55
56 wxXmlNode::wxXmlNode(wxXmlNodeType type, const wxString& name,
57 const wxString& content)
58 : m_type(type), m_name(name), m_content(content),
59 m_properties(NULL), m_parent(NULL),
60 m_children(NULL), m_next(NULL)
61 {}
62
63
64
65 wxXmlNode::wxXmlNode(const wxXmlNode& node)
66 {
67 m_next = NULL;
68 m_parent = NULL;
69 DoCopy(node);
70 }
71
72
73
74 wxXmlNode& wxXmlNode::operator=(const wxXmlNode& node)
75 {
76 delete m_properties;
77 delete m_children;
78 DoCopy(node);
79 return *this;
80 }
81
82
83
84 void wxXmlNode::DoCopy(const wxXmlNode& node)
85 {
86 m_type = node.m_type;
87 m_name = node.m_name;
88 m_content = node.m_content;
89 m_children = NULL;
90
91 wxXmlNode *n = node.m_children;
92 while (n)
93 {
94 AddChild(new wxXmlNode(*n));
95 n = n->GetNext();
96 }
97
98 m_properties = NULL;
99 wxXmlProperty *p = node.m_properties;
100 while (p)
101 {
102 AddProperty(p->GetName(), p->GetValue());
103 p = p->GetNext();
104 }
105 }
106
107
108 bool wxXmlNode::HasProp(const wxString& propName) const
109 {
110 wxXmlProperty *prop = GetProperties();
111
112 while (prop)
113 {
114 if (prop->GetName() == propName) return TRUE;
115 prop = prop->GetNext();
116 }
117
118 return FALSE;
119 }
120
121
122
123 bool wxXmlNode::GetPropVal(const wxString& propName, wxString *value) const
124 {
125 wxXmlProperty *prop = GetProperties();
126
127 while (prop)
128 {
129 if (prop->GetName() == propName)
130 {
131 *value = prop->GetValue();
132 return TRUE;
133 }
134 prop = prop->GetNext();
135 }
136
137 return FALSE;
138 }
139
140
141
142 wxString wxXmlNode::GetPropVal(const wxString& propName, const wxString& defaultVal) const
143 {
144 wxString tmp;
145 if (GetPropVal(propName, &tmp))
146 return tmp;
147 else
148 return defaultVal;
149 }
150
151
152
153 void wxXmlNode::AddChild(wxXmlNode *child)
154 {
155 if (m_children == NULL)
156 m_children = child;
157 else
158 {
159 wxXmlNode *ch = m_children;
160 while (ch->m_next) ch = ch->m_next;
161 ch->m_next = child;
162 }
163 child->m_next = NULL;
164 child->m_parent = this;
165 }
166
167
168
169 void wxXmlNode::InsertChild(wxXmlNode *child, wxXmlNode *before_node)
170 {
171 wxASSERT_MSG(before_node->GetParent() == this, wxT("wxXmlNode::InsertChild - the node has incorrect parent"));
172
173 if (m_children == before_node)
174 m_children = child;
175 else
176 {
177 wxXmlNode *ch = m_children;
178 while (ch->m_next != before_node) ch = ch->m_next;
179 ch->m_next = child;
180 }
181
182 child->m_parent = this;
183 child->m_next = before_node;
184 }
185
186
187
188 bool wxXmlNode::RemoveChild(wxXmlNode *child)
189 {
190 if (m_children == NULL)
191 return FALSE;
192 else if (m_children == child)
193 {
194 m_children = child->m_next;
195 child->m_parent = NULL;
196 child->m_next = NULL;
197 return TRUE;
198 }
199 else
200 {
201 wxXmlNode *ch = m_children;
202 while (ch->m_next)
203 {
204 if (ch->m_next == child)
205 {
206 ch->m_next = child->m_next;
207 child->m_parent = NULL;
208 child->m_next = NULL;
209 return TRUE;
210 }
211 ch = ch->m_next;
212 }
213 return FALSE;
214 }
215 }
216
217
218
219 void wxXmlNode::AddProperty(const wxString& name, const wxString& value)
220 {
221 AddProperty(new wxXmlProperty(name, value, NULL));
222 }
223
224 void wxXmlNode::AddProperty(wxXmlProperty *prop)
225 {
226 if (m_properties == NULL)
227 m_properties = prop;
228 else
229 {
230 wxXmlProperty *p = m_properties;
231 while (p->GetNext()) p = p->GetNext();
232 p->SetNext(prop);
233 }
234 }
235
236
237
238 bool wxXmlNode::DeleteProperty(const wxString& name)
239 {
240 if (m_properties == NULL)
241 return FALSE;
242
243 else if (m_properties->GetName() == name)
244 {
245 wxXmlProperty *prop = m_properties;
246 m_properties = prop->GetNext();
247 prop->SetNext(NULL);
248 delete prop;
249 return TRUE;
250 }
251
252 else
253 {
254 wxXmlProperty *p = m_properties;
255 while (p->GetNext())
256 {
257 if (p->GetNext()->GetName() == name)
258 {
259 wxXmlProperty *prop = p->GetNext();
260 p->SetNext(prop->GetNext());
261 prop->SetNext(NULL);
262 delete prop;
263 return TRUE;
264 }
265 p = p->GetNext();
266 }
267 return FALSE;
268 }
269 }
270
271
272
273
274
275
276
277
278 wxList *wxXmlDocument::sm_handlers = NULL;
279
280
281
282 wxXmlDocument::wxXmlDocument(const wxString& filename, wxXmlIOType io_type)
283 : wxObject(), m_root(NULL)
284 {
285 if (!Load(filename, io_type))
286 {
287 delete m_root;
288 m_root = NULL;
289 }
290 }
291
292
293
294 wxXmlDocument::wxXmlDocument(wxInputStream& stream, wxXmlIOType io_type)
295 : wxObject(), m_root(NULL)
296 {
297 if (!Load(stream, io_type))
298 {
299 delete m_root;
300 m_root = NULL;
301 }
302 }
303
304
305
306 wxXmlDocument::wxXmlDocument(const wxXmlDocument& doc)
307 {
308 DoCopy(doc);
309 }
310
311
312
313 wxXmlDocument& wxXmlDocument::operator=(const wxXmlDocument& doc)
314 {
315 delete m_root;
316 DoCopy(doc);
317 return *this;
318 }
319
320
321
322 void wxXmlDocument::DoCopy(const wxXmlDocument& doc)
323 {
324 m_version = doc.m_version;
325 m_encoding = doc.m_encoding;
326 m_root = new wxXmlNode(*doc.m_root);
327 }
328
329
330
331 bool wxXmlDocument::Load(const wxString& filename, wxXmlIOType io_type)
332 {
333 wxFileInputStream stream(filename);
334 return Load(stream, io_type);
335 }
336
337
338
339 bool wxXmlDocument::Load(wxInputStream& stream, wxXmlIOType io_type)
340 {
341 wxNode *n = sm_handlers->GetFirst();
342 while (n)
343 {
344 wxXmlIOHandler *h = (wxXmlIOHandler*) n->GetData();
345
346 if ((io_type == wxXML_IO_AUTO || io_type == h->GetType()) &&
347 h->CanLoad(stream))
348 {
349 return h->Load(stream, *this);
350 }
351 n = n->GetNext();
352 }
353 wxLogError(_("Cannot find XML I/O handler capable of loading this format."));
354 return FALSE;
355 }
356
357
358
359 bool wxXmlDocument::Save(const wxString& filename, wxXmlIOType io_type) const
360 {
361 wxFileOutputStream stream(filename);
362 return Save(stream, io_type);
363 }
364
365
366
367 bool wxXmlDocument::Save(wxOutputStream& stream, wxXmlIOType io_type) const
368 {
369 wxNode *n = sm_handlers->GetFirst();
370 while (n)
371 {
372 wxXmlIOHandler *h = (wxXmlIOHandler*) n->GetData();
373 if (io_type == h->GetType() && h->CanSave())
374 {
375 return h->Save(stream, *this);
376 }
377 n = n->GetNext();
378 }
379 wxLogError(_("Cannot find XML I/O handler capable of saving in this format."));
380 return FALSE;
381 }
382
383
384
385
386
387
388 void wxXmlDocument::AddHandler(wxXmlIOHandler *handler)
389 {
390 if (sm_handlers == NULL)
391 {
392 sm_handlers = new wxList;
393 sm_handlers->DeleteContents(TRUE);
394 }
395 sm_handlers->Append(handler);
396 }
397
398
399 void wxXmlDocument::CleanUpHandlers()
400 {
401 delete sm_handlers;
402 sm_handlers = NULL;
403 }
404
405
406 void wxXmlDocument::InitStandardHandlers()
407 {
408 AddHandler(new wxXmlIOHandlerBin);
409 AddHandler(new wxXmlIOHandlerBinZ);
410 AddHandler(new wxXmlIOHandlerExpat);
411 AddHandler(new wxXmlIOHandlerWriter);
412 }
413
414
415 #include "wx/module.h"
416
417 class wxXmlModule: public wxModule
418 {
419 DECLARE_DYNAMIC_CLASS(wxXmlModule)
420 public:
421 wxXmlModule() {}
422 bool OnInit() { wxXmlDocument::InitStandardHandlers(); return TRUE; };
423 void OnExit() { wxXmlDocument::CleanUpHandlers(); };
424 };
425
426 IMPLEMENT_DYNAMIC_CLASS(wxXmlModule, wxModule)