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