epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * Copyright 2006 The Android Open Source Project |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 4 | * |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 9 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 10 | #ifndef SkDOM_DEFINED |
| 11 | #define SkDOM_DEFINED |
| 12 | |
| 13 | #include "SkChunkAlloc.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 14 | #include "SkScalar.h" |
| 15 | #include "SkTemplates.h" |
| 16 | |
| 17 | struct SkDOMNode; |
| 18 | struct SkDOMAttr; |
| 19 | |
fmalita | 7a04869 | 2015-02-20 13:54:40 -0800 | [diff] [blame] | 20 | class SkDOMParser; |
| 21 | class SkXMLParser; |
| 22 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 23 | class SkDOM { |
| 24 | public: |
| 25 | SkDOM(); |
| 26 | ~SkDOM(); |
| 27 | |
| 28 | typedef SkDOMNode Node; |
| 29 | typedef SkDOMAttr Attr; |
| 30 | |
| 31 | /** Returns null on failure |
| 32 | */ |
| 33 | const Node* build(const char doc[], size_t len); |
| 34 | const Node* copy(const SkDOM& dom, const Node* node); |
| 35 | |
| 36 | const Node* getRootNode() const; |
| 37 | |
fmalita | 7a04869 | 2015-02-20 13:54:40 -0800 | [diff] [blame] | 38 | SkXMLParser* beginParsing(); |
| 39 | const Node* finishParsing(); |
| 40 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 41 | enum Type { |
| 42 | kElement_Type, |
| 43 | kText_Type |
| 44 | }; |
| 45 | Type getType(const Node*) const; |
| 46 | |
| 47 | const char* getName(const Node*) const; |
| 48 | const Node* getFirstChild(const Node*, const char elem[] = NULL) const; |
| 49 | const Node* getNextSibling(const Node*, const char elem[] = NULL) const; |
| 50 | |
| 51 | const char* findAttr(const Node*, const char attrName[]) const; |
| 52 | const Attr* getFirstAttr(const Node*) const; |
| 53 | const Attr* getNextAttr(const Node*, const Attr*) const; |
| 54 | const char* getAttrName(const Node*, const Attr*) const; |
| 55 | const char* getAttrValue(const Node*, const Attr*) const; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 56 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 57 | // helpers for walking children |
| 58 | int countChildren(const Node* node, const char elem[] = NULL) const; |
| 59 | |
| 60 | // helpers for calling SkParse |
| 61 | bool findS32(const Node*, const char name[], int32_t* value) const; |
| 62 | bool findScalars(const Node*, const char name[], SkScalar value[], int count) const; |
| 63 | bool findHex(const Node*, const char name[], uint32_t* value) const; |
| 64 | bool findBool(const Node*, const char name[], bool*) const; |
| 65 | int findList(const Node*, const char name[], const char list[]) const; |
| 66 | |
| 67 | bool findScalar(const Node* node, const char name[], SkScalar value[]) const |
| 68 | { |
| 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: |
| 80 | AttrIter(const class SkDOM&, const Node*); |
| 81 | const char* next(const char** value); |
| 82 | private: |
| 83 | const Attr* fAttr; |
| 84 | const Attr* fStop; |
| 85 | }; |
| 86 | |
| 87 | SkDEBUGCODE(void dump(const Node* node = NULL, int tabLevel = 0) const;) |
| 88 | SkDEBUGCODE(static void UnitTest();) |
| 89 | |
| 90 | private: |
fmalita | 7a04869 | 2015-02-20 13:54:40 -0800 | [diff] [blame] | 91 | SkChunkAlloc fAlloc; |
| 92 | Node* fRoot; |
| 93 | SkAutoTDelete<SkDOMParser> fParser; |
| 94 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 95 | friend class AttrIter; |
| 96 | friend class SkDOMParser; |
| 97 | }; |
| 98 | |
| 99 | #endif |