blob: 0be9c5560a9508bf30e7b840f65a20363372bfb1 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
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
reed@android.com8a1c16f2008-12-17 15:59:43 +00008#include "SkXMLWriter.h"
Hal Canaryc640d0d2018-06-13 09:59:02 -04009
reed@android.com8a1c16f2008-12-17 15:59:43 +000010#include "SkStream.h"
Hal Canaryc640d0d2018-06-13 09:59:02 -040011#include "SkTo.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000012
13SkXMLWriter::SkXMLWriter(bool doEscapeMarkup) : fDoEscapeMarkup(doEscapeMarkup)
Mike Reedc3063e52017-01-07 16:16:02 -050014{}
reed@android.com8a1c16f2008-12-17 15:59:43 +000015
Mike Reedc3063e52017-01-07 16:16:02 -050016SkXMLWriter::~SkXMLWriter() {
reed@android.com8a1c16f2008-12-17 15:59:43 +000017 SkASSERT(fElems.count() == 0);
18}
19
Mike Reedc3063e52017-01-07 16:16:02 -050020void SkXMLWriter::flush() {
21 while (fElems.count()) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000022 this->endElement();
Mike Reedc3063e52017-01-07 16:16:02 -050023 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000024}
25
Mike Reedc3063e52017-01-07 16:16:02 -050026void SkXMLWriter::addAttribute(const char name[], const char value[]) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000027 this->addAttributeLen(name, value, strlen(value));
28}
29
Mike Reedc3063e52017-01-07 16:16:02 -050030void SkXMLWriter::addS32Attribute(const char name[], int32_t value) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000031 SkString tmp;
32 tmp.appendS32(value);
33 this->addAttribute(name, tmp.c_str());
34}
35
Mike Reedc3063e52017-01-07 16:16:02 -050036void SkXMLWriter::addHexAttribute(const char name[], uint32_t value, int minDigits) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000037 SkString tmp("0x");
38 tmp.appendHex(value, minDigits);
39 this->addAttribute(name, tmp.c_str());
40}
41
Mike Reedc3063e52017-01-07 16:16:02 -050042void SkXMLWriter::addScalarAttribute(const char name[], SkScalar value) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000043 SkString tmp;
44 tmp.appendScalar(value);
45 this->addAttribute(name, tmp.c_str());
46}
47
reede73da402015-02-04 18:29:27 -080048void SkXMLWriter::addText(const char text[], size_t length) {
fmalitafe3f2602015-02-03 17:47:12 -080049 if (fElems.isEmpty()) {
50 return;
51 }
Ben Wagner63fd7602017-10-09 15:45:33 -040052
reede73da402015-02-04 18:29:27 -080053 this->onAddText(text, length);
Ben Wagner63fd7602017-10-09 15:45:33 -040054
fmalitafe3f2602015-02-03 17:47:12 -080055 fElems.top()->fHasText = true;
56}
57
Mike Reedc3063e52017-01-07 16:16:02 -050058void SkXMLWriter::doEnd(Elem* elem) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000059 delete elem;
60}
61
Mike Reedc3063e52017-01-07 16:16:02 -050062bool SkXMLWriter::doStart(const char name[], size_t length) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000063 int level = fElems.count();
64 bool firstChild = level > 0 && !fElems[level-1]->fHasChildren;
Mike Reedc3063e52017-01-07 16:16:02 -050065 if (firstChild) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000066 fElems[level-1]->fHasChildren = true;
Mike Reedc3063e52017-01-07 16:16:02 -050067 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000068 Elem** elem = fElems.push();
fmalitafe3f2602015-02-03 17:47:12 -080069 *elem = new Elem(name, length);
reed@android.com8a1c16f2008-12-17 15:59:43 +000070 return firstChild;
71}
72
Mike Reedc3063e52017-01-07 16:16:02 -050073SkXMLWriter::Elem* SkXMLWriter::getEnd() {
reed@android.com8a1c16f2008-12-17 15:59:43 +000074 Elem* elem;
75 fElems.pop(&elem);
76 return elem;
77}
78
Mike Reedc3063e52017-01-07 16:16:02 -050079const char* SkXMLWriter::getHeader() {
reed@android.com8a1c16f2008-12-17 15:59:43 +000080 static const char gHeader[] = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>";
81 return gHeader;
82}
83
Mike Reedc3063e52017-01-07 16:16:02 -050084void SkXMLWriter::startElement(const char name[]) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000085 this->startElementLen(name, strlen(name));
86}
87
Mike Reedc3063e52017-01-07 16:16:02 -050088static const char* escape_char(char c, char storage[2]) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000089 static const char* gEscapeChars[] = {
90 "<&lt;",
91 ">&gt;",
92 //"\"&quot;",
93 //"'&apos;",
94 "&&amp;"
95 };
96
97 const char** array = gEscapeChars;
Mike Reedc3063e52017-01-07 16:16:02 -050098 for (unsigned i = 0; i < SK_ARRAY_COUNT(gEscapeChars); i++) {
99 if (array[i][0] == c) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000100 return &array[i][1];
Mike Reedc3063e52017-01-07 16:16:02 -0500101 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000102 }
103 storage[0] = c;
104 storage[1] = 0;
105 return storage;
106}
107
Mike Reedc3063e52017-01-07 16:16:02 -0500108static size_t escape_markup(char dst[], const char src[], size_t length) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000109 size_t extra = 0;
110 const char* stop = src + length;
111
Mike Reedc3063e52017-01-07 16:16:02 -0500112 while (src < stop) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000113 char orig[2];
114 const char* seq = escape_char(*src, orig);
115 size_t seqSize = strlen(seq);
116
Mike Reedc3063e52017-01-07 16:16:02 -0500117 if (dst) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000118 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
Mike Reedc3063e52017-01-07 16:16:02 -0500131void SkXMLWriter::addAttributeLen(const char name[], const char value[], size_t length) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000132 SkString valueStr;
133
Mike Reedc3063e52017-01-07 16:16:02 -0500134 if (fDoEscapeMarkup) {
halcanary96fcdcc2015-08-27 07:41:13 -0700135 size_t extra = escape_markup(nullptr, value, length);
Mike Reedc3063e52017-01-07 16:16:02 -0500136 if (extra) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000137 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
Mike Reedc3063e52017-01-07 16:16:02 -0500146void SkXMLWriter::startElementLen(const char elem[], size_t length) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000147 this->onStartElementLen(elem, length);
148}
149
150////////////////////////////////////////////////////////////////////////////////////////
151
Mike Reedc3063e52017-01-07 16:16:02 -0500152static void write_dom(const SkDOM& dom, const SkDOM::Node* node, SkXMLWriter* w, bool skipRoot) {
153 if (!skipRoot) {
fmalita7a048692015-02-20 13:54:40 -0800154 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);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000162
163 SkDOM::AttrIter iter(dom, node);
164 const char* name;
165 const char* value;
Mike Reedc3063e52017-01-07 16:16:02 -0500166 while ((name = iter.next(&value)) != nullptr) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000167 w->addAttribute(name, value);
Mike Reedc3063e52017-01-07 16:16:02 -0500168 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000169 }
170
halcanary96fcdcc2015-08-27 07:41:13 -0700171 node = dom.getFirstChild(node, nullptr);
Mike Reedc3063e52017-01-07 16:16:02 -0500172 while (node) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000173 write_dom(dom, node, w, false);
halcanary96fcdcc2015-08-27 07:41:13 -0700174 node = dom.getNextSibling(node, nullptr);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000175 }
176
Mike Reedc3063e52017-01-07 16:16:02 -0500177 if (!skipRoot) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000178 w->endElement();
Mike Reedc3063e52017-01-07 16:16:02 -0500179 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000180}
181
Mike Reedc3063e52017-01-07 16:16:02 -0500182void SkXMLWriter::writeDOM(const SkDOM& dom, const SkDOM::Node* node, bool skipRoot) {
183 if (node) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000184 write_dom(dom, node, this, skipRoot);
Mike Reedc3063e52017-01-07 16:16:02 -0500185 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000186}
187
188void SkXMLWriter::writeHeader()
Mike Reedc3063e52017-01-07 16:16:02 -0500189{}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000190
191// SkXMLStreamWriter
192
Mike Reedc3063e52017-01-07 16:16:02 -0500193static void tab(SkWStream& stream, int level) {
194 for (int i = 0; i < level; i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000195 stream.writeText("\t");
Mike Reedc3063e52017-01-07 16:16:02 -0500196 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000197}
198
199SkXMLStreamWriter::SkXMLStreamWriter(SkWStream* stream) : fStream(*stream)
Mike Reedc3063e52017-01-07 16:16:02 -0500200{}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000201
Mike Reedc3063e52017-01-07 16:16:02 -0500202SkXMLStreamWriter::~SkXMLStreamWriter() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000203 this->flush();
204}
205
Mike Reedc3063e52017-01-07 16:16:02 -0500206void SkXMLStreamWriter::onAddAttributeLen(const char name[], const char value[], size_t length) {
fmalitafe3f2602015-02-03 17:47:12 -0800207 SkASSERT(!fElems.top()->fHasChildren && !fElems.top()->fHasText);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000208 fStream.writeText(" ");
209 fStream.writeText(name);
210 fStream.writeText("=\"");
211 fStream.write(value, length);
212 fStream.writeText("\"");
213}
214
reede73da402015-02-04 18:29:27 -0800215void SkXMLStreamWriter::onAddText(const char text[], size_t length) {
fmalitafe3f2602015-02-03 17:47:12 -0800216 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);
reede73da402015-02-04 18:29:27 -0800224 fStream.write(text, length);
fmalitafe3f2602015-02-03 17:47:12 -0800225 fStream.newline();
226}
227
Mike Reedc3063e52017-01-07 16:16:02 -0500228void SkXMLStreamWriter::onEndElement() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000229 Elem* elem = getEnd();
Mike Reedc3063e52017-01-07 16:16:02 -0500230 if (elem->fHasChildren || elem->fHasText) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000231 tab(fStream, fElems.count());
232 fStream.writeText("</");
233 fStream.writeText(elem->fName.c_str());
234 fStream.writeText(">");
fmalitafe3f2602015-02-03 17:47:12 -0800235 } else {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000236 fStream.writeText("/>");
fmalitafe3f2602015-02-03 17:47:12 -0800237 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000238 fStream.newline();
239 doEnd(elem);
240}
241
Mike Reedc3063e52017-01-07 16:16:02 -0500242void SkXMLStreamWriter::onStartElementLen(const char name[], size_t length) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000243 int level = fElems.count();
Mike Reedc3063e52017-01-07 16:16:02 -0500244 if (this->doStart(name, length)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000245 // 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
Mike Reedc3063e52017-01-07 16:16:02 -0500255void SkXMLStreamWriter::writeHeader() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000256 const char* header = getHeader();
257 fStream.write(header, strlen(header));
258 fStream.newline();
259}
260
261////////////////////////////////////////////////////////////////////////////////////////////////
262
263#include "SkXMLParser.h"
264
265SkXMLParserWriter::SkXMLParserWriter(SkXMLParser* parser)
266 : SkXMLWriter(false), fParser(*parser)
267{
268}
269
Mike Reedc3063e52017-01-07 16:16:02 -0500270SkXMLParserWriter::~SkXMLParserWriter() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000271 this->flush();
272}
273
Mike Reedc3063e52017-01-07 16:16:02 -0500274void SkXMLParserWriter::onAddAttributeLen(const char name[], const char value[], size_t length) {
fmalitafe3f2602015-02-03 17:47:12 -0800275 SkASSERT(fElems.count() == 0 || (!fElems.top()->fHasChildren && !fElems.top()->fHasText));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000276 SkString str(value, length);
277 fParser.addAttribute(name, str.c_str());
278}
279
reede73da402015-02-04 18:29:27 -0800280void SkXMLParserWriter::onAddText(const char text[], size_t length) {
281 fParser.text(text, SkToInt(length));
fmalitafe3f2602015-02-03 17:47:12 -0800282}
283
Mike Reedc3063e52017-01-07 16:16:02 -0500284void SkXMLParserWriter::onEndElement() {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000285 Elem* elem = this->getEnd();
286 fParser.endElement(elem->fName.c_str());
287 this->doEnd(elem);
288}
289
Mike Reedc3063e52017-01-07 16:16:02 -0500290void SkXMLParserWriter::onStartElementLen(const char name[], size_t length) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000291 (void)this->doStart(name, length);
292 SkString str(name, length);
293 fParser.startElement(str.c_str());
294}