Warning fix about assigning unused value + little source cleaning.
[wxWidgets.git] / src / xml / xml.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
12 #pragma implementation "xml.h"
13 #endif
14
15 // For compilers that support precompilation, includes "wx.h".
16 #include "wx/wxprec.h"
17
18 #ifdef __BORLANDC__
19 #pragma hdrstop
20 #endif
21
22 #include "wx/xml/xml.h"
23
24 #if wxUSE_XML
25
26 #include "wx/wfstream.h"
27 #include "wx/datstrm.h"
28 #include "wx/zstream.h"
29 #include "wx/log.h"
30 #include "wx/intl.h"
31 #include "wx/strconv.h"
32
33 #include "expat.h" // from Expat
34
35 // DLL options compatibility check:
36 #include "wx/app.h"
37 WX_CHECK_BUILD_OPTIONS("wxXML")
38
39 //-----------------------------------------------------------------------------
40 // wxXmlNode
41 //-----------------------------------------------------------------------------
42
43 wxXmlNode::wxXmlNode(wxXmlNode *parent,wxXmlNodeType type,
44 const wxString& name, const wxString& content,
45 wxXmlProperty *props, wxXmlNode *next)
46 : m_type(type), m_name(name), m_content(content),
47 m_properties(props), m_parent(parent),
48 m_children(NULL), m_next(next)
49 {
50 if (m_parent)
51 {
52 if (m_parent->m_children)
53 {
54 m_next = m_parent->m_children;
55 m_parent->m_children = this;
56 }
57 else
58 m_parent->m_children = this;
59 }
60 }
61
62 wxXmlNode::wxXmlNode(wxXmlNodeType type, const wxString& name,
63 const wxString& content)
64 : m_type(type), m_name(name), m_content(content),
65 m_properties(NULL), m_parent(NULL),
66 m_children(NULL), m_next(NULL)
67 {}
68
69 wxXmlNode::wxXmlNode(const wxXmlNode& node)
70 {
71 m_next = NULL;
72 m_parent = NULL;
73 DoCopy(node);
74 }
75
76 wxXmlNode::~wxXmlNode()
77 {
78 wxXmlNode *c, *c2;
79 for (c = m_children; c; c = c2)
80 {
81 c2 = c->m_next;
82 delete c;
83 }
84
85 wxXmlProperty *p, *p2;
86 for (p = m_properties; p; p = p2)
87 {
88 p2 = p->GetNext();
89 delete p;
90 }
91 }
92
93 wxXmlNode& wxXmlNode::operator=(const wxXmlNode& node)
94 {
95 wxDELETE(m_properties);
96 wxDELETE(m_children);
97 DoCopy(node);
98 return *this;
99 }
100
101 void wxXmlNode::DoCopy(const wxXmlNode& node)
102 {
103 m_type = node.m_type;
104 m_name = node.m_name;
105 m_content = node.m_content;
106 m_children = NULL;
107
108 wxXmlNode *n = node.m_children;
109 while (n)
110 {
111 AddChild(new wxXmlNode(*n));
112 n = n->GetNext();
113 }
114
115 m_properties = NULL;
116 wxXmlProperty *p = node.m_properties;
117 while (p)
118 {
119 AddProperty(p->GetName(), p->GetValue());
120 p = p->GetNext();
121 }
122 }
123
124 bool wxXmlNode::HasProp(const wxString& propName) const
125 {
126 wxXmlProperty *prop = GetProperties();
127
128 while (prop)
129 {
130 if (prop->GetName() == propName) return true;
131 prop = prop->GetNext();
132 }
133
134 return false;
135 }
136
137 bool wxXmlNode::GetPropVal(const wxString& propName, wxString *value) const
138 {
139 wxXmlProperty *prop = GetProperties();
140
141 while (prop)
142 {
143 if (prop->GetName() == propName)
144 {
145 *value = prop->GetValue();
146 return true;
147 }
148 prop = prop->GetNext();
149 }
150
151 return false;
152 }
153
154 wxString wxXmlNode::GetPropVal(const wxString& propName, const wxString& defaultVal) const
155 {
156 wxString tmp;
157 if (GetPropVal(propName, &tmp))
158 return tmp;
159
160 return defaultVal;
161 }
162
163 void wxXmlNode::AddChild(wxXmlNode *child)
164 {
165 if (m_children == NULL)
166 m_children = child;
167 else
168 {
169 wxXmlNode *ch = m_children;
170 while (ch->m_next) ch = ch->m_next;
171 ch->m_next = child;
172 }
173 child->m_next = NULL;
174 child->m_parent = this;
175 }
176
177 void wxXmlNode::InsertChild(wxXmlNode *child, wxXmlNode *before_node)
178 {
179 wxASSERT_MSG(before_node->GetParent() == this, wxT("wxXmlNode::InsertChild - the node has incorrect parent"));
180
181 if (m_children == before_node)
182 m_children = child;
183 else
184 {
185 wxXmlNode *ch = m_children;
186 while (ch->m_next != before_node) ch = ch->m_next;
187 ch->m_next = child;
188 }
189
190 child->m_parent = this;
191 child->m_next = before_node;
192 }
193
194 bool wxXmlNode::RemoveChild(wxXmlNode *child)
195 {
196 if (m_children == NULL)
197 return false;
198 else if (m_children == child)
199 {
200 m_children = child->m_next;
201 child->m_parent = NULL;
202 child->m_next = NULL;
203 return true;
204 }
205 else
206 {
207 wxXmlNode *ch = m_children;
208 while (ch->m_next)
209 {
210 if (ch->m_next == child)
211 {
212 ch->m_next = child->m_next;
213 child->m_parent = NULL;
214 child->m_next = NULL;
215 return true;
216 }
217 ch = ch->m_next;
218 }
219 return false;
220 }
221 }
222
223 void wxXmlNode::AddProperty(const wxString& name, const wxString& value)
224 {
225 AddProperty(new wxXmlProperty(name, value, NULL));
226 }
227
228 void wxXmlNode::AddProperty(wxXmlProperty *prop)
229 {
230 if (m_properties == NULL)
231 m_properties = prop;
232 else
233 {
234 wxXmlProperty *p = m_properties;
235 while (p->GetNext()) p = p->GetNext();
236 p->SetNext(prop);
237 }
238 }
239
240 bool wxXmlNode::DeleteProperty(const wxString& name)
241 {
242 wxXmlProperty *prop;
243
244 if (m_properties == NULL)
245 return false;
246
247 else if (m_properties->GetName() == name)
248 {
249 prop = m_properties;
250 m_properties = prop->GetNext();
251 prop->SetNext(NULL);
252 delete prop;
253 return true;
254 }
255
256 else
257 {
258 wxXmlProperty *p = m_properties;
259 while (p->GetNext())
260 {
261 if (p->GetNext()->GetName() == name)
262 {
263 prop = p->GetNext();
264 p->SetNext(prop->GetNext());
265 prop->SetNext(NULL);
266 delete prop;
267 return true;
268 }
269 p = p->GetNext();
270 }
271 return false;
272 }
273 }
274
275
276
277 //-----------------------------------------------------------------------------
278 // wxXmlDocument
279 //-----------------------------------------------------------------------------
280
281 wxXmlDocument::wxXmlDocument()
282 : m_version(wxT("1.0")), m_fileEncoding(wxT("utf-8")), m_root(NULL)
283 {
284 #if !wxUSE_UNICODE
285 m_encoding = wxT("UTF-8");
286 #endif
287 }
288
289 wxXmlDocument::wxXmlDocument(const wxString& filename, const wxString& encoding)
290 :wxObject(), m_root(NULL)
291 {
292 if ( !Load(filename, encoding) )
293 {
294 wxDELETE(m_root);
295 }
296 }
297
298 wxXmlDocument::wxXmlDocument(wxInputStream& stream, const wxString& encoding)
299 :wxObject(), m_root(NULL)
300 {
301 if ( !Load(stream, encoding) )
302 {
303 wxDELETE(m_root);
304 }
305 }
306
307 wxXmlDocument::wxXmlDocument(const wxXmlDocument& doc)
308 :wxObject()
309 {
310 DoCopy(doc);
311 }
312
313 wxXmlDocument& wxXmlDocument::operator=(const wxXmlDocument& doc)
314 {
315 wxDELETE(m_root);
316 DoCopy(doc);
317 return *this;
318 }
319
320 void wxXmlDocument::DoCopy(const wxXmlDocument& doc)
321 {
322 m_version = doc.m_version;
323 #if !wxUSE_UNICODE
324 m_encoding = doc.m_encoding;
325 #endif
326 m_fileEncoding = doc.m_fileEncoding;
327 m_root = new wxXmlNode(*doc.m_root);
328 }
329
330 bool wxXmlDocument::Load(const wxString& filename, const wxString& encoding)
331 {
332 wxFileInputStream stream(filename);
333 return Load(stream, encoding);
334 }
335
336 bool wxXmlDocument::Save(const wxString& filename) const
337 {
338 wxFileOutputStream stream(filename);
339 return Save(stream);
340 }
341
342
343
344 //-----------------------------------------------------------------------------
345 // wxXmlDocument loading routines
346 //-----------------------------------------------------------------------------
347
348 /*
349 FIXME:
350 - process all elements, including CDATA
351 */
352
353 // converts Expat-produced string in UTF-8 into wxString.
354 inline static wxString CharToString(wxMBConv *conv,
355 const char *s, size_t len = wxSTRING_MAXLEN)
356 {
357 #if wxUSE_UNICODE
358 (void)conv;
359 return wxString(s, wxConvUTF8, len);
360 #else
361 if ( conv )
362 {
363 size_t nLen = (len != wxSTRING_MAXLEN) ? len :
364 wxConvUTF8.MB2WC((wchar_t*) NULL, s, 0);
365
366 wchar_t *buf = new wchar_t[nLen+1];
367 wxConvUTF8.MB2WC(buf, s, nLen);
368 buf[nLen] = 0;
369 wxString str(buf, *conv, len);
370 delete[] buf;
371 return str;
372 }
373 else
374 return wxString(s, len != wxSTRING_MAXLEN ? len : strlen(s));
375 #endif
376 }
377
378 struct wxXmlParsingContext
379 {
380 wxMBConv *conv;
381 wxXmlNode *root;
382 wxXmlNode *node;
383 wxXmlNode *lastAsText;
384 wxString encoding;
385 wxString version;
386 };
387
388 extern "C" {
389 static void StartElementHnd(void *userData, const char *name, const char **atts)
390 {
391 wxXmlParsingContext *ctx = (wxXmlParsingContext*)userData;
392 wxXmlNode *node = new wxXmlNode(wxXML_ELEMENT_NODE, CharToString(ctx->conv, name));
393 const char **a = atts;
394 while (*a)
395 {
396 node->AddProperty(CharToString(ctx->conv, a[0]), CharToString(ctx->conv, a[1]));
397 a += 2;
398 }
399 if (ctx->root == NULL)
400 ctx->root = node;
401 else
402 ctx->node->AddChild(node);
403 ctx->node = node;
404 ctx->lastAsText = NULL;
405 }
406 }
407
408 extern "C" {
409 static void EndElementHnd(void *userData, const char* WXUNUSED(name))
410 {
411 wxXmlParsingContext *ctx = (wxXmlParsingContext*)userData;
412
413 ctx->node = ctx->node->GetParent();
414 ctx->lastAsText = NULL;
415 }
416 }
417
418 extern "C" {
419 static void TextHnd(void *userData, const char *s, int len)
420 {
421 wxXmlParsingContext *ctx = (wxXmlParsingContext*)userData;
422 char *buf = new char[len + 1];
423
424 buf[len] = '\0';
425 memcpy(buf, s, (size_t)len);
426
427 if (ctx->lastAsText)
428 {
429 ctx->lastAsText->SetContent(ctx->lastAsText->GetContent() +
430 CharToString(ctx->conv, buf));
431 }
432 else
433 {
434 bool whiteOnly = true;
435 for (char *c = buf; *c != '\0'; c++)
436 if (*c != ' ' && *c != '\t' && *c != '\n' && *c != '\r')
437 {
438 whiteOnly = false;
439 break;
440 }
441 if (!whiteOnly)
442 {
443 ctx->lastAsText = new wxXmlNode(wxXML_TEXT_NODE, wxT("text"),
444 CharToString(ctx->conv, buf));
445 ctx->node->AddChild(ctx->lastAsText);
446 }
447 }
448
449 delete[] buf;
450 }
451 }
452
453 extern "C" {
454 static void CommentHnd(void *userData, const char *data)
455 {
456 wxXmlParsingContext *ctx = (wxXmlParsingContext*)userData;
457
458 if (ctx->node)
459 {
460 // VS: ctx->node == NULL happens if there is a comment before
461 // the root element (e.g. wxDesigner's output). We ignore such
462 // comments, no big deal...
463 ctx->node->AddChild(new wxXmlNode(wxXML_COMMENT_NODE,
464 wxT("comment"), CharToString(ctx->conv, data)));
465 }
466 ctx->lastAsText = NULL;
467 }
468 }
469
470 extern "C" {
471 static void DefaultHnd(void *userData, const char *s, int len)
472 {
473 // XML header:
474 if (len > 6 && memcmp(s, "<?xml ", 6) == 0)
475 {
476 wxXmlParsingContext *ctx = (wxXmlParsingContext*)userData;
477
478 wxString buf = CharToString(ctx->conv, s, (size_t)len);
479 int pos;
480 pos = buf.Find(wxT("encoding="));
481 if (pos != wxNOT_FOUND)
482 ctx->encoding = buf.Mid(pos + 10).BeforeFirst(buf[(size_t)pos+9]);
483 pos = buf.Find(wxT("version="));
484 if (pos != wxNOT_FOUND)
485 ctx->version = buf.Mid(pos + 9).BeforeFirst(buf[(size_t)pos+8]);
486 }
487 }
488 }
489
490 extern "C" {
491 static int UnknownEncodingHnd(void * WXUNUSED(encodingHandlerData),
492 const XML_Char *name, XML_Encoding *info)
493 {
494 // We must build conversion table for expat. The easiest way to do so
495 // is to let wxCSConv convert as string containing all characters to
496 // wide character representation:
497 wxString str(name, wxConvLibc);
498 wxCSConv conv(str);
499 char mbBuf[2];
500 wchar_t wcBuf[10];
501 size_t i;
502
503 mbBuf[1] = 0;
504 info->map[0] = 0;
505 for (i = 0; i < 255; i++)
506 {
507 mbBuf[0] = (char)(i+1);
508 if (conv.MB2WC(wcBuf, mbBuf, 2) == (size_t)-1)
509 {
510 // invalid/undefined byte in the encoding:
511 info->map[i+1] = -1;
512 }
513 info->map[i+1] = (int)wcBuf[0];
514 }
515
516 info->data = NULL;
517 info->convert = NULL;
518 info->release = NULL;
519
520 return 1;
521 }
522 }
523
524 bool wxXmlDocument::Load(wxInputStream& stream, const wxString& encoding)
525 {
526 #if wxUSE_UNICODE
527 (void)encoding;
528 #else
529 m_encoding = encoding;
530 #endif
531
532 const size_t BUFSIZE = 1024;
533 char buf[BUFSIZE];
534 wxXmlParsingContext ctx;
535 bool done;
536 XML_Parser parser = XML_ParserCreate(NULL);
537
538 ctx.root = ctx.node = NULL;
539 ctx.encoding = wxT("UTF-8"); // default in absence of encoding=""
540 ctx.conv = NULL;
541 #if !wxUSE_UNICODE
542 if ( encoding != wxT("UTF-8") && encoding != wxT("utf-8") )
543 ctx.conv = new wxCSConv(encoding);
544 #endif
545
546 XML_SetUserData(parser, (void*)&ctx);
547 XML_SetElementHandler(parser, StartElementHnd, EndElementHnd);
548 XML_SetCharacterDataHandler(parser, TextHnd);
549 XML_SetCommentHandler(parser, CommentHnd);
550 XML_SetDefaultHandler(parser, DefaultHnd);
551 XML_SetUnknownEncodingHandler(parser, UnknownEncodingHnd, NULL);
552
553 bool ok = true;
554 do
555 {
556 size_t len = stream.Read(buf, BUFSIZE).LastRead();
557 done = (len < BUFSIZE);
558 if (!XML_Parse(parser, buf, len, done))
559 {
560 wxString error(XML_ErrorString(XML_GetErrorCode(parser)),
561 *wxConvCurrent);
562 wxLogError(_("XML parsing error: '%s' at line %d"),
563 error.c_str(),
564 XML_GetCurrentLineNumber(parser));
565 ok = false;
566 break;
567 }
568 } while (!done);
569
570 if (ok)
571 {
572 if (!ctx.version.empty())
573 SetVersion(ctx.version);
574 if (!ctx.encoding.empty())
575 SetFileEncoding(ctx.encoding);
576 SetRoot(ctx.root);
577 }
578 else
579 {
580 delete ctx.root;
581 }
582
583 XML_ParserFree(parser);
584 #if !wxUSE_UNICODE
585 if ( ctx.conv )
586 delete ctx.conv;
587 #endif
588
589 return ok;
590
591 }
592
593
594
595 //-----------------------------------------------------------------------------
596 // wxXmlDocument saving routines
597 //-----------------------------------------------------------------------------
598
599 // write string to output:
600 inline static void OutputString(wxOutputStream& stream, const wxString& str,
601 #if wxUSE_UNICODE
602 wxMBConv * WXUNUSED(convMem),
603 #else
604 wxMBConv *convMem,
605 #endif
606 wxMBConv *convFile)
607 {
608 if (str.empty()) return;
609 #if wxUSE_UNICODE
610 const wxWX2MBbuf buf(str.mb_str(*(convFile ? convFile : &wxConvUTF8)));
611 stream.Write((const char*)buf, strlen((const char*)buf));
612 #else
613 if ( convFile == NULL )
614 stream.Write(str.mb_str(), str.Len());
615 else
616 {
617 wxString str2(str.wc_str(*convMem), *convFile);
618 stream.Write(str2.mb_str(), str2.Len());
619 }
620 #endif
621 }
622
623 // Same as above, but create entities first.
624 // Translates '<' to "&lt;", '>' to "&gt;" and '&' to "&amp;"
625 static void OutputStringEnt(wxOutputStream& stream, const wxString& str,
626 wxMBConv *convMem, wxMBConv *convFile,
627 bool escapeQuotes = false)
628 {
629 wxString buf;
630 size_t i, last, len;
631 wxChar c;
632
633 len = str.Len();
634 last = 0;
635 for (i = 0; i < len; i++)
636 {
637 c = str.GetChar(i);
638 if (c == wxT('<') || c == wxT('>') ||
639 (c == wxT('&') && str.Mid(i+1, 4) != wxT("amp;")) ||
640 (escapeQuotes && c == wxT('"')))
641 {
642 OutputString(stream, str.Mid(last, i - last), convMem, convFile);
643 switch (c)
644 {
645 case wxT('<'):
646 OutputString(stream, wxT("&lt;"), NULL, NULL);
647 break;
648 case wxT('>'):
649 OutputString(stream, wxT("&gt;"), NULL, NULL);
650 break;
651 case wxT('&'):
652 OutputString(stream, wxT("&amp;"), NULL, NULL);
653 break;
654 case wxT('"'):
655 OutputString(stream, wxT("&quot;"), NULL, NULL);
656 break;
657 default: break;
658 }
659 last = i + 1;
660 }
661 }
662 OutputString(stream, str.Mid(last, i - last), convMem, convFile);
663 }
664
665 inline static void OutputIndentation(wxOutputStream& stream, int indent)
666 {
667 wxString str = wxT("\n");
668 for (int i = 0; i < indent; i++)
669 str << wxT(' ') << wxT(' ');
670 OutputString(stream, str, NULL, NULL);
671 }
672
673 static void OutputNode(wxOutputStream& stream, wxXmlNode *node, int indent,
674 wxMBConv *convMem, wxMBConv *convFile)
675 {
676 wxXmlNode *n, *prev;
677 wxXmlProperty *prop;
678
679 switch (node->GetType())
680 {
681 case wxXML_TEXT_NODE:
682 OutputStringEnt(stream, node->GetContent(), convMem, convFile);
683 break;
684
685 case wxXML_ELEMENT_NODE:
686 OutputString(stream, wxT("<"), NULL, NULL);
687 OutputString(stream, node->GetName(), NULL, NULL);
688
689 prop = node->GetProperties();
690 while (prop)
691 {
692 OutputString(stream, wxT(" ") + prop->GetName() + wxT("=\""),
693 NULL, NULL);
694 OutputStringEnt(stream, prop->GetValue(), NULL, NULL,
695 true/*escapeQuotes*/);
696 OutputString(stream, wxT("\""), NULL, NULL);
697 prop = prop->GetNext();
698 }
699
700 if (node->GetChildren())
701 {
702 OutputString(stream, wxT(">"), NULL, NULL);
703 prev = NULL;
704 n = node->GetChildren();
705 while (n)
706 {
707 if (n && n->GetType() != wxXML_TEXT_NODE)
708 OutputIndentation(stream, indent + 1);
709 OutputNode(stream, n, indent + 1, convMem, convFile);
710 prev = n;
711 n = n->GetNext();
712 }
713 if (prev && prev->GetType() != wxXML_TEXT_NODE)
714 OutputIndentation(stream, indent);
715 OutputString(stream, wxT("</"), NULL, NULL);
716 OutputString(stream, node->GetName(), NULL, NULL);
717 OutputString(stream, wxT(">"), NULL, NULL);
718 }
719 else
720 OutputString(stream, wxT("/>"), NULL, NULL);
721 break;
722
723 case wxXML_COMMENT_NODE:
724 OutputString(stream, wxT("<!--"), NULL, NULL);
725 OutputString(stream, node->GetContent(), convMem, convFile);
726 OutputString(stream, wxT("-->"), NULL, NULL);
727 break;
728
729 default:
730 wxFAIL_MSG(wxT("unsupported node type"));
731 }
732 }
733
734 bool wxXmlDocument::Save(wxOutputStream& stream) const
735 {
736 if ( !IsOk() )
737 return false;
738
739 wxString s;
740
741 wxMBConv *convMem = NULL;
742
743 #if wxUSE_UNICODE
744 wxMBConv *convFile = new wxCSConv(GetFileEncoding());
745 #else
746 wxMBConv *convFile = NULL;
747 if ( GetFileEncoding() != GetEncoding() )
748 {
749 convFile = new wxCSConv(GetFileEncoding());
750 convMem = new wxCSConv(GetEncoding());
751 }
752 #endif
753
754 s.Printf(wxT("<?xml version=\"%s\" encoding=\"%s\"?>\n"),
755 GetVersion().c_str(), GetFileEncoding().c_str());
756 OutputString(stream, s, NULL, NULL);
757
758 OutputNode(stream, GetRoot(), 0, convMem, convFile);
759 OutputString(stream, wxT("\n"), NULL, NULL);
760
761 if ( convFile )
762 delete convFile;
763 if ( convMem )
764 delete convMem;
765
766 return true;
767 }
768
769 #endif // wxUSE_XML