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