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