blob: ed042281a73f8a558ba9789b16b9c5d3576ae697 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
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.com8a1c16f2008-12-17 15:59:43 +00008#include "SkXMLParser.h"
9#include "SkStream.h"
10
11static void reset(SkXMLPullParser::Curr* curr)
12{
13 curr->fEventType = SkXMLPullParser::ERROR;
14 curr->fName = "";
15 curr->fAttrInfoCount = 0;
16 curr->fIsWhitespace = false;
17}
18
19SkXMLPullParser::SkXMLPullParser() : fStream(NULL)
20{
21 fCurr.fEventType = ERROR;
22 fDepth = -1;
23}
24
25SkXMLPullParser::SkXMLPullParser(SkStream* stream) : fStream(NULL)
26{
27 fCurr.fEventType = ERROR;
28 fDepth = 0;
rmistry@google.comd6176b02012-08-23 18:14:13 +000029
reed@android.com8a1c16f2008-12-17 15:59:43 +000030 this->setStream(stream);
31}
32
33SkXMLPullParser::~SkXMLPullParser()
34{
35 this->setStream(NULL);
36}
37
38SkStream* 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
59SkXMLPullParser::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.comd6176b02012-08-23 18:14:13 +000068 default:
reed@android.com8a1c16f2008-12-17 15:59:43 +000069 reset(&fCurr);
70 fCurr.fEventType = this->onNextToken();
71 break;
72 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000073
reed@android.com8a1c16f2008-12-17 15:59:43 +000074 switch (fCurr.fEventType) {
75 case START_TAG:
76 fDepth += 1;
77 break;
78 default:
79 break;
80 }
81
82 return fCurr.fEventType;
83}
84
85const 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
96const 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
107bool 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
120int SkXMLPullParser::getAttributeCount()
121{
122 return fCurr.fAttrInfoCount;
123}
124
125void SkXMLPullParser::getAttributeInfo(int index, AttrInfo* info)
126{
127 SkASSERT((unsigned)index < (unsigned)fCurr.fAttrInfoCount);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000128
reed@android.com8a1c16f2008-12-17 15:59:43 +0000129 if (info)
130 *info = fCurr.fAttrInfos[index];
131}
rmistry@google.comd6176b02012-08-23 18:14:13 +0000132
reed@android.com8a1c16f2008-12-17 15:59:43 +0000133bool SkXMLPullParser::onEntityReplacement(const char name[],
134 SkString* replacement)
135{
136 // TODO: std 5 entities here
137 return false;
138}