blob: e0bb744d6d1368b64cc5cce3583958874e29780a [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
13#include "SkChunkAlloc.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000014#include "SkScalar.h"
15#include "SkTemplates.h"
16
17struct SkDOMNode;
18struct SkDOMAttr;
19
20class SkDOM {
21public:
22 SkDOM();
23 ~SkDOM();
24
25 typedef SkDOMNode Node;
26 typedef SkDOMAttr Attr;
27
28 /** Returns null on failure
29 */
30 const Node* build(const char doc[], size_t len);
31 const Node* copy(const SkDOM& dom, const Node* node);
32
33 const Node* getRootNode() const;
34
35 enum Type {
36 kElement_Type,
37 kText_Type
38 };
39 Type getType(const Node*) const;
40
41 const char* getName(const Node*) const;
42 const Node* getFirstChild(const Node*, const char elem[] = NULL) const;
43 const Node* getNextSibling(const Node*, const char elem[] = NULL) const;
44
45 const char* findAttr(const Node*, const char attrName[]) const;
46 const Attr* getFirstAttr(const Node*) const;
47 const Attr* getNextAttr(const Node*, const Attr*) const;
48 const char* getAttrName(const Node*, const Attr*) const;
49 const char* getAttrValue(const Node*, const Attr*) const;
rmistry@google.comfbfcd562012-08-23 18:09:54 +000050
reed@android.com8a1c16f2008-12-17 15:59:43 +000051 // helpers for walking children
52 int countChildren(const Node* node, const char elem[] = NULL) const;
53
54 // helpers for calling SkParse
55 bool findS32(const Node*, const char name[], int32_t* value) const;
56 bool findScalars(const Node*, const char name[], SkScalar value[], int count) const;
57 bool findHex(const Node*, const char name[], uint32_t* value) const;
58 bool findBool(const Node*, const char name[], bool*) const;
59 int findList(const Node*, const char name[], const char list[]) const;
60
61 bool findScalar(const Node* node, const char name[], SkScalar value[]) const
62 {
63 return this->findScalars(node, name, value, 1);
64 }
65
66 bool hasAttr(const Node*, const char name[], const char value[]) const;
67 bool hasS32(const Node*, const char name[], int32_t value) const;
68 bool hasScalar(const Node*, const char name[], SkScalar value) const;
69 bool hasHex(const Node*, const char name[], uint32_t value) const;
70 bool hasBool(const Node*, const char name[], bool value) const;
71
72 class AttrIter {
73 public:
74 AttrIter(const class SkDOM&, const Node*);
75 const char* next(const char** value);
76 private:
77 const Attr* fAttr;
78 const Attr* fStop;
79 };
80
81 SkDEBUGCODE(void dump(const Node* node = NULL, int tabLevel = 0) const;)
82 SkDEBUGCODE(static void UnitTest();)
83
84private:
85 SkChunkAlloc fAlloc;
86 Node* fRoot;
87 friend class AttrIter;
88 friend class SkDOMParser;
89};
90
91#endif