• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  */
8 #include "SkXMLParser.h"
9 #include "SkStream.h"
10 
reset(SkXMLPullParser::Curr * curr)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 
SkXMLPullParser()19 SkXMLPullParser::SkXMLPullParser() : fStream(NULL)
20 {
21     fCurr.fEventType = ERROR;
22     fDepth = -1;
23 }
24 
SkXMLPullParser(SkStream * stream)25 SkXMLPullParser::SkXMLPullParser(SkStream* stream) : fStream(NULL)
26 {
27     fCurr.fEventType = ERROR;
28     fDepth = 0;
29 
30     this->setStream(stream);
31 }
32 
~SkXMLPullParser()33 SkXMLPullParser::~SkXMLPullParser()
34 {
35     this->setStream(NULL);
36 }
37 
setStream(SkStream * stream)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 
nextToken()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
68     default:
69         reset(&fCurr);
70         fCurr.fEventType = this->onNextToken();
71         break;
72     }
73 
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 
getName()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 
getText()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 
isWhitespace()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 
getAttributeCount()120 int SkXMLPullParser::getAttributeCount()
121 {
122     return fCurr.fAttrInfoCount;
123 }
124 
getAttributeInfo(int index,AttrInfo * info)125 void SkXMLPullParser::getAttributeInfo(int index, AttrInfo* info)
126 {
127     SkASSERT((unsigned)index < (unsigned)fCurr.fAttrInfoCount);
128 
129     if (info)
130         *info = fCurr.fAttrInfos[index];
131 }
132 
onEntityReplacement(const char name[],SkString * replacement)133 bool SkXMLPullParser::onEntityReplacement(const char name[],
134                                           SkString* replacement)
135 {
136     // TODO: std 5 entities here
137     return false;
138 }
139