]>
git.saurik.com Git - wxWidgets.git/blob - src/xrc/xmlwrite.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxXmlDocument - XML text writer
4 // Author: Vaclav Slavik
7 // Copyright: (c) 2001 Vaclav Slavik
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
12 // nothing - already in xml.cpp
15 // For compilers that support precompilation, includes "wx.h".
16 #include "wx/wxprec.h"
22 #include "wx/wfstream.h"
25 #include "wx/strconv.h"
26 #include "wx/xrc/xml.h"
27 #include "wx/xrc/xmlio.h"
29 // write string to output:
30 inline static void OutputString(wxOutputStream
& stream
, const wxString
& str
)
32 if (str
.IsEmpty()) return;
34 const char *buf
= str
.mb_str(wxConvUTF8
);
35 stream
.Write(buf
, strlen(buf
));
37 stream
.Write(str
.mb_str(), str
.Len());
41 // Same as above, but create entities first.
42 // Translates '<' to "<", '>' to ">" and '&' to "&"
43 static void OutputStringEnt(wxOutputStream
& stream
, const wxString
& str
)
51 for (i
= 0; i
< len
; i
++)
54 if (c
== wxT('<') || c
== wxT('>') ||
55 (c
== wxT('&') && str
.Mid(i
+1, 4) != wxT("amp;")))
57 OutputString(stream
, str
.Mid(last
, i
- last
));
60 case wxT('<'): OutputString(stream
, wxT("<")); break;
61 case wxT('>'): OutputString(stream
, wxT(">")); break;
62 case wxT('&'): OutputString(stream
, wxT("&")); break;
68 OutputString(stream
, str
.Mid(last
, i
- last
));
71 inline static void OutputIndentation(wxOutputStream
& stream
, int indent
)
73 wxString str
= wxT("\n");
74 for (int i
= 0; i
< indent
; i
++)
75 str
<< wxT(' ') << wxT(' ');
76 OutputString(stream
, str
);
79 static void OutputNode(wxOutputStream
& stream
, wxXmlNode
*node
, int indent
)
84 switch (node
->GetType())
87 OutputStringEnt(stream
, node
->GetContent());
90 case wxXML_ELEMENT_NODE
:
91 OutputString(stream
, wxT("<"));
92 OutputString(stream
, node
->GetName());
94 prop
= node
->GetProperties();
97 OutputString(stream
, wxT(" ") + prop
->GetName() +
98 wxT("=\"") + prop
->GetValue() + wxT("\""));
99 // FIXME - what if prop contains '"'?
100 prop
= prop
->GetNext();
103 if (node
->GetChildren())
105 OutputString(stream
, wxT(">"));
107 n
= node
->GetChildren();
110 if (n
&& n
->GetType() != wxXML_TEXT_NODE
)
111 OutputIndentation(stream
, indent
+ 1);
112 OutputNode(stream
, n
, indent
+ 1);
116 if (prev
&& prev
->GetType() != wxXML_TEXT_NODE
)
117 OutputIndentation(stream
, indent
);
118 OutputString(stream
, wxT("</"));
119 OutputString(stream
, node
->GetName());
120 OutputString(stream
, wxT(">"));
123 OutputString(stream
, wxT("/>"));
126 case wxXML_COMMENT_NODE
:
127 OutputString(stream
, wxT("<!--"));
128 OutputString(stream
, node
->GetContent());
129 OutputString(stream
, wxT("-->"));
133 wxFAIL_MSG(wxT("unsupported node type"));
137 bool wxXmlIOHandlerWriter::Save(wxOutputStream
& stream
, const wxXmlDocument
& doc
)
144 s
= wxT("<?xml version=\"") + doc
.GetVersion() +
145 wxT("\" encoding=\"utf-8\"?>\n");
146 OutputString(stream
, s
);
148 OutputNode(stream
, doc
.GetRoot(), 0);
149 OutputString(stream
, wxT("\n"));