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