]>
Commit | Line | Data |
---|---|---|
78d14f80 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/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 | ||
93cd80d6 VS |
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 | ||
78d14f80 VS |
93 | wxXmlNode& wxXmlNode::operator=(const wxXmlNode& node) |
94 | { | |
3fb676f8 GT |
95 | wxDELETE(m_properties); |
96 | wxDELETE(m_children); | |
78d14f80 VS |
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 | { | |
3fb676f8 | 306 | wxDELETE(m_root); |
78d14f80 VS |
307 | } |
308 | } | |
309 | ||
310 | ||
311 | ||
312 | wxXmlDocument::wxXmlDocument(wxInputStream& stream, wxXmlIOType io_type) | |
313 | : wxObject(), m_root(NULL) | |
314 | { | |
315 | if (!Load(stream, io_type)) | |
316 | { | |
3fb676f8 | 317 | wxDELETE(m_root); |
78d14f80 VS |
318 | } |
319 | } | |
320 | ||
321 | ||
322 | ||
323 | wxXmlDocument::wxXmlDocument(const wxXmlDocument& doc) | |
324 | { | |
325 | DoCopy(doc); | |
326 | } | |
327 | ||
328 | ||
329 | ||
330 | wxXmlDocument& wxXmlDocument::operator=(const wxXmlDocument& doc) | |
331 | { | |
3fb676f8 | 332 | wxDELETE(m_root); |
78d14f80 VS |
333 | DoCopy(doc); |
334 | return *this; | |
335 | } | |
336 | ||
337 | ||
338 | ||
339 | void wxXmlDocument::DoCopy(const wxXmlDocument& doc) | |
340 | { | |
341 | m_version = doc.m_version; | |
342 | m_encoding = doc.m_encoding; | |
343 | m_root = new wxXmlNode(*doc.m_root); | |
344 | } | |
345 | ||
346 | ||
347 | ||
348 | bool wxXmlDocument::Load(const wxString& filename, wxXmlIOType io_type) | |
349 | { | |
350 | wxFileInputStream stream(filename); | |
351 | return Load(stream, io_type); | |
352 | } | |
353 | ||
354 | ||
355 | ||
356 | bool wxXmlDocument::Load(wxInputStream& stream, wxXmlIOType io_type) | |
357 | { | |
358 | wxNode *n = sm_handlers->GetFirst(); | |
359 | while (n) | |
360 | { | |
361 | wxXmlIOHandler *h = (wxXmlIOHandler*) n->GetData(); | |
362 | ||
363 | if ((io_type == wxXML_IO_AUTO || io_type == h->GetType()) && | |
364 | h->CanLoad(stream)) | |
365 | { | |
366 | return h->Load(stream, *this); | |
367 | } | |
368 | n = n->GetNext(); | |
369 | } | |
370 | wxLogError(_("Cannot find XML I/O handler capable of loading this format.")); | |
371 | return FALSE; | |
372 | } | |
373 | ||
374 | ||
375 | ||
376 | bool wxXmlDocument::Save(const wxString& filename, wxXmlIOType io_type) const | |
377 | { | |
378 | wxFileOutputStream stream(filename); | |
379 | return Save(stream, io_type); | |
380 | } | |
381 | ||
382 | ||
383 | ||
384 | bool wxXmlDocument::Save(wxOutputStream& stream, wxXmlIOType io_type) const | |
385 | { | |
386 | wxNode *n = sm_handlers->GetFirst(); | |
387 | while (n) | |
388 | { | |
389 | wxXmlIOHandler *h = (wxXmlIOHandler*) n->GetData(); | |
390 | if (io_type == h->GetType() && h->CanSave()) | |
391 | { | |
392 | return h->Save(stream, *this); | |
393 | } | |
394 | n = n->GetNext(); | |
395 | } | |
396 | wxLogError(_("Cannot find XML I/O handler capable of saving in this format.")); | |
397 | return FALSE; | |
398 | } | |
399 | ||
400 | ||
401 | ||
402 | ||
403 | ||
404 | ||
405 | void wxXmlDocument::AddHandler(wxXmlIOHandler *handler) | |
406 | { | |
407 | if (sm_handlers == NULL) | |
408 | { | |
409 | sm_handlers = new wxList; | |
410 | sm_handlers->DeleteContents(TRUE); | |
411 | } | |
412 | sm_handlers->Append(handler); | |
413 | } | |
414 | ||
415 | ||
416 | void wxXmlDocument::CleanUpHandlers() | |
417 | { | |
3fb676f8 | 418 | wxDELETE(sm_handlers); |
78d14f80 VS |
419 | } |
420 | ||
421 | ||
422 | void wxXmlDocument::InitStandardHandlers() | |
423 | { | |
424 | AddHandler(new wxXmlIOHandlerBin); | |
425 | #if wxUSE_ZLIB | |
426 | AddHandler(new wxXmlIOHandlerBinZ); | |
427 | #endif | |
428 | AddHandler(new wxXmlIOHandlerExpat); | |
429 | AddHandler(new wxXmlIOHandlerWriter); | |
430 | } | |
431 | ||
432 | ||
433 | #include "wx/module.h" | |
434 | ||
435 | class wxXmlModule: public wxModule | |
436 | { | |
437 | DECLARE_DYNAMIC_CLASS(wxXmlModule) | |
438 | public: | |
439 | wxXmlModule() {} | |
440 | bool OnInit() { wxXmlDocument::InitStandardHandlers(); return TRUE; }; | |
441 | void OnExit() { wxXmlDocument::CleanUpHandlers(); }; | |
442 | }; | |
443 | ||
444 | IMPLEMENT_DYNAMIC_CLASS(wxXmlModule, wxModule) | |
445 | ||
446 | ||
447 | ||
448 | ||
449 | // When wxXml is loaded dynamically after the application is already running | |
450 | // then the built-in module system won't pick this one up. Add it manually. | |
451 | void wxXmlInitXmlModule() | |
452 | { | |
453 | wxModule* module = new wxXmlModule; | |
454 | module->Init(); | |
455 | wxModule::RegisterModule(module); | |
456 | } | |
457 |