michael@0: michael@0: /* michael@0: * Copyright 2006 The Android Open Source Project michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: michael@0: #include "SkXMLParser.h" michael@0: #include "SkChunkAlloc.h" michael@0: #include "SkString.h" michael@0: #include "SkStream.h" michael@0: michael@0: #include "expat.h" michael@0: michael@0: static inline char* dupstr(SkChunkAlloc& chunk, const char src[], size_t len) michael@0: { michael@0: SkASSERT(src); michael@0: char* dst = (char*)chunk.alloc(len + 1, SkChunkAlloc::kThrow_AllocFailType); michael@0: michael@0: memcpy(dst, src, len); michael@0: dst[len] = 0; michael@0: return dst; michael@0: } michael@0: michael@0: static inline int count_pairs(const char** p) michael@0: { michael@0: const char** start = p; michael@0: while (*p) michael@0: { michael@0: SkASSERT(p[1] != NULL); michael@0: p += 2; michael@0: } michael@0: return (p - start) >> 1; michael@0: } michael@0: michael@0: struct Data { michael@0: Data() : fAlloc(2048), fState(NORMAL) {} michael@0: michael@0: XML_Parser fParser; michael@0: SkXMLPullParser::Curr* fCurr; michael@0: SkChunkAlloc fAlloc; michael@0: michael@0: enum State { michael@0: NORMAL, michael@0: MISSED_START_TAG, michael@0: RETURN_END_TAG michael@0: }; michael@0: State fState; michael@0: const char* fEndTag; // if state is RETURN_END_TAG michael@0: }; michael@0: michael@0: static void XMLCALL start_proc(void *data, const char *el, const char **attr) michael@0: { michael@0: Data* p = (Data*)data; michael@0: SkXMLPullParser::Curr* c = p->fCurr; michael@0: SkChunkAlloc& alloc = p->fAlloc; michael@0: michael@0: c->fName = dupstr(alloc, el, strlen(el)); michael@0: michael@0: int n = count_pairs(attr); michael@0: SkXMLPullParser::AttrInfo* info = (SkXMLPullParser::AttrInfo*)alloc.alloc(n * sizeof(SkXMLPullParser::AttrInfo), michael@0: SkChunkAlloc::kThrow_AllocFailType); michael@0: c->fAttrInfoCount = n; michael@0: c->fAttrInfos = info; michael@0: michael@0: for (int i = 0; i < n; i++) michael@0: { michael@0: info[i].fName = dupstr(alloc, attr[0], strlen(attr[0])); michael@0: info[i].fValue = dupstr(alloc, attr[1], strlen(attr[1])); michael@0: attr += 2; michael@0: } michael@0: michael@0: c->fEventType = SkXMLPullParser::START_TAG; michael@0: XML_StopParser(p->fParser, true); michael@0: } michael@0: michael@0: static void XMLCALL end_proc(void *data, const char *el) michael@0: { michael@0: Data* p = (Data*)data; michael@0: SkXMLPullParser::Curr* c = p->fCurr; michael@0: michael@0: if (c->fEventType == SkXMLPullParser::START_TAG) michael@0: { michael@0: /* if we get here, we were called with a start_tag immediately michael@0: followed by this end_tag. The caller will only see the end_tag, michael@0: so we set a flag to notify them of the missed start_tag michael@0: */ michael@0: p->fState = Data::MISSED_START_TAG; michael@0: michael@0: SkASSERT(c->fName != NULL); michael@0: SkASSERT(strcmp(c->fName, el) == 0); michael@0: } michael@0: else michael@0: c->fName = dupstr(p->fAlloc, el, strlen(el)); michael@0: michael@0: c->fEventType = SkXMLPullParser::END_TAG; michael@0: XML_StopParser(p->fParser, true); michael@0: } michael@0: michael@0: #include michael@0: michael@0: static bool isws(const char s[]) michael@0: { michael@0: for (; *s; s++) michael@0: if (!isspace(*s)) michael@0: return false; michael@0: return true; michael@0: } michael@0: michael@0: static void XMLCALL text_proc(void* data, const char* text, int len) michael@0: { michael@0: Data* p = (Data*)data; michael@0: SkXMLPullParser::Curr* c = p->fCurr; michael@0: michael@0: c->fName = dupstr(p->fAlloc, text, len); michael@0: c->fIsWhitespace = isws(c->fName); michael@0: michael@0: c->fEventType = SkXMLPullParser::TEXT; michael@0: XML_StopParser(p->fParser, true); michael@0: } michael@0: michael@0: ////////////////////////////////////////////////////////////////////////// michael@0: michael@0: struct SkXMLPullParser::Impl { michael@0: Data fData; michael@0: void* fBuffer; michael@0: size_t fBufferLen; michael@0: }; michael@0: michael@0: static void reportError(XML_Parser parser) michael@0: { michael@0: XML_Error code = XML_GetErrorCode(parser); michael@0: int lineNumber = XML_GetCurrentLineNumber(parser); michael@0: const char* msg = XML_ErrorString(code); michael@0: michael@0: printf("-------- XML error [%d] on line %d, %s\n", code, lineNumber, msg); michael@0: } michael@0: michael@0: bool SkXMLPullParser::onInit() michael@0: { michael@0: fImpl = new Impl; michael@0: michael@0: XML_Parser p = XML_ParserCreate(NULL); michael@0: SkASSERT(p); michael@0: michael@0: fImpl->fData.fParser = p; michael@0: fImpl->fData.fCurr = &fCurr; michael@0: michael@0: XML_SetElementHandler(p, start_proc, end_proc); michael@0: XML_SetCharacterDataHandler(p, text_proc); michael@0: XML_SetUserData(p, &fImpl->fData); michael@0: michael@0: size_t len = fStream->getLength(); michael@0: fImpl->fBufferLen = len; michael@0: fImpl->fBuffer = sk_malloc_throw(len); michael@0: fStream->rewind(); michael@0: size_t len2 = fStream->read(fImpl->fBuffer, len); michael@0: return len2 == len; michael@0: } michael@0: michael@0: void SkXMLPullParser::onExit() michael@0: { michael@0: sk_free(fImpl->fBuffer); michael@0: XML_ParserFree(fImpl->fData.fParser); michael@0: delete fImpl; michael@0: fImpl = NULL; michael@0: } michael@0: michael@0: SkXMLPullParser::EventType SkXMLPullParser::onNextToken() michael@0: { michael@0: if (Data::RETURN_END_TAG == fImpl->fData.fState) michael@0: { michael@0: fImpl->fData.fState = Data::NORMAL; michael@0: fCurr.fName = fImpl->fData.fEndTag; // restore name from (below) save michael@0: return SkXMLPullParser::END_TAG; michael@0: } michael@0: michael@0: fImpl->fData.fAlloc.reset(); michael@0: michael@0: XML_Parser p = fImpl->fData.fParser; michael@0: XML_Status status; michael@0: michael@0: status = XML_ResumeParser(p); michael@0: michael@0: CHECK_STATUS: michael@0: switch (status) { michael@0: case XML_STATUS_OK: michael@0: return SkXMLPullParser::END_DOCUMENT; michael@0: michael@0: case XML_STATUS_ERROR: michael@0: if (XML_GetErrorCode(p) != XML_ERROR_NOT_SUSPENDED) michael@0: { michael@0: reportError(p); michael@0: return SkXMLPullParser::ERROR; michael@0: } michael@0: status = XML_Parse(p, (const char*)fImpl->fBuffer, fImpl->fBufferLen, true); michael@0: goto CHECK_STATUS; michael@0: michael@0: case XML_STATUS_SUSPENDED: michael@0: if (Data::MISSED_START_TAG == fImpl->fData.fState) michael@0: { michael@0: // return a start_tag, and clear the flag so we return end_tag next michael@0: SkASSERT(SkXMLPullParser::END_TAG == fCurr.fEventType); michael@0: fImpl->fData.fState = Data::RETURN_END_TAG; michael@0: fImpl->fData.fEndTag = fCurr.fName; // save this pointer michael@0: return SkXMLPullParser::START_TAG; michael@0: } michael@0: break; michael@0: } michael@0: return fCurr.fEventType; michael@0: }