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