1 /*
2 * Copyright 2006 The Android Open Source Project
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include "src/xml/SkXMLWriter.h"
9
10 #include "include/core/SkStream.h"
11 #include "include/private/SkTo.h"
12
SkXMLWriter(bool doEscapeMarkup)13 SkXMLWriter::SkXMLWriter(bool doEscapeMarkup) : fDoEscapeMarkup(doEscapeMarkup)
14 {}
15
~SkXMLWriter()16 SkXMLWriter::~SkXMLWriter() {
17 SkASSERT(fElems.count() == 0);
18 }
19
flush()20 void SkXMLWriter::flush() {
21 while (fElems.count()) {
22 this->endElement();
23 }
24 }
25
addAttribute(const char name[],const char value[])26 void SkXMLWriter::addAttribute(const char name[], const char value[]) {
27 this->addAttributeLen(name, value, strlen(value));
28 }
29
addS32Attribute(const char name[],int32_t value)30 void SkXMLWriter::addS32Attribute(const char name[], int32_t value) {
31 SkString tmp;
32 tmp.appendS32(value);
33 this->addAttribute(name, tmp.c_str());
34 }
35
addHexAttribute(const char name[],uint32_t value,int minDigits)36 void SkXMLWriter::addHexAttribute(const char name[], uint32_t value, int minDigits) {
37 SkString tmp("0x");
38 tmp.appendHex(value, minDigits);
39 this->addAttribute(name, tmp.c_str());
40 }
41
addScalarAttribute(const char name[],SkScalar value)42 void SkXMLWriter::addScalarAttribute(const char name[], SkScalar value) {
43 SkString tmp;
44 tmp.appendScalar(value);
45 this->addAttribute(name, tmp.c_str());
46 }
47
addText(const char text[],size_t length)48 void SkXMLWriter::addText(const char text[], size_t length) {
49 if (fElems.isEmpty()) {
50 return;
51 }
52
53 this->onAddText(text, length);
54
55 fElems.top()->fHasText = true;
56 }
57
doEnd(Elem * elem)58 void SkXMLWriter::doEnd(Elem* elem) {
59 delete elem;
60 }
61
doStart(const char name[],size_t length)62 bool SkXMLWriter::doStart(const char name[], size_t length) {
63 int level = fElems.count();
64 bool firstChild = level > 0 && !fElems[level-1]->fHasChildren;
65 if (firstChild) {
66 fElems[level-1]->fHasChildren = true;
67 }
68 Elem** elem = fElems.push();
69 *elem = new Elem(name, length);
70 return firstChild;
71 }
72
getEnd()73 SkXMLWriter::Elem* SkXMLWriter::getEnd() {
74 Elem* elem;
75 fElems.pop(&elem);
76 return elem;
77 }
78
getHeader()79 const char* SkXMLWriter::getHeader() {
80 static const char gHeader[] = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>";
81 return gHeader;
82 }
83
startElement(const char name[])84 void SkXMLWriter::startElement(const char name[]) {
85 this->startElementLen(name, strlen(name));
86 }
87
escape_char(char c,char storage[2])88 static const char* escape_char(char c, char storage[2]) {
89 static const char* gEscapeChars[] = {
90 "<<",
91 ">>",
92 //"\""",
93 //"''",
94 "&&"
95 };
96
97 const char** array = gEscapeChars;
98 for (unsigned i = 0; i < SK_ARRAY_COUNT(gEscapeChars); i++) {
99 if (array[i][0] == c) {
100 return &array[i][1];
101 }
102 }
103 storage[0] = c;
104 storage[1] = 0;
105 return storage;
106 }
107
escape_markup(char dst[],const char src[],size_t length)108 static size_t escape_markup(char dst[], const char src[], size_t length) {
109 size_t extra = 0;
110 const char* stop = src + length;
111
112 while (src < stop) {
113 char orig[2];
114 const char* seq = escape_char(*src, orig);
115 size_t seqSize = strlen(seq);
116
117 if (dst) {
118 memcpy(dst, seq, seqSize);
119 dst += seqSize;
120 }
121
122 // now record the extra size needed
123 extra += seqSize - 1; // minus one to subtract the original char
124
125 // bump to the next src char
126 src += 1;
127 }
128 return extra;
129 }
130
addAttributeLen(const char name[],const char value[],size_t length)131 void SkXMLWriter::addAttributeLen(const char name[], const char value[], size_t length) {
132 SkString valueStr;
133
134 if (fDoEscapeMarkup) {
135 size_t extra = escape_markup(nullptr, value, length);
136 if (extra) {
137 valueStr.resize(length + extra);
138 (void)escape_markup(valueStr.writable_str(), value, length);
139 value = valueStr.c_str();
140 length += extra;
141 }
142 }
143 this->onAddAttributeLen(name, value, length);
144 }
145
startElementLen(const char elem[],size_t length)146 void SkXMLWriter::startElementLen(const char elem[], size_t length) {
147 this->onStartElementLen(elem, length);
148 }
149
150 ////////////////////////////////////////////////////////////////////////////////////////
151
write_dom(const SkDOM & dom,const SkDOM::Node * node,SkXMLWriter * w,bool skipRoot)152 static void write_dom(const SkDOM& dom, const SkDOM::Node* node, SkXMLWriter* w, bool skipRoot) {
153 if (!skipRoot) {
154 const char* elem = dom.getName(node);
155 if (dom.getType(node) == SkDOM::kText_Type) {
156 SkASSERT(dom.countChildren(node) == 0);
157 w->addText(elem, strlen(elem));
158 return;
159 }
160
161 w->startElement(elem);
162
163 SkDOM::AttrIter iter(dom, node);
164 const char* name;
165 const char* value;
166 while ((name = iter.next(&value)) != nullptr) {
167 w->addAttribute(name, value);
168 }
169 }
170
171 node = dom.getFirstChild(node, nullptr);
172 while (node) {
173 write_dom(dom, node, w, false);
174 node = dom.getNextSibling(node, nullptr);
175 }
176
177 if (!skipRoot) {
178 w->endElement();
179 }
180 }
181
writeDOM(const SkDOM & dom,const SkDOM::Node * node,bool skipRoot)182 void SkXMLWriter::writeDOM(const SkDOM& dom, const SkDOM::Node* node, bool skipRoot) {
183 if (node) {
184 write_dom(dom, node, this, skipRoot);
185 }
186 }
187
writeHeader()188 void SkXMLWriter::writeHeader()
189 {}
190
191 // SkXMLStreamWriter
192
tab(SkWStream & stream,int level)193 static void tab(SkWStream& stream, int level) {
194 for (int i = 0; i < level; i++) {
195 stream.writeText("\t");
196 }
197 }
198
SkXMLStreamWriter(SkWStream * stream)199 SkXMLStreamWriter::SkXMLStreamWriter(SkWStream* stream) : fStream(*stream)
200 {}
201
~SkXMLStreamWriter()202 SkXMLStreamWriter::~SkXMLStreamWriter() {
203 this->flush();
204 }
205
onAddAttributeLen(const char name[],const char value[],size_t length)206 void SkXMLStreamWriter::onAddAttributeLen(const char name[], const char value[], size_t length) {
207 SkASSERT(!fElems.top()->fHasChildren && !fElems.top()->fHasText);
208 fStream.writeText(" ");
209 fStream.writeText(name);
210 fStream.writeText("=\"");
211 fStream.write(value, length);
212 fStream.writeText("\"");
213 }
214
onAddText(const char text[],size_t length)215 void SkXMLStreamWriter::onAddText(const char text[], size_t length) {
216 Elem* elem = fElems.top();
217
218 if (!elem->fHasChildren && !elem->fHasText) {
219 fStream.writeText(">");
220 fStream.newline();
221 }
222
223 tab(fStream, fElems.count() + 1);
224 fStream.write(text, length);
225 fStream.newline();
226 }
227
onEndElement()228 void SkXMLStreamWriter::onEndElement() {
229 Elem* elem = getEnd();
230 if (elem->fHasChildren || elem->fHasText) {
231 tab(fStream, fElems.count());
232 fStream.writeText("</");
233 fStream.writeText(elem->fName.c_str());
234 fStream.writeText(">");
235 } else {
236 fStream.writeText("/>");
237 }
238 fStream.newline();
239 doEnd(elem);
240 }
241
onStartElementLen(const char name[],size_t length)242 void SkXMLStreamWriter::onStartElementLen(const char name[], size_t length) {
243 int level = fElems.count();
244 if (this->doStart(name, length)) {
245 // the first child, need to close with >
246 fStream.writeText(">");
247 fStream.newline();
248 }
249
250 tab(fStream, level);
251 fStream.writeText("<");
252 fStream.write(name, length);
253 }
254
writeHeader()255 void SkXMLStreamWriter::writeHeader() {
256 const char* header = getHeader();
257 fStream.write(header, strlen(header));
258 fStream.newline();
259 }
260
261 ////////////////////////////////////////////////////////////////////////////////////////////////
262
263 #include "src/xml/SkXMLParser.h"
264
SkXMLParserWriter(SkXMLParser * parser)265 SkXMLParserWriter::SkXMLParserWriter(SkXMLParser* parser)
266 : SkXMLWriter(false), fParser(*parser)
267 {
268 }
269
~SkXMLParserWriter()270 SkXMLParserWriter::~SkXMLParserWriter() {
271 this->flush();
272 }
273
onAddAttributeLen(const char name[],const char value[],size_t length)274 void SkXMLParserWriter::onAddAttributeLen(const char name[], const char value[], size_t length) {
275 SkASSERT(fElems.count() == 0 || (!fElems.top()->fHasChildren && !fElems.top()->fHasText));
276 SkString str(value, length);
277 fParser.addAttribute(name, str.c_str());
278 }
279
onAddText(const char text[],size_t length)280 void SkXMLParserWriter::onAddText(const char text[], size_t length) {
281 fParser.text(text, SkToInt(length));
282 }
283
onEndElement()284 void SkXMLParserWriter::onEndElement() {
285 Elem* elem = this->getEnd();
286 fParser.endElement(elem->fName.c_str());
287 this->doEnd(elem);
288 }
289
onStartElementLen(const char name[],size_t length)290 void SkXMLParserWriter::onStartElementLen(const char name[], size_t length) {
291 (void)this->doStart(name, length);
292 SkString str(name, length);
293 fParser.startElement(str.c_str());
294 }
295