]> git.saurik.com Git - wxWidgets.git/blame - contrib/src/xml/xml.cpp
added wxHtmlHelpController::GetHelpData and removed SetBookBasePath (you can use...
[wxWidgets.git] / contrib / src / xml / xml.cpp
CommitLineData
56d2f750
VS
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
35wxXmlNode::wxXmlNode(wxXmlNode *parent,wxXmlNodeType type,
36 const wxString& name, const wxString& content,
37 wxXmlProperty *props, wxXmlNode *next)
ab708d5d
VS
38 : m_type(type), m_name(name), m_content(content),
39 m_properties(props), m_parent(parent),
40 m_children(NULL), m_next(next)
56d2f750 41{
ab708d5d 42 if (m_parent)
56d2f750 43 {
ab708d5d 44 if (m_parent->m_children)
56d2f750 45 {
ab708d5d
VS
46 m_next = m_parent->m_children;
47 m_parent->m_children = this;
56d2f750
VS
48 }
49 else
ab708d5d 50 m_parent->m_children = this;
56d2f750
VS
51 }
52}
53
54
55
56wxXmlNode::wxXmlNode(wxXmlNodeType type, const wxString& name,
57 const wxString& content)
ab708d5d
VS
58 : m_type(type), m_name(name), m_content(content),
59 m_properties(NULL), m_parent(NULL),
60 m_children(NULL), m_next(NULL)
56d2f750
VS
61{}
62
63
64
65wxXmlNode::wxXmlNode(const wxXmlNode& node)
66{
ab708d5d
VS
67 m_next = NULL;
68 m_parent = NULL;
56d2f750
VS
69 DoCopy(node);
70}
71
72
73
74wxXmlNode& wxXmlNode::operator=(const wxXmlNode& node)
75{
ab708d5d
VS
76 delete m_properties;
77 delete m_children;
56d2f750
VS
78 DoCopy(node);
79 return *this;
80}
81
82
83
84void wxXmlNode::DoCopy(const wxXmlNode& node)
85{
ab708d5d
VS
86 m_type = node.m_type;
87 m_name = node.m_name;
88 m_content = node.m_content;
89 m_children = NULL;
56d2f750 90
ab708d5d 91 wxXmlNode *n = node.m_children;
56d2f750
VS
92 while (n)
93 {
94 AddChild(new wxXmlNode(*n));
95 n = n->GetNext();
96 }
97
ab708d5d
VS
98 m_properties = NULL;
99 wxXmlProperty *p = node.m_properties;
56d2f750
VS
100 while (p)
101 {
102 AddProperty(p->GetName(), p->GetValue());
103 p = p->GetNext();
104 }
105}
106
107
108bool 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
123bool 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
142wxString 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
153void wxXmlNode::AddChild(wxXmlNode *child)
154{
ab708d5d
VS
155 if (m_children == NULL)
156 m_children = child;
56d2f750
VS
157 else
158 {
ab708d5d
VS
159 wxXmlNode *ch = m_children;
160 while (ch->m_next) ch = ch->m_next;
161 ch->m_next = child;
56d2f750 162 }
ab708d5d
VS
163 child->m_next = NULL;
164 child->m_parent = this;
56d2f750
VS
165}
166
167
168
169void wxXmlNode::InsertChild(wxXmlNode *child, wxXmlNode *before_node)
170{
a559d708 171 wxASSERT_MSG(before_node->GetParent() == this, wxT("wxXmlNode::InsertChild - the node has incorrect parent"));
56d2f750 172
ab708d5d
VS
173 if (m_children == before_node)
174 m_children = child;
56d2f750
VS
175 else
176 {
ab708d5d
VS
177 wxXmlNode *ch = m_children;
178 while (ch->m_next != before_node) ch = ch->m_next;
179 ch->m_next = child;
56d2f750
VS
180 }
181
ab708d5d
VS
182 child->m_parent = this;
183 child->m_next = before_node;
56d2f750
VS
184}
185
186
187
188bool wxXmlNode::RemoveChild(wxXmlNode *child)
189{
ab708d5d 190 if (m_children == NULL)
56d2f750 191 return FALSE;
ab708d5d 192 else if (m_children == child)
56d2f750 193 {
ab708d5d
VS
194 m_children = child->m_next;
195 child->m_parent = NULL;
196 child->m_next = NULL;
56d2f750
VS
197 return TRUE;
198 }
199 else
200 {
ab708d5d
VS
201 wxXmlNode *ch = m_children;
202 while (ch->m_next)
56d2f750 203 {
ab708d5d 204 if (ch->m_next == child)
56d2f750 205 {
ab708d5d
VS
206 ch->m_next = child->m_next;
207 child->m_parent = NULL;
208 child->m_next = NULL;
56d2f750
VS
209 return TRUE;
210 }
ab708d5d 211 ch = ch->m_next;
56d2f750
VS
212 }
213 return FALSE;
214 }
215}
216
217
218
219void wxXmlNode::AddProperty(const wxString& name, const wxString& value)
220{
221 AddProperty(new wxXmlProperty(name, value, NULL));
222}
223
224void wxXmlNode::AddProperty(wxXmlProperty *prop)
225{
ab708d5d
VS
226 if (m_properties == NULL)
227 m_properties = prop;
56d2f750
VS
228 else
229 {
ab708d5d 230 wxXmlProperty *p = m_properties;
56d2f750
VS
231 while (p->GetNext()) p = p->GetNext();
232 p->SetNext(prop);
233 }
234}
235
236
237
238bool wxXmlNode::DeleteProperty(const wxString& name)
239{
ab708d5d 240 if (m_properties == NULL)
56d2f750
VS
241 return FALSE;
242
ab708d5d 243 else if (m_properties->GetName() == name)
56d2f750 244 {
ab708d5d
VS
245 wxXmlProperty *prop = m_properties;
246 m_properties = prop->GetNext();
56d2f750
VS
247 prop->SetNext(NULL);
248 delete prop;
249 return TRUE;
250 }
251
252 else
253 {
ab708d5d 254 wxXmlProperty *p = m_properties;
56d2f750
VS
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
ab708d5d 278wxList *wxXmlDocument::sm_handlers = NULL;
ecdce304 279
56d2f750
VS
280
281
282wxXmlDocument::wxXmlDocument(const wxString& filename, wxXmlIOType io_type)
ab708d5d 283 : wxObject(), m_root(NULL)
56d2f750
VS
284{
285 if (!Load(filename, io_type))
286 {
ab708d5d
VS
287 delete m_root;
288 m_root = NULL;
56d2f750
VS
289 }
290}
291
292
293
294wxXmlDocument::wxXmlDocument(wxInputStream& stream, wxXmlIOType io_type)
ab708d5d 295 : wxObject(), m_root(NULL)
56d2f750
VS
296{
297 if (!Load(stream, io_type))
298 {
ab708d5d
VS
299 delete m_root;
300 m_root = NULL;
56d2f750
VS
301 }
302}
303
304
305
306wxXmlDocument::wxXmlDocument(const wxXmlDocument& doc)
307{
308 DoCopy(doc);
309}
310
311
312
313wxXmlDocument& wxXmlDocument::operator=(const wxXmlDocument& doc)
314{
ab708d5d 315 delete m_root;
56d2f750
VS
316 DoCopy(doc);
317 return *this;
318}
319
320
321
322void wxXmlDocument::DoCopy(const wxXmlDocument& doc)
323{
ab708d5d
VS
324 m_version = doc.m_version;
325 m_encoding = doc.m_encoding;
326 m_root = new wxXmlNode(*doc.m_root);
56d2f750
VS
327}
328
329
330
331bool wxXmlDocument::Load(const wxString& filename, wxXmlIOType io_type)
332{
333 wxFileInputStream stream(filename);
334 return Load(stream, io_type);
335}
336
337
338
339bool wxXmlDocument::Load(wxInputStream& stream, wxXmlIOType io_type)
340{
ab708d5d 341 wxNode *n = sm_handlers->GetFirst();
56d2f750
VS
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
359bool wxXmlDocument::Save(const wxString& filename, wxXmlIOType io_type) const
360{
361 wxFileOutputStream stream(filename);
362 return Save(stream, io_type);
363}
364
365
366
367bool wxXmlDocument::Save(wxOutputStream& stream, wxXmlIOType io_type) const
368{
ab708d5d 369 wxNode *n = sm_handlers->GetFirst();
56d2f750
VS
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
56d2f750
VS
388void wxXmlDocument::AddHandler(wxXmlIOHandler *handler)
389{
ab708d5d 390 if (sm_handlers == NULL)
56d2f750 391 {
ab708d5d
VS
392 sm_handlers = new wxList;
393 sm_handlers->DeleteContents(TRUE);
56d2f750 394 }
ab708d5d 395 sm_handlers->Append(handler);
56d2f750
VS
396}
397
398
399void wxXmlDocument::CleanUpHandlers()
400{
ab708d5d
VS
401 delete sm_handlers;
402 sm_handlers = NULL;
56d2f750
VS
403}
404
405
406void wxXmlDocument::InitStandardHandlers()
407{
408 AddHandler(new wxXmlIOHandlerBin);
6ba549f2 409 AddHandler(new wxXmlIOHandlerBinZ);
ab708d5d
VS
410 AddHandler(new wxXmlIOHandlerExpat);
411 AddHandler(new wxXmlIOHandlerWriter);
56d2f750
VS
412}
413
414
415#include "wx/module.h"
416
417class 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
426IMPLEMENT_DYNAMIC_CLASS(wxXmlModule, wxModule)