Home | History | Annotate | Download | only in xml
      1 
      2 /*
      3  * Copyright 2006 The Android Open Source Project
      4  *
      5  * Use of this source code is governed by a BSD-style license that can be
      6  * found in the LICENSE file.
      7  */
      8 
      9 
     10 #ifndef SkDOM_DEFINED
     11 #define SkDOM_DEFINED
     12 
     13 #include "SkChunkAlloc.h"
     14 #include "SkScalar.h"
     15 #include "SkTemplates.h"
     16 
     17 struct SkDOMNode;
     18 struct SkDOMAttr;
     19 
     20 class SkDOM {
     21 public:
     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;
     50 
     51     // 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 
     84 private:
     85     SkChunkAlloc    fAlloc;
     86     Node*           fRoot;
     87     friend class AttrIter;
     88     friend class SkDOMParser;
     89 };
     90 
     91 #endif
     92