1 /*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <iostream>
18 #include <string>
19
20 #include "Maybe.h"
21 #include "SourceXmlPullParser.h"
22 #include "Util.h"
23
24 namespace aapt {
25
26 constexpr char kXmlNamespaceSep = 1;
27
SourceXmlPullParser(std::istream & in)28 SourceXmlPullParser::SourceXmlPullParser(std::istream& in) : mIn(in), mEmpty(), mDepth(0) {
29 mParser = XML_ParserCreateNS(nullptr, kXmlNamespaceSep);
30 XML_SetUserData(mParser, this);
31 XML_SetElementHandler(mParser, startElementHandler, endElementHandler);
32 XML_SetNamespaceDeclHandler(mParser, startNamespaceHandler, endNamespaceHandler);
33 XML_SetCharacterDataHandler(mParser, characterDataHandler);
34 XML_SetCommentHandler(mParser, commentDataHandler);
35 mEventQueue.push(EventData{ Event::kStartDocument, 0, mDepth++ });
36 }
37
~SourceXmlPullParser()38 SourceXmlPullParser::~SourceXmlPullParser() {
39 XML_ParserFree(mParser);
40 }
41
next()42 SourceXmlPullParser::Event SourceXmlPullParser::next() {
43 const Event currentEvent = getEvent();
44 if (currentEvent == Event::kBadDocument || currentEvent == Event::kEndDocument) {
45 return currentEvent;
46 }
47
48 mEventQueue.pop();
49 while (mEventQueue.empty()) {
50 mIn.read(mBuffer, sizeof(mBuffer) / sizeof(*mBuffer));
51
52 const bool done = mIn.eof();
53 if (mIn.bad() && !done) {
54 mLastError = strerror(errno);
55 mEventQueue.push(EventData{ Event::kBadDocument });
56 continue;
57 }
58
59 if (XML_Parse(mParser, mBuffer, mIn.gcount(), done) == XML_STATUS_ERROR) {
60 mLastError = XML_ErrorString(XML_GetErrorCode(mParser));
61 mEventQueue.push(EventData{ Event::kBadDocument });
62 continue;
63 }
64
65 if (done) {
66 mEventQueue.push(EventData{ Event::kEndDocument, 0, 0 });
67 }
68 }
69
70 Event event = getEvent();
71
72 // Record namespace prefixes and package names so that we can do our own
73 // handling of references that use namespace aliases.
74 if (event == Event::kStartNamespace || event == Event::kEndNamespace) {
75 Maybe<std::u16string> result = util::extractPackageFromNamespace(getNamespaceUri());
76 if (event == Event::kStartNamespace) {
77 if (result) {
78 mPackageAliases.emplace_back(getNamespacePrefix(), result.value());
79 }
80 } else {
81 if (result) {
82 assert(mPackageAliases.back().second == result.value());
83 mPackageAliases.pop_back();
84 }
85 }
86 }
87
88 return event;
89 }
90
getEvent() const91 SourceXmlPullParser::Event SourceXmlPullParser::getEvent() const {
92 return mEventQueue.front().event;
93 }
94
getLastError() const95 const std::string& SourceXmlPullParser::getLastError() const {
96 return mLastError;
97 }
98
getComment() const99 const std::u16string& SourceXmlPullParser::getComment() const {
100 return mEventQueue.front().comment;
101 }
102
getLineNumber() const103 size_t SourceXmlPullParser::getLineNumber() const {
104 return mEventQueue.front().lineNumber;
105 }
106
getDepth() const107 size_t SourceXmlPullParser::getDepth() const {
108 return mEventQueue.front().depth;
109 }
110
getText() const111 const std::u16string& SourceXmlPullParser::getText() const {
112 if (getEvent() != Event::kText) {
113 return mEmpty;
114 }
115 return mEventQueue.front().data1;
116 }
117
getNamespacePrefix() const118 const std::u16string& SourceXmlPullParser::getNamespacePrefix() const {
119 const Event currentEvent = getEvent();
120 if (currentEvent != Event::kStartNamespace && currentEvent != Event::kEndNamespace) {
121 return mEmpty;
122 }
123 return mEventQueue.front().data1;
124 }
125
getNamespaceUri() const126 const std::u16string& SourceXmlPullParser::getNamespaceUri() const {
127 const Event currentEvent = getEvent();
128 if (currentEvent != Event::kStartNamespace && currentEvent != Event::kEndNamespace) {
129 return mEmpty;
130 }
131 return mEventQueue.front().data2;
132 }
133
applyPackageAlias(std::u16string * package,const std::u16string & defaultPackage) const134 bool SourceXmlPullParser::applyPackageAlias(std::u16string* package,
135 const std::u16string& defaultPackage) const {
136 const auto endIter = mPackageAliases.rend();
137 for (auto iter = mPackageAliases.rbegin(); iter != endIter; ++iter) {
138 if (iter->first == *package) {
139 if (iter->second.empty()) {
140 *package = defaultPackage;
141 } else {
142 *package = iter->second;
143 }
144 return true;
145 }
146 }
147 return false;
148 }
149
getElementNamespace() const150 const std::u16string& SourceXmlPullParser::getElementNamespace() const {
151 const Event currentEvent = getEvent();
152 if (currentEvent != Event::kStartElement && currentEvent != Event::kEndElement) {
153 return mEmpty;
154 }
155 return mEventQueue.front().data1;
156 }
157
getElementName() const158 const std::u16string& SourceXmlPullParser::getElementName() const {
159 const Event currentEvent = getEvent();
160 if (currentEvent != Event::kStartElement && currentEvent != Event::kEndElement) {
161 return mEmpty;
162 }
163 return mEventQueue.front().data2;
164 }
165
beginAttributes() const166 XmlPullParser::const_iterator SourceXmlPullParser::beginAttributes() const {
167 return mEventQueue.front().attributes.begin();
168 }
169
endAttributes() const170 XmlPullParser::const_iterator SourceXmlPullParser::endAttributes() const {
171 return mEventQueue.front().attributes.end();
172 }
173
getAttributeCount() const174 size_t SourceXmlPullParser::getAttributeCount() const {
175 if (getEvent() != Event::kStartElement) {
176 return 0;
177 }
178 return mEventQueue.front().attributes.size();
179 }
180
181 /**
182 * Extracts the namespace and name of an expanded element or attribute name.
183 */
splitName(const char * name,std::u16string & outNs,std::u16string & outName)184 static void splitName(const char* name, std::u16string& outNs, std::u16string& outName) {
185 const char* p = name;
186 while (*p != 0 && *p != kXmlNamespaceSep) {
187 p++;
188 }
189
190 if (*p == 0) {
191 outNs = std::u16string();
192 outName = util::utf8ToUtf16(name);
193 } else {
194 outNs = util::utf8ToUtf16(StringPiece(name, (p - name)));
195 outName = util::utf8ToUtf16(p + 1);
196 }
197 }
198
startNamespaceHandler(void * userData,const char * prefix,const char * uri)199 void XMLCALL SourceXmlPullParser::startNamespaceHandler(void* userData, const char* prefix,
200 const char* uri) {
201 SourceXmlPullParser* parser = reinterpret_cast<SourceXmlPullParser*>(userData);
202 std::u16string namespaceUri = uri != nullptr ? util::utf8ToUtf16(uri) : std::u16string();
203 parser->mNamespaceUris.push(namespaceUri);
204 parser->mEventQueue.push(EventData{
205 Event::kStartNamespace,
206 XML_GetCurrentLineNumber(parser->mParser),
207 parser->mDepth++,
208 prefix != nullptr ? util::utf8ToUtf16(prefix) : std::u16string(),
209 namespaceUri
210 });
211 }
212
startElementHandler(void * userData,const char * name,const char ** attrs)213 void XMLCALL SourceXmlPullParser::startElementHandler(void* userData, const char* name,
214 const char** attrs) {
215 SourceXmlPullParser* parser = reinterpret_cast<SourceXmlPullParser*>(userData);
216
217 EventData data = {
218 Event::kStartElement, XML_GetCurrentLineNumber(parser->mParser), parser->mDepth++
219 };
220 splitName(name, data.data1, data.data2);
221
222 while (*attrs) {
223 Attribute attribute;
224 splitName(*attrs++, attribute.namespaceUri, attribute.name);
225 attribute.value = util::utf8ToUtf16(*attrs++);
226
227 // Insert in sorted order.
228 auto iter = std::lower_bound(data.attributes.begin(), data.attributes.end(), attribute);
229 data.attributes.insert(iter, std::move(attribute));
230 }
231
232 // Move the structure into the queue (no copy).
233 parser->mEventQueue.push(std::move(data));
234 }
235
characterDataHandler(void * userData,const char * s,int len)236 void XMLCALL SourceXmlPullParser::characterDataHandler(void* userData, const char* s, int len) {
237 SourceXmlPullParser* parser = reinterpret_cast<SourceXmlPullParser*>(userData);
238
239 parser->mEventQueue.push(EventData{
240 Event::kText,
241 XML_GetCurrentLineNumber(parser->mParser),
242 parser->mDepth,
243 util::utf8ToUtf16(StringPiece(s, len))
244 });
245 }
246
endElementHandler(void * userData,const char * name)247 void XMLCALL SourceXmlPullParser::endElementHandler(void* userData, const char* name) {
248 SourceXmlPullParser* parser = reinterpret_cast<SourceXmlPullParser*>(userData);
249
250 EventData data = {
251 Event::kEndElement, XML_GetCurrentLineNumber(parser->mParser), --(parser->mDepth)
252 };
253 splitName(name, data.data1, data.data2);
254
255 // Move the data into the queue (no copy).
256 parser->mEventQueue.push(std::move(data));
257 }
258
endNamespaceHandler(void * userData,const char * prefix)259 void XMLCALL SourceXmlPullParser::endNamespaceHandler(void* userData, const char* prefix) {
260 SourceXmlPullParser* parser = reinterpret_cast<SourceXmlPullParser*>(userData);
261
262 parser->mEventQueue.push(EventData{
263 Event::kEndNamespace,
264 XML_GetCurrentLineNumber(parser->mParser),
265 --(parser->mDepth),
266 prefix != nullptr ? util::utf8ToUtf16(prefix) : std::u16string(),
267 parser->mNamespaceUris.top()
268 });
269 parser->mNamespaceUris.pop();
270 }
271
commentDataHandler(void * userData,const char * comment)272 void XMLCALL SourceXmlPullParser::commentDataHandler(void* userData, const char* comment) {
273 SourceXmlPullParser* parser = reinterpret_cast<SourceXmlPullParser*>(userData);
274
275 parser->mEventQueue.push(EventData{
276 Event::kComment,
277 XML_GetCurrentLineNumber(parser->mParser),
278 parser->mDepth,
279 util::utf8ToUtf16(comment)
280 });
281 }
282
283 } // namespace aapt
284