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 8 #ifndef SkDOM_DEFINED 9 #define SkDOM_DEFINED 10 11 #include "include/core/SkScalar.h" 12 #include "include/core/SkTypes.h" 13 #include "include/private/SkNoncopyable.h" 14 #include "include/private/SkTemplates.h" 15 #include "src/core/SkArenaAlloc.h" 16 17 class SkDOMParser; 18 class SkStream; 19 class SkXMLParser; 20 21 struct SkDOMAttr { 22 const char* fName; 23 const char* fValue; 24 }; 25 26 struct SkDOMNode { 27 const char* fName; 28 SkDOMNode* fFirstChild; 29 SkDOMNode* fNextSibling; 30 SkDOMAttr* fAttrs; 31 uint16_t fAttrCount; 32 uint8_t fType; 33 uint8_t fPad; 34 attrsSkDOMNode35 const SkDOMAttr* attrs() const { 36 return fAttrs; 37 } 38 attrsSkDOMNode39 SkDOMAttr* attrs() { 40 return fAttrs; 41 } 42 }; 43 44 class SK_API SkDOM : public SkNoncopyable { 45 public: 46 SkDOM(); 47 virtual ~SkDOM(); 48 49 typedef SkDOMNode Node; 50 typedef SkDOMAttr Attr; 51 static void walk_dom(const SkDOM& dom, const SkDOM::Node* node, SkXMLParser* parser); 52 /** Returns null on failure 53 */ 54 virtual const Node* build(SkStream&); 55 56 virtual const Node* copy(const SkDOM& dom, const Node* node); 57 virtual SkXMLParser* beginParsing(); 58 virtual const Node* finishParsing(); 59 60 const Node* getRootNode() const; 61 62 enum Type { 63 kElement_Type, 64 kText_Type 65 }; 66 Type getType(const Node*) const; 67 68 const char* getName(const Node*) const; 69 const Node* getFirstChild(const Node*, const char elem[] = nullptr) const; 70 const Node* getNextSibling(const Node*, const char elem[] = nullptr) const; 71 72 const char* findAttr(const Node*, const char attrName[]) const; 73 const Attr* getFirstAttr(const Node*) const; 74 const Attr* getNextAttr(const Node*, const Attr*) const; 75 const char* getAttrName(const Node*, const Attr*) const; 76 const char* getAttrValue(const Node*, const Attr*) const; 77 78 // helpers for walking children 79 int countChildren(const Node* node, const char elem[] = nullptr) const; 80 81 // helpers for calling SkParse 82 bool findS32(const Node*, const char name[], int32_t* value) const; 83 bool findScalars(const Node*, const char name[], SkScalar value[], int count) const; 84 bool findHex(const Node*, const char name[], uint32_t* value) const; 85 bool findBool(const Node*, const char name[], bool*) const; 86 int findList(const Node*, const char name[], const char list[]) const; 87 findScalar(const Node * node,const char name[],SkScalar value[])88 bool findScalar(const Node* node, const char name[], SkScalar value[]) const { 89 return this->findScalars(node, name, value, 1); 90 } 91 92 bool hasAttr(const Node*, const char name[], const char value[]) const; 93 bool hasS32(const Node*, const char name[], int32_t value) const; 94 bool hasScalar(const Node*, const char name[], SkScalar value) const; 95 bool hasHex(const Node*, const char name[], uint32_t value) const; 96 bool hasBool(const Node*, const char name[], bool value) const; 97 98 class AttrIter { 99 public: 100 AttrIter(const SkDOM&, const Node*); 101 const char* next(const char** value); 102 private: 103 const Attr* fAttr; 104 const Attr* fStop; 105 }; 106 107 protected: 108 SkArenaAllocWithReset fAlloc; 109 Node* fRoot; 110 std::unique_ptr<SkDOMParser> fParser; 111 112 using INHERITED = SkNoncopyable; 113 }; 114 115 #endif 116