Home | History | Annotate | Download | only in ports
      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 #include "SkXMLParser.h"
     11 #include "SkChunkAlloc.h"
     12 #include "SkString.h"
     13 #include "SkStream.h"
     14 
     15 #include "expat.h"
     16 
     17 static inline char* dupstr(SkChunkAlloc& chunk, const char src[], size_t len)
     18 {
     19     SkASSERT(src);
     20     char*   dst = (char*)chunk.alloc(len + 1, SkChunkAlloc::kThrow_AllocFailType);
     21 
     22     memcpy(dst, src, len);
     23     dst[len] = 0;
     24     return dst;
     25 }
     26 
     27 static inline int count_pairs(const char** p)
     28 {
     29     const char** start = p;
     30     while (*p)
     31     {
     32         SkASSERT(p[1] != NULL);
     33         p += 2;
     34     }
     35     return (p - start) >> 1;
     36 }
     37 
     38 struct Data {
     39     Data() : fAlloc(2048), fState(NORMAL) {}
     40 
     41     XML_Parser              fParser;
     42     SkXMLPullParser::Curr*  fCurr;
     43     SkChunkAlloc            fAlloc;
     44 
     45     enum State {
     46         NORMAL,
     47         MISSED_START_TAG,
     48         RETURN_END_TAG
     49     };
     50     State fState;
     51     const char* fEndTag;    // if state is RETURN_END_TAG
     52 };
     53 
     54 static void XMLCALL start_proc(void *data, const char *el, const char **attr)
     55 {
     56     Data*                   p = (Data*)data;
     57     SkXMLPullParser::Curr*  c = p->fCurr;
     58     SkChunkAlloc&           alloc = p->fAlloc;
     59 
     60     c->fName = dupstr(alloc, el, strlen(el));
     61 
     62     int n = count_pairs(attr);
     63     SkXMLPullParser::AttrInfo* info = (SkXMLPullParser::AttrInfo*)alloc.alloc(n * sizeof(SkXMLPullParser::AttrInfo),
     64                                                                               SkChunkAlloc::kThrow_AllocFailType);
     65     c->fAttrInfoCount = n;
     66     c->fAttrInfos = info;
     67 
     68     for (int i = 0; i < n; i++)
     69     {
     70         info[i].fName = dupstr(alloc, attr[0], strlen(attr[0]));
     71         info[i].fValue = dupstr(alloc, attr[1], strlen(attr[1]));
     72         attr += 2;
     73     }
     74 
     75     c->fEventType = SkXMLPullParser::START_TAG;
     76     XML_StopParser(p->fParser, true);
     77 }
     78 
     79 static void XMLCALL end_proc(void *data, const char *el)
     80 {
     81     Data*                   p = (Data*)data;
     82     SkXMLPullParser::Curr*  c = p->fCurr;
     83 
     84     if (c->fEventType == SkXMLPullParser::START_TAG)
     85     {
     86         /*  if we get here, we were called with a start_tag immediately
     87             followed by this end_tag. The caller will only see the end_tag,
     88             so we set a flag to notify them of the missed start_tag
     89         */
     90         p->fState = Data::MISSED_START_TAG;
     91 
     92         SkASSERT(c->fName != NULL);
     93         SkASSERT(strcmp(c->fName, el) == 0);
     94     }
     95     else
     96         c->fName = dupstr(p->fAlloc, el, strlen(el));
     97 
     98     c->fEventType = SkXMLPullParser::END_TAG;
     99     XML_StopParser(p->fParser, true);
    100 }
    101 
    102 #include <ctype.h>
    103 
    104 static bool isws(const char s[])
    105 {
    106     for (; *s; s++)
    107         if (!isspace(*s))
    108             return false;
    109     return true;
    110 }
    111 
    112 static void XMLCALL text_proc(void* data, const char* text, int len)
    113 {
    114     Data*                   p = (Data*)data;
    115     SkXMLPullParser::Curr*  c = p->fCurr;
    116 
    117     c->fName = dupstr(p->fAlloc, text, len);
    118     c->fIsWhitespace = isws(c->fName);
    119 
    120     c->fEventType = SkXMLPullParser::TEXT;
    121     XML_StopParser(p->fParser, true);
    122 }
    123 
    124 //////////////////////////////////////////////////////////////////////////
    125 
    126 struct SkXMLPullParser::Impl {
    127     Data    fData;
    128     void*   fBuffer;
    129     size_t  fBufferLen;
    130 };
    131 
    132 static void reportError(XML_Parser parser)
    133 {
    134     XML_Error code = XML_GetErrorCode(parser);
    135     int lineNumber = XML_GetCurrentLineNumber(parser);
    136     const char* msg = XML_ErrorString(code);
    137 
    138     printf("-------- XML error [%d] on line %d, %s\n", code, lineNumber, msg);
    139 }
    140 
    141 bool SkXMLPullParser::onInit()
    142 {
    143     fImpl = new Impl;
    144 
    145     XML_Parser p = XML_ParserCreate(NULL);
    146     SkASSERT(p);
    147 
    148     fImpl->fData.fParser = p;
    149     fImpl->fData.fCurr = &fCurr;
    150 
    151     XML_SetElementHandler(p, start_proc, end_proc);
    152     XML_SetCharacterDataHandler(p, text_proc);
    153     XML_SetUserData(p, &fImpl->fData);
    154 
    155     size_t len = fStream->read(NULL, 0);
    156     fImpl->fBufferLen = len;
    157     fImpl->fBuffer = sk_malloc_throw(len);
    158     fStream->rewind();
    159     size_t  len2 = fStream->read(fImpl->fBuffer, len);
    160     return len2 == len;
    161 }
    162 
    163 void SkXMLPullParser::onExit()
    164 {
    165     sk_free(fImpl->fBuffer);
    166     XML_ParserFree(fImpl->fData.fParser);
    167     delete fImpl;
    168     fImpl = NULL;
    169 }
    170 
    171 SkXMLPullParser::EventType SkXMLPullParser::onNextToken()
    172 {
    173     if (Data::RETURN_END_TAG == fImpl->fData.fState)
    174     {
    175         fImpl->fData.fState = Data::NORMAL;
    176         fCurr.fName = fImpl->fData.fEndTag; // restore name from (below) save
    177         return SkXMLPullParser::END_TAG;
    178     }
    179 
    180     fImpl->fData.fAlloc.reuse();
    181 
    182     XML_Parser p = fImpl->fData.fParser;
    183     XML_Status status;
    184 
    185     status = XML_ResumeParser(p);
    186 
    187 CHECK_STATUS:
    188     switch (status) {
    189     case XML_STATUS_OK:
    190         return SkXMLPullParser::END_DOCUMENT;
    191 
    192     case XML_STATUS_ERROR:
    193         if (XML_GetErrorCode(p) != XML_ERROR_NOT_SUSPENDED)
    194         {
    195             reportError(p);
    196             return SkXMLPullParser::ERROR;
    197         }
    198         status = XML_Parse(p, (const char*)fImpl->fBuffer, fImpl->fBufferLen, true);
    199         goto CHECK_STATUS;
    200 
    201     case XML_STATUS_SUSPENDED:
    202         if (Data::MISSED_START_TAG == fImpl->fData.fState)
    203         {
    204             // return a start_tag, and clear the flag so we return end_tag next
    205             SkASSERT(SkXMLPullParser::END_TAG == fCurr.fEventType);
    206             fImpl->fData.fState = Data::RETURN_END_TAG;
    207             fImpl->fData.fEndTag = fCurr.fName;  // save this pointer
    208             return SkXMLPullParser::START_TAG;
    209         }
    210         break;
    211     }
    212     return fCurr.fEventType;
    213 }
    214 
    215