]>
Commit | Line | Data |
---|---|---|
78d14f80 VS |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: 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 | #ifdef __GNUG__ | |
12 | #pragma implementation "xml.h" | |
13 | #pragma implementation "xmlio.h" | |
14 | #endif | |
15 | ||
16 | // For compilers that support precompilation, includes "wx.h". | |
17 | #include "wx/wxprec.h" | |
18 | ||
19 | #ifdef __BORLANDC__ | |
20 | #pragma hdrstop | |
21 | #endif | |
22 | ||
23 | ||
24 | #include "wx/wfstream.h" | |
25 | #include "wx/datstrm.h" | |
26 | #include "wx/zstream.h" | |
27 | #include "wx/log.h" | |
28 | #include "wx/intl.h" | |
4d876ee3 | 29 | #include "wx/strconv.h" |
78d14f80 VS |
30 | |
31 | #include "wx/xrc/xml.h" | |
78d14f80 | 32 | |
4d876ee3 | 33 | #include "xmlparse.h" // from Expat |
78d14f80 | 34 | |
4d876ee3 VS |
35 | //----------------------------------------------------------------------------- |
36 | // wxXmlNode | |
37 | //----------------------------------------------------------------------------- | |
78d14f80 VS |
38 | |
39 | wxXmlNode::wxXmlNode(wxXmlNode *parent,wxXmlNodeType type, | |
40 | const wxString& name, const wxString& content, | |
41 | wxXmlProperty *props, wxXmlNode *next) | |
42 | : m_type(type), m_name(name), m_content(content), | |
43 | m_properties(props), m_parent(parent), | |
44 | m_children(NULL), m_next(next) | |
45 | { | |
46 | if (m_parent) | |
47 | { | |
48 | if (m_parent->m_children) | |
49 | { | |
50 | m_next = m_parent->m_children; | |
51 | m_parent->m_children = this; | |
52 | } | |
53 | else | |
54 | m_parent->m_children = this; | |
55 | } | |
56 | } | |
57 | ||
78d14f80 VS |
58 | wxXmlNode::wxXmlNode(wxXmlNodeType type, const wxString& name, |
59 | const wxString& content) | |
60 | : m_type(type), m_name(name), m_content(content), | |
61 | m_properties(NULL), m_parent(NULL), | |
62 | m_children(NULL), m_next(NULL) | |
63 | {} | |
64 | ||
78d14f80 VS |
65 | wxXmlNode::wxXmlNode(const wxXmlNode& node) |
66 | { | |
67 | m_next = NULL; | |
68 | m_parent = NULL; | |
69 | DoCopy(node); | |
70 | } | |
71 | ||
93cd80d6 VS |
72 | wxXmlNode::~wxXmlNode() |
73 | { | |
74 | wxXmlNode *c, *c2; | |
75 | for (c = m_children; c; c = c2) | |
76 | { | |
77 | c2 = c->m_next; | |
78 | delete c; | |
79 | } | |
80 | ||
81 | wxXmlProperty *p, *p2; | |
82 | for (p = m_properties; p; p = p2) | |
83 | { | |
84 | p2 = p->GetNext(); | |
85 | delete p; | |
86 | } | |
87 | } | |
88 | ||
78d14f80 VS |
89 | wxXmlNode& wxXmlNode::operator=(const wxXmlNode& node) |
90 | { | |
3fb676f8 GT |
91 | wxDELETE(m_properties); |
92 | wxDELETE(m_children); | |
78d14f80 VS |
93 | DoCopy(node); |
94 | return *this; | |
95 | } | |
96 | ||
78d14f80 VS |
97 | void wxXmlNode::DoCopy(const wxXmlNode& node) |
98 | { | |
99 | m_type = node.m_type; | |
100 | m_name = node.m_name; | |
101 | m_content = node.m_content; | |
102 | m_children = NULL; | |
103 | ||
104 | wxXmlNode *n = node.m_children; | |
105 | while (n) | |
106 | { | |
107 | AddChild(new wxXmlNode(*n)); | |
108 | n = n->GetNext(); | |
109 | } | |
110 | ||
111 | m_properties = NULL; | |
112 | wxXmlProperty *p = node.m_properties; | |
113 | while (p) | |
114 | { | |
115 | AddProperty(p->GetName(), p->GetValue()); | |
116 | p = p->GetNext(); | |
117 | } | |
118 | } | |
119 | ||
78d14f80 VS |
120 | bool wxXmlNode::HasProp(const wxString& propName) const |
121 | { | |
122 | wxXmlProperty *prop = GetProperties(); | |
123 | ||
124 | while (prop) | |
125 | { | |
126 | if (prop->GetName() == propName) return TRUE; | |
127 | prop = prop->GetNext(); | |
128 | } | |
129 | ||
130 | return FALSE; | |
131 | } | |
132 | ||
78d14f80 VS |
133 | bool wxXmlNode::GetPropVal(const wxString& propName, wxString *value) const |
134 | { | |
135 | wxXmlProperty *prop = GetProperties(); | |
136 | ||
137 | while (prop) | |
138 | { | |
139 | if (prop->GetName() == propName) | |
140 | { | |
141 | *value = prop->GetValue(); | |
142 | return TRUE; | |
143 | } | |
144 | prop = prop->GetNext(); | |
145 | } | |
146 | ||
147 | return FALSE; | |
148 | } | |
149 | ||
78d14f80 VS |
150 | wxString wxXmlNode::GetPropVal(const wxString& propName, const wxString& defaultVal) const |
151 | { | |
152 | wxString tmp; | |
153 | if (GetPropVal(propName, &tmp)) | |
154 | return tmp; | |
155 | else | |
156 | return defaultVal; | |
157 | } | |
158 | ||
78d14f80 VS |
159 | void wxXmlNode::AddChild(wxXmlNode *child) |
160 | { | |
161 | if (m_children == NULL) | |
162 | m_children = child; | |
163 | else | |
164 | { | |
165 | wxXmlNode *ch = m_children; | |
166 | while (ch->m_next) ch = ch->m_next; | |
167 | ch->m_next = child; | |
168 | } | |
169 | child->m_next = NULL; | |
170 | child->m_parent = this; | |
171 | } | |
172 | ||
78d14f80 VS |
173 | void wxXmlNode::InsertChild(wxXmlNode *child, wxXmlNode *before_node) |
174 | { | |
175 | wxASSERT_MSG(before_node->GetParent() == this, wxT("wxXmlNode::InsertChild - the node has incorrect parent")); | |
176 | ||
177 | if (m_children == before_node) | |
178 | m_children = child; | |
179 | else | |
180 | { | |
181 | wxXmlNode *ch = m_children; | |
182 | while (ch->m_next != before_node) ch = ch->m_next; | |
183 | ch->m_next = child; | |
184 | } | |
185 | ||
186 | child->m_parent = this; | |
187 | child->m_next = before_node; | |
188 | } | |
189 | ||
78d14f80 VS |
190 | bool wxXmlNode::RemoveChild(wxXmlNode *child) |
191 | { | |
192 | if (m_children == NULL) | |
193 | return FALSE; | |
194 | else if (m_children == child) | |
195 | { | |
196 | m_children = child->m_next; | |
197 | child->m_parent = NULL; | |
198 | child->m_next = NULL; | |
199 | return TRUE; | |
200 | } | |
201 | else | |
202 | { | |
203 | wxXmlNode *ch = m_children; | |
204 | while (ch->m_next) | |
205 | { | |
206 | if (ch->m_next == child) | |
207 | { | |
208 | ch->m_next = child->m_next; | |
209 | child->m_parent = NULL; | |
210 | child->m_next = NULL; | |
211 | return TRUE; | |
212 | } | |
213 | ch = ch->m_next; | |
214 | } | |
215 | return FALSE; | |
216 | } | |
217 | } | |
218 | ||
78d14f80 VS |
219 | void wxXmlNode::AddProperty(const wxString& name, const wxString& value) |
220 | { | |
221 | AddProperty(new wxXmlProperty(name, value, NULL)); | |
222 | } | |
223 | ||
224 | void wxXmlNode::AddProperty(wxXmlProperty *prop) | |
225 | { | |
226 | if (m_properties == NULL) | |
227 | m_properties = prop; | |
228 | else | |
229 | { | |
230 | wxXmlProperty *p = m_properties; | |
231 | while (p->GetNext()) p = p->GetNext(); | |
232 | p->SetNext(prop); | |
233 | } | |
234 | } | |
235 | ||
78d14f80 VS |
236 | bool wxXmlNode::DeleteProperty(const wxString& name) |
237 | { | |
46b8b00b GT |
238 | wxXmlProperty *prop; |
239 | ||
78d14f80 VS |
240 | if (m_properties == NULL) |
241 | return FALSE; | |
242 | ||
243 | else if (m_properties->GetName() == name) | |
244 | { | |
46b8b00b | 245 | prop = m_properties; |
78d14f80 VS |
246 | m_properties = prop->GetNext(); |
247 | prop->SetNext(NULL); | |
248 | delete prop; | |
249 | return TRUE; | |
250 | } | |
251 | ||
252 | else | |
253 | { | |
254 | wxXmlProperty *p = m_properties; | |
255 | while (p->GetNext()) | |
256 | { | |
257 | if (p->GetNext()->GetName() == name) | |
258 | { | |
46b8b00b | 259 | prop = p->GetNext(); |
78d14f80 VS |
260 | p->SetNext(prop->GetNext()); |
261 | prop->SetNext(NULL); | |
262 | delete prop; | |
263 | return TRUE; | |
264 | } | |
265 | p = p->GetNext(); | |
266 | } | |
267 | return FALSE; | |
268 | } | |
269 | } | |
270 | ||
271 | ||
272 | ||
4d876ee3 VS |
273 | //----------------------------------------------------------------------------- |
274 | // wxXmlDocument | |
275 | //----------------------------------------------------------------------------- | |
78d14f80 | 276 | |
4d876ee3 | 277 | wxXmlDocument::wxXmlDocument(const wxString& filename, const wxString& encoding) |
78d14f80 VS |
278 | : wxObject(), m_root(NULL) |
279 | { | |
4d876ee3 | 280 | if ( !Load(filename, encoding) ) |
78d14f80 | 281 | { |
3fb676f8 | 282 | wxDELETE(m_root); |
78d14f80 VS |
283 | } |
284 | } | |
285 | ||
4d876ee3 | 286 | wxXmlDocument::wxXmlDocument(wxInputStream& stream, const wxString& encoding) |
78d14f80 VS |
287 | : wxObject(), m_root(NULL) |
288 | { | |
4d876ee3 | 289 | if ( !Load(stream, encoding) ) |
78d14f80 | 290 | { |
3fb676f8 | 291 | wxDELETE(m_root); |
78d14f80 VS |
292 | } |
293 | } | |
294 | ||
78d14f80 VS |
295 | wxXmlDocument::wxXmlDocument(const wxXmlDocument& doc) |
296 | { | |
297 | DoCopy(doc); | |
298 | } | |
299 | ||
78d14f80 VS |
300 | wxXmlDocument& wxXmlDocument::operator=(const wxXmlDocument& doc) |
301 | { | |
3fb676f8 | 302 | wxDELETE(m_root); |
78d14f80 VS |
303 | DoCopy(doc); |
304 | return *this; | |
305 | } | |
306 | ||
78d14f80 VS |
307 | void wxXmlDocument::DoCopy(const wxXmlDocument& doc) |
308 | { | |
309 | m_version = doc.m_version; | |
480505bc | 310 | #if !wxUSE_UNICODE |
78d14f80 | 311 | m_encoding = doc.m_encoding; |
480505bc VS |
312 | #endif |
313 | m_fileEncoding = doc.m_fileEncoding; | |
78d14f80 VS |
314 | m_root = new wxXmlNode(*doc.m_root); |
315 | } | |
316 | ||
4d876ee3 | 317 | bool wxXmlDocument::Load(const wxString& filename, const wxString& encoding) |
78d14f80 VS |
318 | { |
319 | wxFileInputStream stream(filename); | |
4d876ee3 VS |
320 | return Load(stream, encoding); |
321 | } | |
322 | ||
323 | bool wxXmlDocument::Save(const wxString& filename) const | |
324 | { | |
325 | wxFileOutputStream stream(filename); | |
326 | return Save(stream); | |
78d14f80 VS |
327 | } |
328 | ||
329 | ||
330 | ||
4d876ee3 VS |
331 | //----------------------------------------------------------------------------- |
332 | // wxXmlDocument loading routines | |
333 | //----------------------------------------------------------------------------- | |
334 | ||
335 | /* | |
336 | FIXME: | |
337 | - process all elements, including CDATA | |
338 | */ | |
339 | ||
340 | // converts Expat-produced string in UTF-8 into wxString. | |
341 | inline static wxString CharToString(wxMBConv *conv, | |
342 | const char *s, size_t len = wxSTRING_MAXLEN) | |
78d14f80 | 343 | { |
480505bc | 344 | #if wxUSE_UNICODE |
4d876ee3 VS |
345 | (void)conv; |
346 | return wxString(s, wxConvUTF8, len); | |
480505bc | 347 | #else |
4d876ee3 | 348 | if ( conv ) |
78d14f80 | 349 | { |
4d876ee3 VS |
350 | size_t nLen = (len != wxSTRING_MAXLEN) ? len : |
351 | nLen = wxConvUTF8.MB2WC((wchar_t*) NULL, s, 0); | |
352 | ||
353 | wchar_t *buf = new wchar_t[nLen+1]; | |
354 | wxConvUTF8.MB2WC(buf, s, nLen); | |
355 | buf[nLen] = 0; | |
356 | return wxString(buf, *conv, len); | |
357 | delete[] buf; | |
78d14f80 | 358 | } |
4d876ee3 VS |
359 | else |
360 | return wxString(s, len); | |
361 | #endif | |
78d14f80 VS |
362 | } |
363 | ||
4d876ee3 VS |
364 | struct wxXmlParsingContext |
365 | { | |
366 | wxMBConv *conv; | |
367 | wxXmlNode *root; | |
368 | wxXmlNode *node; | |
369 | wxXmlNode *lastAsText; | |
370 | wxString encoding; | |
371 | wxString version; | |
372 | }; | |
78d14f80 | 373 | |
4d876ee3 | 374 | static void StartElementHnd(void *userData, const char *name, const char **atts) |
78d14f80 | 375 | { |
4d876ee3 VS |
376 | wxXmlParsingContext *ctx = (wxXmlParsingContext*)userData; |
377 | wxXmlNode *node = new wxXmlNode(wxXML_ELEMENT_NODE, CharToString(ctx->conv, name)); | |
378 | const char **a = atts; | |
379 | while (*a) | |
380 | { | |
381 | node->AddProperty(CharToString(ctx->conv, a[0]), CharToString(ctx->conv, a[1])); | |
382 | a += 2; | |
383 | } | |
384 | if (ctx->root == NULL) | |
385 | ctx->root = node; | |
386 | else | |
387 | ctx->node->AddChild(node); | |
388 | ctx->node = node; | |
389 | ctx->lastAsText = NULL; | |
78d14f80 VS |
390 | } |
391 | ||
4d876ee3 VS |
392 | static void EndElementHnd(void *userData, const char* WXUNUSED(name)) |
393 | { | |
394 | wxXmlParsingContext *ctx = (wxXmlParsingContext*)userData; | |
78d14f80 | 395 | |
4d876ee3 VS |
396 | ctx->node = ctx->node->GetParent(); |
397 | ctx->lastAsText = NULL; | |
398 | } | |
78d14f80 | 399 | |
4d876ee3 | 400 | static void TextHnd(void *userData, const char *s, int len) |
78d14f80 | 401 | { |
4d876ee3 VS |
402 | wxXmlParsingContext *ctx = (wxXmlParsingContext*)userData; |
403 | char *buf = new char[len + 1]; | |
404 | ||
405 | buf[len] = '\0'; | |
406 | memcpy(buf, s, (size_t)len); | |
407 | ||
408 | if (ctx->lastAsText) | |
409 | { | |
410 | ctx->lastAsText->SetContent(ctx->lastAsText->GetContent() + | |
411 | CharToString(ctx->conv, buf)); | |
412 | } | |
413 | else | |
78d14f80 | 414 | { |
4d876ee3 VS |
415 | bool whiteOnly = TRUE; |
416 | for (char *c = buf; *c != '\0'; c++) | |
417 | if (*c != ' ' && *c != '\t' && *c != '\n' && *c != '\r') | |
418 | { | |
419 | whiteOnly = FALSE; | |
420 | break; | |
421 | } | |
422 | if (!whiteOnly) | |
78d14f80 | 423 | { |
4d876ee3 VS |
424 | ctx->lastAsText = new wxXmlNode(wxXML_TEXT_NODE, wxT("text"), |
425 | CharToString(ctx->conv, buf)); | |
426 | ctx->node->AddChild(ctx->lastAsText); | |
78d14f80 | 427 | } |
78d14f80 | 428 | } |
78d14f80 | 429 | |
4d876ee3 VS |
430 | delete[] buf; |
431 | } | |
78d14f80 | 432 | |
4d876ee3 VS |
433 | static void CommentHnd(void *userData, const char *data) |
434 | { | |
435 | wxXmlParsingContext *ctx = (wxXmlParsingContext*)userData; | |
78d14f80 | 436 | |
4d876ee3 VS |
437 | if (ctx->node) |
438 | { | |
439 | // VS: ctx->node == NULL happens if there is a comment before | |
440 | // the root element (e.g. wxDesigner's output). We ignore such | |
441 | // comments, no big deal... | |
442 | ctx->node->AddChild(new wxXmlNode(wxXML_COMMENT_NODE, | |
443 | wxT("comment"), CharToString(ctx->conv, data))); | |
444 | } | |
445 | ctx->lastAsText = NULL; | |
446 | } | |
78d14f80 | 447 | |
4d876ee3 | 448 | static void DefaultHnd(void *userData, const char *s, int len) |
78d14f80 | 449 | { |
4d876ee3 VS |
450 | // XML header: |
451 | if (len > 6 && memcmp(s, "<?xml ", 6) == 0) | |
78d14f80 | 452 | { |
4d876ee3 VS |
453 | wxXmlParsingContext *ctx = (wxXmlParsingContext*)userData; |
454 | ||
455 | wxString buf = CharToString(ctx->conv, s, (size_t)len); | |
456 | int pos; | |
457 | pos = buf.Find(wxT("encoding=")); | |
458 | if (pos != wxNOT_FOUND) | |
459 | ctx->encoding = buf.Mid(pos + 10).BeforeFirst(buf[(size_t)pos+9]); | |
460 | pos = buf.Find(wxT("version=")); | |
461 | if (pos != wxNOT_FOUND) | |
462 | ctx->version = buf.Mid(pos + 9).BeforeFirst(buf[(size_t)pos+8]); | |
78d14f80 | 463 | } |
78d14f80 VS |
464 | } |
465 | ||
4d876ee3 VS |
466 | static int UnknownEncodingHnd(void * WXUNUSED(encodingHandlerData), |
467 | const XML_Char *name, XML_Encoding *info) | |
78d14f80 | 468 | { |
4d876ee3 VS |
469 | // We must build conversion table for expat. The easiest way to do so |
470 | // is to let wxCSConv convert as string containing all characters to | |
471 | // wide character representation: | |
97ddad38 | 472 | wxCSConv conv(wxString(name, wxConvLibc)); |
4d876ee3 VS |
473 | char mbBuf[255]; |
474 | wchar_t wcBuf[255]; | |
475 | size_t i; | |
476 | ||
477 | for (i = 0; i < 255; i++) | |
478 | mbBuf[i] = i+1; | |
479 | mbBuf[255] = 0; | |
480 | conv.MB2WC(wcBuf, mbBuf, 255); | |
481 | wcBuf[255] = 0; | |
482 | ||
483 | info->map[0] = 0; | |
484 | for (i = 0; i < 255; i++) | |
485 | info->map[i+1] = (int)wcBuf[i]; | |
486 | ||
487 | info->data = NULL; | |
488 | info->convert = NULL; | |
489 | info->release = NULL; | |
490 | ||
491 | return 1; | |
78d14f80 VS |
492 | } |
493 | ||
4d876ee3 | 494 | bool wxXmlDocument::Load(wxInputStream& stream, const wxString& encoding) |
78d14f80 | 495 | { |
4d876ee3 VS |
496 | #if wxUSE_UNICODE |
497 | (void)encoding; | |
498 | #else | |
499 | m_encoding = encoding; | |
500 | #endif | |
501 | ||
502 | const size_t BUFSIZE = 1024; | |
503 | char buf[BUFSIZE]; | |
504 | wxXmlParsingContext ctx; | |
505 | bool done; | |
506 | XML_Parser parser = XML_ParserCreate(NULL); | |
507 | ||
508 | ctx.root = ctx.node = NULL; | |
509 | ctx.encoding = wxT("UTF-8"); // default in absence of encoding="" | |
510 | ctx.conv = NULL; | |
511 | #if !wxUSE_UNICODE | |
512 | if ( encoding != wxT("UTF-8") && encoding != wxT("utf-8") ) | |
513 | ctx.conv = new wxCSConv(encoding); | |
78d14f80 | 514 | #endif |
4d876ee3 VS |
515 | |
516 | XML_SetUserData(parser, (void*)&ctx); | |
517 | XML_SetElementHandler(parser, StartElementHnd, EndElementHnd); | |
518 | XML_SetCharacterDataHandler(parser, TextHnd); | |
519 | XML_SetCommentHandler(parser, CommentHnd); | |
520 | XML_SetDefaultHandler(parser, DefaultHnd); | |
521 | XML_SetUnknownEncodingHandler(parser, UnknownEncodingHnd, NULL); | |
522 | ||
523 | do | |
524 | { | |
525 | size_t len = stream.Read(buf, BUFSIZE).LastRead(); | |
526 | done = (len < BUFSIZE); | |
527 | if (!XML_Parse(parser, buf, len, done)) | |
528 | { | |
529 | wxLogError(_("XML parsing error: '%s' at line %d"), | |
530 | XML_ErrorString(XML_GetErrorCode(parser)), | |
531 | XML_GetCurrentLineNumber(parser)); | |
532 | return FALSE; | |
533 | } | |
534 | } while (!done); | |
535 | ||
536 | SetVersion(ctx.version); | |
537 | SetFileEncoding(ctx.encoding); | |
538 | SetRoot(ctx.root); | |
539 | ||
540 | XML_ParserFree(parser); | |
541 | #if !wxUSE_UNICODE | |
542 | if ( ctx.conv ) | |
543 | delete ctx.conv; | |
544 | #endif | |
545 | ||
546 | return TRUE; | |
547 | ||
78d14f80 VS |
548 | } |
549 | ||
550 | ||
78d14f80 | 551 | |
4d876ee3 VS |
552 | //----------------------------------------------------------------------------- |
553 | // wxXmlDocument saving routines | |
554 | //----------------------------------------------------------------------------- | |
555 | ||
556 | // write string to output: | |
557 | inline static void OutputString(wxOutputStream& stream, const wxString& str, | |
558 | wxMBConv *convMem, wxMBConv *convFile) | |
78d14f80 | 559 | { |
4d876ee3 VS |
560 | if (str.IsEmpty()) return; |
561 | #if wxUSE_UNICODE | |
97ddad38 | 562 | const wxWX2MBbuf buf(str.mb_str(convFile ? *convFile : wxConvUTF8)); |
4d876ee3 VS |
563 | stream.Write((const char*)buf, strlen((const char*)buf)); |
564 | #else | |
565 | if ( convFile == NULL ) | |
566 | stream.Write(str.mb_str(), str.Len()); | |
567 | else | |
568 | { | |
569 | wxString str2(str.wc_str(*convMem), *convFile); | |
570 | stream.Write(str2.mb_str(), str2.Len()); | |
571 | } | |
572 | #endif | |
573 | } | |
78d14f80 | 574 | |
4d876ee3 VS |
575 | // Same as above, but create entities first. |
576 | // Translates '<' to "<", '>' to ">" and '&' to "&" | |
577 | static void OutputStringEnt(wxOutputStream& stream, const wxString& str, | |
578 | wxMBConv *convMem, wxMBConv *convFile) | |
579 | { | |
580 | wxString buf; | |
581 | size_t i, last, len; | |
582 | wxChar c; | |
583 | ||
584 | len = str.Len(); | |
585 | last = 0; | |
586 | for (i = 0; i < len; i++) | |
587 | { | |
588 | c = str.GetChar(i); | |
589 | if (c == wxT('<') || c == wxT('>') || | |
590 | (c == wxT('&') && str.Mid(i+1, 4) != wxT("amp;"))) | |
591 | { | |
592 | OutputString(stream, str.Mid(last, i - last), convMem, convFile); | |
593 | switch (c) | |
594 | { | |
595 | case wxT('<'): | |
596 | OutputString(stream, wxT("<"), NULL, NULL); | |
597 | break; | |
598 | case wxT('>'): | |
599 | OutputString(stream, wxT(">"), NULL, NULL); | |
600 | break; | |
601 | case wxT('&'): | |
602 | OutputString(stream, wxT("&"), NULL, NULL); | |
603 | break; | |
604 | default: break; | |
605 | } | |
606 | last = i + 1; | |
607 | } | |
608 | } | |
609 | OutputString(stream, str.Mid(last, i - last), convMem, convFile); | |
610 | } | |
78d14f80 | 611 | |
4d876ee3 VS |
612 | inline static void OutputIndentation(wxOutputStream& stream, int indent) |
613 | { | |
614 | wxString str = wxT("\n"); | |
615 | for (int i = 0; i < indent; i++) | |
616 | str << wxT(' ') << wxT(' '); | |
617 | OutputString(stream, str, NULL, NULL); | |
618 | } | |
78d14f80 | 619 | |
4d876ee3 VS |
620 | static void OutputNode(wxOutputStream& stream, wxXmlNode *node, int indent, |
621 | wxMBConv *convMem, wxMBConv *convFile) | |
622 | { | |
623 | wxXmlNode *n, *prev; | |
624 | wxXmlProperty *prop; | |
78d14f80 | 625 | |
4d876ee3 VS |
626 | switch (node->GetType()) |
627 | { | |
628 | case wxXML_TEXT_NODE: | |
629 | OutputStringEnt(stream, node->GetContent(), convMem, convFile); | |
630 | break; | |
631 | ||
632 | case wxXML_ELEMENT_NODE: | |
633 | OutputString(stream, wxT("<"), NULL, NULL); | |
634 | OutputString(stream, node->GetName(), NULL, NULL); | |
635 | ||
636 | prop = node->GetProperties(); | |
637 | while (prop) | |
638 | { | |
639 | OutputString(stream, wxT(" ") + prop->GetName() + | |
640 | wxT("=\"") + prop->GetValue() + wxT("\""), | |
641 | NULL, NULL); | |
642 | // FIXME - what if prop contains '"'? | |
643 | prop = prop->GetNext(); | |
644 | } | |
645 | ||
646 | if (node->GetChildren()) | |
647 | { | |
648 | OutputString(stream, wxT(">"), NULL, NULL); | |
649 | prev = NULL; | |
650 | n = node->GetChildren(); | |
651 | while (n) | |
652 | { | |
653 | if (n && n->GetType() != wxXML_TEXT_NODE) | |
654 | OutputIndentation(stream, indent + 1); | |
655 | OutputNode(stream, n, indent + 1, convMem, convFile); | |
656 | prev = n; | |
657 | n = n->GetNext(); | |
658 | } | |
659 | if (prev && prev->GetType() != wxXML_TEXT_NODE) | |
660 | OutputIndentation(stream, indent); | |
661 | OutputString(stream, wxT("</"), NULL, NULL); | |
662 | OutputString(stream, node->GetName(), NULL, NULL); | |
663 | OutputString(stream, wxT(">"), NULL, NULL); | |
664 | } | |
665 | else | |
666 | OutputString(stream, wxT("/>"), NULL, NULL); | |
667 | break; | |
668 | ||
669 | case wxXML_COMMENT_NODE: | |
670 | OutputString(stream, wxT("<!--"), NULL, NULL); | |
671 | OutputString(stream, node->GetContent(), convMem, convFile); | |
672 | OutputString(stream, wxT("-->"), NULL, NULL); | |
673 | break; | |
674 | ||
675 | default: | |
676 | wxFAIL_MSG(wxT("unsupported node type")); | |
677 | } | |
678 | } | |
78d14f80 | 679 | |
4d876ee3 | 680 | bool wxXmlDocument::Save(wxOutputStream& stream) const |
78d14f80 | 681 | { |
4d876ee3 VS |
682 | if ( !IsOk() ) |
683 | return FALSE; | |
684 | ||
685 | wxString s; | |
686 | ||
687 | wxMBConv *convMem = NULL, *convFile = NULL; | |
688 | #if wxUSE_UNICODE | |
689 | convFile = new wxCSConv(GetFileEncoding()); | |
690 | #else | |
691 | if ( GetFileEncoding() != GetEncoding() ) | |
692 | { | |
693 | convFile = new wxCSConv(GetFileEncoding()); | |
694 | convMem = new wxCSConv(GetEncoding()); | |
695 | } | |
696 | #endif | |
697 | ||
698 | s.Printf(wxT("<?xml version=\"%s\" encoding=\"%s\"?>\n"), | |
699 | GetVersion().c_str(), GetFileEncoding().c_str()); | |
700 | OutputString(stream, s, NULL, NULL); | |
701 | ||
702 | OutputNode(stream, GetRoot(), 0, convMem, convFile); | |
703 | OutputString(stream, wxT("\n"), NULL, NULL); | |
704 | ||
705 | if ( convFile ) | |
706 | delete convFile; | |
707 | if ( convMem ) | |
708 | delete convMem; | |
709 | ||
710 | return TRUE; | |
78d14f80 | 711 | } |