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 "../private/SkTemplates.h"
12#include "SkArenaAlloc.h"
13#include "SkScalar.h"
14#include "SkTypes.h"
15
16struct SkDOMNode;
17struct SkDOMAttr;
18
19class SkDOMParser;
20class SkStream;
21class SkXMLParser;
22
23class SkDOM : public SkNoncopyable {
24public:
25    SkDOM();
26    ~SkDOM();
27
28    typedef SkDOMNode Node;
29    typedef SkDOMAttr Attr;
30
31    /** Returns null on failure
32    */
33    const Node* build(SkStream&);
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[] = nullptr) const;
49    const Node* getNextSibling(const Node*, const char elem[] = nullptr) 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[] = nullptr) 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        return this->findScalars(node, name, value, 1);
69    }
70
71    bool hasAttr(const Node*, const char name[], const char value[]) const;
72    bool hasS32(const Node*, const char name[], int32_t value) const;
73    bool hasScalar(const Node*, const char name[], SkScalar value) const;
74    bool hasHex(const Node*, const char name[], uint32_t value) const;
75    bool hasBool(const Node*, const char name[], bool value) const;
76
77    class AttrIter {
78    public:
79        AttrIter(const SkDOM&, const Node*);
80        const char* next(const char** value);
81    private:
82        const Attr* fAttr;
83        const Attr* fStop;
84    };
85
86private:
87    SkArenaAlloc                 fAlloc;
88    Node*                        fRoot;
89    std::unique_ptr<SkDOMParser> fParser;
90
91    typedef SkNoncopyable INHERITED;
92};
93
94#endif
95