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