1 
2 /*
3  * Copyright 2006 The Android Open Source Project
4  *
5  * Use of this source code is governed by a BSD-style license that can be
6  * found in the LICENSE file.
7  */
8 
9 
10 #ifndef SkDOM_DEFINED
11 #define SkDOM_DEFINED
12 
13 #include "SkChunkAlloc.h"
14 #include "SkScalar.h"
15 #include "SkTemplates.h"
16 
17 struct SkDOMNode;
18 struct SkDOMAttr;
19 
20 class SkDOMParser;
21 class SkXMLParser;
22 
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 
38     SkXMLParser* beginParsing();
39     const Node* finishParsing();
40 
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;
56 
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 
findScalar(const Node * node,const char name[],SkScalar value[])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:
91     SkChunkAlloc               fAlloc;
92     Node*                      fRoot;
93     SkAutoTDelete<SkDOMParser> fParser;
94 
95     friend class AttrIter;
96     friend class SkDOMParser;
97 };
98 
99 #endif
100