blob: d3b7b95133d7cf40e2bb01d739cf01dddc8ad965 [file] [log] [blame]
reed@android.com8a1c16f2008-12-17 15:59:43 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2006 The Android Open Source Project
reed@android.com8a1c16f2008-12-17 15:59:43 +00003 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00004 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
reed@android.com8a1c16f2008-12-17 15:59:43 +00006 */
7
reed@android.com8a1c16f2008-12-17 15:59:43 +00008#ifndef SkDOM_DEFINED
9#define SkDOM_DEFINED
10
Herb Derby70c60632017-02-14 15:21:07 -050011#include "SkArenaAlloc.h"
Ben Wagnerd5148e32018-07-16 17:44:06 -040012#include "SkNoncopyable.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000013#include "SkScalar.h"
Ben Wagnerd5148e32018-07-16 17:44:06 -040014#include "SkTemplates.h"
fmalita7445e862016-07-14 19:14:06 -070015#include "SkTypes.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000016
17struct SkDOMNode;
18struct SkDOMAttr;
19
fmalita7a048692015-02-20 13:54:40 -080020class SkDOMParser;
fmalita7445e862016-07-14 19:14:06 -070021class SkStream;
fmalita7a048692015-02-20 13:54:40 -080022class SkXMLParser;
23
Herb Derby5a8fe972017-02-14 14:42:30 -050024class SkDOM : public SkNoncopyable {
reed@android.com8a1c16f2008-12-17 15:59:43 +000025public:
26 SkDOM();
27 ~SkDOM();
28
29 typedef SkDOMNode Node;
30 typedef SkDOMAttr Attr;
31
32 /** Returns null on failure
33 */
fmalita7445e862016-07-14 19:14:06 -070034 const Node* build(SkStream&);
reed@android.com8a1c16f2008-12-17 15:59:43 +000035 const Node* copy(const SkDOM& dom, const Node* node);
36
37 const Node* getRootNode() const;
38
fmalita7a048692015-02-20 13:54:40 -080039 SkXMLParser* beginParsing();
40 const Node* finishParsing();
41
reed@android.com8a1c16f2008-12-17 15:59:43 +000042 enum Type {
43 kElement_Type,
44 kText_Type
45 };
Mike Reedc3063e52017-01-07 16:16:02 -050046 Type getType(const Node*) const;
reed@android.com8a1c16f2008-12-17 15:59:43 +000047
48 const char* getName(const Node*) const;
Ben Wagnera93a14a2017-08-28 10:34:05 -040049 const Node* getFirstChild(const Node*, const char elem[] = nullptr) const;
50 const Node* getNextSibling(const Node*, const char elem[] = nullptr) const;
reed@android.com8a1c16f2008-12-17 15:59:43 +000051
52 const char* findAttr(const Node*, const char attrName[]) const;
53 const Attr* getFirstAttr(const Node*) const;
54 const Attr* getNextAttr(const Node*, const Attr*) const;
55 const char* getAttrName(const Node*, const Attr*) const;
56 const char* getAttrValue(const Node*, const Attr*) const;
rmistry@google.comfbfcd562012-08-23 18:09:54 +000057
reed@android.com8a1c16f2008-12-17 15:59:43 +000058 // helpers for walking children
Ben Wagnera93a14a2017-08-28 10:34:05 -040059 int countChildren(const Node* node, const char elem[] = nullptr) const;
reed@android.com8a1c16f2008-12-17 15:59:43 +000060
61 // helpers for calling SkParse
62 bool findS32(const Node*, const char name[], int32_t* value) const;
63 bool findScalars(const Node*, const char name[], SkScalar value[], int count) const;
64 bool findHex(const Node*, const char name[], uint32_t* value) const;
65 bool findBool(const Node*, const char name[], bool*) const;
66 int findList(const Node*, const char name[], const char list[]) const;
67
Mike Reedc3063e52017-01-07 16:16:02 -050068 bool findScalar(const Node* node, const char name[], SkScalar value[]) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +000069 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:
fmalita6ceef3d2016-07-26 18:46:34 -070080 AttrIter(const SkDOM&, const Node*);
reed@android.com8a1c16f2008-12-17 15:59:43 +000081 const char* next(const char** value);
82 private:
83 const Attr* fAttr;
84 const Attr* fStop;
85 };
86
reed@android.com8a1c16f2008-12-17 15:59:43 +000087private:
Herb Derby70c60632017-02-14 15:21:07 -050088 SkArenaAlloc fAlloc;
bungeman6bd52842016-10-27 09:30:08 -070089 Node* fRoot;
90 std::unique_ptr<SkDOMParser> fParser;
fmalita7a048692015-02-20 13:54:40 -080091
fmalita7445e862016-07-14 19:14:06 -070092 typedef SkNoncopyable INHERITED;
reed@android.com8a1c16f2008-12-17 15:59:43 +000093};
94
95#endif