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