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