]>
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 | ||
5e05df3c VS |
190 | // inserts a new node in front of 'followingNode' |
191 | bool wxXmlNode::InsertChild(wxXmlNode *child, wxXmlNode *followingNode) | |
27b0c286 | 192 | { |
5e05df3c VS |
193 | wxCHECK_MSG( child, false, "cannot insert a NULL node!" ); |
194 | wxCHECK_MSG( child->m_parent == NULL, false, "node already has a parent" ); | |
195 | wxCHECK_MSG( child->m_next == NULL, false, "node already has m_next" ); | |
196 | wxCHECK_MSG( followingNode == NULL || followingNode->GetParent() == this, | |
197 | false, | |
198 | "wxXmlNode::InsertChild - followingNode has incorrect parent" ); | |
199 | ||
200 | // this is for backward compatibility, NULL was allowed here thanks to | |
201 | // the confusion about followingNode's meaning | |
202 | if ( followingNode == NULL ) | |
203 | followingNode = m_children; | |
204 | ||
205 | if ( m_children == followingNode ) | |
fa6a8373 | 206 | { |
fa6a8373 RR |
207 | child->m_next = m_children; |
208 | m_children = child; | |
fa6a8373 | 209 | } |
27b0c286 VS |
210 | else |
211 | { | |
212 | wxXmlNode *ch = m_children; | |
5e05df3c VS |
213 | while ( ch && ch->m_next != followingNode ) |
214 | ch = ch->m_next; | |
215 | if ( !ch ) | |
216 | { | |
217 | wxFAIL_MSG( "followingNode has this node as parent, but couldn't be found among children" ); | |
218 | return false; | |
219 | } | |
220 | ||
221 | child->m_next = followingNode; | |
27b0c286 VS |
222 | ch->m_next = child; |
223 | } | |
224 | ||
225 | child->m_parent = this; | |
fa6a8373 | 226 | return true; |
27b0c286 VS |
227 | } |
228 | ||
43a302f2 VS |
229 | // inserts a new node right after 'precedingNode' |
230 | bool wxXmlNode::InsertChildAfter(wxXmlNode *child, wxXmlNode *precedingNode) | |
231 | { | |
232 | wxCHECK_MSG( child, false, "cannot insert a NULL node!" ); | |
233 | wxCHECK_MSG( child->m_parent == NULL, false, "node already has a parent" ); | |
234 | wxCHECK_MSG( child->m_next == NULL, false, "node already has m_next" ); | |
235 | wxCHECK_MSG( precedingNode == NULL || precedingNode->m_parent == this, false, | |
236 | "precedingNode has wrong parent" ); | |
237 | ||
238 | if ( precedingNode ) | |
239 | { | |
240 | child->m_next = precedingNode->m_next; | |
241 | precedingNode->m_next = child; | |
242 | } | |
243 | else // precedingNode == NULL | |
244 | { | |
245 | wxCHECK_MSG( m_children == NULL, false, | |
246 | "NULL precedingNode only makes sense when there are no children" ); | |
247 | ||
248 | child->m_next = m_children; | |
249 | m_children = child; | |
250 | } | |
251 | ||
252 | child->m_parent = this; | |
253 | return true; | |
254 | } | |
255 | ||
27b0c286 VS |
256 | bool wxXmlNode::RemoveChild(wxXmlNode *child) |
257 | { | |
258 | if (m_children == NULL) | |
759f7272 | 259 | return false; |
27b0c286 VS |
260 | else if (m_children == child) |
261 | { | |
262 | m_children = child->m_next; | |
263 | child->m_parent = NULL; | |
264 | child->m_next = NULL; | |
759f7272 | 265 | return true; |
27b0c286 VS |
266 | } |
267 | else | |
268 | { | |
269 | wxXmlNode *ch = m_children; | |
270 | while (ch->m_next) | |
271 | { | |
272 | if (ch->m_next == child) | |
273 | { | |
274 | ch->m_next = child->m_next; | |
275 | child->m_parent = NULL; | |
276 | child->m_next = NULL; | |
759f7272 | 277 | return true; |
27b0c286 VS |
278 | } |
279 | ch = ch->m_next; | |
280 | } | |
759f7272 | 281 | return false; |
27b0c286 VS |
282 | } |
283 | } | |
284 | ||
e13ce4a3 VZ |
285 | void wxXmlNode::AddAttribute(const wxString& name, const wxString& value) |
286 | { | |
287 | AddProperty(name, value); | |
288 | } | |
289 | ||
290 | void wxXmlNode::AddAttribute(wxXmlAttribute *attr) | |
291 | { | |
292 | AddProperty(attr); | |
293 | } | |
294 | ||
295 | bool wxXmlNode::DeleteAttribute(const wxString& name) | |
296 | { | |
297 | return DeleteProperty(name); | |
298 | } | |
299 | ||
27b0c286 VS |
300 | void wxXmlNode::AddProperty(const wxString& name, const wxString& value) |
301 | { | |
288b6107 | 302 | AddProperty(new wxXmlAttribute(name, value, NULL)); |
27b0c286 VS |
303 | } |
304 | ||
288b6107 | 305 | void wxXmlNode::AddProperty(wxXmlAttribute *attr) |
27b0c286 | 306 | { |
288b6107 VS |
307 | if (m_attrs == NULL) |
308 | m_attrs = attr; | |
27b0c286 VS |
309 | else |
310 | { | |
288b6107 | 311 | wxXmlAttribute *p = m_attrs; |
27b0c286 | 312 | while (p->GetNext()) p = p->GetNext(); |
288b6107 | 313 | p->SetNext(attr); |
27b0c286 VS |
314 | } |
315 | } | |
316 | ||
317 | bool wxXmlNode::DeleteProperty(const wxString& name) | |
318 | { | |
288b6107 | 319 | wxXmlAttribute *attr; |
27b0c286 | 320 | |
288b6107 | 321 | if (m_attrs == NULL) |
759f7272 | 322 | return false; |
27b0c286 | 323 | |
288b6107 | 324 | else if (m_attrs->GetName() == name) |
27b0c286 | 325 | { |
288b6107 VS |
326 | attr = m_attrs; |
327 | m_attrs = attr->GetNext(); | |
328 | attr->SetNext(NULL); | |
329 | delete attr; | |
759f7272 | 330 | return true; |
27b0c286 VS |
331 | } |
332 | ||
333 | else | |
334 | { | |
288b6107 | 335 | wxXmlAttribute *p = m_attrs; |
27b0c286 VS |
336 | while (p->GetNext()) |
337 | { | |
338 | if (p->GetNext()->GetName() == name) | |
339 | { | |
288b6107 VS |
340 | attr = p->GetNext(); |
341 | p->SetNext(attr->GetNext()); | |
342 | attr->SetNext(NULL); | |
343 | delete attr; | |
759f7272 | 344 | return true; |
27b0c286 VS |
345 | } |
346 | p = p->GetNext(); | |
347 | } | |
759f7272 | 348 | return false; |
27b0c286 VS |
349 | } |
350 | } | |
351 | ||
4c43dd90 JS |
352 | wxString wxXmlNode::GetNodeContent() const |
353 | { | |
354 | wxXmlNode *n = GetChildren(); | |
355 | ||
356 | while (n) | |
357 | { | |
358 | if (n->GetType() == wxXML_TEXT_NODE || | |
359 | n->GetType() == wxXML_CDATA_SECTION_NODE) | |
360 | return n->GetContent(); | |
361 | n = n->GetNext(); | |
362 | } | |
363 | return wxEmptyString; | |
364 | } | |
365 | ||
538f3830 VS |
366 | int wxXmlNode::GetDepth(wxXmlNode *grandparent) const |
367 | { | |
368 | const wxXmlNode *n = this; | |
369 | int ret = -1; | |
370 | ||
371 | do | |
372 | { | |
373 | ret++; | |
374 | n = n->GetParent(); | |
375 | if (n == grandparent) | |
376 | return ret; | |
377 | ||
378 | } while (n); | |
379 | ||
380 | return wxNOT_FOUND; | |
381 | } | |
382 | ||
383 | bool wxXmlNode::IsWhitespaceOnly() const | |
384 | { | |
385 | return wxIsWhiteOnly(m_content); | |
386 | } | |
387 | ||
27b0c286 VS |
388 | |
389 | ||
390 | //----------------------------------------------------------------------------- | |
391 | // wxXmlDocument | |
392 | //----------------------------------------------------------------------------- | |
393 | ||
394 | wxXmlDocument::wxXmlDocument() | |
395 | : m_version(wxT("1.0")), m_fileEncoding(wxT("utf-8")), m_root(NULL) | |
396 | { | |
397 | #if !wxUSE_UNICODE | |
398 | m_encoding = wxT("UTF-8"); | |
399 | #endif | |
400 | } | |
401 | ||
402 | wxXmlDocument::wxXmlDocument(const wxString& filename, const wxString& encoding) | |
d0468e8c | 403 | :wxObject(), m_root(NULL) |
27b0c286 VS |
404 | { |
405 | if ( !Load(filename, encoding) ) | |
406 | { | |
407 | wxDELETE(m_root); | |
408 | } | |
409 | } | |
410 | ||
411 | wxXmlDocument::wxXmlDocument(wxInputStream& stream, const wxString& encoding) | |
d0468e8c | 412 | :wxObject(), m_root(NULL) |
27b0c286 VS |
413 | { |
414 | if ( !Load(stream, encoding) ) | |
415 | { | |
416 | wxDELETE(m_root); | |
417 | } | |
418 | } | |
419 | ||
420 | wxXmlDocument::wxXmlDocument(const wxXmlDocument& doc) | |
d0468e8c | 421 | :wxObject() |
27b0c286 VS |
422 | { |
423 | DoCopy(doc); | |
424 | } | |
425 | ||
426 | wxXmlDocument& wxXmlDocument::operator=(const wxXmlDocument& doc) | |
427 | { | |
428 | wxDELETE(m_root); | |
429 | DoCopy(doc); | |
430 | return *this; | |
431 | } | |
432 | ||
433 | void wxXmlDocument::DoCopy(const wxXmlDocument& doc) | |
434 | { | |
435 | m_version = doc.m_version; | |
436 | #if !wxUSE_UNICODE | |
437 | m_encoding = doc.m_encoding; | |
438 | #endif | |
439 | m_fileEncoding = doc.m_fileEncoding; | |
e8da6b7c RR |
440 | |
441 | if (doc.m_root) | |
442 | m_root = new wxXmlNode(*doc.m_root); | |
443 | else | |
444 | m_root = NULL; | |
27b0c286 VS |
445 | } |
446 | ||
538f3830 | 447 | bool wxXmlDocument::Load(const wxString& filename, const wxString& encoding, int flags) |
27b0c286 VS |
448 | { |
449 | wxFileInputStream stream(filename); | |
97757cee VZ |
450 | if (!stream.Ok()) |
451 | return false; | |
538f3830 | 452 | return Load(stream, encoding, flags); |
27b0c286 VS |
453 | } |
454 | ||
538f3830 | 455 | bool wxXmlDocument::Save(const wxString& filename, int indentstep) const |
27b0c286 VS |
456 | { |
457 | wxFileOutputStream stream(filename); | |
97757cee VZ |
458 | if (!stream.Ok()) |
459 | return false; | |
538f3830 | 460 | return Save(stream, indentstep); |
27b0c286 VS |
461 | } |
462 | ||
463 | ||
464 | ||
465 | //----------------------------------------------------------------------------- | |
466 | // wxXmlDocument loading routines | |
467 | //----------------------------------------------------------------------------- | |
468 | ||
ddae497f VZ |
469 | // converts Expat-produced string in UTF-8 into wxString using the specified |
470 | // conv or keep in UTF-8 if conv is NULL | |
471 | static wxString CharToString(wxMBConv *conv, | |
b04edcaf | 472 | const char *s, size_t len = wxString::npos) |
27b0c286 | 473 | { |
b04edcaf | 474 | #if !wxUSE_UNICODE |
27b0c286 VS |
475 | if ( conv ) |
476 | { | |
ddae497f VZ |
477 | // there can be no embedded NULs in this string so we don't need the |
478 | // output length, it will be NUL-terminated | |
479 | const wxWCharBuffer wbuf( | |
6df6e35a | 480 | wxConvUTF8.cMB2WC(s, len == wxString::npos ? wxNO_LEN : len, NULL)); |
ddae497f | 481 | |
da452b9e | 482 | return wxString(wbuf, *conv); |
27b0c286 | 483 | } |
b04edcaf VS |
484 | // else: the string is wanted in UTF-8 |
485 | #endif // !wxUSE_UNICODE | |
486 | ||
487 | wxUnusedVar(conv); | |
488 | return wxString::FromUTF8(s, len); | |
27b0c286 VS |
489 | } |
490 | ||
538f3830 | 491 | // returns true if the given string contains only whitespaces |
352d9b89 | 492 | bool wxIsWhiteOnly(const wxString& buf) |
538f3830 | 493 | { |
352d9b89 VS |
494 | for ( wxString::const_iterator i = buf.begin(); i != buf.end(); ++i ) |
495 | { | |
496 | wxChar c = *i; | |
497 | if ( c != wxT(' ') && c != wxT('\t') && c != wxT('\n') && c != wxT('\r')) | |
538f3830 | 498 | return false; |
352d9b89 | 499 | } |
538f3830 VS |
500 | return true; |
501 | } | |
502 | ||
503 | ||
27b0c286 VS |
504 | struct wxXmlParsingContext |
505 | { | |
43a302f2 VS |
506 | wxXmlParsingContext() |
507 | : conv(NULL), | |
508 | root(NULL), | |
509 | node(NULL), | |
510 | lastChild(NULL), | |
511 | lastAsText(NULL), | |
512 | removeWhiteOnlyNodes(false) | |
513 | {} | |
514 | ||
6e26d6b7 | 515 | XML_Parser parser; |
27b0c286 VS |
516 | wxMBConv *conv; |
517 | wxXmlNode *root; | |
43a302f2 VS |
518 | wxXmlNode *node; // the node being parsed |
519 | wxXmlNode *lastChild; // the last child of "node" | |
520 | wxXmlNode *lastAsText; // the last _text_ child of "node" | |
27b0c286 VS |
521 | wxString encoding; |
522 | wxString version; | |
538f3830 | 523 | bool removeWhiteOnlyNodes; |
27b0c286 VS |
524 | }; |
525 | ||
43a302f2 VS |
526 | // checks that ctx->lastChild is in consistent state |
527 | #define ASSERT_LAST_CHILD_OK(ctx) \ | |
528 | wxASSERT( ctx->lastChild == NULL || \ | |
529 | ctx->lastChild->GetNext() == NULL ); \ | |
530 | wxASSERT( ctx->lastChild == NULL || \ | |
531 | ctx->lastChild->GetParent() == ctx->node ) | |
532 | ||
865bb325 | 533 | extern "C" { |
27b0c286 VS |
534 | static void StartElementHnd(void *userData, const char *name, const char **atts) |
535 | { | |
536 | wxXmlParsingContext *ctx = (wxXmlParsingContext*)userData; | |
6e26d6b7 VS |
537 | wxXmlNode *node = new wxXmlNode(wxXML_ELEMENT_NODE, |
538 | CharToString(ctx->conv, name), | |
539 | wxEmptyString, | |
540 | XML_GetCurrentLineNumber(ctx->parser)); | |
27b0c286 | 541 | const char **a = atts; |
6e26d6b7 | 542 | |
43a302f2 | 543 | // add node attributes |
27b0c286 VS |
544 | while (*a) |
545 | { | |
288b6107 | 546 | node->AddAttribute(CharToString(ctx->conv, a[0]), CharToString(ctx->conv, a[1])); |
27b0c286 VS |
547 | a += 2; |
548 | } | |
43a302f2 | 549 | |
27b0c286 | 550 | if (ctx->root == NULL) |
43a302f2 | 551 | { |
27b0c286 | 552 | ctx->root = node; |
43a302f2 | 553 | } |
27b0c286 | 554 | else |
43a302f2 VS |
555 | { |
556 | ASSERT_LAST_CHILD_OK(ctx); | |
557 | ctx->node->InsertChildAfter(node, ctx->lastChild); | |
558 | } | |
559 | ||
27b0c286 | 560 | ctx->lastAsText = NULL; |
43a302f2 VS |
561 | ctx->lastChild = NULL; // our new node "node" has no children yet |
562 | ||
563 | ctx->node = node; | |
27b0c286 VS |
564 | } |
565 | ||
566 | static void EndElementHnd(void *userData, const char* WXUNUSED(name)) | |
567 | { | |
568 | wxXmlParsingContext *ctx = (wxXmlParsingContext*)userData; | |
569 | ||
43a302f2 VS |
570 | // we're exiting the last children of ctx->node->GetParent() and going |
571 | // back one level up, so current value of ctx->node points to the last | |
572 | // child of ctx->node->GetParent() | |
573 | ctx->lastChild = ctx->node; | |
574 | ||
27b0c286 VS |
575 | ctx->node = ctx->node->GetParent(); |
576 | ctx->lastAsText = NULL; | |
577 | } | |
578 | ||
579 | static void TextHnd(void *userData, const char *s, int len) | |
580 | { | |
581 | wxXmlParsingContext *ctx = (wxXmlParsingContext*)userData; | |
538f3830 | 582 | wxString str = CharToString(ctx->conv, s, len); |
27b0c286 VS |
583 | |
584 | if (ctx->lastAsText) | |
585 | { | |
d6cb1287 | 586 | ctx->lastAsText->SetContent(ctx->lastAsText->GetContent() + str); |
27b0c286 VS |
587 | } |
588 | else | |
589 | { | |
538f3830 VS |
590 | bool whiteOnly = false; |
591 | if (ctx->removeWhiteOnlyNodes) | |
592 | whiteOnly = wxIsWhiteOnly(str); | |
593 | ||
27b0c286 VS |
594 | if (!whiteOnly) |
595 | { | |
43a302f2 | 596 | wxXmlNode *textnode = |
6e26d6b7 VS |
597 | new wxXmlNode(wxXML_TEXT_NODE, wxT("text"), str, |
598 | XML_GetCurrentLineNumber(ctx->parser)); | |
43a302f2 VS |
599 | |
600 | ASSERT_LAST_CHILD_OK(ctx); | |
601 | ctx->node->InsertChildAfter(textnode, ctx->lastChild); | |
602 | ctx->lastChild= ctx->lastAsText = textnode; | |
27b0c286 VS |
603 | } |
604 | } | |
27b0c286 VS |
605 | } |
606 | ||
a01cbf0a RR |
607 | static void StartCdataHnd(void *userData) |
608 | { | |
609 | wxXmlParsingContext *ctx = (wxXmlParsingContext*)userData; | |
610 | ||
43a302f2 | 611 | wxXmlNode *textnode = |
6e26d6b7 VS |
612 | new wxXmlNode(wxXML_CDATA_SECTION_NODE, wxT("cdata"), wxT(""), |
613 | XML_GetCurrentLineNumber(ctx->parser)); | |
43a302f2 VS |
614 | |
615 | ASSERT_LAST_CHILD_OK(ctx); | |
616 | ctx->node->InsertChildAfter(textnode, ctx->lastChild); | |
617 | ctx->lastChild= ctx->lastAsText = textnode; | |
a01cbf0a | 618 | } |
a01cbf0a | 619 | |
27b0c286 VS |
620 | static void CommentHnd(void *userData, const char *data) |
621 | { | |
622 | wxXmlParsingContext *ctx = (wxXmlParsingContext*)userData; | |
623 | ||
624 | if (ctx->node) | |
625 | { | |
43a302f2 | 626 | wxXmlNode *commentnode = |
6e26d6b7 VS |
627 | new wxXmlNode(wxXML_COMMENT_NODE, |
628 | wxT("comment"), CharToString(ctx->conv, data), | |
43a302f2 VS |
629 | XML_GetCurrentLineNumber(ctx->parser)); |
630 | ||
631 | ASSERT_LAST_CHILD_OK(ctx); | |
632 | ctx->node->InsertChildAfter(commentnode, ctx->lastChild); | |
633 | ctx->lastChild = commentnode; | |
27b0c286 | 634 | } |
43a302f2 VS |
635 | //else: ctx->node == NULL happens if there is a comment before |
636 | // the root element. We current don't have a way to represent | |
637 | // these in wxXmlDocument (FIXME). | |
638 | ||
27b0c286 VS |
639 | ctx->lastAsText = NULL; |
640 | } | |
641 | ||
642 | static void DefaultHnd(void *userData, const char *s, int len) | |
643 | { | |
644 | // XML header: | |
645 | if (len > 6 && memcmp(s, "<?xml ", 6) == 0) | |
646 | { | |
647 | wxXmlParsingContext *ctx = (wxXmlParsingContext*)userData; | |
648 | ||
649 | wxString buf = CharToString(ctx->conv, s, (size_t)len); | |
650 | int pos; | |
651 | pos = buf.Find(wxT("encoding=")); | |
652 | if (pos != wxNOT_FOUND) | |
653 | ctx->encoding = buf.Mid(pos + 10).BeforeFirst(buf[(size_t)pos+9]); | |
654 | pos = buf.Find(wxT("version=")); | |
655 | if (pos != wxNOT_FOUND) | |
656 | ctx->version = buf.Mid(pos + 9).BeforeFirst(buf[(size_t)pos+8]); | |
657 | } | |
658 | } | |
659 | ||
660 | static int UnknownEncodingHnd(void * WXUNUSED(encodingHandlerData), | |
661 | const XML_Char *name, XML_Encoding *info) | |
662 | { | |
663 | // We must build conversion table for expat. The easiest way to do so | |
664 | // is to let wxCSConv convert as string containing all characters to | |
665 | // wide character representation: | |
86501081 | 666 | wxCSConv conv(name); |
27b0c286 VS |
667 | char mbBuf[2]; |
668 | wchar_t wcBuf[10]; | |
669 | size_t i; | |
670 | ||
671 | mbBuf[1] = 0; | |
672 | info->map[0] = 0; | |
673 | for (i = 0; i < 255; i++) | |
674 | { | |
675 | mbBuf[0] = (char)(i+1); | |
676 | if (conv.MB2WC(wcBuf, mbBuf, 2) == (size_t)-1) | |
677 | { | |
678 | // invalid/undefined byte in the encoding: | |
679 | info->map[i+1] = -1; | |
680 | } | |
681 | info->map[i+1] = (int)wcBuf[0]; | |
682 | } | |
42841dfc | 683 | |
27b0c286 VS |
684 | info->data = NULL; |
685 | info->convert = NULL; | |
686 | info->release = NULL; | |
687 | ||
688 | return 1; | |
689 | } | |
26296ac9 VS |
690 | |
691 | } // extern "C" | |
27b0c286 | 692 | |
538f3830 | 693 | bool wxXmlDocument::Load(wxInputStream& stream, const wxString& encoding, int flags) |
27b0c286 VS |
694 | { |
695 | #if wxUSE_UNICODE | |
696 | (void)encoding; | |
697 | #else | |
698 | m_encoding = encoding; | |
699 | #endif | |
700 | ||
701 | const size_t BUFSIZE = 1024; | |
702 | char buf[BUFSIZE]; | |
703 | wxXmlParsingContext ctx; | |
704 | bool done; | |
705 | XML_Parser parser = XML_ParserCreate(NULL); | |
706 | ||
27b0c286 VS |
707 | ctx.encoding = wxT("UTF-8"); // default in absence of encoding="" |
708 | ctx.conv = NULL; | |
709 | #if !wxUSE_UNICODE | |
8605f9c5 | 710 | if ( encoding.CmpNoCase(wxT("UTF-8")) != 0 ) |
27b0c286 VS |
711 | ctx.conv = new wxCSConv(encoding); |
712 | #endif | |
538f3830 | 713 | ctx.removeWhiteOnlyNodes = (flags & wxXMLDOC_KEEP_WHITESPACE_NODES) == 0; |
6e26d6b7 | 714 | ctx.parser = parser; |
27b0c286 VS |
715 | |
716 | XML_SetUserData(parser, (void*)&ctx); | |
717 | XML_SetElementHandler(parser, StartElementHnd, EndElementHnd); | |
718 | XML_SetCharacterDataHandler(parser, TextHnd); | |
d6cb1287 | 719 | XML_SetStartCdataSectionHandler(parser, StartCdataHnd); |
27b0c286 VS |
720 | XML_SetCommentHandler(parser, CommentHnd); |
721 | XML_SetDefaultHandler(parser, DefaultHnd); | |
722 | XML_SetUnknownEncodingHandler(parser, UnknownEncodingHnd, NULL); | |
723 | ||
724 | bool ok = true; | |
725 | do | |
726 | { | |
727 | size_t len = stream.Read(buf, BUFSIZE).LastRead(); | |
728 | done = (len < BUFSIZE); | |
729 | if (!XML_Parse(parser, buf, len, done)) | |
730 | { | |
6a8fb6bd VS |
731 | wxString error(XML_ErrorString(XML_GetErrorCode(parser)), |
732 | *wxConvCurrent); | |
27b0c286 | 733 | wxLogError(_("XML parsing error: '%s' at line %d"), |
6a8fb6bd | 734 | error.c_str(), |
27b0c286 VS |
735 | XML_GetCurrentLineNumber(parser)); |
736 | ok = false; | |
737 | break; | |
738 | } | |
739 | } while (!done); | |
740 | ||
741 | if (ok) | |
742 | { | |
759f7272 | 743 | if (!ctx.version.empty()) |
6a8fb6bd | 744 | SetVersion(ctx.version); |
759f7272 | 745 | if (!ctx.encoding.empty()) |
6a8fb6bd | 746 | SetFileEncoding(ctx.encoding); |
27b0c286 VS |
747 | SetRoot(ctx.root); |
748 | } | |
6a8fb6bd VS |
749 | else |
750 | { | |
751 | delete ctx.root; | |
752 | } | |
27b0c286 VS |
753 | |
754 | XML_ParserFree(parser); | |
755 | #if !wxUSE_UNICODE | |
756 | if ( ctx.conv ) | |
757 | delete ctx.conv; | |
758 | #endif | |
759 | ||
760 | return ok; | |
761 | ||
762 | } | |
763 | ||
764 | ||
765 | ||
766 | //----------------------------------------------------------------------------- | |
767 | // wxXmlDocument saving routines | |
768 | //----------------------------------------------------------------------------- | |
769 | ||
770 | // write string to output: | |
771 | inline static void OutputString(wxOutputStream& stream, const wxString& str, | |
8dfd4fad VZ |
772 | wxMBConv *convMem = NULL, |
773 | wxMBConv *convFile = NULL) | |
27b0c286 | 774 | { |
8dfd4fad VZ |
775 | if (str.empty()) |
776 | return; | |
777 | ||
27b0c286 | 778 | #if wxUSE_UNICODE |
8dfd4fad VZ |
779 | wxUnusedVar(convMem); |
780 | ||
6a8fb6bd | 781 | const wxWX2MBbuf buf(str.mb_str(*(convFile ? convFile : &wxConvUTF8))); |
27b0c286 | 782 | stream.Write((const char*)buf, strlen((const char*)buf)); |
8dfd4fad VZ |
783 | #else // !wxUSE_UNICODE |
784 | if ( convFile && convMem ) | |
27b0c286 VS |
785 | { |
786 | wxString str2(str.wc_str(*convMem), *convFile); | |
787 | stream.Write(str2.mb_str(), str2.Len()); | |
788 | } | |
8dfd4fad VZ |
789 | else // no conversions to do |
790 | { | |
791 | stream.Write(str.mb_str(), str.Len()); | |
792 | } | |
793 | #endif // wxUSE_UNICODE/!wxUSE_UNICODE | |
27b0c286 VS |
794 | } |
795 | ||
8dfd4fad VZ |
796 | // flags for OutputStringEnt() |
797 | enum | |
798 | { | |
799 | XML_ESCAPE_QUOTES = 1 | |
800 | }; | |
801 | ||
27b0c286 VS |
802 | // Same as above, but create entities first. |
803 | // Translates '<' to "<", '>' to ">" and '&' to "&" | |
804 | static void OutputStringEnt(wxOutputStream& stream, const wxString& str, | |
8dfd4fad VZ |
805 | wxMBConv *convMem = NULL, |
806 | wxMBConv *convFile = NULL, | |
807 | int flags = 0) | |
27b0c286 VS |
808 | { |
809 | wxString buf; | |
810 | size_t i, last, len; | |
811 | wxChar c; | |
812 | ||
813 | len = str.Len(); | |
814 | last = 0; | |
815 | for (i = 0; i < len; i++) | |
816 | { | |
817 | c = str.GetChar(i); | |
818 | if (c == wxT('<') || c == wxT('>') || | |
ebf0700d | 819 | (c == wxT('&') && str.Mid(i+1, 4) != wxT("amp;")) || |
8dfd4fad | 820 | ((flags & XML_ESCAPE_QUOTES) && c == wxT('"'))) |
27b0c286 VS |
821 | { |
822 | OutputString(stream, str.Mid(last, i - last), convMem, convFile); | |
823 | switch (c) | |
824 | { | |
825 | case wxT('<'): | |
8dfd4fad | 826 | OutputString(stream, wxT("<")); |
27b0c286 VS |
827 | break; |
828 | case wxT('>'): | |
8dfd4fad | 829 | OutputString(stream, wxT(">")); |
27b0c286 VS |
830 | break; |
831 | case wxT('&'): | |
8dfd4fad | 832 | OutputString(stream, wxT("&")); |
27b0c286 | 833 | break; |
ebf0700d | 834 | case wxT('"'): |
8dfd4fad VZ |
835 | OutputString(stream, wxT(""")); |
836 | break; | |
837 | default: | |
ebf0700d | 838 | break; |
27b0c286 VS |
839 | } |
840 | last = i + 1; | |
841 | } | |
842 | } | |
843 | OutputString(stream, str.Mid(last, i - last), convMem, convFile); | |
844 | } | |
845 | ||
846 | inline static void OutputIndentation(wxOutputStream& stream, int indent) | |
847 | { | |
848 | wxString str = wxT("\n"); | |
849 | for (int i = 0; i < indent; i++) | |
850 | str << wxT(' ') << wxT(' '); | |
8dfd4fad | 851 | OutputString(stream, str); |
27b0c286 VS |
852 | } |
853 | ||
854 | static void OutputNode(wxOutputStream& stream, wxXmlNode *node, int indent, | |
538f3830 | 855 | wxMBConv *convMem, wxMBConv *convFile, int indentstep) |
27b0c286 VS |
856 | { |
857 | wxXmlNode *n, *prev; | |
288b6107 | 858 | wxXmlAttribute *attr; |
27b0c286 VS |
859 | |
860 | switch (node->GetType()) | |
861 | { | |
eac789ff VS |
862 | case wxXML_CDATA_SECTION_NODE: |
863 | OutputString( stream, wxT("<![CDATA[")); | |
864 | OutputString( stream, node->GetContent() ); | |
865 | OutputString( stream, wxT("]]>") ); | |
866 | break; | |
a01cbf0a | 867 | |
27b0c286 VS |
868 | case wxXML_TEXT_NODE: |
869 | OutputStringEnt(stream, node->GetContent(), convMem, convFile); | |
870 | break; | |
871 | ||
872 | case wxXML_ELEMENT_NODE: | |
8dfd4fad VZ |
873 | OutputString(stream, wxT("<")); |
874 | OutputString(stream, node->GetName()); | |
27b0c286 | 875 | |
288b6107 VS |
876 | attr = node->GetAttributes(); |
877 | while (attr) | |
27b0c286 | 878 | { |
288b6107 VS |
879 | OutputString(stream, wxT(" ") + attr->GetName() + wxT("=\"")); |
880 | OutputStringEnt(stream, attr->GetValue(), convMem, convFile, | |
8dfd4fad VZ |
881 | XML_ESCAPE_QUOTES); |
882 | OutputString(stream, wxT("\"")); | |
288b6107 | 883 | attr = attr->GetNext(); |
27b0c286 VS |
884 | } |
885 | ||
886 | if (node->GetChildren()) | |
887 | { | |
8dfd4fad | 888 | OutputString(stream, wxT(">")); |
27b0c286 VS |
889 | prev = NULL; |
890 | n = node->GetChildren(); | |
891 | while (n) | |
892 | { | |
538f3830 VS |
893 | if (indentstep >= 0 && n && n->GetType() != wxXML_TEXT_NODE) |
894 | OutputIndentation(stream, indent + indentstep); | |
895 | OutputNode(stream, n, indent + indentstep, convMem, convFile, indentstep); | |
27b0c286 VS |
896 | prev = n; |
897 | n = n->GetNext(); | |
898 | } | |
538f3830 | 899 | if (indentstep >= 0 && prev && prev->GetType() != wxXML_TEXT_NODE) |
27b0c286 | 900 | OutputIndentation(stream, indent); |
8dfd4fad VZ |
901 | OutputString(stream, wxT("</")); |
902 | OutputString(stream, node->GetName()); | |
903 | OutputString(stream, wxT(">")); | |
27b0c286 VS |
904 | } |
905 | else | |
8dfd4fad | 906 | OutputString(stream, wxT("/>")); |
27b0c286 VS |
907 | break; |
908 | ||
909 | case wxXML_COMMENT_NODE: | |
8dfd4fad | 910 | OutputString(stream, wxT("<!--")); |
27b0c286 | 911 | OutputString(stream, node->GetContent(), convMem, convFile); |
8dfd4fad | 912 | OutputString(stream, wxT("-->")); |
27b0c286 VS |
913 | break; |
914 | ||
915 | default: | |
916 | wxFAIL_MSG(wxT("unsupported node type")); | |
917 | } | |
918 | } | |
919 | ||
538f3830 | 920 | bool wxXmlDocument::Save(wxOutputStream& stream, int indentstep) const |
27b0c286 VS |
921 | { |
922 | if ( !IsOk() ) | |
759f7272 | 923 | return false; |
27b0c286 VS |
924 | |
925 | wxString s; | |
926 | ||
8605f9c5 VZ |
927 | wxMBConv *convMem = NULL, |
928 | *convFile; | |
759f7272 | 929 | |
27b0c286 | 930 | #if wxUSE_UNICODE |
8605f9c5 VZ |
931 | convFile = new wxCSConv(GetFileEncoding()); |
932 | convMem = NULL; | |
27b0c286 | 933 | #else |
8605f9c5 | 934 | if ( GetFileEncoding().CmpNoCase(GetEncoding()) != 0 ) |
27b0c286 VS |
935 | { |
936 | convFile = new wxCSConv(GetFileEncoding()); | |
937 | convMem = new wxCSConv(GetEncoding()); | |
938 | } | |
8605f9c5 VZ |
939 | else // file and in-memory encodings are the same, no conversion needed |
940 | { | |
941 | convFile = | |
942 | convMem = NULL; | |
943 | } | |
27b0c286 VS |
944 | #endif |
945 | ||
946 | s.Printf(wxT("<?xml version=\"%s\" encoding=\"%s\"?>\n"), | |
947 | GetVersion().c_str(), GetFileEncoding().c_str()); | |
8dfd4fad | 948 | OutputString(stream, s); |
27b0c286 | 949 | |
538f3830 | 950 | OutputNode(stream, GetRoot(), 0, convMem, convFile, indentstep); |
8dfd4fad | 951 | OutputString(stream, wxT("\n")); |
27b0c286 | 952 | |
8605f9c5 VZ |
953 | delete convFile; |
954 | delete convMem; | |
27b0c286 | 955 | |
759f7272 | 956 | return true; |
27b0c286 VS |
957 | } |
958 | ||
959 | #endif // wxUSE_XML |