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 SkXMLParser_DEFINED
     18 #define SkXMLParser_DEFINED
     19 
     20 #include "SkMath.h"
     21 #include "SkString.h"
     22 
     23 class SkStream;
     24 
     25 class SkDOM;
     26 struct SkDOMNode;
     27 
     28 class SkXMLParserError {
     29 public:
     30     enum ErrorCode {
     31         kNoError,
     32         kEmptyFile,
     33         kUnknownElement,
     34         kUnknownAttributeName,
     35         kErrorInAttributeValue,
     36         kDuplicateIDs,
     37         kUnknownError
     38     };
     39 
     40     SkXMLParserError();
     41     virtual ~SkXMLParserError();
     42     ErrorCode getErrorCode() const { return fCode; }
     43     virtual void getErrorString(SkString* str) const;
     44     int getLineNumber() const { return fLineNumber; }
     45     int getNativeCode() const { return fNativeCode; }
     46     bool hasError() const { return fCode != kNoError || fNativeCode != -1; }
     47     bool hasNoun() const { return fNoun.size() > 0; }
     48     void reset();
     49     void setCode(ErrorCode code) { fCode = code; }
     50     void setNoun(const SkString& str) { fNoun.set(str); }
     51     void setNoun(const char* ch)  { fNoun.set(ch); }
     52     void setNoun(const char* ch, size_t len) { fNoun.set(ch, len); }
     53 protected:
     54     ErrorCode fCode;
     55 private:
     56     int fLineNumber;
     57     int fNativeCode;
     58     SkString fNoun;
     59     friend class SkXMLParser;
     60 };
     61 
     62 class SkXMLParser {
     63 public:
     64             SkXMLParser(SkXMLParserError* parserError = NULL);
     65     virtual ~SkXMLParser();
     66 
     67     /** Returns true for success
     68     */
     69     bool parse(const char doc[], size_t len);
     70     bool parse(SkStream& docStream);
     71     bool parse(const SkDOM&, const SkDOMNode*);
     72 
     73     static void GetNativeErrorString(int nativeErrorCode, SkString* str);
     74 
     75 protected:
     76     // override in subclasses; return true to stop parsing
     77     virtual bool onStartElement(const char elem[]);
     78     virtual bool onAddAttribute(const char name[], const char value[]);
     79     virtual bool onEndElement(const char elem[]);
     80     virtual bool onText(const char text[], int len);
     81 
     82 public:
     83     // public for ported implementation, not meant for clients to call
     84     virtual bool startElement(const char elem[]);
     85     virtual bool addAttribute(const char name[], const char value[]);
     86     virtual bool endElement(const char elem[]);
     87     virtual bool text(const char text[], int len);
     88     void* fParser;
     89 protected:
     90     SkXMLParserError* fError;
     91 private:
     92     void reportError(void* parser);
     93 };
     94 
     95 #if 0
     96 class SkXMLPullParser {
     97 public:
     98             SkXMLPullParser();
     99     explicit SkXMLPullParser(SkStream*);
    100     virtual ~SkXMLPullParser();
    101 
    102     SkStream*   getStream() const { return fStream; }
    103     SkStream*   setStream(SkStream* stream);
    104 
    105     enum EventType {
    106         ERROR = -1,
    107         START_DOCUMENT,
    108         END_DOCUMENT,
    109         START_TAG,
    110         END_TAG,
    111         TEXT,
    112         CDSECT,
    113         ENTITY_REF,
    114         IGNORABLE_WHITESPACE,
    115         PROCESSING_INSTRUCTION,
    116         COMMENT,
    117         DOCDECL
    118     };
    119 
    120     EventType   nextToken();
    121     EventType   getEventType() const { return fCurr.fEventType; }
    122 
    123     struct AttrInfo {
    124         const char* fName;
    125         const char* fValue;
    126     };
    127 
    128     int         getDepth() const { return fDepth; }
    129     const char* getName();
    130     int         getAttributeCount();
    131     void        getAttributeInfo(int, AttrInfo*);
    132     const char* getText();
    133     bool        isWhitespace();
    134 
    135 protected:
    136     virtual bool onEntityReplacement(const char name[],
    137                                      SkString* replacement);
    138 
    139 public:
    140     struct Curr {
    141         EventType   fEventType;
    142         const char* fName;
    143         AttrInfo*   fAttrInfos;
    144         int         fAttrInfoCount;
    145         bool        fIsWhitespace;
    146     };
    147 
    148 private:
    149     // implemented in the porting layer
    150     bool        onInit();   // return false on failure
    151     EventType   onNextToken();
    152     void        onExit();
    153 
    154     SkStream*   fStream;
    155     Curr        fCurr;
    156     int         fDepth;
    157 
    158     struct Impl;
    159     Impl*   fImpl;
    160 };
    161 #endif
    162 
    163 #endif
    164