| 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 "include/core/SkScalar.h" |
| 12 |
#include "include/core/SkTypes.h" |
| 13 |
#include "include/private/base/SkNoncopyable.h" |
| 14 |
#include "include/private/base/SkTemplates.h" |
| 15 |
#include "src/base/SkArenaAlloc.h" |
| 16 |
|
| 17 |
struct SkDOMNode; |
| 18 |
struct SkDOMAttr; |
| 19 |
|
| 20 |
class SkDOMParser; |
| 21 |
class SkStream; |
| 22 |
class SkXMLParser; |
| 23 |
|
| 24 |
class SkDOM : public SkNoncopyable { |
| 25 |
public: |
| 26 |
SkDOM(); |
| 27 |
~SkDOM(); |
| 28 |
|
| 29 |
typedef SkDOMNode Node; |
| 30 |
typedef SkDOMAttr Attr; |
| 31 |
|
| 32 |
/** |
| 33 |
* Returns null on failure, and if errorOnLineNumber is present, it is set |
| 34 |
* to the line number where the error occured. On success, returns the |
| 35 |
* root node, and, if present, sets errorOnLineNumber to -1 (no error). |
| 36 |
*/ |
| 37 |
const Node* build(SkStream&, int* errorOnLineNumber = nullptr); |
| 38 |
const Node* copy(const SkDOM& dom, const Node* node); |
| 39 |
|
| 40 |
const Node* getRootNode() const; |
| 41 |
|
| 42 |
SkXMLParser* beginParsing(); |
| 43 |
const Node* finishParsing(); |
| 44 |
|
| 45 |
enum Type { |
| 46 |
kElement_Type, |
| 47 |
kText_Type |
| 48 |
}; |
| 49 |
Type getType(const Node*) const; |
| 50 |
|
| 51 |
const char* getName(const Node*) const; |
| 52 |
const Node* getFirstChild(const Node*, const char elem[] = nullptr) const; |
| 53 |
const Node* getNextSibling(const Node*, const char elem[] = nullptr) const; |
| 54 |
|
| 55 |
const char* findAttr(const Node*, const char attrName[]) const; |
| 56 |
const Attr* getFirstAttr(const Node*) const; |
| 57 |
const Attr* getNextAttr(const Node*, const Attr*) const; |
| 58 |
const char* getAttrName(const Node*, const Attr*) const; |
| 59 |
const char* getAttrValue(const Node*, const Attr*) const; |
| 60 |
|
| 61 |
// helpers for walking children |
| 62 |
int countChildren(const Node* node, const char elem[] = nullptr) const; |
| 63 |
|
| 64 |
// helpers for calling SkParse |
| 65 |
bool findS32(const Node*, const char name[], int32_t* value) const; |
| 66 |
bool findScalars(const Node*, const char name[], SkScalar value[], int count) const; |
| 67 |
bool findHex(const Node*, const char name[], uint32_t* value) const; |
| 68 |
bool findBool(const Node*, const char name[], bool*) const; |
| 69 |
int findList(const Node*, const char name[], const char list[]) const; |
| 70 |
|
| 71 |
bool findScalar(const Node* node, const char name[], SkScalar value[]) const { |
| 72 |
return this->findScalars(node, name, value, 1); |
| 73 |
} |
| 74 |
|
| 75 |
bool hasAttr(const Node*, const char name[], const char value[]) const; |
| 76 |
bool hasS32(const Node*, const char name[], int32_t value) const; |
| 77 |
bool hasScalar(const Node*, const char name[], SkScalar value) const; |
| 78 |
bool hasHex(const Node*, const char name[], uint32_t value) const; |
| 79 |
bool hasBool(const Node*, const char name[], bool value) const; |
| 80 |
|
| 81 |
class AttrIter { |
| 82 |
public: |
| 83 |
AttrIter(const SkDOM&, const Node*); |
| 84 |
const char* next(const char** value); |
| 85 |
private: |
| 86 |
const Attr* fAttr; |
| 87 |
const Attr* fStop; |
| 88 |
}; |
| 89 |
|
| 90 |
private: |
| 91 |
SkArenaAllocWithReset fAlloc; |
| 92 |
Node* fRoot; |
| 93 |
std::unique_ptr<SkDOMParser> fParser; |
| 94 |
|
| 95 |
using INHERITED = SkNoncopyable; |
| 96 |
}; |
| 97 |
|
| 98 |
#endif |