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 | |
13 | SkXMLWriter::SkXMLWriter(bool doEscapeMarkup) : fDoEscapeMarkup(doEscapeMarkup) |
14 | {} |
15 | |
16 | SkXMLWriter::~SkXMLWriter() { |
17 | SkASSERT(fElems.count() == 0); |
18 | } |
19 | |
20 | void SkXMLWriter::flush() { |
21 | while (fElems.count()) { |
22 | this->endElement(); |
23 | } |
24 | } |
25 | |
26 | void SkXMLWriter::addAttribute(const char name[], const char value[]) { |
27 | this->addAttributeLen(name, value, strlen(value)); |
28 | } |
29 | |
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 | |
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 | |
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 | |
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 | |
58 | void SkXMLWriter::doEnd(Elem* elem) { |
59 | delete elem; |
60 | } |
61 | |
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 | |
73 | SkXMLWriter::Elem* SkXMLWriter::getEnd() { |
74 | Elem* elem; |
75 | fElems.pop(&elem); |
76 | return elem; |
77 | } |
78 | |
79 | const char* SkXMLWriter::() { |
80 | static const char [] = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" ; |
81 | return gHeader; |
82 | } |
83 | |
84 | void SkXMLWriter::startElement(const char name[]) { |
85 | this->startElementLen(name, strlen(name)); |
86 | } |
87 | |
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 | |
108 | static size_t escape_markup(char dst[], const char src[], size_t length) { |
109 | size_t = 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 | |
131 | void SkXMLWriter::addAttributeLen(const char name[], const char value[], size_t length) { |
132 | SkString valueStr; |
133 | |
134 | if (fDoEscapeMarkup) { |
135 | size_t = 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 | |
146 | void SkXMLWriter::startElementLen(const char elem[], size_t length) { |
147 | this->onStartElementLen(elem, length); |
148 | } |
149 | |
150 | //////////////////////////////////////////////////////////////////////////////////////// |
151 | |
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 | |
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 | |
188 | void SkXMLWriter::() |
189 | {} |
190 | |
191 | // SkXMLStreamWriter |
192 | |
193 | SkXMLStreamWriter::SkXMLStreamWriter(SkWStream* stream, uint32_t flags) |
194 | : fStream(*stream) |
195 | , fFlags(flags) {} |
196 | |
197 | SkXMLStreamWriter::~SkXMLStreamWriter() { |
198 | this->flush(); |
199 | } |
200 | |
201 | void SkXMLStreamWriter::onAddAttributeLen(const char name[], const char value[], size_t length) { |
202 | SkASSERT(!fElems.top()->fHasChildren && !fElems.top()->fHasText); |
203 | fStream.writeText(" " ); |
204 | fStream.writeText(name); |
205 | fStream.writeText("=\"" ); |
206 | fStream.write(value, length); |
207 | fStream.writeText("\"" ); |
208 | } |
209 | |
210 | void SkXMLStreamWriter::onAddText(const char text[], size_t length) { |
211 | Elem* elem = fElems.top(); |
212 | |
213 | if (!elem->fHasChildren && !elem->fHasText) { |
214 | fStream.writeText(">" ); |
215 | this->newline(); |
216 | } |
217 | |
218 | this->tab(fElems.count() + 1); |
219 | fStream.write(text, length); |
220 | this->newline(); |
221 | } |
222 | |
223 | void SkXMLStreamWriter::onEndElement() { |
224 | Elem* elem = getEnd(); |
225 | if (elem->fHasChildren || elem->fHasText) { |
226 | this->tab(fElems.count()); |
227 | fStream.writeText("</" ); |
228 | fStream.writeText(elem->fName.c_str()); |
229 | fStream.writeText(">" ); |
230 | } else { |
231 | fStream.writeText("/>" ); |
232 | } |
233 | this->newline(); |
234 | doEnd(elem); |
235 | } |
236 | |
237 | void SkXMLStreamWriter::onStartElementLen(const char name[], size_t length) { |
238 | int level = fElems.count(); |
239 | if (this->doStart(name, length)) { |
240 | // the first child, need to close with > |
241 | fStream.writeText(">" ); |
242 | this->newline(); |
243 | } |
244 | |
245 | this->tab(level); |
246 | fStream.writeText("<" ); |
247 | fStream.write(name, length); |
248 | } |
249 | |
250 | void SkXMLStreamWriter::() { |
251 | const char* = getHeader(); |
252 | fStream.write(header, strlen(header)); |
253 | this->newline(); |
254 | } |
255 | |
256 | void SkXMLStreamWriter::newline() { |
257 | if (!(fFlags & kNoPretty_Flag)) { |
258 | fStream.newline(); |
259 | } |
260 | } |
261 | |
262 | void SkXMLStreamWriter::tab(int level) { |
263 | if (!(fFlags & kNoPretty_Flag)) { |
264 | for (int i = 0; i < level; i++) { |
265 | fStream.writeText("\t" ); |
266 | } |
267 | } |
268 | } |
269 | |
270 | //////////////////////////////////////////////////////////////////////////////////////////////// |
271 | |
272 | #include "src/xml/SkXMLParser.h" |
273 | |
274 | SkXMLParserWriter::SkXMLParserWriter(SkXMLParser* parser) |
275 | : SkXMLWriter(false), fParser(*parser) |
276 | { |
277 | } |
278 | |
279 | SkXMLParserWriter::~SkXMLParserWriter() { |
280 | this->flush(); |
281 | } |
282 | |
283 | void SkXMLParserWriter::onAddAttributeLen(const char name[], const char value[], size_t length) { |
284 | SkASSERT(fElems.count() == 0 || (!fElems.top()->fHasChildren && !fElems.top()->fHasText)); |
285 | SkString str(value, length); |
286 | fParser.addAttribute(name, str.c_str()); |
287 | } |
288 | |
289 | void SkXMLParserWriter::onAddText(const char text[], size_t length) { |
290 | fParser.text(text, SkToInt(length)); |
291 | } |
292 | |
293 | void SkXMLParserWriter::onEndElement() { |
294 | Elem* elem = this->getEnd(); |
295 | fParser.endElement(elem->fName.c_str()); |
296 | this->doEnd(elem); |
297 | } |
298 | |
299 | void SkXMLParserWriter::onStartElementLen(const char name[], size_t length) { |
300 | (void)this->doStart(name, length); |
301 | SkString str(name, length); |
302 | fParser.startElement(str.c_str()); |
303 | } |
304 | |