Home | History | Annotate | Download | only in xml
      1 #include "SkXMLParser.h"
      2 #include "SkStream.h"
      3 
      4 static void reset(SkXMLPullParser::Curr* curr)
      5 {
      6     curr->fEventType = SkXMLPullParser::ERROR;
      7     curr->fName = "";
      8     curr->fAttrInfoCount = 0;
      9     curr->fIsWhitespace = false;
     10 }
     11 
     12 SkXMLPullParser::SkXMLPullParser() : fStream(NULL)
     13 {
     14     fCurr.fEventType = ERROR;
     15     fDepth = -1;
     16 }
     17 
     18 SkXMLPullParser::SkXMLPullParser(SkStream* stream) : fStream(NULL)
     19 {
     20     fCurr.fEventType = ERROR;
     21     fDepth = 0;
     22 
     23     this->setStream(stream);
     24 }
     25 
     26 SkXMLPullParser::~SkXMLPullParser()
     27 {
     28     this->setStream(NULL);
     29 }
     30 
     31 SkStream* SkXMLPullParser::setStream(SkStream* stream)
     32 {
     33     if (fStream && !stream)
     34         this->onExit();
     35 
     36     SkRefCnt_SafeAssign(fStream, stream);
     37 
     38     if (fStream)
     39     {
     40         fCurr.fEventType = START_DOCUMENT;
     41         this->onInit();
     42     }
     43     else
     44     {
     45         fCurr.fEventType = ERROR;
     46     }
     47     fDepth = 0;
     48 
     49     return fStream;
     50 }
     51 
     52 SkXMLPullParser::EventType SkXMLPullParser::nextToken()
     53 {
     54     switch (fCurr.fEventType) {
     55     case ERROR:
     56     case END_DOCUMENT:
     57         break;
     58     case END_TAG:
     59         fDepth -= 1;
     60         // fall through
     61     default:
     62         reset(&fCurr);
     63         fCurr.fEventType = this->onNextToken();
     64         break;
     65     }
     66 
     67     switch (fCurr.fEventType) {
     68     case START_TAG:
     69         fDepth += 1;
     70         break;
     71     default:
     72         break;
     73     }
     74 
     75     return fCurr.fEventType;
     76 }
     77 
     78 const char* SkXMLPullParser::getName()
     79 {
     80     switch (fCurr.fEventType) {
     81     case START_TAG:
     82     case END_TAG:
     83         return fCurr.fName;
     84     default:
     85         return NULL;
     86     }
     87 }
     88 
     89 const char* SkXMLPullParser::getText()
     90 {
     91     switch (fCurr.fEventType) {
     92     case TEXT:
     93     case IGNORABLE_WHITESPACE:
     94         return fCurr.fName;
     95     default:
     96         return NULL;
     97     }
     98 }
     99 
    100 bool SkXMLPullParser::isWhitespace()
    101 {
    102     switch (fCurr.fEventType) {
    103     case IGNORABLE_WHITESPACE:
    104         return true;
    105     case TEXT:
    106     case CDSECT:
    107         return fCurr.fIsWhitespace;
    108     default:
    109         return false;   // unknown/illegal
    110     }
    111 }
    112 
    113 int SkXMLPullParser::getAttributeCount()
    114 {
    115     return fCurr.fAttrInfoCount;
    116 }
    117 
    118 void SkXMLPullParser::getAttributeInfo(int index, AttrInfo* info)
    119 {
    120     SkASSERT((unsigned)index < (unsigned)fCurr.fAttrInfoCount);
    121 
    122     if (info)
    123         *info = fCurr.fAttrInfos[index];
    124 }
    125 
    126 bool SkXMLPullParser::onEntityReplacement(const char name[],
    127                                           SkString* replacement)
    128 {
    129     // TODO: std 5 entities here
    130     return false;
    131 }
    132 
    133