gfx/skia/trunk/src/xml/SkXMLWriter.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial