epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 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 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 8 | #include "SkXMLWriter.h" |
Hal Canary | c640d0d | 2018-06-13 09:59:02 -0400 | [diff] [blame] | 9 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 10 | #include "SkStream.h" |
Hal Canary | c640d0d | 2018-06-13 09:59:02 -0400 | [diff] [blame] | 11 | #include "SkTo.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 12 | |
| 13 | SkXMLWriter::SkXMLWriter(bool doEscapeMarkup) : fDoEscapeMarkup(doEscapeMarkup) |
Mike Reed | c3063e5 | 2017-01-07 16:16:02 -0500 | [diff] [blame] | 14 | {} |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 15 | |
Mike Reed | c3063e5 | 2017-01-07 16:16:02 -0500 | [diff] [blame] | 16 | SkXMLWriter::~SkXMLWriter() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 17 | SkASSERT(fElems.count() == 0); |
| 18 | } |
| 19 | |
Mike Reed | c3063e5 | 2017-01-07 16:16:02 -0500 | [diff] [blame] | 20 | void SkXMLWriter::flush() { |
| 21 | while (fElems.count()) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 22 | this->endElement(); |
Mike Reed | c3063e5 | 2017-01-07 16:16:02 -0500 | [diff] [blame] | 23 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 24 | } |
| 25 | |
Mike Reed | c3063e5 | 2017-01-07 16:16:02 -0500 | [diff] [blame] | 26 | void SkXMLWriter::addAttribute(const char name[], const char value[]) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 27 | this->addAttributeLen(name, value, strlen(value)); |
| 28 | } |
| 29 | |
Mike Reed | c3063e5 | 2017-01-07 16:16:02 -0500 | [diff] [blame] | 30 | void SkXMLWriter::addS32Attribute(const char name[], int32_t value) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 31 | SkString tmp; |
| 32 | tmp.appendS32(value); |
| 33 | this->addAttribute(name, tmp.c_str()); |
| 34 | } |
| 35 | |
Mike Reed | c3063e5 | 2017-01-07 16:16:02 -0500 | [diff] [blame] | 36 | void SkXMLWriter::addHexAttribute(const char name[], uint32_t value, int minDigits) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 37 | SkString tmp("0x"); |
| 38 | tmp.appendHex(value, minDigits); |
| 39 | this->addAttribute(name, tmp.c_str()); |
| 40 | } |
| 41 | |
Mike Reed | c3063e5 | 2017-01-07 16:16:02 -0500 | [diff] [blame] | 42 | void SkXMLWriter::addScalarAttribute(const char name[], SkScalar value) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 43 | SkString tmp; |
| 44 | tmp.appendScalar(value); |
| 45 | this->addAttribute(name, tmp.c_str()); |
| 46 | } |
| 47 | |
reed | e73da40 | 2015-02-04 18:29:27 -0800 | [diff] [blame] | 48 | void SkXMLWriter::addText(const char text[], size_t length) { |
fmalita | fe3f260 | 2015-02-03 17:47:12 -0800 | [diff] [blame] | 49 | if (fElems.isEmpty()) { |
| 50 | return; |
| 51 | } |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 52 | |
reed | e73da40 | 2015-02-04 18:29:27 -0800 | [diff] [blame] | 53 | this->onAddText(text, length); |
Ben Wagner | 63fd760 | 2017-10-09 15:45:33 -0400 | [diff] [blame] | 54 | |
fmalita | fe3f260 | 2015-02-03 17:47:12 -0800 | [diff] [blame] | 55 | fElems.top()->fHasText = true; |
| 56 | } |
| 57 | |
Mike Reed | c3063e5 | 2017-01-07 16:16:02 -0500 | [diff] [blame] | 58 | void SkXMLWriter::doEnd(Elem* elem) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 59 | delete elem; |
| 60 | } |
| 61 | |
Mike Reed | c3063e5 | 2017-01-07 16:16:02 -0500 | [diff] [blame] | 62 | bool SkXMLWriter::doStart(const char name[], size_t length) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 63 | int level = fElems.count(); |
| 64 | bool firstChild = level > 0 && !fElems[level-1]->fHasChildren; |
Mike Reed | c3063e5 | 2017-01-07 16:16:02 -0500 | [diff] [blame] | 65 | if (firstChild) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 66 | fElems[level-1]->fHasChildren = true; |
Mike Reed | c3063e5 | 2017-01-07 16:16:02 -0500 | [diff] [blame] | 67 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 68 | Elem** elem = fElems.push(); |
fmalita | fe3f260 | 2015-02-03 17:47:12 -0800 | [diff] [blame] | 69 | *elem = new Elem(name, length); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 70 | return firstChild; |
| 71 | } |
| 72 | |
Mike Reed | c3063e5 | 2017-01-07 16:16:02 -0500 | [diff] [blame] | 73 | SkXMLWriter::Elem* SkXMLWriter::getEnd() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 74 | Elem* elem; |
| 75 | fElems.pop(&elem); |
| 76 | return elem; |
| 77 | } |
| 78 | |
Mike Reed | c3063e5 | 2017-01-07 16:16:02 -0500 | [diff] [blame] | 79 | const char* SkXMLWriter::getHeader() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 80 | static const char gHeader[] = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>"; |
| 81 | return gHeader; |
| 82 | } |
| 83 | |
Mike Reed | c3063e5 | 2017-01-07 16:16:02 -0500 | [diff] [blame] | 84 | void SkXMLWriter::startElement(const char name[]) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 85 | this->startElementLen(name, strlen(name)); |
| 86 | } |
| 87 | |
Mike Reed | c3063e5 | 2017-01-07 16:16:02 -0500 | [diff] [blame] | 88 | static const char* escape_char(char c, char storage[2]) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 89 | static const char* gEscapeChars[] = { |
| 90 | "<<", |
| 91 | ">>", |
| 92 | //"\""", |
| 93 | //"''", |
| 94 | "&&" |
| 95 | }; |
| 96 | |
| 97 | const char** array = gEscapeChars; |
Mike Reed | c3063e5 | 2017-01-07 16:16:02 -0500 | [diff] [blame] | 98 | for (unsigned i = 0; i < SK_ARRAY_COUNT(gEscapeChars); i++) { |
| 99 | if (array[i][0] == c) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 100 | return &array[i][1]; |
Mike Reed | c3063e5 | 2017-01-07 16:16:02 -0500 | [diff] [blame] | 101 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 102 | } |
| 103 | storage[0] = c; |
| 104 | storage[1] = 0; |
| 105 | return storage; |
| 106 | } |
| 107 | |
Mike Reed | c3063e5 | 2017-01-07 16:16:02 -0500 | [diff] [blame] | 108 | static size_t escape_markup(char dst[], const char src[], size_t length) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 109 | size_t extra = 0; |
| 110 | const char* stop = src + length; |
| 111 | |
Mike Reed | c3063e5 | 2017-01-07 16:16:02 -0500 | [diff] [blame] | 112 | while (src < stop) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 113 | char orig[2]; |
| 114 | const char* seq = escape_char(*src, orig); |
| 115 | size_t seqSize = strlen(seq); |
| 116 | |
Mike Reed | c3063e5 | 2017-01-07 16:16:02 -0500 | [diff] [blame] | 117 | if (dst) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 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 | |
Mike Reed | c3063e5 | 2017-01-07 16:16:02 -0500 | [diff] [blame] | 131 | void SkXMLWriter::addAttributeLen(const char name[], const char value[], size_t length) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 132 | SkString valueStr; |
| 133 | |
Mike Reed | c3063e5 | 2017-01-07 16:16:02 -0500 | [diff] [blame] | 134 | if (fDoEscapeMarkup) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 135 | size_t extra = escape_markup(nullptr, value, length); |
Mike Reed | c3063e5 | 2017-01-07 16:16:02 -0500 | [diff] [blame] | 136 | if (extra) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 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 | |
Mike Reed | c3063e5 | 2017-01-07 16:16:02 -0500 | [diff] [blame] | 146 | void SkXMLWriter::startElementLen(const char elem[], size_t length) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 147 | this->onStartElementLen(elem, length); |
| 148 | } |
| 149 | |
| 150 | //////////////////////////////////////////////////////////////////////////////////////// |
| 151 | |
Mike Reed | c3063e5 | 2017-01-07 16:16:02 -0500 | [diff] [blame] | 152 | static void write_dom(const SkDOM& dom, const SkDOM::Node* node, SkXMLWriter* w, bool skipRoot) { |
| 153 | if (!skipRoot) { |
fmalita | 7a04869 | 2015-02-20 13:54:40 -0800 | [diff] [blame] | 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); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 162 | |
| 163 | SkDOM::AttrIter iter(dom, node); |
| 164 | const char* name; |
| 165 | const char* value; |
Mike Reed | c3063e5 | 2017-01-07 16:16:02 -0500 | [diff] [blame] | 166 | while ((name = iter.next(&value)) != nullptr) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 167 | w->addAttribute(name, value); |
Mike Reed | c3063e5 | 2017-01-07 16:16:02 -0500 | [diff] [blame] | 168 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 169 | } |
| 170 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 171 | node = dom.getFirstChild(node, nullptr); |
Mike Reed | c3063e5 | 2017-01-07 16:16:02 -0500 | [diff] [blame] | 172 | while (node) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 173 | write_dom(dom, node, w, false); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 174 | node = dom.getNextSibling(node, nullptr); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 175 | } |
| 176 | |
Mike Reed | c3063e5 | 2017-01-07 16:16:02 -0500 | [diff] [blame] | 177 | if (!skipRoot) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 178 | w->endElement(); |
Mike Reed | c3063e5 | 2017-01-07 16:16:02 -0500 | [diff] [blame] | 179 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 180 | } |
| 181 | |
Mike Reed | c3063e5 | 2017-01-07 16:16:02 -0500 | [diff] [blame] | 182 | void SkXMLWriter::writeDOM(const SkDOM& dom, const SkDOM::Node* node, bool skipRoot) { |
| 183 | if (node) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 184 | write_dom(dom, node, this, skipRoot); |
Mike Reed | c3063e5 | 2017-01-07 16:16:02 -0500 | [diff] [blame] | 185 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | void SkXMLWriter::writeHeader() |
Mike Reed | c3063e5 | 2017-01-07 16:16:02 -0500 | [diff] [blame] | 189 | {} |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 190 | |
| 191 | // SkXMLStreamWriter |
| 192 | |
Mike Reed | c3063e5 | 2017-01-07 16:16:02 -0500 | [diff] [blame] | 193 | static void tab(SkWStream& stream, int level) { |
| 194 | for (int i = 0; i < level; i++) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 195 | stream.writeText("\t"); |
Mike Reed | c3063e5 | 2017-01-07 16:16:02 -0500 | [diff] [blame] | 196 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | SkXMLStreamWriter::SkXMLStreamWriter(SkWStream* stream) : fStream(*stream) |
Mike Reed | c3063e5 | 2017-01-07 16:16:02 -0500 | [diff] [blame] | 200 | {} |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 201 | |
Mike Reed | c3063e5 | 2017-01-07 16:16:02 -0500 | [diff] [blame] | 202 | SkXMLStreamWriter::~SkXMLStreamWriter() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 203 | this->flush(); |
| 204 | } |
| 205 | |
Mike Reed | c3063e5 | 2017-01-07 16:16:02 -0500 | [diff] [blame] | 206 | void SkXMLStreamWriter::onAddAttributeLen(const char name[], const char value[], size_t length) { |
fmalita | fe3f260 | 2015-02-03 17:47:12 -0800 | [diff] [blame] | 207 | SkASSERT(!fElems.top()->fHasChildren && !fElems.top()->fHasText); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 208 | fStream.writeText(" "); |
| 209 | fStream.writeText(name); |
| 210 | fStream.writeText("=\""); |
| 211 | fStream.write(value, length); |
| 212 | fStream.writeText("\""); |
| 213 | } |
| 214 | |
reed | e73da40 | 2015-02-04 18:29:27 -0800 | [diff] [blame] | 215 | void SkXMLStreamWriter::onAddText(const char text[], size_t length) { |
fmalita | fe3f260 | 2015-02-03 17:47:12 -0800 | [diff] [blame] | 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); |
reed | e73da40 | 2015-02-04 18:29:27 -0800 | [diff] [blame] | 224 | fStream.write(text, length); |
fmalita | fe3f260 | 2015-02-03 17:47:12 -0800 | [diff] [blame] | 225 | fStream.newline(); |
| 226 | } |
| 227 | |
Mike Reed | c3063e5 | 2017-01-07 16:16:02 -0500 | [diff] [blame] | 228 | void SkXMLStreamWriter::onEndElement() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 229 | Elem* elem = getEnd(); |
Mike Reed | c3063e5 | 2017-01-07 16:16:02 -0500 | [diff] [blame] | 230 | if (elem->fHasChildren || elem->fHasText) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 231 | tab(fStream, fElems.count()); |
| 232 | fStream.writeText("</"); |
| 233 | fStream.writeText(elem->fName.c_str()); |
| 234 | fStream.writeText(">"); |
fmalita | fe3f260 | 2015-02-03 17:47:12 -0800 | [diff] [blame] | 235 | } else { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 236 | fStream.writeText("/>"); |
fmalita | fe3f260 | 2015-02-03 17:47:12 -0800 | [diff] [blame] | 237 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 238 | fStream.newline(); |
| 239 | doEnd(elem); |
| 240 | } |
| 241 | |
Mike Reed | c3063e5 | 2017-01-07 16:16:02 -0500 | [diff] [blame] | 242 | void SkXMLStreamWriter::onStartElementLen(const char name[], size_t length) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 243 | int level = fElems.count(); |
Mike Reed | c3063e5 | 2017-01-07 16:16:02 -0500 | [diff] [blame] | 244 | if (this->doStart(name, length)) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 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 | |
Mike Reed | c3063e5 | 2017-01-07 16:16:02 -0500 | [diff] [blame] | 255 | void SkXMLStreamWriter::writeHeader() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 256 | const char* header = getHeader(); |
| 257 | fStream.write(header, strlen(header)); |
| 258 | fStream.newline(); |
| 259 | } |
| 260 | |
| 261 | //////////////////////////////////////////////////////////////////////////////////////////////// |
| 262 | |
| 263 | #include "SkXMLParser.h" |
| 264 | |
| 265 | SkXMLParserWriter::SkXMLParserWriter(SkXMLParser* parser) |
| 266 | : SkXMLWriter(false), fParser(*parser) |
| 267 | { |
| 268 | } |
| 269 | |
Mike Reed | c3063e5 | 2017-01-07 16:16:02 -0500 | [diff] [blame] | 270 | SkXMLParserWriter::~SkXMLParserWriter() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 271 | this->flush(); |
| 272 | } |
| 273 | |
Mike Reed | c3063e5 | 2017-01-07 16:16:02 -0500 | [diff] [blame] | 274 | void SkXMLParserWriter::onAddAttributeLen(const char name[], const char value[], size_t length) { |
fmalita | fe3f260 | 2015-02-03 17:47:12 -0800 | [diff] [blame] | 275 | SkASSERT(fElems.count() == 0 || (!fElems.top()->fHasChildren && !fElems.top()->fHasText)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 276 | SkString str(value, length); |
| 277 | fParser.addAttribute(name, str.c_str()); |
| 278 | } |
| 279 | |
reed | e73da40 | 2015-02-04 18:29:27 -0800 | [diff] [blame] | 280 | void SkXMLParserWriter::onAddText(const char text[], size_t length) { |
| 281 | fParser.text(text, SkToInt(length)); |
fmalita | fe3f260 | 2015-02-03 17:47:12 -0800 | [diff] [blame] | 282 | } |
| 283 | |
Mike Reed | c3063e5 | 2017-01-07 16:16:02 -0500 | [diff] [blame] | 284 | void SkXMLParserWriter::onEndElement() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 285 | Elem* elem = this->getEnd(); |
| 286 | fParser.endElement(elem->fName.c_str()); |
| 287 | this->doEnd(elem); |
| 288 | } |
| 289 | |
Mike Reed | c3063e5 | 2017-01-07 16:16:02 -0500 | [diff] [blame] | 290 | void SkXMLParserWriter::onStartElementLen(const char name[], size_t length) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 291 | (void)this->doStart(name, length); |
| 292 | SkString str(name, length); |
| 293 | fParser.startElement(str.c_str()); |
| 294 | } |