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 | |
Hal Canary | fdcfb8b | 2018-06-13 09:42:32 -0400 | [diff] [blame] | 8 | #include "SkDOM.h" |
Hal Canary | c640d0d | 2018-06-13 09:59:02 -0400 | [diff] [blame] | 9 | |
fmalita | 7a04869 | 2015-02-20 13:54:40 -0800 | [diff] [blame] | 10 | #include "SkStream.h" |
Hal Canary | c640d0d | 2018-06-13 09:59:02 -0400 | [diff] [blame] | 11 | #include "SkTo.h" |
Mike Reed | c3063e5 | 2017-01-07 16:16:02 -0500 | [diff] [blame] | 12 | #include "SkXMLParser.h" |
fmalita | 7a04869 | 2015-02-20 13:54:40 -0800 | [diff] [blame] | 13 | #include "SkXMLWriter.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 14 | |
Mike Reed | c3063e5 | 2017-01-07 16:16:02 -0500 | [diff] [blame] | 15 | bool SkXMLParser::parse(const SkDOM& dom, const SkDOMNode* node) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 16 | const char* elemName = dom.getName(node); |
| 17 | |
Mike Reed | c3063e5 | 2017-01-07 16:16:02 -0500 | [diff] [blame] | 18 | if (this->startElement(elemName)) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 19 | return false; |
Mike Reed | c3063e5 | 2017-01-07 16:16:02 -0500 | [diff] [blame] | 20 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 21 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 22 | SkDOM::AttrIter iter(dom, node); |
| 23 | const char* name, *value; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 24 | |
Mike Reed | c3063e5 | 2017-01-07 16:16:02 -0500 | [diff] [blame] | 25 | while ((name = iter.next(&value)) != nullptr) { |
| 26 | if (this->addAttribute(name, value)) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 27 | return false; |
Mike Reed | c3063e5 | 2017-01-07 16:16:02 -0500 | [diff] [blame] | 28 | } |
| 29 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 30 | |
Mike Reed | c3063e5 | 2017-01-07 16:16:02 -0500 | [diff] [blame] | 31 | if ((node = dom.getFirstChild(node)) != nullptr) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 32 | do { |
Mike Reed | c3063e5 | 2017-01-07 16:16:02 -0500 | [diff] [blame] | 33 | if (!this->parse(dom, node)) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 34 | return false; |
Mike Reed | c3063e5 | 2017-01-07 16:16:02 -0500 | [diff] [blame] | 35 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 36 | } while ((node = dom.getNextSibling(node)) != nullptr); |
Mike Reed | c3063e5 | 2017-01-07 16:16:02 -0500 | [diff] [blame] | 37 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 38 | return !this->endElement(elemName); |
| 39 | } |
| 40 | |
| 41 | ///////////////////////////////////////////////////////////////////////// |
| 42 | |
| 43 | struct SkDOMAttr { |
| 44 | const char* fName; |
| 45 | const char* fValue; |
| 46 | }; |
| 47 | |
| 48 | struct SkDOMNode { |
| 49 | const char* fName; |
| 50 | SkDOMNode* fFirstChild; |
| 51 | SkDOMNode* fNextSibling; |
Herb Derby | 70c6063 | 2017-02-14 15:21:07 -0500 | [diff] [blame] | 52 | SkDOMAttr* fAttrs; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 53 | uint16_t fAttrCount; |
| 54 | uint8_t fType; |
| 55 | uint8_t fPad; |
| 56 | |
Mike Reed | c3063e5 | 2017-01-07 16:16:02 -0500 | [diff] [blame] | 57 | const SkDOMAttr* attrs() const { |
Herb Derby | 70c6063 | 2017-02-14 15:21:07 -0500 | [diff] [blame] | 58 | return fAttrs; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 59 | } |
Mike Reed | c3063e5 | 2017-01-07 16:16:02 -0500 | [diff] [blame] | 60 | |
| 61 | SkDOMAttr* attrs() { |
Herb Derby | 70c6063 | 2017-02-14 15:21:07 -0500 | [diff] [blame] | 62 | return fAttrs; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 63 | } |
| 64 | }; |
| 65 | |
| 66 | ///////////////////////////////////////////////////////////////////////// |
| 67 | |
Mike Reed | f67c459 | 2017-02-17 17:06:11 -0500 | [diff] [blame] | 68 | #define kMinChunkSize 4096 |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 69 | |
Mike Reed | c3063e5 | 2017-01-07 16:16:02 -0500 | [diff] [blame] | 70 | SkDOM::SkDOM() : fAlloc(kMinChunkSize), fRoot(nullptr) {} |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 71 | |
Mike Reed | c3063e5 | 2017-01-07 16:16:02 -0500 | [diff] [blame] | 72 | SkDOM::~SkDOM() {} |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 73 | |
Mike Reed | c3063e5 | 2017-01-07 16:16:02 -0500 | [diff] [blame] | 74 | const SkDOM::Node* SkDOM::getRootNode() const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 75 | return fRoot; |
| 76 | } |
| 77 | |
Mike Reed | c3063e5 | 2017-01-07 16:16:02 -0500 | [diff] [blame] | 78 | const SkDOM::Node* SkDOM::getFirstChild(const Node* node, const char name[]) const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 79 | SkASSERT(node); |
| 80 | const Node* child = node->fFirstChild; |
| 81 | |
Mike Reed | c3063e5 | 2017-01-07 16:16:02 -0500 | [diff] [blame] | 82 | if (name) { |
| 83 | for (; child != nullptr; child = child->fNextSibling) { |
| 84 | if (!strcmp(name, child->fName)) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 85 | break; |
Mike Reed | c3063e5 | 2017-01-07 16:16:02 -0500 | [diff] [blame] | 86 | } |
| 87 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 88 | } |
| 89 | return child; |
| 90 | } |
| 91 | |
Mike Reed | c3063e5 | 2017-01-07 16:16:02 -0500 | [diff] [blame] | 92 | const SkDOM::Node* SkDOM::getNextSibling(const Node* node, const char name[]) const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 93 | SkASSERT(node); |
| 94 | const Node* sibling = node->fNextSibling; |
Mike Reed | c3063e5 | 2017-01-07 16:16:02 -0500 | [diff] [blame] | 95 | if (name) { |
| 96 | for (; sibling != nullptr; sibling = sibling->fNextSibling) { |
| 97 | if (!strcmp(name, sibling->fName)) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 98 | break; |
Mike Reed | c3063e5 | 2017-01-07 16:16:02 -0500 | [diff] [blame] | 99 | } |
| 100 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 101 | } |
| 102 | return sibling; |
| 103 | } |
| 104 | |
Mike Reed | c3063e5 | 2017-01-07 16:16:02 -0500 | [diff] [blame] | 105 | SkDOM::Type SkDOM::getType(const Node* node) const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 106 | SkASSERT(node); |
| 107 | return (Type)node->fType; |
| 108 | } |
| 109 | |
Mike Reed | c3063e5 | 2017-01-07 16:16:02 -0500 | [diff] [blame] | 110 | const char* SkDOM::getName(const Node* node) const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 111 | SkASSERT(node); |
| 112 | return node->fName; |
| 113 | } |
| 114 | |
Mike Reed | c3063e5 | 2017-01-07 16:16:02 -0500 | [diff] [blame] | 115 | const char* SkDOM::findAttr(const Node* node, const char name[]) const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 116 | SkASSERT(node); |
| 117 | const Attr* attr = node->attrs(); |
| 118 | const Attr* stop = attr + node->fAttrCount; |
| 119 | |
Mike Reed | c3063e5 | 2017-01-07 16:16:02 -0500 | [diff] [blame] | 120 | while (attr < stop) { |
| 121 | if (!strcmp(attr->fName, name)) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 122 | return attr->fValue; |
Mike Reed | c3063e5 | 2017-01-07 16:16:02 -0500 | [diff] [blame] | 123 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 124 | attr += 1; |
| 125 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 126 | return nullptr; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | ///////////////////////////////////////////////////////////////////////////////////// |
| 130 | |
Mike Reed | c3063e5 | 2017-01-07 16:16:02 -0500 | [diff] [blame] | 131 | const SkDOM::Attr* SkDOM::getFirstAttr(const Node* node) const { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 132 | return node->fAttrCount ? node->attrs() : nullptr; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 133 | } |
| 134 | |
Mike Reed | c3063e5 | 2017-01-07 16:16:02 -0500 | [diff] [blame] | 135 | const SkDOM::Attr* SkDOM::getNextAttr(const Node* node, const Attr* attr) const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 136 | SkASSERT(node); |
Mike Reed | c3063e5 | 2017-01-07 16:16:02 -0500 | [diff] [blame] | 137 | if (attr == nullptr) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 138 | return nullptr; |
Mike Reed | c3063e5 | 2017-01-07 16:16:02 -0500 | [diff] [blame] | 139 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 140 | return (attr - node->attrs() + 1) < node->fAttrCount ? attr + 1 : nullptr; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 141 | } |
| 142 | |
Mike Reed | c3063e5 | 2017-01-07 16:16:02 -0500 | [diff] [blame] | 143 | const char* SkDOM::getAttrName(const Node* node, const Attr* attr) const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 144 | SkASSERT(node); |
| 145 | SkASSERT(attr); |
| 146 | return attr->fName; |
| 147 | } |
| 148 | |
Mike Reed | c3063e5 | 2017-01-07 16:16:02 -0500 | [diff] [blame] | 149 | const char* SkDOM::getAttrValue(const Node* node, const Attr* attr) const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 150 | SkASSERT(node); |
| 151 | SkASSERT(attr); |
| 152 | return attr->fValue; |
| 153 | } |
| 154 | |
| 155 | ///////////////////////////////////////////////////////////////////////////////////// |
| 156 | |
Mike Reed | c3063e5 | 2017-01-07 16:16:02 -0500 | [diff] [blame] | 157 | SkDOM::AttrIter::AttrIter(const SkDOM&, const SkDOM::Node* node) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 158 | SkASSERT(node); |
| 159 | fAttr = node->attrs(); |
| 160 | fStop = fAttr + node->fAttrCount; |
| 161 | } |
| 162 | |
Mike Reed | c3063e5 | 2017-01-07 16:16:02 -0500 | [diff] [blame] | 163 | const char* SkDOM::AttrIter::next(const char** value) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 164 | const char* name = nullptr; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 165 | |
Mike Reed | c3063e5 | 2017-01-07 16:16:02 -0500 | [diff] [blame] | 166 | if (fAttr < fStop) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 167 | name = fAttr->fName; |
| 168 | if (value) |
| 169 | *value = fAttr->fValue; |
| 170 | fAttr += 1; |
| 171 | } |
| 172 | return name; |
| 173 | } |
| 174 | |
| 175 | ////////////////////////////////////////////////////////////////////////////// |
| 176 | |
| 177 | #include "SkXMLParser.h" |
| 178 | #include "SkTDArray.h" |
| 179 | |
Herb Derby | 70c6063 | 2017-02-14 15:21:07 -0500 | [diff] [blame] | 180 | static char* dupstr(SkArenaAlloc* chunk, const char src[]) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 181 | SkASSERT(chunk && src); |
| 182 | size_t len = strlen(src); |
Herb Derby | 70c6063 | 2017-02-14 15:21:07 -0500 | [diff] [blame] | 183 | char* dst = chunk->makeArrayDefault<char>(len + 1); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 184 | memcpy(dst, src, len + 1); |
| 185 | return dst; |
| 186 | } |
| 187 | |
| 188 | class SkDOMParser : public SkXMLParser { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 189 | public: |
Herb Derby | 70c6063 | 2017-02-14 15:21:07 -0500 | [diff] [blame] | 190 | SkDOMParser(SkArenaAlloc* chunk) : SkXMLParser(&fParserError), fAlloc(chunk) { |
fmalita | 7a04869 | 2015-02-20 13:54:40 -0800 | [diff] [blame] | 191 | fAlloc->reset(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 192 | fRoot = nullptr; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 193 | fLevel = 0; |
| 194 | fNeedToFlush = true; |
| 195 | } |
| 196 | SkDOM::Node* getRoot() const { return fRoot; } |
| 197 | SkXMLParserError fParserError; |
fmalita | 7a04869 | 2015-02-20 13:54:40 -0800 | [diff] [blame] | 198 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 199 | protected: |
Mike Reed | c3063e5 | 2017-01-07 16:16:02 -0500 | [diff] [blame] | 200 | void flushAttributes() { |
fmalita | 7a04869 | 2015-02-20 13:54:40 -0800 | [diff] [blame] | 201 | SkASSERT(fLevel > 0); |
| 202 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 203 | int attrCount = fAttrs.count(); |
| 204 | |
Herb Derby | 70c6063 | 2017-02-14 15:21:07 -0500 | [diff] [blame] | 205 | SkDOMAttr* attrs = fAlloc->makeArrayDefault<SkDOMAttr>(attrCount); |
| 206 | SkDOM::Node* node = fAlloc->make<SkDOM::Node>(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 207 | |
| 208 | node->fName = fElemName; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 209 | node->fFirstChild = nullptr; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 210 | node->fAttrCount = SkToU16(attrCount); |
Herb Derby | 70c6063 | 2017-02-14 15:21:07 -0500 | [diff] [blame] | 211 | node->fAttrs = attrs; |
fmalita | 7a04869 | 2015-02-20 13:54:40 -0800 | [diff] [blame] | 212 | node->fType = fElemType; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 213 | |
Mike Reed | c3063e5 | 2017-01-07 16:16:02 -0500 | [diff] [blame] | 214 | if (fRoot == nullptr) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 215 | node->fNextSibling = nullptr; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 216 | fRoot = node; |
Mike Reed | c3063e5 | 2017-01-07 16:16:02 -0500 | [diff] [blame] | 217 | } else { // this adds siblings in reverse order. gets corrected in onEndElement() |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 218 | SkDOM::Node* parent = fParentStack.top(); |
| 219 | SkASSERT(fRoot && parent); |
| 220 | node->fNextSibling = parent->fFirstChild; |
| 221 | parent->fFirstChild = node; |
| 222 | } |
| 223 | *fParentStack.push() = node; |
| 224 | |
mtklein | 067e90e | 2015-12-10 07:42:47 -0800 | [diff] [blame] | 225 | sk_careful_memcpy(node->attrs(), fAttrs.begin(), attrCount * sizeof(SkDOM::Attr)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 226 | fAttrs.reset(); |
| 227 | |
| 228 | } |
fmalita | 7a04869 | 2015-02-20 13:54:40 -0800 | [diff] [blame] | 229 | |
| 230 | bool onStartElement(const char elem[]) override { |
| 231 | this->startCommon(elem, SkDOM::kElement_Type); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 232 | return false; |
| 233 | } |
fmalita | 7a04869 | 2015-02-20 13:54:40 -0800 | [diff] [blame] | 234 | |
| 235 | bool onAddAttribute(const char name[], const char value[]) override { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 236 | SkDOM::Attr* attr = fAttrs.append(); |
| 237 | attr->fName = dupstr(fAlloc, name); |
| 238 | attr->fValue = dupstr(fAlloc, value); |
| 239 | return false; |
| 240 | } |
fmalita | 7a04869 | 2015-02-20 13:54:40 -0800 | [diff] [blame] | 241 | |
| 242 | bool onEndElement(const char elem[]) override { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 243 | --fLevel; |
| 244 | if (fNeedToFlush) |
| 245 | this->flushAttributes(); |
| 246 | fNeedToFlush = false; |
| 247 | |
| 248 | SkDOM::Node* parent; |
| 249 | |
| 250 | fParentStack.pop(&parent); |
| 251 | |
| 252 | SkDOM::Node* child = parent->fFirstChild; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 253 | SkDOM::Node* prev = nullptr; |
Mike Reed | c3063e5 | 2017-01-07 16:16:02 -0500 | [diff] [blame] | 254 | while (child) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 255 | SkDOM::Node* next = child->fNextSibling; |
| 256 | child->fNextSibling = prev; |
| 257 | prev = child; |
| 258 | child = next; |
| 259 | } |
| 260 | parent->fFirstChild = prev; |
| 261 | return false; |
| 262 | } |
fmalita | 7a04869 | 2015-02-20 13:54:40 -0800 | [diff] [blame] | 263 | |
| 264 | bool onText(const char text[], int len) override { |
| 265 | SkString str(text, len); |
| 266 | this->startCommon(str.c_str(), SkDOM::kText_Type); |
| 267 | this->SkDOMParser::onEndElement(str.c_str()); |
| 268 | |
| 269 | return false; |
| 270 | } |
| 271 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 272 | private: |
fmalita | 7a04869 | 2015-02-20 13:54:40 -0800 | [diff] [blame] | 273 | void startCommon(const char elem[], SkDOM::Type type) { |
Mike Reed | c3063e5 | 2017-01-07 16:16:02 -0500 | [diff] [blame] | 274 | if (fLevel > 0 && fNeedToFlush) { |
fmalita | 7a04869 | 2015-02-20 13:54:40 -0800 | [diff] [blame] | 275 | this->flushAttributes(); |
Mike Reed | c3063e5 | 2017-01-07 16:16:02 -0500 | [diff] [blame] | 276 | } |
fmalita | 7a04869 | 2015-02-20 13:54:40 -0800 | [diff] [blame] | 277 | fNeedToFlush = true; |
| 278 | fElemName = dupstr(fAlloc, elem); |
| 279 | fElemType = type; |
| 280 | ++fLevel; |
| 281 | } |
| 282 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 283 | SkTDArray<SkDOM::Node*> fParentStack; |
Herb Derby | 70c6063 | 2017-02-14 15:21:07 -0500 | [diff] [blame] | 284 | SkArenaAlloc* fAlloc; |
fmalita | 7a04869 | 2015-02-20 13:54:40 -0800 | [diff] [blame] | 285 | SkDOM::Node* fRoot; |
| 286 | bool fNeedToFlush; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 287 | |
| 288 | // state needed for flushAttributes() |
| 289 | SkTDArray<SkDOM::Attr> fAttrs; |
| 290 | char* fElemName; |
fmalita | 7a04869 | 2015-02-20 13:54:40 -0800 | [diff] [blame] | 291 | SkDOM::Type fElemType; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 292 | int fLevel; |
| 293 | }; |
| 294 | |
fmalita | 7445e86 | 2016-07-14 19:14:06 -0700 | [diff] [blame] | 295 | const SkDOM::Node* SkDOM::build(SkStream& docStream) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 296 | SkDOMParser parser(&fAlloc); |
fmalita | 7445e86 | 2016-07-14 19:14:06 -0700 | [diff] [blame] | 297 | if (!parser.parse(docStream)) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 298 | { |
| 299 | SkDEBUGCODE(SkDebugf("xml parse error, line %d\n", parser.fParserError.getLineNumber());) |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 300 | fRoot = nullptr; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 301 | fAlloc.reset(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 302 | return nullptr; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 303 | } |
| 304 | fRoot = parser.getRoot(); |
| 305 | return fRoot; |
| 306 | } |
| 307 | |
| 308 | /////////////////////////////////////////////////////////////////////////// |
| 309 | |
Mike Reed | c3063e5 | 2017-01-07 16:16:02 -0500 | [diff] [blame] | 310 | static void walk_dom(const SkDOM& dom, const SkDOM::Node* node, SkXMLParser* parser) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 311 | const char* elem = dom.getName(node); |
fmalita | 7a04869 | 2015-02-20 13:54:40 -0800 | [diff] [blame] | 312 | if (dom.getType(node) == SkDOM::kText_Type) { |
| 313 | SkASSERT(dom.countChildren(node) == 0); |
| 314 | parser->text(elem, SkToInt(strlen(elem))); |
| 315 | return; |
| 316 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 317 | |
| 318 | parser->startElement(elem); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 319 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 320 | SkDOM::AttrIter iter(dom, node); |
| 321 | const char* name; |
| 322 | const char* value; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 323 | while ((name = iter.next(&value)) != nullptr) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 324 | parser->addAttribute(name, value); |
| 325 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 326 | node = dom.getFirstChild(node, nullptr); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 327 | while (node) |
| 328 | { |
| 329 | walk_dom(dom, node, parser); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 330 | node = dom.getNextSibling(node, nullptr); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | parser->endElement(elem); |
| 334 | } |
| 335 | |
Mike Reed | c3063e5 | 2017-01-07 16:16:02 -0500 | [diff] [blame] | 336 | const SkDOM::Node* SkDOM::copy(const SkDOM& dom, const SkDOM::Node* node) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 337 | SkDOMParser parser(&fAlloc); |
| 338 | |
| 339 | walk_dom(dom, node, &parser); |
| 340 | |
| 341 | fRoot = parser.getRoot(); |
| 342 | return fRoot; |
| 343 | } |
| 344 | |
fmalita | 7a04869 | 2015-02-20 13:54:40 -0800 | [diff] [blame] | 345 | SkXMLParser* SkDOM::beginParsing() { |
| 346 | SkASSERT(!fParser); |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 347 | fParser.reset(new SkDOMParser(&fAlloc)); |
fmalita | 7a04869 | 2015-02-20 13:54:40 -0800 | [diff] [blame] | 348 | |
| 349 | return fParser.get(); |
| 350 | } |
| 351 | |
| 352 | const SkDOM::Node* SkDOM::finishParsing() { |
| 353 | SkASSERT(fParser); |
| 354 | fRoot = fParser->getRoot(); |
mtklein | 852f15d | 2016-03-17 10:51:27 -0700 | [diff] [blame] | 355 | fParser.reset(); |
fmalita | 7a04869 | 2015-02-20 13:54:40 -0800 | [diff] [blame] | 356 | |
| 357 | return fRoot; |
| 358 | } |
| 359 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 360 | ////////////////////////////////////////////////////////////////////////// |
| 361 | |
Mike Reed | c3063e5 | 2017-01-07 16:16:02 -0500 | [diff] [blame] | 362 | int SkDOM::countChildren(const Node* node, const char elem[]) const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 363 | int count = 0; |
| 364 | |
| 365 | node = this->getFirstChild(node, elem); |
Mike Reed | c3063e5 | 2017-01-07 16:16:02 -0500 | [diff] [blame] | 366 | while (node) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 367 | count += 1; |
| 368 | node = this->getNextSibling(node, elem); |
| 369 | } |
| 370 | return count; |
| 371 | } |
| 372 | |
| 373 | ////////////////////////////////////////////////////////////////////////// |
| 374 | |
| 375 | #include "SkParse.h" |
| 376 | |
Mike Reed | c3063e5 | 2017-01-07 16:16:02 -0500 | [diff] [blame] | 377 | bool SkDOM::findS32(const Node* node, const char name[], int32_t* value) const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 378 | const char* vstr = this->findAttr(node, name); |
| 379 | return vstr && SkParse::FindS32(vstr, value); |
| 380 | } |
| 381 | |
Mike Reed | c3063e5 | 2017-01-07 16:16:02 -0500 | [diff] [blame] | 382 | bool SkDOM::findScalars(const Node* node, const char name[], SkScalar value[], int count) const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 383 | const char* vstr = this->findAttr(node, name); |
| 384 | return vstr && SkParse::FindScalars(vstr, value, count); |
| 385 | } |
| 386 | |
Mike Reed | c3063e5 | 2017-01-07 16:16:02 -0500 | [diff] [blame] | 387 | bool SkDOM::findHex(const Node* node, const char name[], uint32_t* value) const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 388 | const char* vstr = this->findAttr(node, name); |
| 389 | return vstr && SkParse::FindHex(vstr, value); |
| 390 | } |
| 391 | |
Mike Reed | c3063e5 | 2017-01-07 16:16:02 -0500 | [diff] [blame] | 392 | bool SkDOM::findBool(const Node* node, const char name[], bool* value) const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 393 | const char* vstr = this->findAttr(node, name); |
| 394 | return vstr && SkParse::FindBool(vstr, value); |
| 395 | } |
| 396 | |
Mike Reed | c3063e5 | 2017-01-07 16:16:02 -0500 | [diff] [blame] | 397 | int SkDOM::findList(const Node* node, const char name[], const char list[]) const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 398 | const char* vstr = this->findAttr(node, name); |
| 399 | return vstr ? SkParse::FindList(vstr, list) : -1; |
| 400 | } |
| 401 | |
Mike Reed | c3063e5 | 2017-01-07 16:16:02 -0500 | [diff] [blame] | 402 | bool SkDOM::hasAttr(const Node* node, const char name[], const char value[]) const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 403 | const char* vstr = this->findAttr(node, name); |
| 404 | return vstr && !strcmp(vstr, value); |
| 405 | } |
| 406 | |
Mike Reed | c3063e5 | 2017-01-07 16:16:02 -0500 | [diff] [blame] | 407 | bool SkDOM::hasS32(const Node* node, const char name[], int32_t target) const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 408 | const char* vstr = this->findAttr(node, name); |
| 409 | int32_t value; |
| 410 | return vstr && SkParse::FindS32(vstr, &value) && value == target; |
| 411 | } |
| 412 | |
Mike Reed | c3063e5 | 2017-01-07 16:16:02 -0500 | [diff] [blame] | 413 | bool SkDOM::hasScalar(const Node* node, const char name[], SkScalar target) const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 414 | const char* vstr = this->findAttr(node, name); |
| 415 | SkScalar value; |
| 416 | return vstr && SkParse::FindScalar(vstr, &value) && value == target; |
| 417 | } |
| 418 | |
Mike Reed | c3063e5 | 2017-01-07 16:16:02 -0500 | [diff] [blame] | 419 | bool SkDOM::hasHex(const Node* node, const char name[], uint32_t target) const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 420 | const char* vstr = this->findAttr(node, name); |
| 421 | uint32_t value; |
| 422 | return vstr && SkParse::FindHex(vstr, &value) && value == target; |
| 423 | } |
| 424 | |
Mike Reed | c3063e5 | 2017-01-07 16:16:02 -0500 | [diff] [blame] | 425 | bool SkDOM::hasBool(const Node* node, const char name[], bool target) const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 426 | const char* vstr = this->findAttr(node, name); |
| 427 | bool value; |
| 428 | return vstr && SkParse::FindBool(vstr, &value) && value == target; |
| 429 | } |