]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/xml/xml.cpp | |
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 | |
8 | // Licence: wxWindows licence | |
9 | ///////////////////////////////////////////////////////////////////////////// | |
10 | ||
11 | // For compilers that support precompilation, includes "wx.h". | |
12 | #include "wx/wxprec.h" | |
13 | ||
14 | #ifdef __BORLANDC__ | |
15 | #pragma hdrstop | |
16 | #endif | |
17 | ||
18 | #if wxUSE_XML | |
19 | ||
20 | #include "wx/xml/xml.h" | |
21 | ||
22 | #ifndef WX_PRECOMP | |
23 | #include "wx/intl.h" | |
24 | #include "wx/log.h" | |
25 | #include "wx/app.h" | |
26 | #endif | |
27 | ||
28 | #include "wx/wfstream.h" | |
29 | #include "wx/datstrm.h" | |
30 | #include "wx/zstream.h" | |
31 | #include "wx/strconv.h" | |
32 | ||
33 | #include "expat.h" // from Expat | |
34 | ||
35 | // DLL options compatibility check: | |
36 | WX_CHECK_BUILD_OPTIONS("wxXML") | |
37 | ||
38 | ||
39 | IMPLEMENT_CLASS(wxXmlDocument, wxObject) | |
40 | ||
41 | ||
42 | // a private utility used by wxXML | |
43 | static bool wxIsWhiteOnly(const wxString& buf); | |
44 | ||
45 | ||
46 | //----------------------------------------------------------------------------- | |
47 | // wxXmlNode | |
48 | //----------------------------------------------------------------------------- | |
49 | ||
50 | wxXmlNode::wxXmlNode(wxXmlNode *parent,wxXmlNodeType type, | |
51 | const wxString& name, const wxString& content, | |
52 | wxXmlAttribute *attrs, wxXmlNode *next, int lineNo) | |
53 | : m_type(type), m_name(name), m_content(content), | |
54 | m_attrs(attrs), m_parent(parent), | |
55 | m_children(NULL), m_next(next), | |
56 | m_lineNo(lineNo) | |
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, | |
71 | const wxString& content, | |
72 | int lineNo) | |
73 | : m_type(type), m_name(name), m_content(content), | |
74 | m_attrs(NULL), m_parent(NULL), | |
75 | m_children(NULL), m_next(NULL), | |
76 | m_lineNo(lineNo) | |
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 | ||
95 | wxXmlAttribute *p, *p2; | |
96 | for (p = m_attrs; p; p = p2) | |
97 | { | |
98 | p2 = p->GetNext(); | |
99 | delete p; | |
100 | } | |
101 | } | |
102 | ||
103 | wxXmlNode& wxXmlNode::operator=(const wxXmlNode& node) | |
104 | { | |
105 | wxDELETE(m_attrs); | |
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; | |
116 | m_lineNo = node.m_lineNo; | |
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 | ||
126 | m_attrs = NULL; | |
127 | wxXmlAttribute *p = node.m_attrs; | |
128 | while (p) | |
129 | { | |
130 | AddAttribute(p->GetName(), p->GetValue()); | |
131 | p = p->GetNext(); | |
132 | } | |
133 | } | |
134 | ||
135 | bool wxXmlNode::HasAttribute(const wxString& attrName) const | |
136 | { | |
137 | wxXmlAttribute *attr = GetAttributes(); | |
138 | ||
139 | while (attr) | |
140 | { | |
141 | if (attr->GetName() == attrName) return true; | |
142 | attr = attr->GetNext(); | |
143 | } | |
144 | ||
145 | return false; | |
146 | } | |
147 | ||
148 | bool wxXmlNode::GetAttribute(const wxString& attrName, wxString *value) const | |
149 | { | |
150 | wxCHECK_MSG( value, false, "value argument must not be NULL" ); | |
151 | ||
152 | wxXmlAttribute *attr = GetAttributes(); | |
153 | ||
154 | while (attr) | |
155 | { | |
156 | if (attr->GetName() == attrName) | |
157 | { | |
158 | *value = attr->GetValue(); | |
159 | return true; | |
160 | } | |
161 | attr = attr->GetNext(); | |
162 | } | |
163 | ||
164 | return false; | |
165 | } | |
166 | ||
167 | wxString wxXmlNode::GetAttribute(const wxString& attrName, const wxString& defaultVal) const | |
168 | { | |
169 | wxString tmp; | |
170 | if (GetAttribute(attrName, &tmp)) | |
171 | return tmp; | |
172 | ||
173 | return defaultVal; | |
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 | ||
190 | // inserts a new node in front of 'followingNode' | |
191 | bool wxXmlNode::InsertChild(wxXmlNode *child, wxXmlNode *followingNode) | |
192 | { | |
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 ) | |
206 | { | |
207 | child->m_next = m_children; | |
208 | m_children = child; | |
209 | } | |
210 | else | |
211 | { | |
212 | wxXmlNode *ch = m_children; | |
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; | |
222 | ch->m_next = child; | |
223 | } | |
224 | ||
225 | child->m_parent = this; | |
226 | return true; | |
227 | } | |
228 | ||
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 | ||
256 | bool wxXmlNode::RemoveChild(wxXmlNode *child) | |
257 | { | |
258 | if (m_children == NULL) | |
259 | return false; | |
260 | else if (m_children == child) | |
261 | { | |
262 | m_children = child->m_next; | |
263 | child->m_parent = NULL; | |
264 | child->m_next = NULL; | |
265 | return true; | |
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; | |
277 | return true; | |
278 | } | |
279 | ch = ch->m_next; | |
280 | } | |
281 | return false; | |
282 | } | |
283 | } | |
284 | ||
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 | ||
300 | void wxXmlNode::AddProperty(const wxString& name, const wxString& value) | |
301 | { | |
302 | AddProperty(new wxXmlAttribute(name, value, NULL)); | |
303 | } | |
304 | ||
305 | void wxXmlNode::AddProperty(wxXmlAttribute *attr) | |
306 | { | |
307 | if (m_attrs == NULL) | |
308 | m_attrs = attr; | |
309 | else | |
310 | { | |
311 | wxXmlAttribute *p = m_attrs; | |
312 | while (p->GetNext()) p = p->GetNext(); | |
313 | p->SetNext(attr); | |
314 | } | |
315 | } | |
316 | ||
317 | bool wxXmlNode::DeleteProperty(const wxString& name) | |
318 | { | |
319 | wxXmlAttribute *attr; | |
320 | ||
321 | if (m_attrs == NULL) | |
322 | return false; | |
323 | ||
324 | else if (m_attrs->GetName() == name) | |
325 | { | |
326 | attr = m_attrs; | |
327 | m_attrs = attr->GetNext(); | |
328 | attr->SetNext(NULL); | |
329 | delete attr; | |
330 | return true; | |
331 | } | |
332 | ||
333 | else | |
334 | { | |
335 | wxXmlAttribute *p = m_attrs; | |
336 | while (p->GetNext()) | |
337 | { | |
338 | if (p->GetNext()->GetName() == name) | |
339 | { | |
340 | attr = p->GetNext(); | |
341 | p->SetNext(attr->GetNext()); | |
342 | attr->SetNext(NULL); | |
343 | delete attr; | |
344 | return true; | |
345 | } | |
346 | p = p->GetNext(); | |
347 | } | |
348 | return false; | |
349 | } | |
350 | } | |
351 | ||
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 | ||
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 | ||
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) | |
403 | :wxObject(), m_root(NULL) | |
404 | { | |
405 | if ( !Load(filename, encoding) ) | |
406 | { | |
407 | wxDELETE(m_root); | |
408 | } | |
409 | } | |
410 | ||
411 | wxXmlDocument::wxXmlDocument(wxInputStream& stream, const wxString& encoding) | |
412 | :wxObject(), m_root(NULL) | |
413 | { | |
414 | if ( !Load(stream, encoding) ) | |
415 | { | |
416 | wxDELETE(m_root); | |
417 | } | |
418 | } | |
419 | ||
420 | wxXmlDocument::wxXmlDocument(const wxXmlDocument& doc) | |
421 | :wxObject() | |
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; | |
440 | ||
441 | if (doc.m_root) | |
442 | m_root = new wxXmlNode(*doc.m_root); | |
443 | else | |
444 | m_root = NULL; | |
445 | } | |
446 | ||
447 | bool wxXmlDocument::Load(const wxString& filename, const wxString& encoding, int flags) | |
448 | { | |
449 | wxFileInputStream stream(filename); | |
450 | if (!stream.Ok()) | |
451 | return false; | |
452 | return Load(stream, encoding, flags); | |
453 | } | |
454 | ||
455 | bool wxXmlDocument::Save(const wxString& filename, int indentstep) const | |
456 | { | |
457 | wxFileOutputStream stream(filename); | |
458 | if (!stream.Ok()) | |
459 | return false; | |
460 | return Save(stream, indentstep); | |
461 | } | |
462 | ||
463 | ||
464 | ||
465 | //----------------------------------------------------------------------------- | |
466 | // wxXmlDocument loading routines | |
467 | //----------------------------------------------------------------------------- | |
468 | ||
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, | |
472 | const char *s, size_t len = wxString::npos) | |
473 | { | |
474 | #if !wxUSE_UNICODE | |
475 | if ( conv ) | |
476 | { | |
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( | |
480 | wxConvUTF8.cMB2WC(s, len == wxString::npos ? wxNO_LEN : len, NULL)); | |
481 | ||
482 | return wxString(wbuf, *conv); | |
483 | } | |
484 | // else: the string is wanted in UTF-8 | |
485 | #endif // !wxUSE_UNICODE | |
486 | ||
487 | wxUnusedVar(conv); | |
488 | return wxString::FromUTF8Unchecked(s, len); | |
489 | } | |
490 | ||
491 | // returns true if the given string contains only whitespaces | |
492 | bool wxIsWhiteOnly(const wxString& buf) | |
493 | { | |
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')) | |
498 | return false; | |
499 | } | |
500 | return true; | |
501 | } | |
502 | ||
503 | ||
504 | struct wxXmlParsingContext | |
505 | { | |
506 | wxXmlParsingContext() | |
507 | : conv(NULL), | |
508 | root(NULL), | |
509 | node(NULL), | |
510 | lastChild(NULL), | |
511 | lastAsText(NULL), | |
512 | removeWhiteOnlyNodes(false) | |
513 | {} | |
514 | ||
515 | XML_Parser parser; | |
516 | wxMBConv *conv; | |
517 | wxXmlNode *root; | |
518 | wxXmlNode *node; // the node being parsed | |
519 | wxXmlNode *lastChild; // the last child of "node" | |
520 | wxXmlNode *lastAsText; // the last _text_ child of "node" | |
521 | wxString encoding; | |
522 | wxString version; | |
523 | bool removeWhiteOnlyNodes; | |
524 | }; | |
525 | ||
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 | ||
533 | extern "C" { | |
534 | static void StartElementHnd(void *userData, const char *name, const char **atts) | |
535 | { | |
536 | wxXmlParsingContext *ctx = (wxXmlParsingContext*)userData; | |
537 | wxXmlNode *node = new wxXmlNode(wxXML_ELEMENT_NODE, | |
538 | CharToString(ctx->conv, name), | |
539 | wxEmptyString, | |
540 | XML_GetCurrentLineNumber(ctx->parser)); | |
541 | const char **a = atts; | |
542 | ||
543 | // add node attributes | |
544 | while (*a) | |
545 | { | |
546 | node->AddAttribute(CharToString(ctx->conv, a[0]), CharToString(ctx->conv, a[1])); | |
547 | a += 2; | |
548 | } | |
549 | ||
550 | if (ctx->root == NULL) | |
551 | { | |
552 | ctx->root = node; | |
553 | } | |
554 | else | |
555 | { | |
556 | ASSERT_LAST_CHILD_OK(ctx); | |
557 | ctx->node->InsertChildAfter(node, ctx->lastChild); | |
558 | } | |
559 | ||
560 | ctx->lastAsText = NULL; | |
561 | ctx->lastChild = NULL; // our new node "node" has no children yet | |
562 | ||
563 | ctx->node = node; | |
564 | } | |
565 | ||
566 | static void EndElementHnd(void *userData, const char* WXUNUSED(name)) | |
567 | { | |
568 | wxXmlParsingContext *ctx = (wxXmlParsingContext*)userData; | |
569 | ||
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 | ||
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; | |
582 | wxString str = CharToString(ctx->conv, s, len); | |
583 | ||
584 | if (ctx->lastAsText) | |
585 | { | |
586 | ctx->lastAsText->SetContent(ctx->lastAsText->GetContent() + str); | |
587 | } | |
588 | else | |
589 | { | |
590 | bool whiteOnly = false; | |
591 | if (ctx->removeWhiteOnlyNodes) | |
592 | whiteOnly = wxIsWhiteOnly(str); | |
593 | ||
594 | if (!whiteOnly) | |
595 | { | |
596 | wxXmlNode *textnode = | |
597 | new wxXmlNode(wxXML_TEXT_NODE, wxT("text"), str, | |
598 | XML_GetCurrentLineNumber(ctx->parser)); | |
599 | ||
600 | ASSERT_LAST_CHILD_OK(ctx); | |
601 | ctx->node->InsertChildAfter(textnode, ctx->lastChild); | |
602 | ctx->lastChild= ctx->lastAsText = textnode; | |
603 | } | |
604 | } | |
605 | } | |
606 | ||
607 | static void StartCdataHnd(void *userData) | |
608 | { | |
609 | wxXmlParsingContext *ctx = (wxXmlParsingContext*)userData; | |
610 | ||
611 | wxXmlNode *textnode = | |
612 | new wxXmlNode(wxXML_CDATA_SECTION_NODE, wxT("cdata"), wxT(""), | |
613 | XML_GetCurrentLineNumber(ctx->parser)); | |
614 | ||
615 | ASSERT_LAST_CHILD_OK(ctx); | |
616 | ctx->node->InsertChildAfter(textnode, ctx->lastChild); | |
617 | ctx->lastChild= ctx->lastAsText = textnode; | |
618 | } | |
619 | ||
620 | static void CommentHnd(void *userData, const char *data) | |
621 | { | |
622 | wxXmlParsingContext *ctx = (wxXmlParsingContext*)userData; | |
623 | ||
624 | if (ctx->node) | |
625 | { | |
626 | wxXmlNode *commentnode = | |
627 | new wxXmlNode(wxXML_COMMENT_NODE, | |
628 | wxT("comment"), CharToString(ctx->conv, data), | |
629 | XML_GetCurrentLineNumber(ctx->parser)); | |
630 | ||
631 | ASSERT_LAST_CHILD_OK(ctx); | |
632 | ctx->node->InsertChildAfter(commentnode, ctx->lastChild); | |
633 | ctx->lastChild = commentnode; | |
634 | } | |
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 | ||
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: | |
666 | wxCSConv conv(name); | |
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 | } | |
683 | ||
684 | info->data = NULL; | |
685 | info->convert = NULL; | |
686 | info->release = NULL; | |
687 | ||
688 | return 1; | |
689 | } | |
690 | ||
691 | } // extern "C" | |
692 | ||
693 | bool wxXmlDocument::Load(wxInputStream& stream, const wxString& encoding, int flags) | |
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 | ||
707 | ctx.encoding = wxT("UTF-8"); // default in absence of encoding="" | |
708 | ctx.conv = NULL; | |
709 | #if !wxUSE_UNICODE | |
710 | if ( encoding.CmpNoCase(wxT("UTF-8")) != 0 ) | |
711 | ctx.conv = new wxCSConv(encoding); | |
712 | #endif | |
713 | ctx.removeWhiteOnlyNodes = (flags & wxXMLDOC_KEEP_WHITESPACE_NODES) == 0; | |
714 | ctx.parser = parser; | |
715 | ||
716 | XML_SetUserData(parser, (void*)&ctx); | |
717 | XML_SetElementHandler(parser, StartElementHnd, EndElementHnd); | |
718 | XML_SetCharacterDataHandler(parser, TextHnd); | |
719 | XML_SetStartCdataSectionHandler(parser, StartCdataHnd); | |
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 | { | |
731 | wxString error(XML_ErrorString(XML_GetErrorCode(parser)), | |
732 | *wxConvCurrent); | |
733 | wxLogError(_("XML parsing error: '%s' at line %d"), | |
734 | error.c_str(), | |
735 | XML_GetCurrentLineNumber(parser)); | |
736 | ok = false; | |
737 | break; | |
738 | } | |
739 | } while (!done); | |
740 | ||
741 | if (ok) | |
742 | { | |
743 | if (!ctx.version.empty()) | |
744 | SetVersion(ctx.version); | |
745 | if (!ctx.encoding.empty()) | |
746 | SetFileEncoding(ctx.encoding); | |
747 | SetRoot(ctx.root); | |
748 | } | |
749 | else | |
750 | { | |
751 | delete ctx.root; | |
752 | } | |
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, | |
772 | wxMBConv *convMem = NULL, | |
773 | wxMBConv *convFile = NULL) | |
774 | { | |
775 | if (str.empty()) | |
776 | return; | |
777 | ||
778 | #if wxUSE_UNICODE | |
779 | wxUnusedVar(convMem); | |
780 | ||
781 | const wxWX2MBbuf buf(str.mb_str(*(convFile ? convFile : &wxConvUTF8))); | |
782 | stream.Write((const char*)buf, strlen((const char*)buf)); | |
783 | #else // !wxUSE_UNICODE | |
784 | if ( convFile && convMem ) | |
785 | { | |
786 | wxString str2(str.wc_str(*convMem), *convFile); | |
787 | stream.Write(str2.mb_str(), str2.Len()); | |
788 | } | |
789 | else // no conversions to do | |
790 | { | |
791 | stream.Write(str.mb_str(), str.Len()); | |
792 | } | |
793 | #endif // wxUSE_UNICODE/!wxUSE_UNICODE | |
794 | } | |
795 | ||
796 | // flags for OutputStringEnt() | |
797 | enum | |
798 | { | |
799 | XML_ESCAPE_QUOTES = 1 | |
800 | }; | |
801 | ||
802 | // Same as above, but create entities first. | |
803 | // Translates '<' to "<", '>' to ">" and '&' to "&" | |
804 | static void OutputStringEnt(wxOutputStream& stream, const wxString& str, | |
805 | wxMBConv *convMem = NULL, | |
806 | wxMBConv *convFile = NULL, | |
807 | int flags = 0) | |
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('>') || | |
819 | (c == wxT('&') && str.Mid(i+1, 4) != wxT("amp;")) || | |
820 | ((flags & XML_ESCAPE_QUOTES) && c == wxT('"'))) | |
821 | { | |
822 | OutputString(stream, str.Mid(last, i - last), convMem, convFile); | |
823 | switch (c) | |
824 | { | |
825 | case wxT('<'): | |
826 | OutputString(stream, wxT("<")); | |
827 | break; | |
828 | case wxT('>'): | |
829 | OutputString(stream, wxT(">")); | |
830 | break; | |
831 | case wxT('&'): | |
832 | OutputString(stream, wxT("&")); | |
833 | break; | |
834 | case wxT('"'): | |
835 | OutputString(stream, wxT(""")); | |
836 | break; | |
837 | default: | |
838 | break; | |
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(' '); | |
851 | OutputString(stream, str); | |
852 | } | |
853 | ||
854 | static void OutputNode(wxOutputStream& stream, wxXmlNode *node, int indent, | |
855 | wxMBConv *convMem, wxMBConv *convFile, int indentstep) | |
856 | { | |
857 | wxXmlNode *n, *prev; | |
858 | wxXmlAttribute *attr; | |
859 | ||
860 | switch (node->GetType()) | |
861 | { | |
862 | case wxXML_CDATA_SECTION_NODE: | |
863 | OutputString( stream, wxT("<![CDATA[")); | |
864 | OutputString( stream, node->GetContent() ); | |
865 | OutputString( stream, wxT("]]>") ); | |
866 | break; | |
867 | ||
868 | case wxXML_TEXT_NODE: | |
869 | OutputStringEnt(stream, node->GetContent(), convMem, convFile); | |
870 | break; | |
871 | ||
872 | case wxXML_ELEMENT_NODE: | |
873 | OutputString(stream, wxT("<")); | |
874 | OutputString(stream, node->GetName()); | |
875 | ||
876 | attr = node->GetAttributes(); | |
877 | while (attr) | |
878 | { | |
879 | OutputString(stream, wxT(" ") + attr->GetName() + wxT("=\"")); | |
880 | OutputStringEnt(stream, attr->GetValue(), convMem, convFile, | |
881 | XML_ESCAPE_QUOTES); | |
882 | OutputString(stream, wxT("\"")); | |
883 | attr = attr->GetNext(); | |
884 | } | |
885 | ||
886 | if (node->GetChildren()) | |
887 | { | |
888 | OutputString(stream, wxT(">")); | |
889 | prev = NULL; | |
890 | n = node->GetChildren(); | |
891 | while (n) | |
892 | { | |
893 | if (indentstep >= 0 && n && n->GetType() != wxXML_TEXT_NODE) | |
894 | OutputIndentation(stream, indent + indentstep); | |
895 | OutputNode(stream, n, indent + indentstep, convMem, convFile, indentstep); | |
896 | prev = n; | |
897 | n = n->GetNext(); | |
898 | } | |
899 | if (indentstep >= 0 && prev && prev->GetType() != wxXML_TEXT_NODE) | |
900 | OutputIndentation(stream, indent); | |
901 | OutputString(stream, wxT("</")); | |
902 | OutputString(stream, node->GetName()); | |
903 | OutputString(stream, wxT(">")); | |
904 | } | |
905 | else | |
906 | OutputString(stream, wxT("/>")); | |
907 | break; | |
908 | ||
909 | case wxXML_COMMENT_NODE: | |
910 | OutputString(stream, wxT("<!--")); | |
911 | OutputString(stream, node->GetContent(), convMem, convFile); | |
912 | OutputString(stream, wxT("-->")); | |
913 | break; | |
914 | ||
915 | default: | |
916 | wxFAIL_MSG(wxT("unsupported node type")); | |
917 | } | |
918 | } | |
919 | ||
920 | bool wxXmlDocument::Save(wxOutputStream& stream, int indentstep) const | |
921 | { | |
922 | if ( !IsOk() ) | |
923 | return false; | |
924 | ||
925 | wxString s; | |
926 | ||
927 | wxMBConv *convMem = NULL, | |
928 | *convFile; | |
929 | ||
930 | #if wxUSE_UNICODE | |
931 | convFile = new wxCSConv(GetFileEncoding()); | |
932 | convMem = NULL; | |
933 | #else | |
934 | if ( GetFileEncoding().CmpNoCase(GetEncoding()) != 0 ) | |
935 | { | |
936 | convFile = new wxCSConv(GetFileEncoding()); | |
937 | convMem = new wxCSConv(GetEncoding()); | |
938 | } | |
939 | else // file and in-memory encodings are the same, no conversion needed | |
940 | { | |
941 | convFile = | |
942 | convMem = NULL; | |
943 | } | |
944 | #endif | |
945 | ||
946 | s.Printf(wxT("<?xml version=\"%s\" encoding=\"%s\"?>\n"), | |
947 | GetVersion().c_str(), GetFileEncoding().c_str()); | |
948 | OutputString(stream, s); | |
949 | ||
950 | OutputNode(stream, GetRoot(), 0, convMem, convFile, indentstep); | |
951 | OutputString(stream, wxT("\n")); | |
952 | ||
953 | delete convFile; | |
954 | delete convMem; | |
955 | ||
956 | return true; | |
957 | } | |
958 | ||
959 | #endif // wxUSE_XML |