Fixed an XML parsing error for complex URLs
[wxWidgets.git] / src / richtext / richtextxml.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/richtext/richtextxml.cpp
3 // Purpose: XML and HTML I/O for wxRichTextCtrl
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 2005-09-30
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #ifdef __BORLANDC__
16 #pragma hdrstop
17 #endif
18
19 #if wxUSE_RICHTEXT && wxUSE_XML
20
21 #include "wx/richtext/richtextxml.h"
22
23 #ifndef WX_PRECOMP
24 #include "wx/intl.h"
25 #include "wx/module.h"
26 #endif
27
28 #include "wx/filename.h"
29 #include "wx/clipbrd.h"
30 #include "wx/wfstream.h"
31 #include "wx/sstream.h"
32 #include "wx/txtstrm.h"
33 #include "wx/tokenzr.h"
34 #include "wx/xml/xml.h"
35
36 IMPLEMENT_DYNAMIC_CLASS(wxRichTextXMLHandler, wxRichTextFileHandler)
37
38 #if wxUSE_STREAMS
39 bool wxRichTextXMLHandler::DoLoadFile(wxRichTextBuffer *buffer, wxInputStream& stream)
40 {
41 if (!stream.IsOk())
42 return false;
43
44 buffer->ResetAndClearCommands();
45 buffer->Clear();
46
47 wxXmlDocument* xmlDoc = new wxXmlDocument;
48 bool success = true;
49
50 // This is the encoding to convert to (memory encoding rather than file encoding)
51 wxString encoding(wxT("UTF-8"));
52
53 #if !wxUSE_UNICODE && wxUSE_INTL
54 encoding = wxLocale::GetSystemEncodingName();
55 #endif
56
57 if (!xmlDoc->Load(stream, encoding))
58 {
59 buffer->ResetAndClearCommands();
60 success = false;
61 }
62 else
63 {
64 if (xmlDoc->GetRoot() && xmlDoc->GetRoot()->GetType() == wxXML_ELEMENT_NODE && xmlDoc->GetRoot()->GetName() == wxT("richtext"))
65 {
66 wxXmlNode* child = xmlDoc->GetRoot()->GetChildren();
67 while (child)
68 {
69 if (child->GetType() == wxXML_ELEMENT_NODE)
70 {
71 wxString name = child->GetName();
72 if (name == wxT("richtext-version"))
73 {
74 }
75 else
76 ImportXML(buffer, child);
77 }
78
79 child = child->GetNext();
80 }
81 }
82 else
83 {
84 success = false;
85 }
86 }
87
88 delete xmlDoc;
89
90 buffer->UpdateRanges();
91
92 return success;
93 }
94
95 /// Recursively import an object
96 bool wxRichTextXMLHandler::ImportXML(wxRichTextBuffer* buffer, wxXmlNode* node)
97 {
98 wxString name = node->GetName();
99
100 bool doneChildren = false;
101
102 if (name == wxT("paragraphlayout"))
103 {
104 wxString partial = node->GetAttribute(wxT("partialparagraph"), wxEmptyString);
105 if (partial == wxT("true"))
106 buffer->SetPartialParagraph(true);
107 }
108 else if (name == wxT("paragraph"))
109 {
110 wxRichTextParagraph* para = new wxRichTextParagraph(buffer);
111 buffer->AppendChild(para);
112
113 GetStyle(para->GetAttributes(), node, true);
114
115 wxXmlNode* child = node->GetChildren();
116 while (child)
117 {
118 wxString childName = child->GetName();
119 if (childName == wxT("text"))
120 {
121 wxString text;
122 wxXmlNode* textChild = child->GetChildren();
123 while (textChild)
124 {
125 if (textChild->GetType() == wxXML_TEXT_NODE ||
126 textChild->GetType() == wxXML_CDATA_SECTION_NODE)
127 {
128 wxString text2 = textChild->GetContent();
129
130 // Strip whitespace from end
131 if (!text2.empty() && text2[text2.length()-1] == wxT('\n'))
132 text2 = text2.Mid(0, text2.length()-1);
133
134 if (!text2.empty() && text2[0] == wxT('"'))
135 text2 = text2.Mid(1);
136 if (!text2.empty() && text2[text2.length()-1] == wxT('"'))
137 text2 = text2.Mid(0, text2.length() - 1);
138
139 text += text2;
140 }
141 textChild = textChild->GetNext();
142 }
143
144 wxRichTextPlainText* textObject = new wxRichTextPlainText(text, para);
145 GetStyle(textObject->GetAttributes(), child, false);
146
147 para->AppendChild(textObject);
148 }
149 else if (childName == wxT("symbol"))
150 {
151 // This is a symbol that XML can't read in the normal way
152 wxString text;
153 wxXmlNode* textChild = child->GetChildren();
154 while (textChild)
155 {
156 if (textChild->GetType() == wxXML_TEXT_NODE ||
157 textChild->GetType() == wxXML_CDATA_SECTION_NODE)
158 {
159 wxString text2 = textChild->GetContent();
160 text += text2;
161 }
162 textChild = textChild->GetNext();
163 }
164
165 wxString actualText;
166 actualText << (wxChar) wxAtoi(text);
167
168 wxRichTextPlainText* textObject = new wxRichTextPlainText(actualText, para);
169 GetStyle(textObject->GetAttributes(), child, false);
170
171 para->AppendChild(textObject);
172 }
173 else if (childName == wxT("image"))
174 {
175 int imageType = wxBITMAP_TYPE_PNG;
176 wxString value = node->GetAttribute(wxT("imagetype"), wxEmptyString);
177 if (!value.empty())
178 imageType = wxAtoi(value);
179
180 wxString data;
181
182 wxXmlNode* imageChild = child->GetChildren();
183 while (imageChild)
184 {
185 wxString childName = imageChild->GetName();
186 if (childName == wxT("data"))
187 {
188 wxXmlNode* dataChild = imageChild->GetChildren();
189 while (dataChild)
190 {
191 data = dataChild->GetContent();
192 // wxLogDebug(data);
193 dataChild = dataChild->GetNext();
194 }
195
196 }
197 imageChild = imageChild->GetNext();
198 }
199
200 if (!data.empty())
201 {
202 wxRichTextImage* imageObj = new wxRichTextImage(para);
203 GetStyle(imageObj->GetAttributes(), child, false);
204 para->AppendChild(imageObj);
205
206 wxStringInputStream strStream(data);
207
208 imageObj->GetImageBlock().ReadHex(strStream, data.length(), imageType);
209 }
210 }
211 child = child->GetNext();
212 }
213
214 doneChildren = true;
215 }
216 else if (name == wxT("stylesheet"))
217 {
218 if (GetFlags() & wxRICHTEXT_HANDLER_INCLUDE_STYLESHEET)
219 {
220 wxRichTextStyleSheet* sheet = new wxRichTextStyleSheet;
221 wxString sheetName = node->GetAttribute(wxT("name"), wxEmptyString);
222 wxString sheetDescription = node->GetAttribute(wxT("description"), wxEmptyString);
223 sheet->SetName(sheetName);
224 sheet->SetDescription(sheetDescription);
225
226 wxXmlNode* child = node->GetChildren();
227 while (child)
228 {
229 ImportStyleDefinition(sheet, child);
230
231 child = child->GetNext();
232 }
233
234 // Notify that styles have changed. If this is vetoed by the app,
235 // the new sheet will be deleted. If it is not vetoed, the
236 // old sheet will be deleted and replaced with the new one.
237 buffer->SetStyleSheetAndNotify(sheet);
238 }
239 doneChildren = true;
240 }
241
242 if (!doneChildren)
243 {
244 wxXmlNode* child = node->GetChildren();
245 while (child)
246 {
247 ImportXML(buffer, child);
248 child = child->GetNext();
249 }
250 }
251
252 return true;
253 }
254
255 bool wxRichTextXMLHandler::ImportStyleDefinition(wxRichTextStyleSheet* sheet, wxXmlNode* node)
256 {
257 wxString styleType = node->GetName();
258 wxString styleName = node->GetAttribute(wxT("name"), wxEmptyString);
259 wxString baseStyleName = node->GetAttribute(wxT("basestyle"), wxEmptyString);
260
261 if (styleName.IsEmpty())
262 return false;
263
264 if (styleType == wxT("characterstyle"))
265 {
266 wxRichTextCharacterStyleDefinition* def = new wxRichTextCharacterStyleDefinition(styleName);
267 def->SetBaseStyle(baseStyleName);
268
269 wxXmlNode* child = node->GetChildren();
270 while (child)
271 {
272 if (child->GetName() == wxT("style"))
273 {
274 wxTextAttr attr;
275 GetStyle(attr, child, false);
276 def->SetStyle(attr);
277 }
278 child = child->GetNext();
279 }
280
281 sheet->AddCharacterStyle(def);
282 }
283 else if (styleType == wxT("paragraphstyle"))
284 {
285 wxRichTextParagraphStyleDefinition* def = new wxRichTextParagraphStyleDefinition(styleName);
286
287 wxString nextStyleName = node->GetAttribute(wxT("nextstyle"), wxEmptyString);
288 def->SetNextStyle(nextStyleName);
289 def->SetBaseStyle(baseStyleName);
290
291 wxXmlNode* child = node->GetChildren();
292 while (child)
293 {
294 if (child->GetName() == wxT("style"))
295 {
296 wxTextAttr attr;
297 GetStyle(attr, child, false);
298 def->SetStyle(attr);
299 }
300 child = child->GetNext();
301 }
302
303 sheet->AddParagraphStyle(def);
304 }
305 else if (styleType == wxT("liststyle"))
306 {
307 wxRichTextListStyleDefinition* def = new wxRichTextListStyleDefinition(styleName);
308
309 wxString nextStyleName = node->GetAttribute(wxT("nextstyle"), wxEmptyString);
310 def->SetNextStyle(nextStyleName);
311 def->SetBaseStyle(baseStyleName);
312
313 wxXmlNode* child = node->GetChildren();
314 while (child)
315 {
316 if (child->GetName() == wxT("style"))
317 {
318 wxTextAttr attr;
319 GetStyle(attr, child, false);
320
321 wxString styleLevel = child->GetAttribute(wxT("level"), wxEmptyString);
322 if (styleLevel.IsEmpty())
323 {
324 def->SetStyle(attr);
325 }
326 else
327 {
328 int level = wxAtoi(styleLevel);
329 if (level > 0 && level <= 10)
330 {
331 def->SetLevelAttributes(level-1, attr);
332 }
333 }
334 }
335 child = child->GetNext();
336 }
337
338 sheet->AddListStyle(def);
339 }
340
341 return true;
342 }
343
344 //-----------------------------------------------------------------------------
345 // xml support routines
346 //-----------------------------------------------------------------------------
347
348 bool wxRichTextXMLHandler::HasParam(wxXmlNode* node, const wxString& param)
349 {
350 return (GetParamNode(node, param) != NULL);
351 }
352
353 wxXmlNode *wxRichTextXMLHandler::GetParamNode(wxXmlNode* node, const wxString& param)
354 {
355 wxCHECK_MSG(node, NULL, wxT("You can't access node data before it was initialized!"));
356
357 wxXmlNode *n = node->GetChildren();
358
359 while (n)
360 {
361 if (n->GetType() == wxXML_ELEMENT_NODE && n->GetName() == param)
362 return n;
363 n = n->GetNext();
364 }
365 return NULL;
366 }
367
368
369 wxString wxRichTextXMLHandler::GetNodeContent(wxXmlNode *node)
370 {
371 wxXmlNode *n = node;
372 if (n == NULL) return wxEmptyString;
373 n = n->GetChildren();
374
375 while (n)
376 {
377 if (n->GetType() == wxXML_TEXT_NODE ||
378 n->GetType() == wxXML_CDATA_SECTION_NODE)
379 return n->GetContent();
380 n = n->GetNext();
381 }
382 return wxEmptyString;
383 }
384
385
386 wxString wxRichTextXMLHandler::GetParamValue(wxXmlNode *node, const wxString& param)
387 {
388 if (param.empty())
389 return GetNodeContent(node);
390 else
391 return GetNodeContent(GetParamNode(node, param));
392 }
393
394 wxString wxRichTextXMLHandler::GetText(wxXmlNode *node, const wxString& param, bool WXUNUSED(translate))
395 {
396 wxXmlNode *parNode = GetParamNode(node, param);
397 if (!parNode)
398 parNode = node;
399 wxString str1(GetNodeContent(parNode));
400 return str1;
401 }
402
403 // For use with earlier versions of wxWidgets
404 #ifndef WXUNUSED_IN_UNICODE
405 #if wxUSE_UNICODE
406 #define WXUNUSED_IN_UNICODE(x) WXUNUSED(x)
407 #else
408 #define WXUNUSED_IN_UNICODE(x) x
409 #endif
410 #endif
411
412 // write string to output:
413 inline static void OutputString(wxOutputStream& stream, const wxString& str,
414 wxMBConv *WXUNUSED_IN_UNICODE(convMem) = NULL, wxMBConv *convFile = NULL)
415 {
416 if (str.empty()) return;
417 #if wxUSE_UNICODE
418 if (convFile)
419 {
420 const wxWX2MBbuf buf(str.mb_str(*convFile));
421 stream.Write((const char*)buf, strlen((const char*)buf));
422 }
423 else
424 {
425 const wxWX2MBbuf buf(str.mb_str(wxConvUTF8));
426 stream.Write((const char*)buf, strlen((const char*)buf));
427 }
428 #else
429 if ( convFile == NULL )
430 stream.Write(str.mb_str(), str.Len());
431 else
432 {
433 wxString str2(str.wc_str(*convMem), *convFile);
434 stream.Write(str2.mb_str(), str2.Len());
435 }
436 #endif
437 }
438
439 // Same as above, but create entities first.
440 // Translates '<' to "&lt;", '>' to "&gt;" and '&' to "&amp;"
441 static void OutputStringEnt(wxOutputStream& stream, const wxString& str,
442 wxMBConv *convMem = NULL, wxMBConv *convFile = NULL)
443 {
444 wxString buf;
445 size_t i, last, len;
446 wxChar c;
447
448 len = str.Len();
449 last = 0;
450 for (i = 0; i < len; i++)
451 {
452 c = str.GetChar(i);
453
454 // Original code excluded "&amp;" but we _do_ want to convert
455 // the ampersand beginning &amp; because otherwise when read in,
456 // the original "&amp;" becomes "&".
457
458 if (c == wxT('<') || c == wxT('>') || c == wxT('"') ||
459 (c == wxT('&') /* && (str.Mid(i+1, 4) != wxT("amp;")) */ ))
460 {
461 OutputString(stream, str.Mid(last, i - last), convMem, convFile);
462 switch (c)
463 {
464 case wxT('<'):
465 OutputString(stream, wxT("&lt;"), NULL, NULL);
466 break;
467 case wxT('>'):
468 OutputString(stream, wxT("&gt;"), NULL, NULL);
469 break;
470 case wxT('&'):
471 OutputString(stream, wxT("&amp;"), NULL, NULL);
472 break;
473 case wxT('"'):
474 OutputString(stream, wxT("&quot;"), NULL, NULL);
475 break;
476 default: break;
477 }
478 last = i + 1;
479 }
480 else if (wxUChar(c) > 127)
481 {
482 OutputString(stream, str.Mid(last, i - last), convMem, convFile);
483
484 wxString s(wxT("&#"));
485 s << (int) c;
486 s << wxT(";");
487 OutputString(stream, s, NULL, NULL);
488 last = i + 1;
489 }
490 }
491 OutputString(stream, str.Mid(last, i - last), convMem, convFile);
492 }
493
494 static wxString AttributeToXML(const wxString& str)
495 {
496 wxString str1;
497 size_t i, last, len;
498 wxChar c;
499
500 len = str.Len();
501 last = 0;
502 for (i = 0; i < len; i++)
503 {
504 c = str.GetChar(i);
505
506 // Original code excluded "&amp;" but we _do_ want to convert
507 // the ampersand beginning &amp; because otherwise when read in,
508 // the original "&amp;" becomes "&".
509
510 if (c == wxT('<') || c == wxT('>') || c == wxT('"') ||
511 (c == wxT('&') /* && (str.Mid(i+1, 4) != wxT("amp;")) */ ))
512 {
513 str1 += str.Mid(last, i - last);
514 switch (c)
515 {
516 case wxT('<'):
517 str1 += wxT("&lt;");
518 break;
519 case wxT('>'):
520 str1 += wxT("&gt;");
521 break;
522 case wxT('&'):
523 str1 += wxT("&amp;");
524 break;
525 case wxT('"'):
526 str1 += wxT("&quot;");
527 break;
528 default: break;
529 }
530 last = i + 1;
531 }
532 else if (wxUChar(c) > 127)
533 {
534 str1 += str.Mid(last, i - last);
535
536 wxString s(wxT("&#"));
537 s << (int) c;
538 s << wxT(";");
539 str1 += s;
540 last = i + 1;
541 }
542 }
543 str1 += str.Mid(last, i - last);
544 return str1;
545 }
546
547 inline static void OutputIndentation(wxOutputStream& stream, int indent)
548 {
549 wxString str = wxT("\n");
550 for (int i = 0; i < indent; i++)
551 str << wxT(' ') << wxT(' ');
552 OutputString(stream, str, NULL, NULL);
553 }
554
555 // Convert a colour to a 6-digit hex string
556 static wxString ColourToHexString(const wxColour& col)
557 {
558 wxString hex;
559
560 hex += wxDecToHex(col.Red());
561 hex += wxDecToHex(col.Green());
562 hex += wxDecToHex(col.Blue());
563
564 return hex;
565 }
566
567 // Convert 6-digit hex string to a colour
568 static wxColour HexStringToColour(const wxString& hex)
569 {
570 unsigned char r = (unsigned char)wxHexToDec(hex.Mid(0, 2));
571 unsigned char g = (unsigned char)wxHexToDec(hex.Mid(2, 2));
572 unsigned char b = (unsigned char)wxHexToDec(hex.Mid(4, 2));
573
574 return wxColour(r, g, b);
575 }
576
577 bool wxRichTextXMLHandler::DoSaveFile(wxRichTextBuffer *buffer, wxOutputStream& stream)
578 {
579 if (!stream.IsOk())
580 return false;
581
582 wxString version(wxT("1.0") ) ;
583
584 bool deleteConvFile = false;
585 wxString fileEncoding;
586 wxMBConv* convFile = NULL;
587
588 #if wxUSE_UNICODE
589 fileEncoding = wxT("UTF-8");
590 convFile = & wxConvUTF8;
591 #else
592 fileEncoding = wxT("ISO-8859-1");
593 convFile = & wxConvISO8859_1;
594 #endif
595
596 // If SetEncoding has been called, change the output encoding.
597 if (!m_encoding.empty() && m_encoding.Lower() != fileEncoding.Lower())
598 {
599 if (m_encoding == wxT("<System>"))
600 {
601 #if wxUSE_INTL
602 fileEncoding = wxLocale::GetSystemEncodingName();
603 // if !wxUSE_INTL, we fall back to UTF-8 or ISO-8859-1 below
604 #endif
605 }
606 else
607 {
608 fileEncoding = m_encoding;
609 }
610
611 // GetSystemEncodingName may not have returned a name
612 if (fileEncoding.empty())
613 #if wxUSE_UNICODE
614 fileEncoding = wxT("UTF-8");
615 #else
616 fileEncoding = wxT("ISO-8859-1");
617 #endif
618 convFile = new wxCSConv(fileEncoding);
619 deleteConvFile = true;
620 }
621
622 #if !wxUSE_UNICODE
623 wxMBConv* convMem = wxConvCurrent;
624 #else
625 wxMBConv* convMem = NULL;
626 #endif
627
628 wxString s ;
629 s.Printf(wxT("<?xml version=\"%s\" encoding=\"%s\"?>\n"),
630 version, fileEncoding);
631 OutputString(stream, s, NULL, NULL);
632 OutputString(stream, wxT("<richtext version=\"1.0.0.0\" xmlns=\"http://www.wxwidgets.org\">") , NULL, NULL);
633
634 int level = 1;
635
636 if (buffer->GetStyleSheet() && (GetFlags() & wxRICHTEXT_HANDLER_INCLUDE_STYLESHEET))
637 {
638 OutputIndentation(stream, level);
639 wxString nameAndDescr;
640 if (!buffer->GetStyleSheet()->GetName().IsEmpty())
641 nameAndDescr << wxT(" name=\"") << buffer->GetStyleSheet()->GetName() << wxT("\"");
642 if (!buffer->GetStyleSheet()->GetDescription().IsEmpty())
643 nameAndDescr << wxT(" description=\"") << buffer->GetStyleSheet()->GetDescription() << wxT("\"");
644 OutputString(stream, wxString(wxT("<stylesheet")) + nameAndDescr + wxT(">"), convMem, convFile);
645
646 int i;
647
648 for (i = 0; i < (int) buffer->GetStyleSheet()->GetCharacterStyleCount(); i++)
649 {
650 wxRichTextCharacterStyleDefinition* def = buffer->GetStyleSheet()->GetCharacterStyle(i);
651 ExportStyleDefinition(stream, convMem, convFile, def, level + 1);
652 }
653
654 for (i = 0; i < (int) buffer->GetStyleSheet()->GetParagraphStyleCount(); i++)
655 {
656 wxRichTextParagraphStyleDefinition* def = buffer->GetStyleSheet()->GetParagraphStyle(i);
657 ExportStyleDefinition(stream, convMem, convFile, def, level + 1);
658 }
659
660 for (i = 0; i < (int) buffer->GetStyleSheet()->GetListStyleCount(); i++)
661 {
662 wxRichTextListStyleDefinition* def = buffer->GetStyleSheet()->GetListStyle(i);
663 ExportStyleDefinition(stream, convMem, convFile, def, level + 1);
664 }
665
666 OutputIndentation(stream, level);
667 OutputString(stream, wxT("</stylesheet>"), convMem, convFile);
668 }
669
670
671 bool success = ExportXML(stream, convMem, convFile, *buffer, level);
672
673 OutputString(stream, wxT("\n</richtext>") , NULL, NULL);
674 OutputString(stream, wxT("\n"), NULL, NULL);
675
676 if (deleteConvFile)
677 delete convFile;
678
679 return success;
680 }
681
682 /// Recursively export an object
683 bool wxRichTextXMLHandler::ExportXML(wxOutputStream& stream, wxMBConv* convMem, wxMBConv* convFile, wxRichTextObject& obj, int indent)
684 {
685 wxString objectName;
686 if (obj.IsKindOf(CLASSINFO(wxRichTextParagraphLayoutBox)))
687 objectName = wxT("paragraphlayout");
688 else if (obj.IsKindOf(CLASSINFO(wxRichTextParagraph)))
689 objectName = wxT("paragraph");
690 else if (obj.IsKindOf(CLASSINFO(wxRichTextPlainText)))
691 objectName = wxT("text");
692 else if (obj.IsKindOf(CLASSINFO(wxRichTextImage)))
693 objectName = wxT("image");
694 else
695 objectName = wxT("object");
696
697 bool terminateTag = true;
698
699 if (obj.IsKindOf(CLASSINFO(wxRichTextPlainText)))
700 {
701 wxRichTextPlainText& textObj = (wxRichTextPlainText&) obj;
702
703 wxString style = CreateStyle(obj.GetAttributes(), false);
704
705 int i;
706 int last = 0;
707 const wxString& text = textObj.GetText();
708 int len = (int) text.Length();
709 for (i = 0; i < len; i++)
710 {
711 int c = (int) text[i];
712 if (c < 32 && c != 9 && c != 10 && c != 13)
713 {
714 if (i > 0)
715 {
716 OutputIndentation(stream, indent);
717 OutputString(stream, wxT("<") + objectName, convMem, convFile);
718
719 OutputString(stream, style + wxT(">"), convMem, convFile);
720
721 wxString fragment(text.Mid(last, i-last));
722 if (!fragment.empty() && (fragment[0] == wxT(' ') || fragment[fragment.length()-1] == wxT(' ')))
723 {
724 OutputString(stream, wxT("\""), convMem, convFile);
725 OutputStringEnt(stream, fragment, convMem, convFile);
726 OutputString(stream, wxT("\""), convMem, convFile);
727 }
728 else
729 OutputStringEnt(stream, fragment, convMem, convFile);
730
731 OutputString(stream, wxT("</text>"), convMem, convFile);
732 }
733
734
735 // Output this character as a number in a separate tag, because XML can't cope
736 // with entities below 32 except for 9, 10 and 13
737 last = i + 1;
738 OutputIndentation(stream, indent);
739 OutputString(stream, wxT("<symbol"), convMem, convFile);
740
741 OutputString(stream, style + wxT(">"), convMem, convFile);
742 OutputString(stream, wxString::Format(wxT("%d"), c), convMem, convFile);
743
744 OutputString(stream, wxT("</symbol>"), convMem, convFile);
745 }
746 }
747
748 wxString fragment;
749 if (last == 0)
750 fragment = text;
751 else
752 fragment = text.Mid(last, i-last);
753
754 if (last < len)
755 {
756 OutputIndentation(stream, indent);
757 OutputString(stream, wxT("<") + objectName, convMem, convFile);
758
759 OutputString(stream, style + wxT(">"), convMem, convFile);
760
761 if (!fragment.empty() && (fragment[0] == wxT(' ') || fragment[fragment.length()-1] == wxT(' ')))
762 {
763 OutputString(stream, wxT("\""), convMem, convFile);
764 OutputStringEnt(stream, fragment, convMem, convFile);
765 OutputString(stream, wxT("\""), convMem, convFile);
766 }
767 else
768 OutputStringEnt(stream, fragment, convMem, convFile);
769 }
770 else
771 terminateTag = false;
772 }
773 else if (obj.IsKindOf(CLASSINFO(wxRichTextImage)))
774 {
775 wxRichTextImage& imageObj = (wxRichTextImage&) obj;
776
777 wxString style = CreateStyle(obj.GetAttributes(), false);
778
779 if (imageObj.GetImage().Ok() && !imageObj.GetImageBlock().Ok())
780 imageObj.MakeBlock();
781
782 OutputIndentation(stream, indent);
783 OutputString(stream, wxT("<") + objectName, convMem, convFile);
784 if (!imageObj.GetImageBlock().Ok())
785 {
786 // No data
787 OutputString(stream, style + wxT(">"), convMem, convFile);
788 }
789 else
790 {
791 OutputString(stream, wxString::Format(wxT(" imagetype=\"%d\"") + style + wxT(">"), (int) imageObj.GetImageBlock().GetImageType()));
792 }
793
794 OutputIndentation(stream, indent+1);
795 OutputString(stream, wxT("<data>"), convMem, convFile);
796
797 imageObj.GetImageBlock().WriteHex(stream);
798
799 OutputString(stream, wxT("</data>"), convMem, convFile);
800 }
801 else if (obj.IsKindOf(CLASSINFO(wxRichTextCompositeObject)))
802 {
803 OutputIndentation(stream, indent);
804 OutputString(stream, wxT("<") + objectName, convMem, convFile);
805
806 bool isPara = false;
807 if (objectName == wxT("paragraph") || objectName == wxT("paragraphlayout"))
808 isPara = true;
809
810 wxString style = CreateStyle(obj.GetAttributes(), isPara);
811
812 if (objectName == wxT("paragraphlayout") && ((wxRichTextParagraphLayoutBox&) obj).GetPartialParagraph())
813 style << wxT(" partialparagraph=\"true\"");
814
815 OutputString(stream, style + wxT(">"), convMem, convFile);
816
817 wxRichTextCompositeObject& composite = (wxRichTextCompositeObject&) obj;
818 size_t i;
819 for (i = 0; i < composite.GetChildCount(); i++)
820 {
821 wxRichTextObject* child = composite.GetChild(i);
822 ExportXML(stream, convMem, convFile, *child, indent+1);
823 }
824 }
825
826 if (objectName != wxT("text"))
827 OutputIndentation(stream, indent);
828
829 if (terminateTag)
830 OutputString(stream, wxT("</") + objectName + wxT(">"), convMem, convFile);
831
832 return true;
833 }
834
835 bool wxRichTextXMLHandler::ExportStyleDefinition(wxOutputStream& stream, wxMBConv* convMem, wxMBConv* convFile, wxRichTextStyleDefinition* def, int level)
836 {
837 wxRichTextCharacterStyleDefinition* charDef = wxDynamicCast(def, wxRichTextCharacterStyleDefinition);
838 wxRichTextParagraphStyleDefinition* paraDef = wxDynamicCast(def, wxRichTextParagraphStyleDefinition);
839 wxRichTextListStyleDefinition* listDef = wxDynamicCast(def, wxRichTextListStyleDefinition);
840
841 wxString baseStyle = def->GetBaseStyle();
842 wxString baseStyleProp;
843 if (!baseStyle.IsEmpty())
844 baseStyleProp = wxT(" basestyle=\"") + baseStyle + wxT("\"");
845
846 wxString descr = def->GetDescription();
847 wxString descrProp;
848 if (!descr.IsEmpty())
849 descrProp = wxT(" description=\"") + descr + wxT("\"");
850
851 if (charDef)
852 {
853 OutputIndentation(stream, level);
854 OutputString(stream, wxT("<characterstyle") + baseStyleProp + descrProp + wxT(">"), convMem, convFile);
855
856 level ++;
857
858 wxString style = CreateStyle(def->GetStyle(), false);
859
860 OutputIndentation(stream, level);
861 OutputString(stream, wxT("<style ") + style + wxT(">"), convMem, convFile);
862
863 OutputIndentation(stream, level);
864 OutputString(stream, wxT("</style>"), convMem, convFile);
865
866 level --;
867
868 OutputIndentation(stream, level);
869 OutputString(stream, wxT("</characterstyle>"), convMem, convFile);
870 }
871 else if (listDef)
872 {
873 OutputIndentation(stream, level);
874
875 if (!listDef->GetNextStyle().IsEmpty())
876 baseStyleProp << wxT(" basestyle=\"") << listDef->GetNextStyle() << wxT("\"");
877
878 OutputString(stream, wxT("<liststyle") + baseStyleProp + descrProp + wxT(">"), convMem, convFile);
879
880 level ++;
881
882 wxString style = CreateStyle(def->GetStyle(), false);
883
884 OutputIndentation(stream, level);
885 OutputString(stream, wxT("<style ") + style + wxT(">"), convMem, convFile);
886
887 OutputIndentation(stream, level);
888 OutputString(stream, wxT("</style>"), convMem, convFile);
889
890 int i;
891 for (i = 0; i < 10; i ++)
892 {
893 wxTextAttr* levelAttr = listDef->GetLevelAttributes(i);
894 if (levelAttr)
895 {
896 wxString style = CreateStyle(def->GetStyle(), false);
897 wxString levelStr = wxString::Format(wxT(" level=\"%d\" "), (i+1));
898
899 OutputIndentation(stream, level);
900 OutputString(stream, wxT("<style ") + levelStr + style + wxT(">"), convMem, convFile);
901
902 OutputIndentation(stream, level);
903 OutputString(stream, wxT("</style>"), convMem, convFile);
904 }
905 }
906
907 level --;
908
909 OutputIndentation(stream, level);
910 OutputString(stream, wxT("</liststyle>"), convMem, convFile);
911 }
912 else if (paraDef)
913 {
914 OutputIndentation(stream, level);
915
916 if (!paraDef->GetNextStyle().IsEmpty())
917 baseStyleProp << wxT(" basestyle=\"") << paraDef->GetNextStyle() << wxT("\"");
918
919 OutputString(stream, wxT("<paragraphstyle") + baseStyleProp + descrProp + wxT(">"), convMem, convFile);
920
921 level ++;
922
923 wxString style = CreateStyle(def->GetStyle(), false);
924
925 OutputIndentation(stream, level);
926 OutputString(stream, wxT("<style ") + style + wxT(">"), convMem, convFile);
927
928 OutputIndentation(stream, level);
929 OutputString(stream, wxT("</style>"), convMem, convFile);
930
931 level --;
932
933 OutputIndentation(stream, level);
934 OutputString(stream, wxT("</paragraphstyle>"), convMem, convFile);
935 }
936
937 return true;
938 }
939
940 /// Create style parameters
941 wxString wxRichTextXMLHandler::CreateStyle(const wxTextAttr& attr, bool isPara)
942 {
943 wxString str;
944 if (attr.HasTextColour() && attr.GetTextColour().Ok())
945 {
946 str << wxT(" textcolor=\"#") << ColourToHexString(attr.GetTextColour()) << wxT("\"");
947 }
948 if (attr.HasBackgroundColour() && attr.GetBackgroundColour().Ok())
949 {
950 str << wxT(" bgcolor=\"#") << ColourToHexString(attr.GetBackgroundColour()) << wxT("\"");
951 }
952
953 if (attr.HasFontSize())
954 str << wxT(" fontsize=\"") << attr.GetFontSize() << wxT("\"");
955
956 //if (attr.HasFontFamily())
957 // str << wxT(" fontfamily=\"") << attr.GetFont().GetFamily() << wxT("\"");
958
959 if (attr.HasFontItalic())
960 str << wxT(" fontstyle=\"") << attr.GetFontStyle() << wxT("\"");
961
962 if (attr.HasFontWeight())
963 str << wxT(" fontweight=\"") << attr.GetFontWeight() << wxT("\"");
964
965 if (attr.HasFontUnderlined())
966 str << wxT(" fontunderlined=\"") << (int) attr.GetFontUnderlined() << wxT("\"");
967
968 if (attr.HasFontFaceName())
969 str << wxT(" fontface=\"") << attr.GetFontFaceName() << wxT("\"");
970
971 if (attr.HasTextEffects())
972 {
973 str << wxT(" texteffects=\"");
974 str << attr.GetTextEffects();
975 str << wxT("\"");
976
977 str << wxT(" texteffectflags=\"");
978 str << attr.GetTextEffectFlags();
979 str << wxT("\"");
980 }
981
982 if (!attr.GetCharacterStyleName().empty())
983 str << wxT(" characterstyle=\"") << wxString(attr.GetCharacterStyleName()) << wxT("\"");
984
985 if (attr.HasURL())
986 str << wxT(" url=\"") << AttributeToXML(attr.GetURL()) << wxT("\"");
987
988 if (isPara)
989 {
990 if (attr.HasAlignment())
991 str << wxT(" alignment=\"") << (int) attr.GetAlignment() << wxT("\"");
992
993 if (attr.HasLeftIndent())
994 {
995 str << wxT(" leftindent=\"") << (int) attr.GetLeftIndent() << wxT("\"");
996 str << wxT(" leftsubindent=\"") << (int) attr.GetLeftSubIndent() << wxT("\"");
997 }
998
999 if (attr.HasRightIndent())
1000 str << wxT(" rightindent=\"") << (int) attr.GetRightIndent() << wxT("\"");
1001
1002 if (attr.HasParagraphSpacingAfter())
1003 str << wxT(" parspacingafter=\"") << (int) attr.GetParagraphSpacingAfter() << wxT("\"");
1004
1005 if (attr.HasParagraphSpacingBefore())
1006 str << wxT(" parspacingbefore=\"") << (int) attr.GetParagraphSpacingBefore() << wxT("\"");
1007
1008 if (attr.HasLineSpacing())
1009 str << wxT(" linespacing=\"") << (int) attr.GetLineSpacing() << wxT("\"");
1010
1011 if (attr.HasBulletStyle())
1012 str << wxT(" bulletstyle=\"") << (int) attr.GetBulletStyle() << wxT("\"");
1013
1014 if (attr.HasBulletNumber())
1015 str << wxT(" bulletnumber=\"") << (int) attr.GetBulletNumber() << wxT("\"");
1016
1017 if (attr.HasBulletText())
1018 {
1019 // If using a bullet symbol, convert to integer in case it's a non-XML-friendly character.
1020 // Otherwise, assume it's XML-friendly text such as outline numbering, e.g. 1.2.3.1
1021 if (!attr.GetBulletText().IsEmpty() && (attr.GetBulletStyle() & wxTEXT_ATTR_BULLET_STYLE_SYMBOL))
1022 str << wxT(" bulletsymbol=\"") << (int) (attr.GetBulletText()[0]) << wxT("\"");
1023 else
1024 str << wxT(" bullettext=\"") << attr.GetBulletText() << wxT("\"");
1025
1026 str << wxT(" bulletfont=\"") << attr.GetBulletFont() << wxT("\"");
1027 }
1028
1029 if (attr.HasBulletName())
1030 str << wxT(" bulletname=\"") << attr.GetBulletName() << wxT("\"");
1031
1032 if (!attr.GetParagraphStyleName().empty())
1033 str << wxT(" parstyle=\"") << wxString(attr.GetParagraphStyleName()) << wxT("\"");
1034
1035 if (!attr.GetListStyleName().empty())
1036 str << wxT(" liststyle=\"") << wxString(attr.GetListStyleName()) << wxT("\"");
1037
1038 if (attr.HasTabs())
1039 {
1040 str << wxT(" tabs=\"");
1041 size_t i;
1042 for (i = 0; i < attr.GetTabs().GetCount(); i++)
1043 {
1044 if (i > 0)
1045 str << wxT(",");
1046 str << attr.GetTabs()[i];
1047 }
1048 str << wxT("\"");
1049 }
1050
1051 if (attr.HasPageBreak())
1052 {
1053 str << wxT(" pagebreak=\"1\"");
1054 }
1055
1056 if (attr.HasOutlineLevel())
1057 str << wxT(" outlinelevel=\"") << (int) attr.GetOutlineLevel() << wxT("\"");
1058
1059 }
1060
1061 return str;
1062 }
1063
1064 /// Get style parameters
1065 bool wxRichTextXMLHandler::GetStyle(wxTextAttr& attr, wxXmlNode* node, bool isPara)
1066 {
1067 wxString fontFacename;
1068 int fontSize = 12;
1069 // int fontFamily = wxDEFAULT;
1070 int fontWeight = wxNORMAL;
1071 int fontStyle = wxNORMAL;
1072 bool fontUnderlined = false;
1073
1074 // int fontFlags = 0;
1075
1076 fontFacename = node->GetAttribute(wxT("fontface"), wxEmptyString);
1077 if (!fontFacename.IsEmpty())
1078 attr.SetFontFaceName(fontFacename);
1079
1080 wxString value;
1081 //value = node->GetAttribute(wxT("fontfamily"), wxEmptyString);
1082 //if (!value.empty())
1083 // fontFamily = wxAtoi(value);
1084
1085 value = node->GetAttribute(wxT("fontstyle"), wxEmptyString);
1086 if (!value.empty())
1087 {
1088 fontStyle = wxAtoi(value);
1089 attr.SetFontStyle(fontStyle);
1090 }
1091
1092 value = node->GetAttribute(wxT("fontsize"), wxEmptyString);
1093 if (!value.empty())
1094 {
1095 fontSize = wxAtoi(value);
1096 attr.SetFontSize(fontSize);
1097 }
1098
1099 value = node->GetAttribute(wxT("fontweight"), wxEmptyString);
1100 if (!value.empty())
1101 {
1102 fontWeight = wxAtoi(value);
1103 attr.SetFontWeight(fontWeight);
1104 }
1105
1106 value = node->GetAttribute(wxT("fontunderlined"), wxEmptyString);
1107 if (!value.empty())
1108 {
1109 fontUnderlined = wxAtoi(value) != 0;
1110 attr.SetFontUnderlined(fontUnderlined);
1111 }
1112
1113 value = node->GetAttribute(wxT("textcolor"), wxEmptyString);
1114 if (!value.empty())
1115 {
1116 if (value[0] == wxT('#'))
1117 attr.SetTextColour(HexStringToColour(value.Mid(1)));
1118 else
1119 attr.SetTextColour(value);
1120 }
1121
1122 value = node->GetAttribute(wxT("bgcolor"), wxEmptyString);
1123 if (!value.empty())
1124 {
1125 if (value[0] == wxT('#'))
1126 attr.SetBackgroundColour(HexStringToColour(value.Mid(1)));
1127 else
1128 attr.SetBackgroundColour(value);
1129 }
1130
1131 value = node->GetAttribute(wxT("characterstyle"), wxEmptyString);
1132 if (!value.empty())
1133 attr.SetCharacterStyleName(value);
1134
1135 value = node->GetAttribute(wxT("texteffects"), wxEmptyString);
1136 if (!value.IsEmpty())
1137 {
1138 attr.SetTextEffects(wxAtoi(value));
1139 }
1140
1141 value = node->GetAttribute(wxT("texteffectflags"), wxEmptyString);
1142 if (!value.IsEmpty())
1143 {
1144 attr.SetTextEffectFlags(wxAtoi(value));
1145 }
1146
1147 value = node->GetAttribute(wxT("url"), wxEmptyString);
1148 if (!value.empty())
1149 attr.SetURL(value);
1150
1151 // Set paragraph attributes
1152 if (isPara)
1153 {
1154 value = node->GetAttribute(wxT("alignment"), wxEmptyString);
1155 if (!value.empty())
1156 attr.SetAlignment((wxTextAttrAlignment) wxAtoi(value));
1157
1158 int leftSubIndent = 0;
1159 int leftIndent = 0;
1160 bool hasLeftIndent = false;
1161
1162 value = node->GetAttribute(wxT("leftindent"), wxEmptyString);
1163 if (!value.empty())
1164 {
1165 leftIndent = wxAtoi(value);
1166 hasLeftIndent = true;
1167 }
1168
1169 value = node->GetAttribute(wxT("leftsubindent"), wxEmptyString);
1170 if (!value.empty())
1171 {
1172 leftSubIndent = wxAtoi(value);
1173 hasLeftIndent = true;
1174 }
1175
1176 if (hasLeftIndent)
1177 attr.SetLeftIndent(leftIndent, leftSubIndent);
1178
1179 value = node->GetAttribute(wxT("rightindent"), wxEmptyString);
1180 if (!value.empty())
1181 attr.SetRightIndent(wxAtoi(value));
1182
1183 value = node->GetAttribute(wxT("parspacingbefore"), wxEmptyString);
1184 if (!value.empty())
1185 attr.SetParagraphSpacingBefore(wxAtoi(value));
1186
1187 value = node->GetAttribute(wxT("parspacingafter"), wxEmptyString);
1188 if (!value.empty())
1189 attr.SetParagraphSpacingAfter(wxAtoi(value));
1190
1191 value = node->GetAttribute(wxT("linespacing"), wxEmptyString);
1192 if (!value.empty())
1193 attr.SetLineSpacing(wxAtoi(value));
1194
1195 value = node->GetAttribute(wxT("bulletstyle"), wxEmptyString);
1196 if (!value.empty())
1197 attr.SetBulletStyle(wxAtoi(value));
1198
1199 value = node->GetAttribute(wxT("bulletnumber"), wxEmptyString);
1200 if (!value.empty())
1201 attr.SetBulletNumber(wxAtoi(value));
1202
1203 value = node->GetAttribute(wxT("bulletsymbol"), wxEmptyString);
1204 if (!value.empty())
1205 {
1206 wxChar ch = wxAtoi(value);
1207 wxString s;
1208 s << ch;
1209 attr.SetBulletText(s);
1210 }
1211
1212 value = node->GetAttribute(wxT("bullettext"), wxEmptyString);
1213 if (!value.empty())
1214 attr.SetBulletText(value);
1215
1216 value = node->GetAttribute(wxT("bulletfont"), wxEmptyString);
1217 if (!value.empty())
1218 attr.SetBulletFont(value);
1219
1220 value = node->GetAttribute(wxT("bulletname"), wxEmptyString);
1221 if (!value.empty())
1222 attr.SetBulletName(value);
1223
1224 value = node->GetAttribute(wxT("parstyle"), wxEmptyString);
1225 if (!value.empty())
1226 attr.SetParagraphStyleName(value);
1227
1228 value = node->GetAttribute(wxT("liststyle"), wxEmptyString);
1229 if (!value.empty())
1230 attr.SetListStyleName(value);
1231
1232 value = node->GetAttribute(wxT("tabs"), wxEmptyString);
1233 if (!value.empty())
1234 {
1235 wxArrayInt tabs;
1236 wxStringTokenizer tkz(value, wxT(","));
1237 while (tkz.HasMoreTokens())
1238 {
1239 wxString token = tkz.GetNextToken();
1240 tabs.Add(wxAtoi(token));
1241 }
1242 attr.SetTabs(tabs);
1243 }
1244
1245 value = node->GetAttribute(wxT("pagebreak"), wxEmptyString);
1246 if (!value.IsEmpty())
1247 {
1248 attr.SetPageBreak(wxAtoi(value) != 0);
1249 }
1250
1251 value = node->GetAttribute(wxT("outlinelevel"), wxEmptyString);
1252 if (!value.IsEmpty())
1253 {
1254 attr.SetOutlineLevel(wxAtoi(value));
1255 }
1256 }
1257
1258 return true;
1259 }
1260
1261 #endif
1262 // wxUSE_STREAMS
1263
1264 #endif
1265 // wxUSE_RICHTEXT && wxUSE_XML
1266