Home | History | Annotate | Download | only in xml
      1 /*
      2  * Copyright (C) 2006 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #ifndef SkDOM_DEFINED
     18 #define SkDOM_DEFINED
     19 
     20 #include "SkChunkAlloc.h"
     21 #include "SkMath.h"
     22 #include "SkScalar.h"
     23 #include "SkTemplates.h"
     24 
     25 struct SkDOMNode;
     26 struct SkDOMAttr;
     27 
     28 class SkDOM {
     29 public:
     30     SkDOM();
     31     ~SkDOM();
     32 
     33     typedef SkDOMNode Node;
     34     typedef SkDOMAttr Attr;
     35 
     36     /** Returns null on failure
     37     */
     38     const Node* build(const char doc[], size_t len);
     39     const Node* copy(const SkDOM& dom, const Node* node);
     40 
     41     const Node* getRootNode() const;
     42 
     43     enum Type {
     44         kElement_Type,
     45         kText_Type
     46     };
     47     Type    getType(const Node*) const;
     48 
     49     const char* getName(const Node*) const;
     50     const Node* getFirstChild(const Node*, const char elem[] = NULL) const;
     51     const Node* getNextSibling(const Node*, const char elem[] = NULL) const;
     52 
     53     const char* findAttr(const Node*, const char attrName[]) const;
     54     const Attr* getFirstAttr(const Node*) const;
     55     const Attr* getNextAttr(const Node*, const Attr*) const;
     56     const char* getAttrName(const Node*, const Attr*) const;
     57     const char* getAttrValue(const Node*, const Attr*) const;
     58 
     59     // helpers for walking children
     60     int countChildren(const Node* node, const char elem[] = NULL) const;
     61 
     62     // helpers for calling SkParse
     63     bool findS32(const Node*, const char name[], int32_t* value) const;
     64     bool findScalars(const Node*, const char name[], SkScalar value[], int count) const;
     65     bool findHex(const Node*, const char name[], uint32_t* value) const;
     66     bool findBool(const Node*, const char name[], bool*) const;
     67     int  findList(const Node*, const char name[], const char list[]) const;
     68 
     69     bool findScalar(const Node* node, const char name[], SkScalar value[]) const
     70     {
     71         return this->findScalars(node, name, value, 1);
     72     }
     73 
     74     bool hasAttr(const Node*, const char name[], const char value[]) const;
     75     bool hasS32(const Node*, const char name[], int32_t value) const;
     76     bool hasScalar(const Node*, const char name[], SkScalar value) const;
     77     bool hasHex(const Node*, const char name[], uint32_t value) const;
     78     bool hasBool(const Node*, const char name[], bool value) const;
     79 
     80     class AttrIter {
     81     public:
     82         AttrIter(const class SkDOM&, const Node*);
     83         const char* next(const char** value);
     84     private:
     85         const Attr* fAttr;
     86         const Attr* fStop;
     87     };
     88 
     89     SkDEBUGCODE(void dump(const Node* node = NULL, int tabLevel = 0) const;)
     90     SkDEBUGCODE(static void UnitTest();)
     91 
     92 private:
     93     SkChunkAlloc    fAlloc;
     94     Node*           fRoot;
     95     friend class AttrIter;
     96     friend class SkDOMParser;
     97 };
     98 
     99 #endif
    100 
    101