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