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