]> git.saurik.com Git - wxWidgets.git/blame - src/xml/xml.cpp
added missing headers (patch 1774617)
[wxWidgets.git] / src / xml / xml.cpp
CommitLineData
27b0c286 1/////////////////////////////////////////////////////////////////////////////
40989e46 2// Name: src/xml/xml.cpp
27b0c286
VS
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
65571936 8// Licence: wxWindows licence
27b0c286
VS
9/////////////////////////////////////////////////////////////////////////////
10
27b0c286
VS
11// For compilers that support precompilation, includes "wx.h".
12#include "wx/wxprec.h"
13
14#ifdef __BORLANDC__
15 #pragma hdrstop
16#endif
17
27b0c286
VS
18#if wxUSE_XML
19
40989e46
WS
20#include "wx/xml/xml.h"
21
88a7a4e1
WS
22#ifndef WX_PRECOMP
23 #include "wx/intl.h"
e4db172a 24 #include "wx/log.h"
670f9935 25 #include "wx/app.h"
88a7a4e1
WS
26#endif
27
27b0c286
VS
28#include "wx/wfstream.h"
29#include "wx/datstrm.h"
30#include "wx/zstream.h"
27b0c286
VS
31#include "wx/strconv.h"
32
33#include "expat.h" // from Expat
34
34fdf762 35// DLL options compatibility check:
34fdf762
VS
36WX_CHECK_BUILD_OPTIONS("wxXML")
37
4c43dd90
JS
38
39IMPLEMENT_CLASS(wxXmlDocument, wxObject)
40
41
538f3830 42// a private utility used by wxXML
352d9b89 43static bool wxIsWhiteOnly(const wxString& buf);
538f3830 44
4c43dd90 45
27b0c286
VS
46//-----------------------------------------------------------------------------
47// wxXmlNode
48//-----------------------------------------------------------------------------
49
50wxXmlNode::wxXmlNode(wxXmlNode *parent,wxXmlNodeType type,
51 const wxString& name, const wxString& content,
288b6107 52 wxXmlAttribute *attrs, wxXmlNode *next)
27b0c286 53 : m_type(type), m_name(name), m_content(content),
288b6107 54 m_attrs(attrs), m_parent(parent),
27b0c286
VS
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
69wxXmlNode::wxXmlNode(wxXmlNodeType type, const wxString& name,
70 const wxString& content)
71 : m_type(type), m_name(name), m_content(content),
288b6107 72 m_attrs(NULL), m_parent(NULL),
27b0c286
VS
73 m_children(NULL), m_next(NULL)
74{}
75
76wxXmlNode::wxXmlNode(const wxXmlNode& node)
77{
78 m_next = NULL;
79 m_parent = NULL;
80 DoCopy(node);
81}
82
83wxXmlNode::~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
288b6107
VS
92 wxXmlAttribute *p, *p2;
93 for (p = m_attrs; p; p = p2)
27b0c286
VS
94 {
95 p2 = p->GetNext();
96 delete p;
97 }
98}
99
100wxXmlNode& wxXmlNode::operator=(const wxXmlNode& node)
101{
288b6107 102 wxDELETE(m_attrs);
27b0c286
VS
103 wxDELETE(m_children);
104 DoCopy(node);
105 return *this;
106}
107
108void 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
288b6107
VS
122 m_attrs = NULL;
123 wxXmlAttribute *p = node.m_attrs;
27b0c286
VS
124 while (p)
125 {
288b6107 126 AddAttribute(p->GetName(), p->GetValue());
27b0c286
VS
127 p = p->GetNext();
128 }
129}
130
288b6107 131bool wxXmlNode::HasAttribute(const wxString& attrName) const
27b0c286 132{
288b6107 133 wxXmlAttribute *attr = GetAttributes();
27b0c286 134
288b6107 135 while (attr)
27b0c286 136 {
288b6107
VS
137 if (attr->GetName() == attrName) return true;
138 attr = attr->GetNext();
27b0c286
VS
139 }
140
759f7272 141 return false;
27b0c286
VS
142}
143
288b6107 144bool wxXmlNode::GetAttribute(const wxString& attrName, wxString *value) const
27b0c286 145{
288b6107 146 wxXmlAttribute *attr = GetAttributes();
27b0c286 147
288b6107 148 while (attr)
27b0c286 149 {
288b6107 150 if (attr->GetName() == attrName)
27b0c286 151 {
288b6107 152 *value = attr->GetValue();
759f7272 153 return true;
27b0c286 154 }
288b6107 155 attr = attr->GetNext();
27b0c286
VS
156 }
157
759f7272 158 return false;
27b0c286
VS
159}
160
288b6107 161wxString wxXmlNode::GetAttribute(const wxString& attrName, const wxString& defaultVal) const
27b0c286
VS
162{
163 wxString tmp;
288b6107 164 if (GetAttribute(attrName, &tmp))
27b0c286 165 return tmp;
0e2710a6
DS
166
167 return defaultVal;
27b0c286
VS
168}
169
170void 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
fa6a8373 184bool wxXmlNode::InsertChild(wxXmlNode *child, wxXmlNode *before_node)
27b0c286 185{
fa6a8373
RR
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!"));
27b0c286
VS
189
190 if (m_children == before_node)
191 m_children = child;
fa6a8373
RR
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 }
27b0c286
VS
206 else
207 {
208 wxXmlNode *ch = m_children;
fa6a8373
RR
209 while (ch && ch->m_next != before_node) ch = ch->m_next;
210 if (!ch)
211 return false; // before_node not found
27b0c286
VS
212 ch->m_next = child;
213 }
214
215 child->m_parent = this;
216 child->m_next = before_node;
fa6a8373 217 return true;
27b0c286
VS
218}
219
220bool wxXmlNode::RemoveChild(wxXmlNode *child)
221{
222 if (m_children == NULL)
759f7272 223 return false;
27b0c286
VS
224 else if (m_children == child)
225 {
226 m_children = child->m_next;
227 child->m_parent = NULL;
228 child->m_next = NULL;
759f7272 229 return true;
27b0c286
VS
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;
759f7272 241 return true;
27b0c286
VS
242 }
243 ch = ch->m_next;
244 }
759f7272 245 return false;
27b0c286
VS
246 }
247}
248
249void wxXmlNode::AddProperty(const wxString& name, const wxString& value)
250{
288b6107 251 AddProperty(new wxXmlAttribute(name, value, NULL));
27b0c286
VS
252}
253
288b6107 254void wxXmlNode::AddProperty(wxXmlAttribute *attr)
27b0c286 255{
288b6107
VS
256 if (m_attrs == NULL)
257 m_attrs = attr;
27b0c286
VS
258 else
259 {
288b6107 260 wxXmlAttribute *p = m_attrs;
27b0c286 261 while (p->GetNext()) p = p->GetNext();
288b6107 262 p->SetNext(attr);
27b0c286
VS
263 }
264}
265
266bool wxXmlNode::DeleteProperty(const wxString& name)
267{
288b6107 268 wxXmlAttribute *attr;
27b0c286 269
288b6107 270 if (m_attrs == NULL)
759f7272 271 return false;
27b0c286 272
288b6107 273 else if (m_attrs->GetName() == name)
27b0c286 274 {
288b6107
VS
275 attr = m_attrs;
276 m_attrs = attr->GetNext();
277 attr->SetNext(NULL);
278 delete attr;
759f7272 279 return true;
27b0c286
VS
280 }
281
282 else
283 {
288b6107 284 wxXmlAttribute *p = m_attrs;
27b0c286
VS
285 while (p->GetNext())
286 {
287 if (p->GetNext()->GetName() == name)
288 {
288b6107
VS
289 attr = p->GetNext();
290 p->SetNext(attr->GetNext());
291 attr->SetNext(NULL);
292 delete attr;
759f7272 293 return true;
27b0c286
VS
294 }
295 p = p->GetNext();
296 }
759f7272 297 return false;
27b0c286
VS
298 }
299}
300
4c43dd90
JS
301wxString 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
538f3830
VS
315int 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
332bool wxXmlNode::IsWhitespaceOnly() const
333{
334 return wxIsWhiteOnly(m_content);
335}
336
27b0c286
VS
337
338
339//-----------------------------------------------------------------------------
340// wxXmlDocument
341//-----------------------------------------------------------------------------
342
343wxXmlDocument::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
351wxXmlDocument::wxXmlDocument(const wxString& filename, const wxString& encoding)
d0468e8c 352 :wxObject(), m_root(NULL)
27b0c286
VS
353{
354 if ( !Load(filename, encoding) )
355 {
356 wxDELETE(m_root);
357 }
358}
359
360wxXmlDocument::wxXmlDocument(wxInputStream& stream, const wxString& encoding)
d0468e8c 361 :wxObject(), m_root(NULL)
27b0c286
VS
362{
363 if ( !Load(stream, encoding) )
364 {
365 wxDELETE(m_root);
366 }
367}
368
369wxXmlDocument::wxXmlDocument(const wxXmlDocument& doc)
d0468e8c 370 :wxObject()
27b0c286
VS
371{
372 DoCopy(doc);
373}
374
375wxXmlDocument& wxXmlDocument::operator=(const wxXmlDocument& doc)
376{
377 wxDELETE(m_root);
378 DoCopy(doc);
379 return *this;
380}
381
382void 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;
e8da6b7c
RR
389
390 if (doc.m_root)
391 m_root = new wxXmlNode(*doc.m_root);
392 else
393 m_root = NULL;
27b0c286
VS
394}
395
538f3830 396bool wxXmlDocument::Load(const wxString& filename, const wxString& encoding, int flags)
27b0c286
VS
397{
398 wxFileInputStream stream(filename);
97757cee
VZ
399 if (!stream.Ok())
400 return false;
538f3830 401 return Load(stream, encoding, flags);
27b0c286
VS
402}
403
538f3830 404bool wxXmlDocument::Save(const wxString& filename, int indentstep) const
27b0c286
VS
405{
406 wxFileOutputStream stream(filename);
97757cee
VZ
407 if (!stream.Ok())
408 return false;
538f3830 409 return Save(stream, indentstep);
27b0c286
VS
410}
411
412
413
414//-----------------------------------------------------------------------------
415// wxXmlDocument loading routines
416//-----------------------------------------------------------------------------
417
ddae497f
VZ
418// converts Expat-produced string in UTF-8 into wxString using the specified
419// conv or keep in UTF-8 if conv is NULL
420static wxString CharToString(wxMBConv *conv,
b04edcaf 421 const char *s, size_t len = wxString::npos)
27b0c286 422{
b04edcaf 423#if !wxUSE_UNICODE
27b0c286
VS
424 if ( conv )
425 {
ddae497f
VZ
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(
6df6e35a 429 wxConvUTF8.cMB2WC(s, len == wxString::npos ? wxNO_LEN : len, NULL));
ddae497f 430
da452b9e 431 return wxString(wbuf, *conv);
27b0c286 432 }
b04edcaf
VS
433 // else: the string is wanted in UTF-8
434#endif // !wxUSE_UNICODE
435
436 wxUnusedVar(conv);
437 return wxString::FromUTF8(s, len);
27b0c286
VS
438}
439
538f3830 440// returns true if the given string contains only whitespaces
352d9b89 441bool wxIsWhiteOnly(const wxString& buf)
538f3830 442{
352d9b89
VS
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'))
538f3830 447 return false;
352d9b89 448 }
538f3830
VS
449 return true;
450}
451
452
27b0c286
VS
453struct wxXmlParsingContext
454{
455 wxMBConv *conv;
456 wxXmlNode *root;
457 wxXmlNode *node;
458 wxXmlNode *lastAsText;
459 wxString encoding;
460 wxString version;
538f3830 461 bool removeWhiteOnlyNodes;
27b0c286
VS
462};
463
865bb325 464extern "C" {
27b0c286
VS
465static 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 {
288b6107 472 node->AddAttribute(CharToString(ctx->conv, a[0]), CharToString(ctx->conv, a[1]));
27b0c286
VS
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
483static 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
491static void TextHnd(void *userData, const char *s, int len)
492{
493 wxXmlParsingContext *ctx = (wxXmlParsingContext*)userData;
538f3830 494 wxString str = CharToString(ctx->conv, s, len);
27b0c286
VS
495
496 if (ctx->lastAsText)
497 {
d6cb1287 498 ctx->lastAsText->SetContent(ctx->lastAsText->GetContent() + str);
27b0c286
VS
499 }
500 else
501 {
538f3830
VS
502 bool whiteOnly = false;
503 if (ctx->removeWhiteOnlyNodes)
504 whiteOnly = wxIsWhiteOnly(str);
505
27b0c286
VS
506 if (!whiteOnly)
507 {
538f3830 508 ctx->lastAsText = new wxXmlNode(wxXML_TEXT_NODE, wxT("text"), str);
27b0c286
VS
509 ctx->node->AddChild(ctx->lastAsText);
510 }
511 }
27b0c286
VS
512}
513
a01cbf0a
RR
514static void StartCdataHnd(void *userData)
515{
516 wxXmlParsingContext *ctx = (wxXmlParsingContext*)userData;
517
eac789ff
VS
518 ctx->lastAsText = new wxXmlNode(wxXML_CDATA_SECTION_NODE, wxT("cdata"),wxT(""));
519 ctx->node->AddChild(ctx->lastAsText);
a01cbf0a 520}
a01cbf0a 521
27b0c286
VS
522static 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
537static 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
555static 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:
86501081 561 wxCSConv conv(name);
27b0c286
VS
562 char mbBuf[2];
563 wchar_t wcBuf[10];
564 size_t i;
565
566 mbBuf[1] = 0;
567 info->map[0] = 0;
568 for (i = 0; i < 255; i++)
569 {
570 mbBuf[0] = (char)(i+1);
571 if (conv.MB2WC(wcBuf, mbBuf, 2) == (size_t)-1)
572 {
573 // invalid/undefined byte in the encoding:
574 info->map[i+1] = -1;
575 }
576 info->map[i+1] = (int)wcBuf[0];
577 }
42841dfc 578
27b0c286
VS
579 info->data = NULL;
580 info->convert = NULL;
581 info->release = NULL;
582
583 return 1;
584}
26296ac9
VS
585
586} // extern "C"
27b0c286 587
538f3830 588bool wxXmlDocument::Load(wxInputStream& stream, const wxString& encoding, int flags)
27b0c286
VS
589{
590#if wxUSE_UNICODE
591 (void)encoding;
592#else
593 m_encoding = encoding;
594#endif
595
596 const size_t BUFSIZE = 1024;
597 char buf[BUFSIZE];
598 wxXmlParsingContext ctx;
599 bool done;
600 XML_Parser parser = XML_ParserCreate(NULL);
601
602 ctx.root = ctx.node = NULL;
603 ctx.encoding = wxT("UTF-8"); // default in absence of encoding=""
604 ctx.conv = NULL;
605#if !wxUSE_UNICODE
8605f9c5 606 if ( encoding.CmpNoCase(wxT("UTF-8")) != 0 )
27b0c286
VS
607 ctx.conv = new wxCSConv(encoding);
608#endif
538f3830 609 ctx.removeWhiteOnlyNodes = (flags & wxXMLDOC_KEEP_WHITESPACE_NODES) == 0;
27b0c286
VS
610
611 XML_SetUserData(parser, (void*)&ctx);
612 XML_SetElementHandler(parser, StartElementHnd, EndElementHnd);
613 XML_SetCharacterDataHandler(parser, TextHnd);
d6cb1287 614 XML_SetStartCdataSectionHandler(parser, StartCdataHnd);
27b0c286
VS
615 XML_SetCommentHandler(parser, CommentHnd);
616 XML_SetDefaultHandler(parser, DefaultHnd);
617 XML_SetUnknownEncodingHandler(parser, UnknownEncodingHnd, NULL);
618
619 bool ok = true;
620 do
621 {
622 size_t len = stream.Read(buf, BUFSIZE).LastRead();
623 done = (len < BUFSIZE);
624 if (!XML_Parse(parser, buf, len, done))
625 {
6a8fb6bd
VS
626 wxString error(XML_ErrorString(XML_GetErrorCode(parser)),
627 *wxConvCurrent);
27b0c286 628 wxLogError(_("XML parsing error: '%s' at line %d"),
6a8fb6bd 629 error.c_str(),
27b0c286
VS
630 XML_GetCurrentLineNumber(parser));
631 ok = false;
632 break;
633 }
634 } while (!done);
635
636 if (ok)
637 {
759f7272 638 if (!ctx.version.empty())
6a8fb6bd 639 SetVersion(ctx.version);
759f7272 640 if (!ctx.encoding.empty())
6a8fb6bd 641 SetFileEncoding(ctx.encoding);
27b0c286
VS
642 SetRoot(ctx.root);
643 }
6a8fb6bd
VS
644 else
645 {
646 delete ctx.root;
647 }
27b0c286
VS
648
649 XML_ParserFree(parser);
650#if !wxUSE_UNICODE
651 if ( ctx.conv )
652 delete ctx.conv;
653#endif
654
655 return ok;
656
657}
658
659
660
661//-----------------------------------------------------------------------------
662// wxXmlDocument saving routines
663//-----------------------------------------------------------------------------
664
665// write string to output:
666inline static void OutputString(wxOutputStream& stream, const wxString& str,
8dfd4fad
VZ
667 wxMBConv *convMem = NULL,
668 wxMBConv *convFile = NULL)
27b0c286 669{
8dfd4fad
VZ
670 if (str.empty())
671 return;
672
27b0c286 673#if wxUSE_UNICODE
8dfd4fad
VZ
674 wxUnusedVar(convMem);
675
6a8fb6bd 676 const wxWX2MBbuf buf(str.mb_str(*(convFile ? convFile : &wxConvUTF8)));
27b0c286 677 stream.Write((const char*)buf, strlen((const char*)buf));
8dfd4fad
VZ
678#else // !wxUSE_UNICODE
679 if ( convFile && convMem )
27b0c286
VS
680 {
681 wxString str2(str.wc_str(*convMem), *convFile);
682 stream.Write(str2.mb_str(), str2.Len());
683 }
8dfd4fad
VZ
684 else // no conversions to do
685 {
686 stream.Write(str.mb_str(), str.Len());
687 }
688#endif // wxUSE_UNICODE/!wxUSE_UNICODE
27b0c286
VS
689}
690
8dfd4fad
VZ
691// flags for OutputStringEnt()
692enum
693{
694 XML_ESCAPE_QUOTES = 1
695};
696
27b0c286
VS
697// Same as above, but create entities first.
698// Translates '<' to "&lt;", '>' to "&gt;" and '&' to "&amp;"
699static void OutputStringEnt(wxOutputStream& stream, const wxString& str,
8dfd4fad
VZ
700 wxMBConv *convMem = NULL,
701 wxMBConv *convFile = NULL,
702 int flags = 0)
27b0c286
VS
703{
704 wxString buf;
705 size_t i, last, len;
706 wxChar c;
707
708 len = str.Len();
709 last = 0;
710 for (i = 0; i < len; i++)
711 {
712 c = str.GetChar(i);
713 if (c == wxT('<') || c == wxT('>') ||
ebf0700d 714 (c == wxT('&') && str.Mid(i+1, 4) != wxT("amp;")) ||
8dfd4fad 715 ((flags & XML_ESCAPE_QUOTES) && c == wxT('"')))
27b0c286
VS
716 {
717 OutputString(stream, str.Mid(last, i - last), convMem, convFile);
718 switch (c)
719 {
720 case wxT('<'):
8dfd4fad 721 OutputString(stream, wxT("&lt;"));
27b0c286
VS
722 break;
723 case wxT('>'):
8dfd4fad 724 OutputString(stream, wxT("&gt;"));
27b0c286
VS
725 break;
726 case wxT('&'):
8dfd4fad 727 OutputString(stream, wxT("&amp;"));
27b0c286 728 break;
ebf0700d 729 case wxT('"'):
8dfd4fad
VZ
730 OutputString(stream, wxT("&quot;"));
731 break;
732 default:
ebf0700d 733 break;
27b0c286
VS
734 }
735 last = i + 1;
736 }
737 }
738 OutputString(stream, str.Mid(last, i - last), convMem, convFile);
739}
740
741inline static void OutputIndentation(wxOutputStream& stream, int indent)
742{
743 wxString str = wxT("\n");
744 for (int i = 0; i < indent; i++)
745 str << wxT(' ') << wxT(' ');
8dfd4fad 746 OutputString(stream, str);
27b0c286
VS
747}
748
749static void OutputNode(wxOutputStream& stream, wxXmlNode *node, int indent,
538f3830 750 wxMBConv *convMem, wxMBConv *convFile, int indentstep)
27b0c286
VS
751{
752 wxXmlNode *n, *prev;
288b6107 753 wxXmlAttribute *attr;
27b0c286
VS
754
755 switch (node->GetType())
756 {
eac789ff
VS
757 case wxXML_CDATA_SECTION_NODE:
758 OutputString( stream, wxT("<![CDATA["));
759 OutputString( stream, node->GetContent() );
760 OutputString( stream, wxT("]]>") );
761 break;
a01cbf0a 762
27b0c286
VS
763 case wxXML_TEXT_NODE:
764 OutputStringEnt(stream, node->GetContent(), convMem, convFile);
765 break;
766
767 case wxXML_ELEMENT_NODE:
8dfd4fad
VZ
768 OutputString(stream, wxT("<"));
769 OutputString(stream, node->GetName());
27b0c286 770
288b6107
VS
771 attr = node->GetAttributes();
772 while (attr)
27b0c286 773 {
288b6107
VS
774 OutputString(stream, wxT(" ") + attr->GetName() + wxT("=\""));
775 OutputStringEnt(stream, attr->GetValue(), convMem, convFile,
8dfd4fad
VZ
776 XML_ESCAPE_QUOTES);
777 OutputString(stream, wxT("\""));
288b6107 778 attr = attr->GetNext();
27b0c286
VS
779 }
780
781 if (node->GetChildren())
782 {
8dfd4fad 783 OutputString(stream, wxT(">"));
27b0c286
VS
784 prev = NULL;
785 n = node->GetChildren();
786 while (n)
787 {
538f3830
VS
788 if (indentstep >= 0 && n && n->GetType() != wxXML_TEXT_NODE)
789 OutputIndentation(stream, indent + indentstep);
790 OutputNode(stream, n, indent + indentstep, convMem, convFile, indentstep);
27b0c286
VS
791 prev = n;
792 n = n->GetNext();
793 }
538f3830 794 if (indentstep >= 0 && prev && prev->GetType() != wxXML_TEXT_NODE)
27b0c286 795 OutputIndentation(stream, indent);
8dfd4fad
VZ
796 OutputString(stream, wxT("</"));
797 OutputString(stream, node->GetName());
798 OutputString(stream, wxT(">"));
27b0c286
VS
799 }
800 else
8dfd4fad 801 OutputString(stream, wxT("/>"));
27b0c286
VS
802 break;
803
804 case wxXML_COMMENT_NODE:
8dfd4fad 805 OutputString(stream, wxT("<!--"));
27b0c286 806 OutputString(stream, node->GetContent(), convMem, convFile);
8dfd4fad 807 OutputString(stream, wxT("-->"));
27b0c286
VS
808 break;
809
810 default:
811 wxFAIL_MSG(wxT("unsupported node type"));
812 }
813}
814
538f3830 815bool wxXmlDocument::Save(wxOutputStream& stream, int indentstep) const
27b0c286
VS
816{
817 if ( !IsOk() )
759f7272 818 return false;
27b0c286
VS
819
820 wxString s;
821
8605f9c5
VZ
822 wxMBConv *convMem = NULL,
823 *convFile;
759f7272 824
27b0c286 825#if wxUSE_UNICODE
8605f9c5
VZ
826 convFile = new wxCSConv(GetFileEncoding());
827 convMem = NULL;
27b0c286 828#else
8605f9c5 829 if ( GetFileEncoding().CmpNoCase(GetEncoding()) != 0 )
27b0c286
VS
830 {
831 convFile = new wxCSConv(GetFileEncoding());
832 convMem = new wxCSConv(GetEncoding());
833 }
8605f9c5
VZ
834 else // file and in-memory encodings are the same, no conversion needed
835 {
836 convFile =
837 convMem = NULL;
838 }
27b0c286
VS
839#endif
840
841 s.Printf(wxT("<?xml version=\"%s\" encoding=\"%s\"?>\n"),
842 GetVersion().c_str(), GetFileEncoding().c_str());
8dfd4fad 843 OutputString(stream, s);
27b0c286 844
538f3830 845 OutputNode(stream, GetRoot(), 0, convMem, convFile, indentstep);
8dfd4fad 846 OutputString(stream, wxT("\n"));
27b0c286 847
8605f9c5
VZ
848 delete convFile;
849 delete convMem;
27b0c286 850
759f7272 851 return true;
27b0c286
VS
852}
853
854#endif // wxUSE_XML