]>
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 | ||
574c939e | 335 | /* |
4d876ee3 VS |
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); | |
574c939e | 352 | |
4d876ee3 VS |
353 | wchar_t *buf = new wchar_t[nLen+1]; |
354 | wxConvUTF8.MB2WC(buf, s, nLen); | |
355 | buf[nLen] = 0; | |
698052d5 | 356 | wxString s(buf, *conv, len); |
4d876ee3 | 357 | delete[] buf; |
698052d5 | 358 | return s; |
78d14f80 | 359 | } |
4d876ee3 VS |
360 | else |
361 | return wxString(s, len); | |
362 | #endif | |
78d14f80 VS |
363 | } |
364 | ||
4d876ee3 VS |
365 | struct wxXmlParsingContext |
366 | { | |
367 | wxMBConv *conv; | |
368 | wxXmlNode *root; | |
369 | wxXmlNode *node; | |
370 | wxXmlNode *lastAsText; | |
371 | wxString encoding; | |
372 | wxString version; | |
373 | }; | |
78d14f80 | 374 | |
4d876ee3 | 375 | static void StartElementHnd(void *userData, const char *name, const char **atts) |
78d14f80 | 376 | { |
4d876ee3 VS |
377 | wxXmlParsingContext *ctx = (wxXmlParsingContext*)userData; |
378 | wxXmlNode *node = new wxXmlNode(wxXML_ELEMENT_NODE, CharToString(ctx->conv, name)); | |
379 | const char **a = atts; | |
380 | while (*a) | |
381 | { | |
382 | node->AddProperty(CharToString(ctx->conv, a[0]), CharToString(ctx->conv, a[1])); | |
383 | a += 2; | |
384 | } | |
385 | if (ctx->root == NULL) | |
386 | ctx->root = node; | |
387 | else | |
388 | ctx->node->AddChild(node); | |
389 | ctx->node = node; | |
390 | ctx->lastAsText = NULL; | |
78d14f80 VS |
391 | } |
392 | ||
4d876ee3 VS |
393 | static void EndElementHnd(void *userData, const char* WXUNUSED(name)) |
394 | { | |
395 | wxXmlParsingContext *ctx = (wxXmlParsingContext*)userData; | |
78d14f80 | 396 | |
4d876ee3 VS |
397 | ctx->node = ctx->node->GetParent(); |
398 | ctx->lastAsText = NULL; | |
399 | } | |
78d14f80 | 400 | |
4d876ee3 | 401 | static void TextHnd(void *userData, const char *s, int len) |
78d14f80 | 402 | { |
4d876ee3 VS |
403 | wxXmlParsingContext *ctx = (wxXmlParsingContext*)userData; |
404 | char *buf = new char[len + 1]; | |
405 | ||
406 | buf[len] = '\0'; | |
407 | memcpy(buf, s, (size_t)len); | |
408 | ||
409 | if (ctx->lastAsText) | |
410 | { | |
411 | ctx->lastAsText->SetContent(ctx->lastAsText->GetContent() + | |
412 | CharToString(ctx->conv, buf)); | |
413 | } | |
414 | else | |
78d14f80 | 415 | { |
4d876ee3 VS |
416 | bool whiteOnly = TRUE; |
417 | for (char *c = buf; *c != '\0'; c++) | |
418 | if (*c != ' ' && *c != '\t' && *c != '\n' && *c != '\r') | |
419 | { | |
420 | whiteOnly = FALSE; | |
421 | break; | |
422 | } | |
423 | if (!whiteOnly) | |
78d14f80 | 424 | { |
4d876ee3 VS |
425 | ctx->lastAsText = new wxXmlNode(wxXML_TEXT_NODE, wxT("text"), |
426 | CharToString(ctx->conv, buf)); | |
427 | ctx->node->AddChild(ctx->lastAsText); | |
78d14f80 | 428 | } |
78d14f80 | 429 | } |
78d14f80 | 430 | |
4d876ee3 VS |
431 | delete[] buf; |
432 | } | |
78d14f80 | 433 | |
4d876ee3 VS |
434 | static void CommentHnd(void *userData, const char *data) |
435 | { | |
436 | wxXmlParsingContext *ctx = (wxXmlParsingContext*)userData; | |
78d14f80 | 437 | |
4d876ee3 VS |
438 | if (ctx->node) |
439 | { | |
440 | // VS: ctx->node == NULL happens if there is a comment before | |
441 | // the root element (e.g. wxDesigner's output). We ignore such | |
442 | // comments, no big deal... | |
443 | ctx->node->AddChild(new wxXmlNode(wxXML_COMMENT_NODE, | |
444 | wxT("comment"), CharToString(ctx->conv, data))); | |
445 | } | |
446 | ctx->lastAsText = NULL; | |
447 | } | |
78d14f80 | 448 | |
4d876ee3 | 449 | static void DefaultHnd(void *userData, const char *s, int len) |
78d14f80 | 450 | { |
4d876ee3 VS |
451 | // XML header: |
452 | if (len > 6 && memcmp(s, "<?xml ", 6) == 0) | |
78d14f80 | 453 | { |
4d876ee3 VS |
454 | wxXmlParsingContext *ctx = (wxXmlParsingContext*)userData; |
455 | ||
456 | wxString buf = CharToString(ctx->conv, s, (size_t)len); | |
457 | int pos; | |
458 | pos = buf.Find(wxT("encoding=")); | |
459 | if (pos != wxNOT_FOUND) | |
460 | ctx->encoding = buf.Mid(pos + 10).BeforeFirst(buf[(size_t)pos+9]); | |
461 | pos = buf.Find(wxT("version=")); | |
462 | if (pos != wxNOT_FOUND) | |
463 | ctx->version = buf.Mid(pos + 9).BeforeFirst(buf[(size_t)pos+8]); | |
78d14f80 | 464 | } |
78d14f80 VS |
465 | } |
466 | ||
4d876ee3 VS |
467 | static int UnknownEncodingHnd(void * WXUNUSED(encodingHandlerData), |
468 | const XML_Char *name, XML_Encoding *info) | |
78d14f80 | 469 | { |
4d876ee3 VS |
470 | // We must build conversion table for expat. The easiest way to do so |
471 | // is to let wxCSConv convert as string containing all characters to | |
472 | // wide character representation: | |
97ddad38 | 473 | wxCSConv conv(wxString(name, wxConvLibc)); |
4d876ee3 VS |
474 | char mbBuf[255]; |
475 | wchar_t wcBuf[255]; | |
476 | size_t i; | |
574c939e | 477 | |
4d876ee3 | 478 | for (i = 0; i < 255; i++) |
574c939e | 479 | mbBuf[i] = (char) (i+1); |
4d876ee3 VS |
480 | mbBuf[255] = 0; |
481 | conv.MB2WC(wcBuf, mbBuf, 255); | |
482 | wcBuf[255] = 0; | |
574c939e | 483 | |
4d876ee3 VS |
484 | info->map[0] = 0; |
485 | for (i = 0; i < 255; i++) | |
486 | info->map[i+1] = (int)wcBuf[i]; | |
574c939e | 487 | |
4d876ee3 VS |
488 | info->data = NULL; |
489 | info->convert = NULL; | |
490 | info->release = NULL; | |
574c939e | 491 | |
4d876ee3 | 492 | return 1; |
78d14f80 VS |
493 | } |
494 | ||
4d876ee3 | 495 | bool wxXmlDocument::Load(wxInputStream& stream, const wxString& encoding) |
78d14f80 | 496 | { |
4d876ee3 VS |
497 | #if wxUSE_UNICODE |
498 | (void)encoding; | |
499 | #else | |
500 | m_encoding = encoding; | |
501 | #endif | |
502 | ||
503 | const size_t BUFSIZE = 1024; | |
504 | char buf[BUFSIZE]; | |
505 | wxXmlParsingContext ctx; | |
506 | bool done; | |
507 | XML_Parser parser = XML_ParserCreate(NULL); | |
508 | ||
509 | ctx.root = ctx.node = NULL; | |
510 | ctx.encoding = wxT("UTF-8"); // default in absence of encoding="" | |
511 | ctx.conv = NULL; | |
512 | #if !wxUSE_UNICODE | |
513 | if ( encoding != wxT("UTF-8") && encoding != wxT("utf-8") ) | |
514 | ctx.conv = new wxCSConv(encoding); | |
78d14f80 | 515 | #endif |
574c939e | 516 | |
4d876ee3 VS |
517 | XML_SetUserData(parser, (void*)&ctx); |
518 | XML_SetElementHandler(parser, StartElementHnd, EndElementHnd); | |
519 | XML_SetCharacterDataHandler(parser, TextHnd); | |
520 | XML_SetCommentHandler(parser, CommentHnd); | |
521 | XML_SetDefaultHandler(parser, DefaultHnd); | |
522 | XML_SetUnknownEncodingHandler(parser, UnknownEncodingHnd, NULL); | |
523 | ||
524 | do | |
525 | { | |
526 | size_t len = stream.Read(buf, BUFSIZE).LastRead(); | |
527 | done = (len < BUFSIZE); | |
528 | if (!XML_Parse(parser, buf, len, done)) | |
529 | { | |
530 | wxLogError(_("XML parsing error: '%s' at line %d"), | |
531 | XML_ErrorString(XML_GetErrorCode(parser)), | |
532 | XML_GetCurrentLineNumber(parser)); | |
533 | return FALSE; | |
534 | } | |
535 | } while (!done); | |
536 | ||
537 | SetVersion(ctx.version); | |
538 | SetFileEncoding(ctx.encoding); | |
539 | SetRoot(ctx.root); | |
540 | ||
541 | XML_ParserFree(parser); | |
542 | #if !wxUSE_UNICODE | |
543 | if ( ctx.conv ) | |
544 | delete ctx.conv; | |
545 | #endif | |
574c939e | 546 | |
4d876ee3 VS |
547 | return TRUE; |
548 | ||
78d14f80 VS |
549 | } |
550 | ||
551 | ||
78d14f80 | 552 | |
4d876ee3 VS |
553 | //----------------------------------------------------------------------------- |
554 | // wxXmlDocument saving routines | |
555 | //----------------------------------------------------------------------------- | |
556 | ||
557 | // write string to output: | |
558 | inline static void OutputString(wxOutputStream& stream, const wxString& str, | |
559 | wxMBConv *convMem, wxMBConv *convFile) | |
78d14f80 | 560 | { |
4d876ee3 VS |
561 | if (str.IsEmpty()) return; |
562 | #if wxUSE_UNICODE | |
97ddad38 | 563 | const wxWX2MBbuf buf(str.mb_str(convFile ? *convFile : wxConvUTF8)); |
4d876ee3 VS |
564 | stream.Write((const char*)buf, strlen((const char*)buf)); |
565 | #else | |
566 | if ( convFile == NULL ) | |
567 | stream.Write(str.mb_str(), str.Len()); | |
568 | else | |
569 | { | |
570 | wxString str2(str.wc_str(*convMem), *convFile); | |
571 | stream.Write(str2.mb_str(), str2.Len()); | |
572 | } | |
573 | #endif | |
574 | } | |
78d14f80 | 575 | |
574c939e | 576 | // Same as above, but create entities first. |
4d876ee3 VS |
577 | // Translates '<' to "<", '>' to ">" and '&' to "&" |
578 | static void OutputStringEnt(wxOutputStream& stream, const wxString& str, | |
579 | wxMBConv *convMem, wxMBConv *convFile) | |
580 | { | |
581 | wxString buf; | |
582 | size_t i, last, len; | |
583 | wxChar c; | |
574c939e | 584 | |
4d876ee3 VS |
585 | len = str.Len(); |
586 | last = 0; | |
587 | for (i = 0; i < len; i++) | |
588 | { | |
589 | c = str.GetChar(i); | |
574c939e | 590 | if (c == wxT('<') || c == wxT('>') || |
4d876ee3 VS |
591 | (c == wxT('&') && str.Mid(i+1, 4) != wxT("amp;"))) |
592 | { | |
593 | OutputString(stream, str.Mid(last, i - last), convMem, convFile); | |
594 | switch (c) | |
595 | { | |
574c939e | 596 | case wxT('<'): |
4d876ee3 VS |
597 | OutputString(stream, wxT("<"), NULL, NULL); |
598 | break; | |
574c939e | 599 | case wxT('>'): |
4d876ee3 VS |
600 | OutputString(stream, wxT(">"), NULL, NULL); |
601 | break; | |
574c939e | 602 | case wxT('&'): |
4d876ee3 VS |
603 | OutputString(stream, wxT("&"), NULL, NULL); |
604 | break; | |
605 | default: break; | |
606 | } | |
607 | last = i + 1; | |
608 | } | |
609 | } | |
610 | OutputString(stream, str.Mid(last, i - last), convMem, convFile); | |
611 | } | |
78d14f80 | 612 | |
4d876ee3 VS |
613 | inline static void OutputIndentation(wxOutputStream& stream, int indent) |
614 | { | |
615 | wxString str = wxT("\n"); | |
616 | for (int i = 0; i < indent; i++) | |
617 | str << wxT(' ') << wxT(' '); | |
618 | OutputString(stream, str, NULL, NULL); | |
619 | } | |
78d14f80 | 620 | |
4d876ee3 VS |
621 | static void OutputNode(wxOutputStream& stream, wxXmlNode *node, int indent, |
622 | wxMBConv *convMem, wxMBConv *convFile) | |
623 | { | |
624 | wxXmlNode *n, *prev; | |
625 | wxXmlProperty *prop; | |
78d14f80 | 626 | |
4d876ee3 VS |
627 | switch (node->GetType()) |
628 | { | |
629 | case wxXML_TEXT_NODE: | |
630 | OutputStringEnt(stream, node->GetContent(), convMem, convFile); | |
631 | break; | |
574c939e | 632 | |
4d876ee3 VS |
633 | case wxXML_ELEMENT_NODE: |
634 | OutputString(stream, wxT("<"), NULL, NULL); | |
635 | OutputString(stream, node->GetName(), NULL, NULL); | |
574c939e | 636 | |
4d876ee3 VS |
637 | prop = node->GetProperties(); |
638 | while (prop) | |
639 | { | |
640 | OutputString(stream, wxT(" ") + prop->GetName() + | |
641 | wxT("=\"") + prop->GetValue() + wxT("\""), | |
642 | NULL, NULL); | |
643 | // FIXME - what if prop contains '"'? | |
644 | prop = prop->GetNext(); | |
645 | } | |
574c939e | 646 | |
4d876ee3 VS |
647 | if (node->GetChildren()) |
648 | { | |
649 | OutputString(stream, wxT(">"), NULL, NULL); | |
650 | prev = NULL; | |
651 | n = node->GetChildren(); | |
652 | while (n) | |
653 | { | |
654 | if (n && n->GetType() != wxXML_TEXT_NODE) | |
655 | OutputIndentation(stream, indent + 1); | |
656 | OutputNode(stream, n, indent + 1, convMem, convFile); | |
657 | prev = n; | |
658 | n = n->GetNext(); | |
659 | } | |
660 | if (prev && prev->GetType() != wxXML_TEXT_NODE) | |
661 | OutputIndentation(stream, indent); | |
662 | OutputString(stream, wxT("</"), NULL, NULL); | |
663 | OutputString(stream, node->GetName(), NULL, NULL); | |
664 | OutputString(stream, wxT(">"), NULL, NULL); | |
665 | } | |
666 | else | |
667 | OutputString(stream, wxT("/>"), NULL, NULL); | |
668 | break; | |
574c939e | 669 | |
4d876ee3 VS |
670 | case wxXML_COMMENT_NODE: |
671 | OutputString(stream, wxT("<!--"), NULL, NULL); | |
672 | OutputString(stream, node->GetContent(), convMem, convFile); | |
673 | OutputString(stream, wxT("-->"), NULL, NULL); | |
674 | break; | |
574c939e | 675 | |
4d876ee3 VS |
676 | default: |
677 | wxFAIL_MSG(wxT("unsupported node type")); | |
678 | } | |
679 | } | |
78d14f80 | 680 | |
4d876ee3 | 681 | bool wxXmlDocument::Save(wxOutputStream& stream) const |
78d14f80 | 682 | { |
4d876ee3 VS |
683 | if ( !IsOk() ) |
684 | return FALSE; | |
574c939e | 685 | |
4d876ee3 | 686 | wxString s; |
574c939e | 687 | |
4d876ee3 VS |
688 | wxMBConv *convMem = NULL, *convFile = NULL; |
689 | #if wxUSE_UNICODE | |
690 | convFile = new wxCSConv(GetFileEncoding()); | |
691 | #else | |
692 | if ( GetFileEncoding() != GetEncoding() ) | |
693 | { | |
694 | convFile = new wxCSConv(GetFileEncoding()); | |
695 | convMem = new wxCSConv(GetEncoding()); | |
696 | } | |
697 | #endif | |
574c939e | 698 | |
4d876ee3 VS |
699 | s.Printf(wxT("<?xml version=\"%s\" encoding=\"%s\"?>\n"), |
700 | GetVersion().c_str(), GetFileEncoding().c_str()); | |
701 | OutputString(stream, s, NULL, NULL); | |
574c939e | 702 | |
4d876ee3 VS |
703 | OutputNode(stream, GetRoot(), 0, convMem, convFile); |
704 | OutputString(stream, wxT("\n"), NULL, NULL); | |
574c939e | 705 | |
4d876ee3 VS |
706 | if ( convFile ) |
707 | delete convFile; | |
708 | if ( convMem ) | |
709 | delete convMem; | |
574c939e | 710 | |
4d876ee3 | 711 | return TRUE; |
78d14f80 | 712 | } |