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