blob: 14d79cf8af30488dcf9b7189b4732bc153e80738 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@android.com8a1c16f2008-12-17 15:59:43 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2006 The Android Open Source Project
reed@android.com8a1c16f2008-12-17 15:59:43 +00004 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00005 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
reed@android.com8a1c16f2008-12-17 15:59:43 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
reed@android.com8a1c16f2008-12-17 15:59:43 +000010#ifndef SkDOM_DEFINED
11#define SkDOM_DEFINED
12
bungemanf3c15b72015-08-19 11:56:48 -070013#include "../private/SkTemplates.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000014#include "SkChunkAlloc.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000015#include "SkScalar.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000016
17struct SkDOMNode;
18struct SkDOMAttr;
19
fmalita7a048692015-02-20 13:54:40 -080020class SkDOMParser;
21class SkXMLParser;
22
reed@android.com8a1c16f2008-12-17 15:59:43 +000023class SkDOM {
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(const char doc[], size_t len);
34 const Node* copy(const SkDOM& dom, const Node* node);
35
36 const Node* getRootNode() const;
37
fmalita7a048692015-02-20 13:54:40 -080038 SkXMLParser* beginParsing();
39 const Node* finishParsing();
40
reed@android.com8a1c16f2008-12-17 15:59:43 +000041 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;
rmistry@google.comfbfcd562012-08-23 18:09:54 +000056
reed@android.com8a1c16f2008-12-17 15:59:43 +000057 // 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
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
90private:
fmalita7a048692015-02-20 13:54:40 -080091 SkChunkAlloc fAlloc;
92 Node* fRoot;
93 SkAutoTDelete<SkDOMParser> fParser;
94
reed@android.com8a1c16f2008-12-17 15:59:43 +000095 friend class AttrIter;
96 friend class SkDOMParser;
97};
98
99#endif