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