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