]> git.saurik.com Git - wxWidgets.git/blob - src/xml/xml.cpp
deal with the situation when a (dynamic) event handler disconnects itself during...
[wxWidgets.git] / 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 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
13
14 #ifdef __BORLANDC__
15 #pragma hdrstop
16 #endif
17
18 #include "wx/xml/xml.h"
19
20 #if wxUSE_XML
21
22 #include "wx/wfstream.h"
23 #include "wx/datstrm.h"
24 #include "wx/zstream.h"
25 #include "wx/log.h"
26 #include "wx/intl.h"
27 #include "wx/strconv.h"
28
29 #include "expat.h" // from Expat
30
31 // DLL options compatibility check:
32 #include "wx/app.h"
33 WX_CHECK_BUILD_OPTIONS("wxXML")
34
35
36 IMPLEMENT_CLASS(wxXmlDocument, wxObject)
37
38
39
40 //-----------------------------------------------------------------------------
41 // wxXmlNode
42 //-----------------------------------------------------------------------------
43
44 wxXmlNode::wxXmlNode(wxXmlNode *parent,wxXmlNodeType type,
45 const wxString& name, const wxString& content,
46 wxXmlProperty *props, wxXmlNode *next)
47 : m_type(type), m_name(name), m_content(content),
48 m_properties(props), m_parent(parent),
49 m_children(NULL), m_next(next)
50 {
51 if (m_parent)
52 {
53 if (m_parent->m_children)
54 {
55 m_next = m_parent->m_children;
56 m_parent->m_children = this;
57 }
58 else
59 m_parent->m_children = this;
60 }
61 }
62
63 wxXmlNode::wxXmlNode(wxXmlNodeType type, const wxString& name,
64 const wxString& content)
65 : m_type(type), m_name(name), m_content(content),
66 m_properties(NULL), m_parent(NULL),
67 m_children(NULL), m_next(NULL)
68 {}
69
70 wxXmlNode::wxXmlNode(const wxXmlNode& node)
71 {
72 m_next = NULL;
73 m_parent = NULL;
74 DoCopy(node);
75 }
76
77 wxXmlNode::~wxXmlNode()
78 {
79 wxXmlNode *c, *c2;
80 for (c = m_children; c; c = c2)
81 {
82 c2 = c->m_next;
83 delete c;
84 }
85
86 wxXmlProperty *p, *p2;
87 for (p = m_properties; p; p = p2)
88 {
89 p2 = p->GetNext();
90 delete p;
91 }
92 }
93
94 wxXmlNode& wxXmlNode::operator=(const wxXmlNode& node)
95 {
96 wxDELETE(m_properties);
97 wxDELETE(m_children);
98 DoCopy(node);
99 return *this;
100 }
101
102 void wxXmlNode::DoCopy(const wxXmlNode& node)
103 {
104 m_type = node.m_type;
105 m_name = node.m_name;
106 m_content = node.m_content;
107 m_children = NULL;
108
109 wxXmlNode *n = node.m_children;
110 while (n)
111 {
112 AddChild(new wxXmlNode(*n));
113 n = n->GetNext();
114 }
115
116 m_properties = NULL;
117 wxXmlProperty *p = node.m_properties;
118 while (p)
119 {
120 AddProperty(p->GetName(), p->GetValue());
121 p = p->GetNext();
122 }
123 }
124
125 bool wxXmlNode::HasProp(const wxString& propName) const
126 {
127 wxXmlProperty *prop = GetProperties();
128
129 while (prop)
130 {
131 if (prop->GetName() == propName) return true;
132 prop = prop->GetNext();
133 }
134
135 return false;
136 }
137
138 bool wxXmlNode::GetPropVal(const wxString& propName, wxString *value) const
139 {
140 wxXmlProperty *prop = GetProperties();
141
142 while (prop)
143 {
144 if (prop->GetName() == propName)
145 {
146 *value = prop->GetValue();
147 return true;
148 }
149 prop = prop->GetNext();
150 }
151
152 return false;
153 }
154
155 wxString wxXmlNode::GetPropVal(const wxString& propName, const wxString& defaultVal) const
156 {
157 wxString tmp;
158 if (GetPropVal(propName, &tmp))
159 return tmp;
160
161 return defaultVal;
162 }
163
164 void wxXmlNode::AddChild(wxXmlNode *child)
165 {
166 if (m_children == NULL)
167 m_children = child;
168 else
169 {
170 wxXmlNode *ch = m_children;
171 while (ch->m_next) ch = ch->m_next;
172 ch->m_next = child;
173 }
174 child->m_next = NULL;
175 child->m_parent = this;
176 }
177
178 void wxXmlNode::InsertChild(wxXmlNode *child, wxXmlNode *before_node)
179 {
180 wxASSERT_MSG(before_node->GetParent() == this, wxT("wxXmlNode::InsertChild - the node has incorrect parent"));
181
182 if (m_children == before_node)
183 m_children = child;
184 else
185 {
186 wxXmlNode *ch = m_children;
187 while (ch->m_next != before_node) ch = ch->m_next;
188 ch->m_next = child;
189 }
190
191 child->m_parent = this;
192 child->m_next = before_node;
193 }
194
195 bool wxXmlNode::RemoveChild(wxXmlNode *child)
196 {
197 if (m_children == NULL)
198 return false;
199 else if (m_children == child)
200 {
201 m_children = child->m_next;
202 child->m_parent = NULL;
203 child->m_next = NULL;
204 return true;
205 }
206 else
207 {
208 wxXmlNode *ch = m_children;
209 while (ch->m_next)
210 {
211 if (ch->m_next == child)
212 {
213 ch->m_next = child->m_next;
214 child->m_parent = NULL;
215 child->m_next = NULL;
216 return true;
217 }
218 ch = ch->m_next;
219 }
220 return false;
221 }
222 }
223
224 void wxXmlNode::AddProperty(const wxString& name, const wxString& value)
225 {
226 AddProperty(new wxXmlProperty(name, value, NULL));
227 }
228
229 void wxXmlNode::AddProperty(wxXmlProperty *prop)
230 {
231 if (m_properties == NULL)
232 m_properties = prop;
233 else
234 {
235 wxXmlProperty *p = m_properties;
236 while (p->GetNext()) p = p->GetNext();
237 p->SetNext(prop);
238 }
239 }
240
241 bool wxXmlNode::DeleteProperty(const wxString& name)
242 {
243 wxXmlProperty *prop;
244
245 if (m_properties == NULL)
246 return false;
247
248 else if (m_properties->GetName() == name)
249 {
250 prop = m_properties;
251 m_properties = prop->GetNext();
252 prop->SetNext(NULL);
253 delete prop;
254 return true;
255 }
256
257 else
258 {
259 wxXmlProperty *p = m_properties;
260 while (p->GetNext())
261 {
262 if (p->GetNext()->GetName() == name)
263 {
264 prop = p->GetNext();
265 p->SetNext(prop->GetNext());
266 prop->SetNext(NULL);
267 delete prop;
268 return true;
269 }
270 p = p->GetNext();
271 }
272 return false;
273 }
274 }
275
276 wxString wxXmlNode::GetNodeContent() const
277 {
278 wxXmlNode *n = GetChildren();
279
280 while (n)
281 {
282 if (n->GetType() == wxXML_TEXT_NODE ||
283 n->GetType() == wxXML_CDATA_SECTION_NODE)
284 return n->GetContent();
285 n = n->GetNext();
286 }
287 return wxEmptyString;
288 }
289
290
291
292 //-----------------------------------------------------------------------------
293 // wxXmlDocument
294 //-----------------------------------------------------------------------------
295
296 wxXmlDocument::wxXmlDocument()
297 : m_version(wxT("1.0")), m_fileEncoding(wxT("utf-8")), m_root(NULL)
298 {
299 #if !wxUSE_UNICODE
300 m_encoding = wxT("UTF-8");
301 #endif
302 }
303
304 wxXmlDocument::wxXmlDocument(const wxString& filename, const wxString& encoding)
305 :wxObject(), m_root(NULL)
306 {
307 if ( !Load(filename, encoding) )
308 {
309 wxDELETE(m_root);
310 }
311 }
312
313 wxXmlDocument::wxXmlDocument(wxInputStream& stream, const wxString& encoding)
314 :wxObject(), m_root(NULL)
315 {
316 if ( !Load(stream, encoding) )
317 {
318 wxDELETE(m_root);
319 }
320 }
321
322 wxXmlDocument::wxXmlDocument(const wxXmlDocument& doc)
323 :wxObject()
324 {
325 DoCopy(doc);
326 }
327
328 wxXmlDocument& wxXmlDocument::operator=(const wxXmlDocument& doc)
329 {
330 wxDELETE(m_root);
331 DoCopy(doc);
332 return *this;
333 }
334
335 void wxXmlDocument::DoCopy(const wxXmlDocument& doc)
336 {
337 m_version = doc.m_version;
338 #if !wxUSE_UNICODE
339 m_encoding = doc.m_encoding;
340 #endif
341 m_fileEncoding = doc.m_fileEncoding;
342 m_root = new wxXmlNode(*doc.m_root);
343 }
344
345 bool wxXmlDocument::Load(const wxString& filename, const wxString& encoding)
346 {
347 wxFileInputStream stream(filename);
348 return Load(stream, encoding);
349 }
350
351 bool wxXmlDocument::Save(const wxString& filename) const
352 {
353 wxFileOutputStream stream(filename);
354 return Save(stream);
355 }
356
357
358
359 //-----------------------------------------------------------------------------
360 // wxXmlDocument loading routines
361 //-----------------------------------------------------------------------------
362
363 /*
364 FIXME:
365 - process all elements, including CDATA
366 */
367
368 // converts Expat-produced string in UTF-8 into wxString.
369 inline static wxString CharToString(wxMBConv *conv,
370 const char *s, size_t len = wxSTRING_MAXLEN)
371 {
372 #if wxUSE_UNICODE
373 (void)conv;
374 return wxString(s, wxConvUTF8, len);
375 #else
376 if ( conv )
377 {
378 size_t nLen = (len != wxSTRING_MAXLEN) ? len :
379 wxConvUTF8.MB2WC((wchar_t*) NULL, s, 0);
380
381 wchar_t *buf = new wchar_t[nLen+1];
382 wxConvUTF8.MB2WC(buf, s, nLen);
383 buf[nLen] = 0;
384 wxString str(buf, *conv, len);
385 delete[] buf;
386 return str;
387 }
388 else
389 return wxString(s, len != wxSTRING_MAXLEN ? len : strlen(s));
390 #endif
391 }
392
393 struct wxXmlParsingContext
394 {
395 wxMBConv *conv;
396 wxXmlNode *root;
397 wxXmlNode *node;
398 wxXmlNode *lastAsText;
399 wxString encoding;
400 wxString version;
401 };
402
403 extern "C" {
404 static void StartElementHnd(void *userData, const char *name, const char **atts)
405 {
406 wxXmlParsingContext *ctx = (wxXmlParsingContext*)userData;
407 wxXmlNode *node = new wxXmlNode(wxXML_ELEMENT_NODE, CharToString(ctx->conv, name));
408 const char **a = atts;
409 while (*a)
410 {
411 node->AddProperty(CharToString(ctx->conv, a[0]), CharToString(ctx->conv, a[1]));
412 a += 2;
413 }
414 if (ctx->root == NULL)
415 ctx->root = node;
416 else
417 ctx->node->AddChild(node);
418 ctx->node = node;
419 ctx->lastAsText = NULL;
420 }
421 }
422
423 extern "C" {
424 static void EndElementHnd(void *userData, const char* WXUNUSED(name))
425 {
426 wxXmlParsingContext *ctx = (wxXmlParsingContext*)userData;
427
428 ctx->node = ctx->node->GetParent();
429 ctx->lastAsText = NULL;
430 }
431 }
432
433 extern "C" {
434 static void TextHnd(void *userData, const char *s, int len)
435 {
436 wxXmlParsingContext *ctx = (wxXmlParsingContext*)userData;
437 char *buf = new char[len + 1];
438
439 buf[len] = '\0';
440 memcpy(buf, s, (size_t)len);
441
442 if (ctx->lastAsText)
443 {
444 ctx->lastAsText->SetContent(ctx->lastAsText->GetContent() +
445 CharToString(ctx->conv, buf));
446 }
447 else
448 {
449 bool whiteOnly = true;
450 for (char *c = buf; *c != '\0'; c++)
451 if (*c != ' ' && *c != '\t' && *c != '\n' && *c != '\r')
452 {
453 whiteOnly = false;
454 break;
455 }
456 if (!whiteOnly)
457 {
458 ctx->lastAsText = new wxXmlNode(wxXML_TEXT_NODE, wxT("text"),
459 CharToString(ctx->conv, buf));
460 ctx->node->AddChild(ctx->lastAsText);
461 }
462 }
463
464 delete[] buf;
465 }
466 }
467
468 extern "C" {
469 static void CommentHnd(void *userData, const char *data)
470 {
471 wxXmlParsingContext *ctx = (wxXmlParsingContext*)userData;
472
473 if (ctx->node)
474 {
475 // VS: ctx->node == NULL happens if there is a comment before
476 // the root element (e.g. wxDesigner's output). We ignore such
477 // comments, no big deal...
478 ctx->node->AddChild(new wxXmlNode(wxXML_COMMENT_NODE,
479 wxT("comment"), CharToString(ctx->conv, data)));
480 }
481 ctx->lastAsText = NULL;
482 }
483 }
484
485 extern "C" {
486 static void DefaultHnd(void *userData, const char *s, int len)
487 {
488 // XML header:
489 if (len > 6 && memcmp(s, "<?xml ", 6) == 0)
490 {
491 wxXmlParsingContext *ctx = (wxXmlParsingContext*)userData;
492
493 wxString buf = CharToString(ctx->conv, s, (size_t)len);
494 int pos;
495 pos = buf.Find(wxT("encoding="));
496 if (pos != wxNOT_FOUND)
497 ctx->encoding = buf.Mid(pos + 10).BeforeFirst(buf[(size_t)pos+9]);
498 pos = buf.Find(wxT("version="));
499 if (pos != wxNOT_FOUND)
500 ctx->version = buf.Mid(pos + 9).BeforeFirst(buf[(size_t)pos+8]);
501 }
502 }
503 }
504
505 extern "C" {
506 static int UnknownEncodingHnd(void * WXUNUSED(encodingHandlerData),
507 const XML_Char *name, XML_Encoding *info)
508 {
509 // We must build conversion table for expat. The easiest way to do so
510 // is to let wxCSConv convert as string containing all characters to
511 // wide character representation:
512 wxString str(name, wxConvLibc);
513 wxCSConv conv(str);
514 char mbBuf[2];
515 wchar_t wcBuf[10];
516 size_t i;
517
518 mbBuf[1] = 0;
519 info->map[0] = 0;
520 for (i = 0; i < 255; i++)
521 {
522 mbBuf[0] = (char)(i+1);
523 if (conv.MB2WC(wcBuf, mbBuf, 2) == (size_t)-1)
524 {
525 // invalid/undefined byte in the encoding:
526 info->map[i+1] = -1;
527 }
528 info->map[i+1] = (int)wcBuf[0];
529 }
530
531 info->data = NULL;
532 info->convert = NULL;
533 info->release = NULL;
534
535 return 1;
536 }
537 }
538
539 bool wxXmlDocument::Load(wxInputStream& stream, const wxString& encoding)
540 {
541 #if wxUSE_UNICODE
542 (void)encoding;
543 #else
544 m_encoding = encoding;
545 #endif
546
547 const size_t BUFSIZE = 1024;
548 char buf[BUFSIZE];
549 wxXmlParsingContext ctx;
550 bool done;
551 XML_Parser parser = XML_ParserCreate(NULL);
552
553 ctx.root = ctx.node = NULL;
554 ctx.encoding = wxT("UTF-8"); // default in absence of encoding=""
555 ctx.conv = NULL;
556 #if !wxUSE_UNICODE
557 if ( encoding != wxT("UTF-8") && encoding != wxT("utf-8") )
558 ctx.conv = new wxCSConv(encoding);
559 #endif
560
561 XML_SetUserData(parser, (void*)&ctx);
562 XML_SetElementHandler(parser, StartElementHnd, EndElementHnd);
563 XML_SetCharacterDataHandler(parser, TextHnd);
564 XML_SetCommentHandler(parser, CommentHnd);
565 XML_SetDefaultHandler(parser, DefaultHnd);
566 XML_SetUnknownEncodingHandler(parser, UnknownEncodingHnd, NULL);
567
568 bool ok = true;
569 do
570 {
571 size_t len = stream.Read(buf, BUFSIZE).LastRead();
572 done = (len < BUFSIZE);
573 if (!XML_Parse(parser, buf, len, done))
574 {
575 wxString error(XML_ErrorString(XML_GetErrorCode(parser)),
576 *wxConvCurrent);
577 wxLogError(_("XML parsing error: '%s' at line %d"),
578 error.c_str(),
579 XML_GetCurrentLineNumber(parser));
580 ok = false;
581 break;
582 }
583 } while (!done);
584
585 if (ok)
586 {
587 if (!ctx.version.empty())
588 SetVersion(ctx.version);
589 if (!ctx.encoding.empty())
590 SetFileEncoding(ctx.encoding);
591 SetRoot(ctx.root);
592 }
593 else
594 {
595 delete ctx.root;
596 }
597
598 XML_ParserFree(parser);
599 #if !wxUSE_UNICODE
600 if ( ctx.conv )
601 delete ctx.conv;
602 #endif
603
604 return ok;
605
606 }
607
608
609
610 //-----------------------------------------------------------------------------
611 // wxXmlDocument saving routines
612 //-----------------------------------------------------------------------------
613
614 // write string to output:
615 inline static void OutputString(wxOutputStream& stream, const wxString& str,
616 wxMBConv *convMem = NULL,
617 wxMBConv *convFile = NULL)
618 {
619 if (str.empty())
620 return;
621
622 #if wxUSE_UNICODE
623 wxUnusedVar(convMem);
624
625 const wxWX2MBbuf buf(str.mb_str(*(convFile ? convFile : &wxConvUTF8)));
626 stream.Write((const char*)buf, strlen((const char*)buf));
627 #else // !wxUSE_UNICODE
628 if ( convFile && convMem )
629 {
630 wxString str2(str.wc_str(*convMem), *convFile);
631 stream.Write(str2.mb_str(), str2.Len());
632 }
633 else // no conversions to do
634 {
635 stream.Write(str.mb_str(), str.Len());
636 }
637 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
638 }
639
640 // flags for OutputStringEnt()
641 enum
642 {
643 XML_ESCAPE_QUOTES = 1
644 };
645
646 // Same as above, but create entities first.
647 // Translates '<' to "&lt;", '>' to "&gt;" and '&' to "&amp;"
648 static void OutputStringEnt(wxOutputStream& stream, const wxString& str,
649 wxMBConv *convMem = NULL,
650 wxMBConv *convFile = NULL,
651 int flags = 0)
652 {
653 wxString buf;
654 size_t i, last, len;
655 wxChar c;
656
657 len = str.Len();
658 last = 0;
659 for (i = 0; i < len; i++)
660 {
661 c = str.GetChar(i);
662 if (c == wxT('<') || c == wxT('>') ||
663 (c == wxT('&') && str.Mid(i+1, 4) != wxT("amp;")) ||
664 ((flags & XML_ESCAPE_QUOTES) && c == wxT('"')))
665 {
666 OutputString(stream, str.Mid(last, i - last), convMem, convFile);
667 switch (c)
668 {
669 case wxT('<'):
670 OutputString(stream, wxT("&lt;"));
671 break;
672 case wxT('>'):
673 OutputString(stream, wxT("&gt;"));
674 break;
675 case wxT('&'):
676 OutputString(stream, wxT("&amp;"));
677 break;
678 case wxT('"'):
679 OutputString(stream, wxT("&quot;"));
680 break;
681 default:
682 break;
683 }
684 last = i + 1;
685 }
686 }
687 OutputString(stream, str.Mid(last, i - last), convMem, convFile);
688 }
689
690 inline static void OutputIndentation(wxOutputStream& stream, int indent)
691 {
692 wxString str = wxT("\n");
693 for (int i = 0; i < indent; i++)
694 str << wxT(' ') << wxT(' ');
695 OutputString(stream, str);
696 }
697
698 static void OutputNode(wxOutputStream& stream, wxXmlNode *node, int indent,
699 wxMBConv *convMem, wxMBConv *convFile)
700 {
701 wxXmlNode *n, *prev;
702 wxXmlProperty *prop;
703
704 switch (node->GetType())
705 {
706 case wxXML_TEXT_NODE:
707 OutputStringEnt(stream, node->GetContent(), convMem, convFile);
708 break;
709
710 case wxXML_ELEMENT_NODE:
711 OutputString(stream, wxT("<"));
712 OutputString(stream, node->GetName());
713
714 prop = node->GetProperties();
715 while (prop)
716 {
717 OutputString(stream, wxT(" ") + prop->GetName() + wxT("=\""));
718 OutputStringEnt(stream, prop->GetValue(), convMem, convFile,
719 XML_ESCAPE_QUOTES);
720 OutputString(stream, wxT("\""));
721 prop = prop->GetNext();
722 }
723
724 if (node->GetChildren())
725 {
726 OutputString(stream, wxT(">"));
727 prev = NULL;
728 n = node->GetChildren();
729 while (n)
730 {
731 if (n && n->GetType() != wxXML_TEXT_NODE)
732 OutputIndentation(stream, indent + 1);
733 OutputNode(stream, n, indent + 1, convMem, convFile);
734 prev = n;
735 n = n->GetNext();
736 }
737 if (prev && prev->GetType() != wxXML_TEXT_NODE)
738 OutputIndentation(stream, indent);
739 OutputString(stream, wxT("</"));
740 OutputString(stream, node->GetName());
741 OutputString(stream, wxT(">"));
742 }
743 else
744 OutputString(stream, wxT("/>"));
745 break;
746
747 case wxXML_COMMENT_NODE:
748 OutputString(stream, wxT("<!--"));
749 OutputString(stream, node->GetContent(), convMem, convFile);
750 OutputString(stream, wxT("-->"));
751 break;
752
753 default:
754 wxFAIL_MSG(wxT("unsupported node type"));
755 }
756 }
757
758 bool wxXmlDocument::Save(wxOutputStream& stream) const
759 {
760 if ( !IsOk() )
761 return false;
762
763 wxString s;
764
765 wxMBConv *convMem = NULL;
766
767 #if wxUSE_UNICODE
768 wxMBConv *convFile = new wxCSConv(GetFileEncoding());
769 #else
770 wxMBConv *convFile = NULL;
771 if ( GetFileEncoding() != GetEncoding() )
772 {
773 convFile = new wxCSConv(GetFileEncoding());
774 convMem = new wxCSConv(GetEncoding());
775 }
776 #endif
777
778 s.Printf(wxT("<?xml version=\"%s\" encoding=\"%s\"?>\n"),
779 GetVersion().c_str(), GetFileEncoding().c_str());
780 OutputString(stream, s);
781
782 OutputNode(stream, GetRoot(), 0, convMem, convFile);
783 OutputString(stream, wxT("\n"));
784
785 if ( convFile )
786 delete convFile;
787 if ( convMem )
788 delete convMem;
789
790 return true;
791 }
792
793 #endif // wxUSE_XML