• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/base/SkNoncopyable.h"
14 #include "include/private/base/SkTemplates.h"
15 #include "src/base/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 
53     /** Returns null on failure
54     */
55     virtual const Node* build(SkStream&);
56 
57     virtual const Node* copy(const SkDOM& dom, const Node* node);
58     virtual SkXMLParser* beginParsing();
59     virtual const Node* finishParsing();
60 
61     const Node* getRootNode() const;
62 
63     enum Type {
64         kElement_Type,
65         kText_Type
66     };
67     Type getType(const Node*) const;
68 
69     const char* getName(const Node*) const;
70     const Node* getFirstChild(const Node*, const char elem[] = nullptr) const;
71     const Node* getNextSibling(const Node*, const char elem[] = nullptr) const;
72 
73     const char* findAttr(const Node*, const char attrName[]) const;
74     const Attr* getFirstAttr(const Node*) const;
75     const Attr* getNextAttr(const Node*, const Attr*) const;
76     const char* getAttrName(const Node*, const Attr*) const;
77     const char* getAttrValue(const Node*, const Attr*) const;
78 
79     // helpers for walking children
80     int countChildren(const Node* node, const char elem[] = nullptr) const;
81 
82     // helpers for calling SkParse
83     bool findS32(const Node*, const char name[], int32_t* value) const;
84     bool findScalars(const Node*, const char name[], SkScalar value[], int count) const;
85     bool findHex(const Node*, const char name[], uint32_t* value) const;
86     bool findBool(const Node*, const char name[], bool*) const;
87     int  findList(const Node*, const char name[], const char list[]) const;
88 
findScalar(const Node * node,const char name[],SkScalar value[])89     bool findScalar(const Node* node, const char name[], SkScalar value[]) const {
90         return this->findScalars(node, name, value, 1);
91     }
92 
93     bool hasAttr(const Node*, const char name[], const char value[]) const;
94     bool hasS32(const Node*, const char name[], int32_t value) const;
95     bool hasScalar(const Node*, const char name[], SkScalar value) const;
96     bool hasHex(const Node*, const char name[], uint32_t value) const;
97     bool hasBool(const Node*, const char name[], bool value) const;
98 
99     class AttrIter {
100     public:
101         AttrIter(const SkDOM&, const Node*);
102         const char* next(const char** value);
103     private:
104         const Attr* fAttr;
105         const Attr* fStop;
106     };
107 
108 protected:
109     SkArenaAllocWithReset        fAlloc;
110     Node*                        fRoot;
111     std::unique_ptr<SkDOMParser> fParser;
112 
113     using INHERITED = SkNoncopyable;
114 };
115 
116 #endif
117