1ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com
2ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com/*
3ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Copyright 2011 Google Inc.
4ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com *
5ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * Use of this source code is governed by a BSD-style license that can be
6ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com * found in the LICENSE file.
7ec3ed6a5ebf6f2c406d7bcf94b6bc34fcaeb976eepoger@google.com */
88a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkXMLParser.h"
98a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com#include "SkStream.h"
108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comstatic void reset(SkXMLPullParser::Curr* curr)
128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    curr->fEventType = SkXMLPullParser::ERROR;
148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    curr->fName = "";
158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    curr->fAttrInfoCount = 0;
168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    curr->fIsWhitespace = false;
178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comSkXMLPullParser::SkXMLPullParser() : fStream(NULL)
208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    fCurr.fEventType = ERROR;
228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    fDepth = -1;
238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comSkXMLPullParser::SkXMLPullParser(SkStream* stream) : fStream(NULL)
268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    fCurr.fEventType = ERROR;
288a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    fDepth = 0;
29d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com
308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    this->setStream(stream);
318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
328a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comSkXMLPullParser::~SkXMLPullParser()
348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    this->setStream(NULL);
368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comSkStream* SkXMLPullParser::setStream(SkStream* stream)
398a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
408a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (fStream && !stream)
418a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        this->onExit();
428a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
438a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkRefCnt_SafeAssign(fStream, stream);
448a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
458a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (fStream)
468a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    {
478a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        fCurr.fEventType = START_DOCUMENT;
488a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        this->onInit();
498a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
508a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    else
518a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    {
528a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        fCurr.fEventType = ERROR;
538a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
548a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    fDepth = 0;
558a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
568a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return fStream;
578a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
588a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
598a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comSkXMLPullParser::EventType SkXMLPullParser::nextToken()
608a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
618a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    switch (fCurr.fEventType) {
628a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    case ERROR:
638a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    case END_DOCUMENT:
648a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        break;
658a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    case END_TAG:
668a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        fDepth -= 1;
678a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        // fall through
68d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com    default:
698a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        reset(&fCurr);
708a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        fCurr.fEventType = this->onNextToken();
718a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        break;
728a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
73d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com
748a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    switch (fCurr.fEventType) {
758a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    case START_TAG:
768a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        fDepth += 1;
778a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        break;
788a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    default:
798a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        break;
808a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
818a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
828a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return fCurr.fEventType;
838a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
848a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
858a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comconst char* SkXMLPullParser::getName()
868a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
878a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    switch (fCurr.fEventType) {
888a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    case START_TAG:
898a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    case END_TAG:
908a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return fCurr.fName;
918a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    default:
928a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return NULL;
938a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
948a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
958a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
968a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comconst char* SkXMLPullParser::getText()
978a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
988a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    switch (fCurr.fEventType) {
998a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    case TEXT:
1008a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    case IGNORABLE_WHITESPACE:
1018a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return fCurr.fName;
1028a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    default:
1038a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return NULL;
1048a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
1058a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
1068a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1078a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.combool SkXMLPullParser::isWhitespace()
1088a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
1098a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    switch (fCurr.fEventType) {
1108a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    case IGNORABLE_WHITESPACE:
1118a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return true;
1128a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    case TEXT:
1138a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    case CDSECT:
1148a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return fCurr.fIsWhitespace;
1158a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    default:
1168a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        return false;   // unknown/illegal
1178a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    }
1188a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
1198a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1208a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comint SkXMLPullParser::getAttributeCount()
1218a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
1228a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return fCurr.fAttrInfoCount;
1238a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
1248a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com
1258a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.comvoid SkXMLPullParser::getAttributeInfo(int index, AttrInfo* info)
1268a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
1278a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    SkASSERT((unsigned)index < (unsigned)fCurr.fAttrInfoCount);
128d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com
1298a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    if (info)
1308a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com        *info = fCurr.fAttrInfos[index];
1318a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
132d6176b0dcacb124539e0cfd051e6d93a9782f020rmistry@google.com
1338a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.combool SkXMLPullParser::onEntityReplacement(const char name[],
1348a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com                                          SkString* replacement)
1358a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com{
1368a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    // TODO: std 5 entities here
1378a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com    return false;
1388a1c16ff38322f0210116fa7293eb8817c7e477ereed@android.com}
139