]>
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 | 31 | #include "wx/strconv.h" |
664e1314 | 32 | #include "wx/scopedptr.h" |
27b0c286 VS |
33 | |
34 | #include "expat.h" // from Expat | |
35 | ||
34fdf762 | 36 | // DLL options compatibility check: |
34fdf762 VS |
37 | WX_CHECK_BUILD_OPTIONS("wxXML") |
38 | ||
4c43dd90 JS |
39 | |
40 | IMPLEMENT_CLASS(wxXmlDocument, wxObject) | |
41 | ||
42 | ||
538f3830 | 43 | // a private utility used by wxXML |
352d9b89 | 44 | static bool wxIsWhiteOnly(const wxString& buf); |
538f3830 | 45 | |
4c43dd90 | 46 | |
27b0c286 VS |
47 | //----------------------------------------------------------------------------- |
48 | // wxXmlNode | |
49 | //----------------------------------------------------------------------------- | |
50 | ||
51 | wxXmlNode::wxXmlNode(wxXmlNode *parent,wxXmlNodeType type, | |
52 | const wxString& name, const wxString& content, | |
6e26d6b7 | 53 | wxXmlAttribute *attrs, wxXmlNode *next, int lineNo) |
27b0c286 | 54 | : m_type(type), m_name(name), m_content(content), |
288b6107 | 55 | m_attrs(attrs), m_parent(parent), |
6e26d6b7 VS |
56 | m_children(NULL), m_next(next), |
57 | m_lineNo(lineNo) | |
27b0c286 VS |
58 | { |
59 | if (m_parent) | |
60 | { | |
61 | if (m_parent->m_children) | |
62 | { | |
63 | m_next = m_parent->m_children; | |
64 | m_parent->m_children = this; | |
65 | } | |
66 | else | |
67 | m_parent->m_children = this; | |
68 | } | |
69 | } | |
70 | ||
71 | wxXmlNode::wxXmlNode(wxXmlNodeType type, const wxString& name, | |
6e26d6b7 VS |
72 | const wxString& content, |
73 | int lineNo) | |
27b0c286 | 74 | : m_type(type), m_name(name), m_content(content), |
288b6107 | 75 | m_attrs(NULL), m_parent(NULL), |
6e26d6b7 VS |
76 | m_children(NULL), m_next(NULL), |
77 | m_lineNo(lineNo) | |
27b0c286 VS |
78 | {} |
79 | ||
80 | wxXmlNode::wxXmlNode(const wxXmlNode& node) | |
81 | { | |
82 | m_next = NULL; | |
83 | m_parent = NULL; | |
84 | DoCopy(node); | |
85 | } | |
86 | ||
87 | wxXmlNode::~wxXmlNode() | |
88 | { | |
89 | wxXmlNode *c, *c2; | |
90 | for (c = m_children; c; c = c2) | |
91 | { | |
92 | c2 = c->m_next; | |
93 | delete c; | |
94 | } | |
95 | ||
288b6107 VS |
96 | wxXmlAttribute *p, *p2; |
97 | for (p = m_attrs; p; p = p2) | |
27b0c286 VS |
98 | { |
99 | p2 = p->GetNext(); | |
100 | delete p; | |
101 | } | |
102 | } | |
103 | ||
104 | wxXmlNode& wxXmlNode::operator=(const wxXmlNode& node) | |
105 | { | |
288b6107 | 106 | wxDELETE(m_attrs); |
27b0c286 VS |
107 | wxDELETE(m_children); |
108 | DoCopy(node); | |
109 | return *this; | |
110 | } | |
111 | ||
112 | void wxXmlNode::DoCopy(const wxXmlNode& node) | |
113 | { | |
114 | m_type = node.m_type; | |
115 | m_name = node.m_name; | |
116 | m_content = node.m_content; | |
6e26d6b7 | 117 | m_lineNo = node.m_lineNo; |
27b0c286 VS |
118 | m_children = NULL; |
119 | ||
120 | wxXmlNode *n = node.m_children; | |
121 | while (n) | |
122 | { | |
123 | AddChild(new wxXmlNode(*n)); | |
124 | n = n->GetNext(); | |
125 | } | |
126 | ||
288b6107 VS |
127 | m_attrs = NULL; |
128 | wxXmlAttribute *p = node.m_attrs; | |
27b0c286 VS |
129 | while (p) |
130 | { | |
288b6107 | 131 | AddAttribute(p->GetName(), p->GetValue()); |
27b0c286 VS |
132 | p = p->GetNext(); |
133 | } | |
134 | } | |
135 | ||
288b6107 | 136 | bool wxXmlNode::HasAttribute(const wxString& attrName) const |
27b0c286 | 137 | { |
288b6107 | 138 | wxXmlAttribute *attr = GetAttributes(); |
27b0c286 | 139 | |
288b6107 | 140 | while (attr) |
27b0c286 | 141 | { |
288b6107 VS |
142 | if (attr->GetName() == attrName) return true; |
143 | attr = attr->GetNext(); | |
27b0c286 VS |
144 | } |
145 | ||
759f7272 | 146 | return false; |
27b0c286 VS |
147 | } |
148 | ||
288b6107 | 149 | bool wxXmlNode::GetAttribute(const wxString& attrName, wxString *value) const |
27b0c286 | 150 | { |
cc24bf91 VS |
151 | wxCHECK_MSG( value, false, "value argument must not be NULL" ); |
152 | ||
288b6107 | 153 | wxXmlAttribute *attr = GetAttributes(); |
27b0c286 | 154 | |
288b6107 | 155 | while (attr) |
27b0c286 | 156 | { |
288b6107 | 157 | if (attr->GetName() == attrName) |
27b0c286 | 158 | { |
288b6107 | 159 | *value = attr->GetValue(); |
759f7272 | 160 | return true; |
27b0c286 | 161 | } |
288b6107 | 162 | attr = attr->GetNext(); |
27b0c286 VS |
163 | } |
164 | ||
759f7272 | 165 | return false; |
27b0c286 VS |
166 | } |
167 | ||
288b6107 | 168 | wxString wxXmlNode::GetAttribute(const wxString& attrName, const wxString& defaultVal) const |
27b0c286 VS |
169 | { |
170 | wxString tmp; | |
288b6107 | 171 | if (GetAttribute(attrName, &tmp)) |
27b0c286 | 172 | return tmp; |
0e2710a6 DS |
173 | |
174 | return defaultVal; | |
27b0c286 VS |
175 | } |
176 | ||
177 | void wxXmlNode::AddChild(wxXmlNode *child) | |
178 | { | |
179 | if (m_children == NULL) | |
180 | m_children = child; | |
181 | else | |
182 | { | |
183 | wxXmlNode *ch = m_children; | |
184 | while (ch->m_next) ch = ch->m_next; | |
185 | ch->m_next = child; | |
186 | } | |
187 | child->m_next = NULL; | |
188 | child->m_parent = this; | |
189 | } | |
190 | ||
5e05df3c VS |
191 | // inserts a new node in front of 'followingNode' |
192 | bool wxXmlNode::InsertChild(wxXmlNode *child, wxXmlNode *followingNode) | |
27b0c286 | 193 | { |
5e05df3c VS |
194 | wxCHECK_MSG( child, false, "cannot insert a NULL node!" ); |
195 | wxCHECK_MSG( child->m_parent == NULL, false, "node already has a parent" ); | |
196 | wxCHECK_MSG( child->m_next == NULL, false, "node already has m_next" ); | |
197 | wxCHECK_MSG( followingNode == NULL || followingNode->GetParent() == this, | |
198 | false, | |
199 | "wxXmlNode::InsertChild - followingNode has incorrect parent" ); | |
200 | ||
201 | // this is for backward compatibility, NULL was allowed here thanks to | |
202 | // the confusion about followingNode's meaning | |
203 | if ( followingNode == NULL ) | |
204 | followingNode = m_children; | |
205 | ||
206 | if ( m_children == followingNode ) | |
fa6a8373 | 207 | { |
fa6a8373 RR |
208 | child->m_next = m_children; |
209 | m_children = child; | |
fa6a8373 | 210 | } |
27b0c286 VS |
211 | else |
212 | { | |
213 | wxXmlNode *ch = m_children; | |
5e05df3c VS |
214 | while ( ch && ch->m_next != followingNode ) |
215 | ch = ch->m_next; | |
216 | if ( !ch ) | |
217 | { | |
218 | wxFAIL_MSG( "followingNode has this node as parent, but couldn't be found among children" ); | |
219 | return false; | |
220 | } | |
221 | ||
222 | child->m_next = followingNode; | |
27b0c286 VS |
223 | ch->m_next = child; |
224 | } | |
225 | ||
226 | child->m_parent = this; | |
fa6a8373 | 227 | return true; |
27b0c286 VS |
228 | } |
229 | ||
43a302f2 VS |
230 | // inserts a new node right after 'precedingNode' |
231 | bool wxXmlNode::InsertChildAfter(wxXmlNode *child, wxXmlNode *precedingNode) | |
232 | { | |
233 | wxCHECK_MSG( child, false, "cannot insert a NULL node!" ); | |
234 | wxCHECK_MSG( child->m_parent == NULL, false, "node already has a parent" ); | |
235 | wxCHECK_MSG( child->m_next == NULL, false, "node already has m_next" ); | |
236 | wxCHECK_MSG( precedingNode == NULL || precedingNode->m_parent == this, false, | |
237 | "precedingNode has wrong parent" ); | |
238 | ||
239 | if ( precedingNode ) | |
240 | { | |
241 | child->m_next = precedingNode->m_next; | |
242 | precedingNode->m_next = child; | |
243 | } | |
244 | else // precedingNode == NULL | |
245 | { | |
246 | wxCHECK_MSG( m_children == NULL, false, | |
247 | "NULL precedingNode only makes sense when there are no children" ); | |
248 | ||
249 | child->m_next = m_children; | |
250 | m_children = child; | |
251 | } | |
252 | ||
253 | child->m_parent = this; | |
254 | return true; | |
255 | } | |
256 | ||
27b0c286 VS |
257 | bool wxXmlNode::RemoveChild(wxXmlNode *child) |
258 | { | |
259 | if (m_children == NULL) | |
759f7272 | 260 | return false; |
27b0c286 VS |
261 | else if (m_children == child) |
262 | { | |
263 | m_children = child->m_next; | |
264 | child->m_parent = NULL; | |
265 | child->m_next = NULL; | |
759f7272 | 266 | return true; |
27b0c286 VS |
267 | } |
268 | else | |
269 | { | |
270 | wxXmlNode *ch = m_children; | |
271 | while (ch->m_next) | |
272 | { | |
273 | if (ch->m_next == child) | |
274 | { | |
275 | ch->m_next = child->m_next; | |
276 | child->m_parent = NULL; | |
277 | child->m_next = NULL; | |
759f7272 | 278 | return true; |
27b0c286 VS |
279 | } |
280 | ch = ch->m_next; | |
281 | } | |
759f7272 | 282 | return false; |
27b0c286 VS |
283 | } |
284 | } | |
285 | ||
e13ce4a3 VZ |
286 | void wxXmlNode::AddAttribute(const wxString& name, const wxString& value) |
287 | { | |
288 | AddProperty(name, value); | |
289 | } | |
290 | ||
291 | void wxXmlNode::AddAttribute(wxXmlAttribute *attr) | |
292 | { | |
293 | AddProperty(attr); | |
294 | } | |
295 | ||
296 | bool wxXmlNode::DeleteAttribute(const wxString& name) | |
297 | { | |
298 | return DeleteProperty(name); | |
299 | } | |
300 | ||
27b0c286 VS |
301 | void wxXmlNode::AddProperty(const wxString& name, const wxString& value) |
302 | { | |
288b6107 | 303 | AddProperty(new wxXmlAttribute(name, value, NULL)); |
27b0c286 VS |
304 | } |
305 | ||
288b6107 | 306 | void wxXmlNode::AddProperty(wxXmlAttribute *attr) |
27b0c286 | 307 | { |
288b6107 VS |
308 | if (m_attrs == NULL) |
309 | m_attrs = attr; | |
27b0c286 VS |
310 | else |
311 | { | |
288b6107 | 312 | wxXmlAttribute *p = m_attrs; |
27b0c286 | 313 | while (p->GetNext()) p = p->GetNext(); |
288b6107 | 314 | p->SetNext(attr); |
27b0c286 VS |
315 | } |
316 | } | |
317 | ||
318 | bool wxXmlNode::DeleteProperty(const wxString& name) | |
319 | { | |
288b6107 | 320 | wxXmlAttribute *attr; |
27b0c286 | 321 | |
288b6107 | 322 | if (m_attrs == NULL) |
759f7272 | 323 | return false; |
27b0c286 | 324 | |
288b6107 | 325 | else if (m_attrs->GetName() == name) |
27b0c286 | 326 | { |
288b6107 VS |
327 | attr = m_attrs; |
328 | m_attrs = attr->GetNext(); | |
329 | attr->SetNext(NULL); | |
330 | delete attr; | |
759f7272 | 331 | return true; |
27b0c286 VS |
332 | } |
333 | ||
334 | else | |
335 | { | |
288b6107 | 336 | wxXmlAttribute *p = m_attrs; |
27b0c286 VS |
337 | while (p->GetNext()) |
338 | { | |
339 | if (p->GetNext()->GetName() == name) | |
340 | { | |
288b6107 VS |
341 | attr = p->GetNext(); |
342 | p->SetNext(attr->GetNext()); | |
343 | attr->SetNext(NULL); | |
344 | delete attr; | |
759f7272 | 345 | return true; |
27b0c286 VS |
346 | } |
347 | p = p->GetNext(); | |
348 | } | |
759f7272 | 349 | return false; |
27b0c286 VS |
350 | } |
351 | } | |
352 | ||
4c43dd90 JS |
353 | wxString wxXmlNode::GetNodeContent() const |
354 | { | |
355 | wxXmlNode *n = GetChildren(); | |
356 | ||
357 | while (n) | |
358 | { | |
359 | if (n->GetType() == wxXML_TEXT_NODE || | |
360 | n->GetType() == wxXML_CDATA_SECTION_NODE) | |
361 | return n->GetContent(); | |
362 | n = n->GetNext(); | |
363 | } | |
364 | return wxEmptyString; | |
365 | } | |
366 | ||
538f3830 VS |
367 | int wxXmlNode::GetDepth(wxXmlNode *grandparent) const |
368 | { | |
369 | const wxXmlNode *n = this; | |
370 | int ret = -1; | |
371 | ||
372 | do | |
373 | { | |
374 | ret++; | |
375 | n = n->GetParent(); | |
376 | if (n == grandparent) | |
377 | return ret; | |
378 | ||
379 | } while (n); | |
380 | ||
381 | return wxNOT_FOUND; | |
382 | } | |
383 | ||
384 | bool wxXmlNode::IsWhitespaceOnly() const | |
385 | { | |
386 | return wxIsWhiteOnly(m_content); | |
387 | } | |
388 | ||
27b0c286 VS |
389 | |
390 | ||
391 | //----------------------------------------------------------------------------- | |
392 | // wxXmlDocument | |
393 | //----------------------------------------------------------------------------- | |
394 | ||
395 | wxXmlDocument::wxXmlDocument() | |
e767076e | 396 | : m_version(wxS("1.0")), m_fileEncoding(wxS("utf-8")), m_root(NULL) |
27b0c286 VS |
397 | { |
398 | #if !wxUSE_UNICODE | |
e767076e | 399 | m_encoding = wxS("UTF-8"); |
27b0c286 VS |
400 | #endif |
401 | } | |
402 | ||
403 | wxXmlDocument::wxXmlDocument(const wxString& filename, const wxString& encoding) | |
d0468e8c | 404 | :wxObject(), m_root(NULL) |
27b0c286 VS |
405 | { |
406 | if ( !Load(filename, encoding) ) | |
407 | { | |
408 | wxDELETE(m_root); | |
409 | } | |
410 | } | |
411 | ||
412 | wxXmlDocument::wxXmlDocument(wxInputStream& stream, const wxString& encoding) | |
d0468e8c | 413 | :wxObject(), m_root(NULL) |
27b0c286 VS |
414 | { |
415 | if ( !Load(stream, encoding) ) | |
416 | { | |
417 | wxDELETE(m_root); | |
418 | } | |
419 | } | |
420 | ||
421 | wxXmlDocument::wxXmlDocument(const wxXmlDocument& doc) | |
d0468e8c | 422 | :wxObject() |
27b0c286 VS |
423 | { |
424 | DoCopy(doc); | |
425 | } | |
426 | ||
427 | wxXmlDocument& wxXmlDocument::operator=(const wxXmlDocument& doc) | |
428 | { | |
429 | wxDELETE(m_root); | |
430 | DoCopy(doc); | |
431 | return *this; | |
432 | } | |
433 | ||
434 | void wxXmlDocument::DoCopy(const wxXmlDocument& doc) | |
435 | { | |
436 | m_version = doc.m_version; | |
437 | #if !wxUSE_UNICODE | |
438 | m_encoding = doc.m_encoding; | |
439 | #endif | |
440 | m_fileEncoding = doc.m_fileEncoding; | |
e8da6b7c RR |
441 | |
442 | if (doc.m_root) | |
443 | m_root = new wxXmlNode(*doc.m_root); | |
444 | else | |
445 | m_root = NULL; | |
27b0c286 VS |
446 | } |
447 | ||
538f3830 | 448 | bool wxXmlDocument::Load(const wxString& filename, const wxString& encoding, int flags) |
27b0c286 VS |
449 | { |
450 | wxFileInputStream stream(filename); | |
97757cee VZ |
451 | if (!stream.Ok()) |
452 | return false; | |
538f3830 | 453 | return Load(stream, encoding, flags); |
27b0c286 VS |
454 | } |
455 | ||
538f3830 | 456 | bool wxXmlDocument::Save(const wxString& filename, int indentstep) const |
27b0c286 VS |
457 | { |
458 | wxFileOutputStream stream(filename); | |
97757cee VZ |
459 | if (!stream.Ok()) |
460 | return false; | |
538f3830 | 461 | return Save(stream, indentstep); |
27b0c286 VS |
462 | } |
463 | ||
464 | ||
465 | ||
466 | //----------------------------------------------------------------------------- | |
467 | // wxXmlDocument loading routines | |
468 | //----------------------------------------------------------------------------- | |
469 | ||
ddae497f VZ |
470 | // converts Expat-produced string in UTF-8 into wxString using the specified |
471 | // conv or keep in UTF-8 if conv is NULL | |
472 | static wxString CharToString(wxMBConv *conv, | |
b04edcaf | 473 | const char *s, size_t len = wxString::npos) |
27b0c286 | 474 | { |
b04edcaf | 475 | #if !wxUSE_UNICODE |
27b0c286 VS |
476 | if ( conv ) |
477 | { | |
ddae497f VZ |
478 | // there can be no embedded NULs in this string so we don't need the |
479 | // output length, it will be NUL-terminated | |
480 | const wxWCharBuffer wbuf( | |
6df6e35a | 481 | wxConvUTF8.cMB2WC(s, len == wxString::npos ? wxNO_LEN : len, NULL)); |
ddae497f | 482 | |
da452b9e | 483 | return wxString(wbuf, *conv); |
27b0c286 | 484 | } |
b04edcaf VS |
485 | // else: the string is wanted in UTF-8 |
486 | #endif // !wxUSE_UNICODE | |
487 | ||
488 | wxUnusedVar(conv); | |
cc209a51 | 489 | return wxString::FromUTF8Unchecked(s, len); |
27b0c286 VS |
490 | } |
491 | ||
538f3830 | 492 | // returns true if the given string contains only whitespaces |
352d9b89 | 493 | bool wxIsWhiteOnly(const wxString& buf) |
538f3830 | 494 | { |
352d9b89 VS |
495 | for ( wxString::const_iterator i = buf.begin(); i != buf.end(); ++i ) |
496 | { | |
497 | wxChar c = *i; | |
e767076e | 498 | if ( c != wxS(' ') && c != wxS('\t') && c != wxS('\n') && c != wxS('\r')) |
538f3830 | 499 | return false; |
352d9b89 | 500 | } |
538f3830 VS |
501 | return true; |
502 | } | |
503 | ||
504 | ||
27b0c286 VS |
505 | struct wxXmlParsingContext |
506 | { | |
43a302f2 VS |
507 | wxXmlParsingContext() |
508 | : conv(NULL), | |
509 | root(NULL), | |
510 | node(NULL), | |
511 | lastChild(NULL), | |
512 | lastAsText(NULL), | |
513 | removeWhiteOnlyNodes(false) | |
514 | {} | |
515 | ||
6e26d6b7 | 516 | XML_Parser parser; |
27b0c286 VS |
517 | wxMBConv *conv; |
518 | wxXmlNode *root; | |
43a302f2 VS |
519 | wxXmlNode *node; // the node being parsed |
520 | wxXmlNode *lastChild; // the last child of "node" | |
521 | wxXmlNode *lastAsText; // the last _text_ child of "node" | |
27b0c286 VS |
522 | wxString encoding; |
523 | wxString version; | |
538f3830 | 524 | bool removeWhiteOnlyNodes; |
27b0c286 VS |
525 | }; |
526 | ||
43a302f2 VS |
527 | // checks that ctx->lastChild is in consistent state |
528 | #define ASSERT_LAST_CHILD_OK(ctx) \ | |
529 | wxASSERT( ctx->lastChild == NULL || \ | |
530 | ctx->lastChild->GetNext() == NULL ); \ | |
531 | wxASSERT( ctx->lastChild == NULL || \ | |
532 | ctx->lastChild->GetParent() == ctx->node ) | |
533 | ||
865bb325 | 534 | extern "C" { |
27b0c286 VS |
535 | static void StartElementHnd(void *userData, const char *name, const char **atts) |
536 | { | |
537 | wxXmlParsingContext *ctx = (wxXmlParsingContext*)userData; | |
6e26d6b7 VS |
538 | wxXmlNode *node = new wxXmlNode(wxXML_ELEMENT_NODE, |
539 | CharToString(ctx->conv, name), | |
540 | wxEmptyString, | |
541 | XML_GetCurrentLineNumber(ctx->parser)); | |
27b0c286 | 542 | const char **a = atts; |
6e26d6b7 | 543 | |
43a302f2 | 544 | // add node attributes |
27b0c286 VS |
545 | while (*a) |
546 | { | |
288b6107 | 547 | node->AddAttribute(CharToString(ctx->conv, a[0]), CharToString(ctx->conv, a[1])); |
27b0c286 VS |
548 | a += 2; |
549 | } | |
43a302f2 | 550 | |
27b0c286 | 551 | if (ctx->root == NULL) |
43a302f2 | 552 | { |
27b0c286 | 553 | ctx->root = node; |
43a302f2 | 554 | } |
27b0c286 | 555 | else |
43a302f2 VS |
556 | { |
557 | ASSERT_LAST_CHILD_OK(ctx); | |
558 | ctx->node->InsertChildAfter(node, ctx->lastChild); | |
559 | } | |
560 | ||
27b0c286 | 561 | ctx->lastAsText = NULL; |
43a302f2 VS |
562 | ctx->lastChild = NULL; // our new node "node" has no children yet |
563 | ||
564 | ctx->node = node; | |
27b0c286 VS |
565 | } |
566 | ||
567 | static void EndElementHnd(void *userData, const char* WXUNUSED(name)) | |
568 | { | |
569 | wxXmlParsingContext *ctx = (wxXmlParsingContext*)userData; | |
570 | ||
43a302f2 VS |
571 | // we're exiting the last children of ctx->node->GetParent() and going |
572 | // back one level up, so current value of ctx->node points to the last | |
573 | // child of ctx->node->GetParent() | |
574 | ctx->lastChild = ctx->node; | |
575 | ||
27b0c286 VS |
576 | ctx->node = ctx->node->GetParent(); |
577 | ctx->lastAsText = NULL; | |
578 | } | |
579 | ||
580 | static void TextHnd(void *userData, const char *s, int len) | |
581 | { | |
582 | wxXmlParsingContext *ctx = (wxXmlParsingContext*)userData; | |
538f3830 | 583 | wxString str = CharToString(ctx->conv, s, len); |
27b0c286 VS |
584 | |
585 | if (ctx->lastAsText) | |
586 | { | |
d6cb1287 | 587 | ctx->lastAsText->SetContent(ctx->lastAsText->GetContent() + str); |
27b0c286 VS |
588 | } |
589 | else | |
590 | { | |
538f3830 VS |
591 | bool whiteOnly = false; |
592 | if (ctx->removeWhiteOnlyNodes) | |
593 | whiteOnly = wxIsWhiteOnly(str); | |
594 | ||
27b0c286 VS |
595 | if (!whiteOnly) |
596 | { | |
43a302f2 | 597 | wxXmlNode *textnode = |
e767076e | 598 | new wxXmlNode(wxXML_TEXT_NODE, wxS("text"), str, |
6e26d6b7 | 599 | XML_GetCurrentLineNumber(ctx->parser)); |
43a302f2 VS |
600 | |
601 | ASSERT_LAST_CHILD_OK(ctx); | |
602 | ctx->node->InsertChildAfter(textnode, ctx->lastChild); | |
603 | ctx->lastChild= ctx->lastAsText = textnode; | |
27b0c286 VS |
604 | } |
605 | } | |
27b0c286 VS |
606 | } |
607 | ||
a01cbf0a RR |
608 | static void StartCdataHnd(void *userData) |
609 | { | |
610 | wxXmlParsingContext *ctx = (wxXmlParsingContext*)userData; | |
611 | ||
43a302f2 | 612 | wxXmlNode *textnode = |
e767076e | 613 | new wxXmlNode(wxXML_CDATA_SECTION_NODE, wxS("cdata"), wxS(""), |
6e26d6b7 | 614 | XML_GetCurrentLineNumber(ctx->parser)); |
43a302f2 VS |
615 | |
616 | ASSERT_LAST_CHILD_OK(ctx); | |
617 | ctx->node->InsertChildAfter(textnode, ctx->lastChild); | |
618 | ctx->lastChild= ctx->lastAsText = textnode; | |
a01cbf0a | 619 | } |
a01cbf0a | 620 | |
a6cf6bcf VZ |
621 | static void EndCdataHnd(void *userData) |
622 | { | |
623 | wxXmlParsingContext *ctx = (wxXmlParsingContext*)userData; | |
624 | ||
625 | // we need to reset this pointer so that subsequent text nodes don't append | |
626 | // their contents to this one but create new wxXML_TEXT_NODE objects (or | |
627 | // not create anything at all if only white space follows the CDATA section | |
628 | // and wxXMLDOC_KEEP_WHITESPACE_NODES is not used as is commonly the case) | |
629 | ctx->lastAsText = NULL; | |
630 | } | |
631 | ||
27b0c286 VS |
632 | static void CommentHnd(void *userData, const char *data) |
633 | { | |
634 | wxXmlParsingContext *ctx = (wxXmlParsingContext*)userData; | |
635 | ||
636 | if (ctx->node) | |
637 | { | |
43a302f2 | 638 | wxXmlNode *commentnode = |
6e26d6b7 | 639 | new wxXmlNode(wxXML_COMMENT_NODE, |
e767076e | 640 | wxS("comment"), CharToString(ctx->conv, data), |
43a302f2 VS |
641 | XML_GetCurrentLineNumber(ctx->parser)); |
642 | ||
643 | ASSERT_LAST_CHILD_OK(ctx); | |
644 | ctx->node->InsertChildAfter(commentnode, ctx->lastChild); | |
645 | ctx->lastChild = commentnode; | |
27b0c286 | 646 | } |
43a302f2 VS |
647 | //else: ctx->node == NULL happens if there is a comment before |
648 | // the root element. We current don't have a way to represent | |
649 | // these in wxXmlDocument (FIXME). | |
650 | ||
27b0c286 VS |
651 | ctx->lastAsText = NULL; |
652 | } | |
653 | ||
654 | static void DefaultHnd(void *userData, const char *s, int len) | |
655 | { | |
656 | // XML header: | |
657 | if (len > 6 && memcmp(s, "<?xml ", 6) == 0) | |
658 | { | |
659 | wxXmlParsingContext *ctx = (wxXmlParsingContext*)userData; | |
660 | ||
661 | wxString buf = CharToString(ctx->conv, s, (size_t)len); | |
662 | int pos; | |
e767076e | 663 | pos = buf.Find(wxS("encoding=")); |
27b0c286 VS |
664 | if (pos != wxNOT_FOUND) |
665 | ctx->encoding = buf.Mid(pos + 10).BeforeFirst(buf[(size_t)pos+9]); | |
e767076e | 666 | pos = buf.Find(wxS("version=")); |
27b0c286 VS |
667 | if (pos != wxNOT_FOUND) |
668 | ctx->version = buf.Mid(pos + 9).BeforeFirst(buf[(size_t)pos+8]); | |
669 | } | |
670 | } | |
671 | ||
672 | static int UnknownEncodingHnd(void * WXUNUSED(encodingHandlerData), | |
673 | const XML_Char *name, XML_Encoding *info) | |
674 | { | |
675 | // We must build conversion table for expat. The easiest way to do so | |
676 | // is to let wxCSConv convert as string containing all characters to | |
677 | // wide character representation: | |
86501081 | 678 | wxCSConv conv(name); |
27b0c286 VS |
679 | char mbBuf[2]; |
680 | wchar_t wcBuf[10]; | |
681 | size_t i; | |
682 | ||
683 | mbBuf[1] = 0; | |
684 | info->map[0] = 0; | |
685 | for (i = 0; i < 255; i++) | |
686 | { | |
687 | mbBuf[0] = (char)(i+1); | |
688 | if (conv.MB2WC(wcBuf, mbBuf, 2) == (size_t)-1) | |
689 | { | |
690 | // invalid/undefined byte in the encoding: | |
691 | info->map[i+1] = -1; | |
692 | } | |
693 | info->map[i+1] = (int)wcBuf[0]; | |
694 | } | |
42841dfc | 695 | |
27b0c286 VS |
696 | info->data = NULL; |
697 | info->convert = NULL; | |
698 | info->release = NULL; | |
699 | ||
700 | return 1; | |
701 | } | |
26296ac9 VS |
702 | |
703 | } // extern "C" | |
27b0c286 | 704 | |
538f3830 | 705 | bool wxXmlDocument::Load(wxInputStream& stream, const wxString& encoding, int flags) |
27b0c286 VS |
706 | { |
707 | #if wxUSE_UNICODE | |
708 | (void)encoding; | |
709 | #else | |
710 | m_encoding = encoding; | |
711 | #endif | |
712 | ||
713 | const size_t BUFSIZE = 1024; | |
714 | char buf[BUFSIZE]; | |
715 | wxXmlParsingContext ctx; | |
716 | bool done; | |
717 | XML_Parser parser = XML_ParserCreate(NULL); | |
718 | ||
e767076e | 719 | ctx.encoding = wxS("UTF-8"); // default in absence of encoding="" |
27b0c286 VS |
720 | ctx.conv = NULL; |
721 | #if !wxUSE_UNICODE | |
e767076e | 722 | if ( encoding.CmpNoCase(wxS("UTF-8")) != 0 ) |
27b0c286 VS |
723 | ctx.conv = new wxCSConv(encoding); |
724 | #endif | |
538f3830 | 725 | ctx.removeWhiteOnlyNodes = (flags & wxXMLDOC_KEEP_WHITESPACE_NODES) == 0; |
6e26d6b7 | 726 | ctx.parser = parser; |
27b0c286 VS |
727 | |
728 | XML_SetUserData(parser, (void*)&ctx); | |
729 | XML_SetElementHandler(parser, StartElementHnd, EndElementHnd); | |
730 | XML_SetCharacterDataHandler(parser, TextHnd); | |
a6cf6bcf | 731 | XML_SetCdataSectionHandler(parser, StartCdataHnd, EndCdataHnd);; |
27b0c286 VS |
732 | XML_SetCommentHandler(parser, CommentHnd); |
733 | XML_SetDefaultHandler(parser, DefaultHnd); | |
734 | XML_SetUnknownEncodingHandler(parser, UnknownEncodingHnd, NULL); | |
735 | ||
736 | bool ok = true; | |
737 | do | |
738 | { | |
739 | size_t len = stream.Read(buf, BUFSIZE).LastRead(); | |
740 | done = (len < BUFSIZE); | |
741 | if (!XML_Parse(parser, buf, len, done)) | |
742 | { | |
6a8fb6bd VS |
743 | wxString error(XML_ErrorString(XML_GetErrorCode(parser)), |
744 | *wxConvCurrent); | |
27b0c286 | 745 | wxLogError(_("XML parsing error: '%s' at line %d"), |
6a8fb6bd | 746 | error.c_str(), |
936759a4 | 747 | (int)XML_GetCurrentLineNumber(parser)); |
27b0c286 VS |
748 | ok = false; |
749 | break; | |
750 | } | |
751 | } while (!done); | |
752 | ||
753 | if (ok) | |
754 | { | |
759f7272 | 755 | if (!ctx.version.empty()) |
6a8fb6bd | 756 | SetVersion(ctx.version); |
759f7272 | 757 | if (!ctx.encoding.empty()) |
6a8fb6bd | 758 | SetFileEncoding(ctx.encoding); |
27b0c286 VS |
759 | SetRoot(ctx.root); |
760 | } | |
6a8fb6bd VS |
761 | else |
762 | { | |
763 | delete ctx.root; | |
764 | } | |
27b0c286 VS |
765 | |
766 | XML_ParserFree(parser); | |
767 | #if !wxUSE_UNICODE | |
768 | if ( ctx.conv ) | |
769 | delete ctx.conv; | |
770 | #endif | |
771 | ||
772 | return ok; | |
773 | ||
774 | } | |
775 | ||
776 | ||
777 | ||
778 | //----------------------------------------------------------------------------- | |
779 | // wxXmlDocument saving routines | |
780 | //----------------------------------------------------------------------------- | |
781 | ||
e767076e VZ |
782 | // helpers for XML generation |
783 | namespace | |
784 | { | |
785 | ||
27b0c286 | 786 | // write string to output: |
e767076e VZ |
787 | bool OutputString(wxOutputStream& stream, |
788 | const wxString& str, | |
789 | wxMBConv *convMem, | |
790 | wxMBConv *convFile) | |
27b0c286 | 791 | { |
8dfd4fad | 792 | if (str.empty()) |
e767076e | 793 | return true; |
8dfd4fad | 794 | |
27b0c286 | 795 | #if wxUSE_UNICODE |
8dfd4fad | 796 | wxUnusedVar(convMem); |
a6ed2786 VZ |
797 | if ( !convFile ) |
798 | convFile = &wxConvUTF8; | |
8dfd4fad | 799 | |
a6ed2786 VZ |
800 | const wxScopedCharBuffer buf(str.mb_str(*convFile)); |
801 | if ( !buf.length() ) | |
802 | { | |
803 | // conversion failed, can't write this string in an XML file in this | |
804 | // (presumably non-UTF-8) encoding | |
e767076e | 805 | return false; |
a6ed2786 | 806 | } |
e767076e | 807 | |
a6ed2786 | 808 | stream.Write(buf, buf.length()); |
8dfd4fad VZ |
809 | #else // !wxUSE_UNICODE |
810 | if ( convFile && convMem ) | |
27b0c286 VS |
811 | { |
812 | wxString str2(str.wc_str(*convMem), *convFile); | |
e767076e | 813 | stream.Write(str2.mb_str(), str2.length()); |
27b0c286 | 814 | } |
8dfd4fad VZ |
815 | else // no conversions to do |
816 | { | |
e767076e | 817 | stream.Write(str.mb_str(), str.length()); |
8dfd4fad VZ |
818 | } |
819 | #endif // wxUSE_UNICODE/!wxUSE_UNICODE | |
e767076e VZ |
820 | |
821 | return stream.IsOk(); | |
27b0c286 VS |
822 | } |
823 | ||
926649a9 | 824 | enum EscapingMode |
8dfd4fad | 825 | { |
926649a9 VS |
826 | Escape_Text, |
827 | Escape_Attribute | |
8dfd4fad VZ |
828 | }; |
829 | ||
27b0c286 | 830 | // Same as above, but create entities first. |
926649a9 VS |
831 | // Translates '<' to "<", '>' to ">" and so on, according to the spec: |
832 | // http://www.w3.org/TR/2000/WD-xml-c14n-20000119.html#charescaping | |
833 | bool OutputEscapedString(wxOutputStream& stream, | |
834 | const wxString& str, | |
835 | wxMBConv *convMem, | |
836 | wxMBConv *convFile, | |
837 | EscapingMode mode) | |
838 | { | |
839 | wxString escaped; | |
840 | escaped.reserve(str.length()); | |
e767076e | 841 | |
926649a9 VS |
842 | for ( wxString::const_iterator i = str.begin(); i != str.end(); ++i ) |
843 | { | |
844 | const wxChar c = *i; | |
e767076e | 845 | |
926649a9 VS |
846 | switch ( c ) |
847 | { | |
848 | case wxS('<'): | |
849 | escaped.append(wxS("<")); | |
850 | break; | |
851 | case wxS('>'): | |
852 | escaped.append(wxS(">")); | |
853 | break; | |
854 | case wxS('&'): | |
855 | escaped.append(wxS("&")); | |
856 | break; | |
857 | case wxS('\r'): | |
858 | escaped.append(wxS("
")); | |
859 | break; | |
860 | default: | |
861 | if ( mode == Escape_Attribute ) | |
862 | { | |
863 | switch ( c ) | |
864 | { | |
865 | case wxS('"'): | |
866 | escaped.append(wxS(""")); | |
867 | break; | |
868 | case wxS('\t'): | |
869 | escaped.append(wxS("	")); | |
870 | break; | |
871 | case wxS('\n'): | |
872 | escaped.append(wxS("
")); | |
873 | break; | |
874 | default: | |
875 | escaped.append(c); | |
876 | } | |
877 | ||
878 | } | |
879 | else | |
880 | { | |
881 | escaped.append(c); | |
882 | } | |
27b0c286 VS |
883 | } |
884 | } | |
e767076e | 885 | |
926649a9 | 886 | return OutputString(stream, escaped, convMem, convFile); |
27b0c286 VS |
887 | } |
888 | ||
e767076e VZ |
889 | bool OutputIndentation(wxOutputStream& stream, |
890 | int indent, | |
891 | wxMBConv *convMem, | |
892 | wxMBConv *convFile) | |
27b0c286 | 893 | { |
e767076e | 894 | wxString str(wxS("\n")); |
57cc93eb | 895 | str += wxString(indent, wxS(' ')); |
e767076e | 896 | return OutputString(stream, str, convMem, convFile); |
27b0c286 VS |
897 | } |
898 | ||
e767076e VZ |
899 | bool OutputNode(wxOutputStream& stream, |
900 | wxXmlNode *node, | |
901 | int indent, | |
902 | wxMBConv *convMem, | |
903 | wxMBConv *convFile, | |
904 | int indentstep) | |
27b0c286 | 905 | { |
e767076e | 906 | bool rc; |
27b0c286 VS |
907 | switch (node->GetType()) |
908 | { | |
eac789ff | 909 | case wxXML_CDATA_SECTION_NODE: |
e767076e VZ |
910 | rc = OutputString(stream, wxS("<![CDATA["), convMem, convFile) && |
911 | OutputString(stream, node->GetContent(), convMem, convFile) && | |
912 | OutputString(stream, wxS("]]>"), convMem, convFile); | |
eac789ff | 913 | break; |
a01cbf0a | 914 | |
27b0c286 | 915 | case wxXML_TEXT_NODE: |
926649a9 VS |
916 | rc = OutputEscapedString(stream, node->GetContent(), |
917 | convMem, convFile, | |
918 | Escape_Text); | |
27b0c286 VS |
919 | break; |
920 | ||
921 | case wxXML_ELEMENT_NODE: | |
e767076e VZ |
922 | rc = OutputString(stream, wxS("<"), convMem, convFile) && |
923 | OutputString(stream, node->GetName(), convMem, convFile); | |
27b0c286 | 924 | |
e767076e | 925 | if ( rc ) |
27b0c286 | 926 | { |
e767076e VZ |
927 | for ( wxXmlAttribute *attr = node->GetAttributes(); |
928 | attr && rc; | |
929 | attr = attr->GetNext() ) | |
930 | { | |
931 | rc = OutputString(stream, | |
932 | wxS(" ") + attr->GetName() + wxS("=\""), | |
933 | convMem, convFile) && | |
926649a9 VS |
934 | OutputEscapedString(stream, attr->GetValue(), |
935 | convMem, convFile, | |
936 | Escape_Attribute) && | |
e767076e VZ |
937 | OutputString(stream, wxS("\""), convMem, convFile); |
938 | } | |
27b0c286 VS |
939 | } |
940 | ||
e767076e | 941 | if ( node->GetChildren() ) |
27b0c286 | 942 | { |
e767076e VZ |
943 | rc = OutputString(stream, wxS(">"), convMem, convFile); |
944 | ||
945 | wxXmlNode *prev = NULL; | |
946 | for ( wxXmlNode *n = node->GetChildren(); | |
947 | n && rc; | |
948 | n = n->GetNext() ) | |
27b0c286 | 949 | { |
e767076e VZ |
950 | if ( indentstep >= 0 && n->GetType() != wxXML_TEXT_NODE ) |
951 | { | |
952 | rc = OutputIndentation(stream, indent + indentstep, | |
953 | convMem, convFile); | |
954 | } | |
955 | ||
956 | if ( rc ) | |
957 | rc = OutputNode(stream, n, indent + indentstep, | |
958 | convMem, convFile, indentstep); | |
959 | ||
27b0c286 | 960 | prev = n; |
27b0c286 | 961 | } |
e767076e VZ |
962 | |
963 | if ( rc && indentstep >= 0 && | |
964 | prev && prev->GetType() != wxXML_TEXT_NODE ) | |
965 | { | |
966 | rc = OutputIndentation(stream, indent, convMem, convFile); | |
967 | } | |
968 | ||
969 | if ( rc ) | |
970 | { | |
971 | rc = OutputString(stream, wxS("</"), convMem, convFile) && | |
972 | OutputString(stream, node->GetName(), | |
973 | convMem, convFile) && | |
974 | OutputString(stream, wxS(">"), convMem, convFile); | |
975 | } | |
976 | } | |
977 | else // no children, output "<foo/>" | |
978 | { | |
979 | rc = OutputString(stream, wxS("/>"), convMem, convFile); | |
27b0c286 | 980 | } |
27b0c286 VS |
981 | break; |
982 | ||
983 | case wxXML_COMMENT_NODE: | |
e767076e VZ |
984 | rc = OutputString(stream, wxS("<!--"), convMem, convFile) && |
985 | OutputString(stream, node->GetContent(), convMem, convFile) && | |
986 | OutputString(stream, wxS("-->"), convMem, convFile); | |
27b0c286 VS |
987 | break; |
988 | ||
989 | default: | |
e767076e VZ |
990 | wxFAIL_MSG("unsupported node type"); |
991 | rc = false; | |
27b0c286 | 992 | } |
e767076e VZ |
993 | |
994 | return rc; | |
27b0c286 VS |
995 | } |
996 | ||
e767076e VZ |
997 | } // anonymous namespace |
998 | ||
538f3830 | 999 | bool wxXmlDocument::Save(wxOutputStream& stream, int indentstep) const |
27b0c286 VS |
1000 | { |
1001 | if ( !IsOk() ) | |
759f7272 | 1002 | return false; |
27b0c286 | 1003 | |
e767076e | 1004 | wxScopedPtr<wxMBConv> convMem, convFile; |
759f7272 | 1005 | |
27b0c286 | 1006 | #if wxUSE_UNICODE |
e767076e | 1007 | convFile.reset(new wxCSConv(GetFileEncoding())); |
27b0c286 | 1008 | #else |
8605f9c5 | 1009 | if ( GetFileEncoding().CmpNoCase(GetEncoding()) != 0 ) |
27b0c286 | 1010 | { |
e767076e VZ |
1011 | convFile.reset(new wxCSConv(GetFileEncoding())); |
1012 | convMem.reset(new wxCSConv(GetEncoding())); | |
8605f9c5 | 1013 | } |
e767076e | 1014 | //else: file and in-memory encodings are the same, no conversion needed |
27b0c286 VS |
1015 | #endif |
1016 | ||
e767076e VZ |
1017 | return OutputString(stream, |
1018 | wxString::Format | |
1019 | ( | |
1020 | wxS("<?xml version=\"%s\" encoding=\"%s\"?>\n"), | |
1021 | GetVersion(), GetFileEncoding() | |
1022 | ), | |
1023 | convMem.get(), | |
1024 | convFile.get()) && | |
1025 | OutputNode(stream, GetRoot(), 0, | |
1026 | convMem.get(), convFile.get(), indentstep) && | |
1027 | OutputString(stream, wxS("\n"), convMem.get(), convFile.get()); | |
27b0c286 VS |
1028 | } |
1029 | ||
1030 | #endif // wxUSE_XML |