]> git.saurik.com Git - wxWidgets.git/blob - src/xrc/xml.cpp
applied patch 475879 (Can't use subclassed GridCellEditor)
[wxWidgets.git] / src / xrc / 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/xrc/xml.h"
31 #include "wx/xrc/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()
75 {
76 wxXmlNode *c, *c2;
77 for (c = m_children; c; c = c2)
78 {
79 c2 = c->m_next;
80 delete c;
81 }
82
83 wxXmlProperty *p, *p2;
84 for (p = m_properties; p; p = p2)
85 {
86 p2 = p->GetNext();
87 delete p;
88 }
89 }
90
91
92
93 wxXmlNode& wxXmlNode::operator=(const wxXmlNode& node)
94 {
95 delete m_properties;
96 delete m_children;
97 DoCopy(node);
98 return *this;
99 }
100
101
102
103 void wxXmlNode::DoCopy(const wxXmlNode& node)
104 {
105 m_type = node.m_type;
106 m_name = node.m_name;
107 m_content = node.m_content;
108 m_children = NULL;
109
110 wxXmlNode *n = node.m_children;
111 while (n)
112 {
113 AddChild(new wxXmlNode(*n));
114 n = n->GetNext();
115 }
116
117 m_properties = NULL;
118 wxXmlProperty *p = node.m_properties;
119 while (p)
120 {
121 AddProperty(p->GetName(), p->GetValue());
122 p = p->GetNext();
123 }
124 }
125
126
127 bool wxXmlNode::HasProp(const wxString& propName) const
128 {
129 wxXmlProperty *prop = GetProperties();
130
131 while (prop)
132 {
133 if (prop->GetName() == propName) return TRUE;
134 prop = prop->GetNext();
135 }
136
137 return FALSE;
138 }
139
140
141
142 bool wxXmlNode::GetPropVal(const wxString& propName, wxString *value) const
143 {
144 wxXmlProperty *prop = GetProperties();
145
146 while (prop)
147 {
148 if (prop->GetName() == propName)
149 {
150 *value = prop->GetValue();
151 return TRUE;
152 }
153 prop = prop->GetNext();
154 }
155
156 return FALSE;
157 }
158
159
160
161 wxString wxXmlNode::GetPropVal(const wxString& propName, const wxString& defaultVal) const
162 {
163 wxString tmp;
164 if (GetPropVal(propName, &tmp))
165 return tmp;
166 else
167 return defaultVal;
168 }
169
170
171
172 void wxXmlNode::AddChild(wxXmlNode *child)
173 {
174 if (m_children == NULL)
175 m_children = child;
176 else
177 {
178 wxXmlNode *ch = m_children;
179 while (ch->m_next) ch = ch->m_next;
180 ch->m_next = child;
181 }
182 child->m_next = NULL;
183 child->m_parent = this;
184 }
185
186
187
188 void wxXmlNode::InsertChild(wxXmlNode *child, wxXmlNode *before_node)
189 {
190 wxASSERT_MSG(before_node->GetParent() == this, wxT("wxXmlNode::InsertChild - the node has incorrect parent"));
191
192 if (m_children == before_node)
193 m_children = child;
194 else
195 {
196 wxXmlNode *ch = m_children;
197 while (ch->m_next != before_node) ch = ch->m_next;
198 ch->m_next = child;
199 }
200
201 child->m_parent = this;
202 child->m_next = before_node;
203 }
204
205
206
207 bool wxXmlNode::RemoveChild(wxXmlNode *child)
208 {
209 if (m_children == NULL)
210 return FALSE;
211 else if (m_children == child)
212 {
213 m_children = child->m_next;
214 child->m_parent = NULL;
215 child->m_next = NULL;
216 return TRUE;
217 }
218 else
219 {
220 wxXmlNode *ch = m_children;
221 while (ch->m_next)
222 {
223 if (ch->m_next == child)
224 {
225 ch->m_next = child->m_next;
226 child->m_parent = NULL;
227 child->m_next = NULL;
228 return TRUE;
229 }
230 ch = ch->m_next;
231 }
232 return FALSE;
233 }
234 }
235
236
237
238 void wxXmlNode::AddProperty(const wxString& name, const wxString& value)
239 {
240 AddProperty(new wxXmlProperty(name, value, NULL));
241 }
242
243 void wxXmlNode::AddProperty(wxXmlProperty *prop)
244 {
245 if (m_properties == NULL)
246 m_properties = prop;
247 else
248 {
249 wxXmlProperty *p = m_properties;
250 while (p->GetNext()) p = p->GetNext();
251 p->SetNext(prop);
252 }
253 }
254
255
256
257 bool wxXmlNode::DeleteProperty(const wxString& name)
258 {
259 if (m_properties == NULL)
260 return FALSE;
261
262 else if (m_properties->GetName() == name)
263 {
264 wxXmlProperty *prop = m_properties;
265 m_properties = prop->GetNext();
266 prop->SetNext(NULL);
267 delete prop;
268 return TRUE;
269 }
270
271 else
272 {
273 wxXmlProperty *p = m_properties;
274 while (p->GetNext())
275 {
276 if (p->GetNext()->GetName() == name)
277 {
278 wxXmlProperty *prop = p->GetNext();
279 p->SetNext(prop->GetNext());
280 prop->SetNext(NULL);
281 delete prop;
282 return TRUE;
283 }
284 p = p->GetNext();
285 }
286 return FALSE;
287 }
288 }
289
290
291
292
293
294
295
296
297 wxList *wxXmlDocument::sm_handlers = NULL;
298
299
300
301 wxXmlDocument::wxXmlDocument(const wxString& filename, wxXmlIOType io_type)
302 : wxObject(), m_root(NULL)
303 {
304 if (!Load(filename, io_type))
305 {
306 delete m_root;
307 m_root = NULL;
308 }
309 }
310
311
312
313 wxXmlDocument::wxXmlDocument(wxInputStream& stream, wxXmlIOType io_type)
314 : wxObject(), m_root(NULL)
315 {
316 if (!Load(stream, io_type))
317 {
318 delete m_root;
319 m_root = NULL;
320 }
321 }
322
323
324
325 wxXmlDocument::wxXmlDocument(const wxXmlDocument& doc)
326 {
327 DoCopy(doc);
328 }
329
330
331
332 wxXmlDocument& wxXmlDocument::operator=(const wxXmlDocument& doc)
333 {
334 delete m_root;
335 DoCopy(doc);
336 return *this;
337 }
338
339
340
341 void wxXmlDocument::DoCopy(const wxXmlDocument& doc)
342 {
343 m_version = doc.m_version;
344 m_encoding = doc.m_encoding;
345 m_root = new wxXmlNode(*doc.m_root);
346 }
347
348
349
350 bool wxXmlDocument::Load(const wxString& filename, wxXmlIOType io_type)
351 {
352 wxFileInputStream stream(filename);
353 return Load(stream, io_type);
354 }
355
356
357
358 bool wxXmlDocument::Load(wxInputStream& stream, wxXmlIOType io_type)
359 {
360 wxNode *n = sm_handlers->GetFirst();
361 while (n)
362 {
363 wxXmlIOHandler *h = (wxXmlIOHandler*) n->GetData();
364
365 if ((io_type == wxXML_IO_AUTO || io_type == h->GetType()) &&
366 h->CanLoad(stream))
367 {
368 return h->Load(stream, *this);
369 }
370 n = n->GetNext();
371 }
372 wxLogError(_("Cannot find XML I/O handler capable of loading this format."));
373 return FALSE;
374 }
375
376
377
378 bool wxXmlDocument::Save(const wxString& filename, wxXmlIOType io_type) const
379 {
380 wxFileOutputStream stream(filename);
381 return Save(stream, io_type);
382 }
383
384
385
386 bool wxXmlDocument::Save(wxOutputStream& stream, wxXmlIOType io_type) const
387 {
388 wxNode *n = sm_handlers->GetFirst();
389 while (n)
390 {
391 wxXmlIOHandler *h = (wxXmlIOHandler*) n->GetData();
392 if (io_type == h->GetType() && h->CanSave())
393 {
394 return h->Save(stream, *this);
395 }
396 n = n->GetNext();
397 }
398 wxLogError(_("Cannot find XML I/O handler capable of saving in this format."));
399 return FALSE;
400 }
401
402
403
404
405
406
407 void wxXmlDocument::AddHandler(wxXmlIOHandler *handler)
408 {
409 if (sm_handlers == NULL)
410 {
411 sm_handlers = new wxList;
412 sm_handlers->DeleteContents(TRUE);
413 }
414 sm_handlers->Append(handler);
415 }
416
417
418 void wxXmlDocument::CleanUpHandlers()
419 {
420 delete sm_handlers;
421 sm_handlers = NULL;
422 }
423
424
425 void wxXmlDocument::InitStandardHandlers()
426 {
427 AddHandler(new wxXmlIOHandlerBin);
428 #if wxUSE_ZLIB
429 AddHandler(new wxXmlIOHandlerBinZ);
430 #endif
431 AddHandler(new wxXmlIOHandlerExpat);
432 AddHandler(new wxXmlIOHandlerWriter);
433 }
434
435
436 #include "wx/module.h"
437
438 class wxXmlModule: public wxModule
439 {
440 DECLARE_DYNAMIC_CLASS(wxXmlModule)
441 public:
442 wxXmlModule() {}
443 bool OnInit() { wxXmlDocument::InitStandardHandlers(); return TRUE; };
444 void OnExit() { wxXmlDocument::CleanUpHandlers(); };
445 };
446
447 IMPLEMENT_DYNAMIC_CLASS(wxXmlModule, wxModule)
448
449
450
451
452 // When wxXml is loaded dynamically after the application is already running
453 // then the built-in module system won't pick this one up. Add it manually.
454 void wxXmlInitXmlModule()
455 {
456 wxModule* module = new wxXmlModule;
457 module->Init();
458 wxModule::RegisterModule(module);
459 }
460