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