reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 2 | * Copyright 2006 The Android Open Source Project |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 3 | * |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 8 | #ifndef SkDOM_DEFINED |
| 9 | #define SkDOM_DEFINED |
| 10 | |
Herb Derby | 70c6063 | 2017-02-14 15:21:07 -0500 | [diff] [blame] | 11 | #include "SkArenaAlloc.h" |
Ben Wagner | d5148e3 | 2018-07-16 17:44:06 -0400 | [diff] [blame] | 12 | #include "SkNoncopyable.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 13 | #include "SkScalar.h" |
Ben Wagner | d5148e3 | 2018-07-16 17:44:06 -0400 | [diff] [blame] | 14 | #include "SkTemplates.h" |
fmalita | 7445e86 | 2016-07-14 19:14:06 -0700 | [diff] [blame] | 15 | #include "SkTypes.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 16 | |
| 17 | struct SkDOMNode; |
| 18 | struct SkDOMAttr; |
| 19 | |
fmalita | 7a04869 | 2015-02-20 13:54:40 -0800 | [diff] [blame] | 20 | class SkDOMParser; |
fmalita | 7445e86 | 2016-07-14 19:14:06 -0700 | [diff] [blame] | 21 | class SkStream; |
fmalita | 7a04869 | 2015-02-20 13:54:40 -0800 | [diff] [blame] | 22 | class SkXMLParser; |
| 23 | |
Herb Derby | 5a8fe97 | 2017-02-14 14:42:30 -0500 | [diff] [blame] | 24 | class SkDOM : public SkNoncopyable { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 25 | public: |
| 26 | SkDOM(); |
| 27 | ~SkDOM(); |
| 28 | |
| 29 | typedef SkDOMNode Node; |
| 30 | typedef SkDOMAttr Attr; |
| 31 | |
| 32 | /** Returns null on failure |
| 33 | */ |
fmalita | 7445e86 | 2016-07-14 19:14:06 -0700 | [diff] [blame] | 34 | const Node* build(SkStream&); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 35 | const Node* copy(const SkDOM& dom, const Node* node); |
| 36 | |
| 37 | const Node* getRootNode() const; |
| 38 | |
fmalita | 7a04869 | 2015-02-20 13:54:40 -0800 | [diff] [blame] | 39 | SkXMLParser* beginParsing(); |
| 40 | const Node* finishParsing(); |
| 41 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 42 | enum Type { |
| 43 | kElement_Type, |
| 44 | kText_Type |
| 45 | }; |
Mike Reed | c3063e5 | 2017-01-07 16:16:02 -0500 | [diff] [blame] | 46 | Type getType(const Node*) const; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 47 | |
| 48 | const char* getName(const Node*) const; |
Ben Wagner | a93a14a | 2017-08-28 10:34:05 -0400 | [diff] [blame] | 49 | const Node* getFirstChild(const Node*, const char elem[] = nullptr) const; |
| 50 | const Node* getNextSibling(const Node*, const char elem[] = nullptr) const; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 51 | |
| 52 | const char* findAttr(const Node*, const char attrName[]) const; |
| 53 | const Attr* getFirstAttr(const Node*) const; |
| 54 | const Attr* getNextAttr(const Node*, const Attr*) const; |
| 55 | const char* getAttrName(const Node*, const Attr*) const; |
| 56 | const char* getAttrValue(const Node*, const Attr*) const; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 57 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 58 | // helpers for walking children |
Ben Wagner | a93a14a | 2017-08-28 10:34:05 -0400 | [diff] [blame] | 59 | int countChildren(const Node* node, const char elem[] = nullptr) const; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 60 | |
| 61 | // helpers for calling SkParse |
| 62 | bool findS32(const Node*, const char name[], int32_t* value) const; |
| 63 | bool findScalars(const Node*, const char name[], SkScalar value[], int count) const; |
| 64 | bool findHex(const Node*, const char name[], uint32_t* value) const; |
| 65 | bool findBool(const Node*, const char name[], bool*) const; |
| 66 | int findList(const Node*, const char name[], const char list[]) const; |
| 67 | |
Mike Reed | c3063e5 | 2017-01-07 16:16:02 -0500 | [diff] [blame] | 68 | bool findScalar(const Node* node, const char name[], SkScalar value[]) const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 69 | return this->findScalars(node, name, value, 1); |
| 70 | } |
| 71 | |
| 72 | bool hasAttr(const Node*, const char name[], const char value[]) const; |
| 73 | bool hasS32(const Node*, const char name[], int32_t value) const; |
| 74 | bool hasScalar(const Node*, const char name[], SkScalar value) const; |
| 75 | bool hasHex(const Node*, const char name[], uint32_t value) const; |
| 76 | bool hasBool(const Node*, const char name[], bool value) const; |
| 77 | |
| 78 | class AttrIter { |
| 79 | public: |
fmalita | 6ceef3d | 2016-07-26 18:46:34 -0700 | [diff] [blame] | 80 | AttrIter(const SkDOM&, const Node*); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 81 | const char* next(const char** value); |
| 82 | private: |
| 83 | const Attr* fAttr; |
| 84 | const Attr* fStop; |
| 85 | }; |
| 86 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 87 | private: |
Herb Derby | 70c6063 | 2017-02-14 15:21:07 -0500 | [diff] [blame] | 88 | SkArenaAlloc fAlloc; |
bungeman | 6bd5284 | 2016-10-27 09:30:08 -0700 | [diff] [blame] | 89 | Node* fRoot; |
| 90 | std::unique_ptr<SkDOMParser> fParser; |
fmalita | 7a04869 | 2015-02-20 13:54:40 -0800 | [diff] [blame] | 91 | |
fmalita | 7445e86 | 2016-07-14 19:14:06 -0700 | [diff] [blame] | 92 | typedef SkNoncopyable INHERITED; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 93 | }; |
| 94 | |
| 95 | #endif |