blob: a023494ad8f78d9553d3ed2d7c0b622ccc65dc81 [file] [log] [blame]
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001/*
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
Adam Lesinskice5e56e2016-10-21 17:56:45 -070017#include <iostream>
18#include <string>
19
Adam Lesinski1ab598f2015-08-14 14:26:04 -070020#include "util/Maybe.h"
21#include "util/Util.h"
Adam Lesinski467f1712015-11-16 17:35:44 -080022#include "xml/XmlPullParser.h"
23#include "xml/XmlUtil.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070024
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070025using ::aapt::io::InputStream;
26using ::android::StringPiece;
Adam Lesinskid5083f62017-01-16 15:07:21 -080027
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080028namespace aapt {
Adam Lesinski467f1712015-11-16 17:35:44 -080029namespace xml {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080030
31constexpr char kXmlNamespaceSep = 1;
32
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070033XmlPullParser::XmlPullParser(InputStream* in) : in_(in), empty_(), depth_(0) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070034 parser_ = XML_ParserCreateNS(nullptr, kXmlNamespaceSep);
35 XML_SetUserData(parser_, this);
36 XML_SetElementHandler(parser_, StartElementHandler, EndElementHandler);
37 XML_SetNamespaceDeclHandler(parser_, StartNamespaceHandler,
38 EndNamespaceHandler);
39 XML_SetCharacterDataHandler(parser_, CharacterDataHandler);
40 XML_SetCommentHandler(parser_, CommentDataHandler);
Ryan Mitchellcb76d732018-06-05 10:15:04 -070041 XML_SetCdataSectionHandler(parser_, StartCdataSectionHandler, EndCdataSectionHandler);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070042 event_queue_.push(EventData{Event::kStartDocument, 0, depth_++});
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080043}
44
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070045XmlPullParser::~XmlPullParser() {
46 XML_ParserFree(parser_);
47}
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080048
Adam Lesinskice5e56e2016-10-21 17:56:45 -070049XmlPullParser::Event XmlPullParser::Next() {
50 const Event currentEvent = event();
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070051 if (currentEvent == Event::kBadDocument || currentEvent == Event::kEndDocument) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070052 return currentEvent;
53 }
54
55 event_queue_.pop();
56 while (event_queue_.empty()) {
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070057 const char* buffer = nullptr;
58 size_t buffer_size = 0;
59 bool done = false;
60 if (!in_->Next(reinterpret_cast<const void**>(&buffer), &buffer_size)) {
61 if (in_->HadError()) {
62 error_ = in_->GetError();
63 event_queue_.push(EventData{Event::kBadDocument});
64 break;
65 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070066
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070067 done = true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080068 }
69
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070070 if (XML_Parse(parser_, buffer, buffer_size, done) == XML_STATUS_ERROR) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070071 error_ = XML_ErrorString(XML_GetErrorCode(parser_));
72 event_queue_.push(EventData{Event::kBadDocument});
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070073 break;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080074 }
75
Adam Lesinskice5e56e2016-10-21 17:56:45 -070076 if (done) {
77 event_queue_.push(EventData{Event::kEndDocument, 0, 0});
Adam Lesinski24aad162015-04-24 19:19:30 -070078 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070079 }
Adam Lesinski24aad162015-04-24 19:19:30 -070080
Adam Lesinskice5e56e2016-10-21 17:56:45 -070081 Event next_event = event();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080082
Adam Lesinskice5e56e2016-10-21 17:56:45 -070083 // Record namespace prefixes and package names so that we can do our own
84 // handling of references that use namespace aliases.
85 if (next_event == Event::kStartNamespace ||
86 next_event == Event::kEndNamespace) {
87 Maybe<ExtractedPackage> result =
88 ExtractPackageFromNamespace(namespace_uri());
89 if (next_event == Event::kStartNamespace) {
90 if (result) {
91 package_aliases_.emplace_back(
92 PackageDecl{namespace_prefix(), std::move(result.value())});
93 }
94 } else {
95 if (result) {
96 package_aliases_.pop_back();
97 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080098 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070099 }
100
101 return next_event;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800102}
103
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700104XmlPullParser::Event XmlPullParser::event() const {
105 return event_queue_.front().event;
106}
107
108const std::string& XmlPullParser::error() const { return error_; }
109
110const std::string& XmlPullParser::comment() const {
111 return event_queue_.front().data1;
112}
113
114size_t XmlPullParser::line_number() const {
115 return event_queue_.front().line_number;
116}
117
118size_t XmlPullParser::depth() const { return event_queue_.front().depth; }
119
120const std::string& XmlPullParser::text() const {
121 if (event() != Event::kText) {
122 return empty_;
123 }
124 return event_queue_.front().data1;
125}
126
127const std::string& XmlPullParser::namespace_prefix() const {
128 const Event current_event = event();
129 if (current_event != Event::kStartNamespace &&
130 current_event != Event::kEndNamespace) {
131 return empty_;
132 }
133 return event_queue_.front().data1;
134}
135
136const std::string& XmlPullParser::namespace_uri() const {
137 const Event current_event = event();
138 if (current_event != Event::kStartNamespace &&
139 current_event != Event::kEndNamespace) {
140 return empty_;
141 }
142 return event_queue_.front().data2;
143}
144
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700145Maybe<ExtractedPackage> XmlPullParser::TransformPackageAlias(const StringPiece& alias) const {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700146 if (alias.empty()) {
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700147 return ExtractedPackage{{}, false /*private*/};
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700148 }
149
150 const auto end_iter = package_aliases_.rend();
151 for (auto iter = package_aliases_.rbegin(); iter != end_iter; ++iter) {
152 if (alias == iter->prefix) {
153 if (iter->package.package.empty()) {
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700154 return ExtractedPackage{{}, iter->package.private_namespace};
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700155 }
156 return iter->package;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800157 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700158 }
159 return {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800160}
161
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700162const std::string& XmlPullParser::element_namespace() const {
163 const Event current_event = event();
164 if (current_event != Event::kStartElement &&
165 current_event != Event::kEndElement) {
166 return empty_;
167 }
168 return event_queue_.front().data1;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800169}
170
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700171const std::string& XmlPullParser::element_name() const {
172 const Event current_event = event();
173 if (current_event != Event::kStartElement &&
174 current_event != Event::kEndElement) {
175 return empty_;
176 }
177 return event_queue_.front().data2;
Adam Lesinski24aad162015-04-24 19:19:30 -0700178}
179
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700180XmlPullParser::const_iterator XmlPullParser::begin_attributes() const {
181 return event_queue_.front().attributes.begin();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800182}
183
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700184XmlPullParser::const_iterator XmlPullParser::end_attributes() const {
185 return event_queue_.front().attributes.end();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800186}
187
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700188size_t XmlPullParser::attribute_count() const {
189 if (event() != Event::kStartElement) {
190 return 0;
191 }
192 return event_queue_.front().attributes.size();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800193}
194
195/**
196 * Extracts the namespace and name of an expanded element or attribute name.
197 */
Adam Lesinskid5083f62017-01-16 15:07:21 -0800198static void SplitName(const char* name, std::string* out_ns, std::string* out_name) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700199 const char* p = name;
200 while (*p != 0 && *p != kXmlNamespaceSep) {
201 p++;
202 }
203
204 if (*p == 0) {
Adam Lesinskid5083f62017-01-16 15:07:21 -0800205 out_ns->clear();
206 out_name->assign(name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700207 } else {
Adam Lesinskid5083f62017-01-16 15:07:21 -0800208 out_ns->assign(name, (p - name));
209 out_name->assign(p + 1);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700210 }
211}
212
213void XMLCALL XmlPullParser::StartNamespaceHandler(void* user_data,
214 const char* prefix,
215 const char* uri) {
216 XmlPullParser* parser = reinterpret_cast<XmlPullParser*>(user_data);
217 std::string namespace_uri = uri != nullptr ? uri : std::string();
218 parser->namespace_uris_.push(namespace_uri);
219 parser->event_queue_.push(
220 EventData{Event::kStartNamespace,
221 XML_GetCurrentLineNumber(parser->parser_), parser->depth_++,
222 prefix != nullptr ? prefix : std::string(), namespace_uri});
223}
224
225void XMLCALL XmlPullParser::StartElementHandler(void* user_data,
226 const char* name,
227 const char** attrs) {
228 XmlPullParser* parser = reinterpret_cast<XmlPullParser*>(user_data);
229
230 EventData data = {Event::kStartElement,
231 XML_GetCurrentLineNumber(parser->parser_),
232 parser->depth_++};
Adam Lesinskid5083f62017-01-16 15:07:21 -0800233 SplitName(name, &data.data1, &data.data2);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700234
235 while (*attrs) {
236 Attribute attribute;
Adam Lesinskid5083f62017-01-16 15:07:21 -0800237 SplitName(*attrs++, &attribute.namespace_uri, &attribute.name);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700238 attribute.value = *attrs++;
239
240 // Insert in sorted order.
241 auto iter = std::lower_bound(data.attributes.begin(), data.attributes.end(),
242 attribute);
243 data.attributes.insert(iter, std::move(attribute));
244 }
245
246 // Move the structure into the queue (no copy).
247 parser->event_queue_.push(std::move(data));
248}
249
250void XMLCALL XmlPullParser::CharacterDataHandler(void* user_data, const char* s,
251 int len) {
252 XmlPullParser* parser = reinterpret_cast<XmlPullParser*>(user_data);
253
Adam Lesinskid5083f62017-01-16 15:07:21 -0800254 parser->event_queue_.push(EventData{Event::kText, XML_GetCurrentLineNumber(parser->parser_),
255 parser->depth_, std::string(s, len)});
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700256}
257
258void XMLCALL XmlPullParser::EndElementHandler(void* user_data,
259 const char* name) {
260 XmlPullParser* parser = reinterpret_cast<XmlPullParser*>(user_data);
261
262 EventData data = {Event::kEndElement,
263 XML_GetCurrentLineNumber(parser->parser_),
264 --(parser->depth_)};
Adam Lesinskid5083f62017-01-16 15:07:21 -0800265 SplitName(name, &data.data1, &data.data2);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700266
267 // Move the data into the queue (no copy).
268 parser->event_queue_.push(std::move(data));
269}
270
271void XMLCALL XmlPullParser::EndNamespaceHandler(void* user_data,
272 const char* prefix) {
273 XmlPullParser* parser = reinterpret_cast<XmlPullParser*>(user_data);
274
275 parser->event_queue_.push(
276 EventData{Event::kEndNamespace, XML_GetCurrentLineNumber(parser->parser_),
277 --(parser->depth_), prefix != nullptr ? prefix : std::string(),
278 parser->namespace_uris_.top()});
279 parser->namespace_uris_.pop();
280}
281
282void XMLCALL XmlPullParser::CommentDataHandler(void* user_data,
283 const char* comment) {
284 XmlPullParser* parser = reinterpret_cast<XmlPullParser*>(user_data);
285
286 parser->event_queue_.push(EventData{Event::kComment,
287 XML_GetCurrentLineNumber(parser->parser_),
288 parser->depth_, comment});
289}
290
Ryan Mitchellcb76d732018-06-05 10:15:04 -0700291void XMLCALL XmlPullParser::StartCdataSectionHandler(void* user_data) {
292 XmlPullParser* parser = reinterpret_cast<XmlPullParser*>(user_data);
293
294 parser->event_queue_.push(EventData{Event::kCdataStart,
295 XML_GetCurrentLineNumber(parser->parser_),
296 parser->depth_ });
297}
298
299void XMLCALL XmlPullParser::EndCdataSectionHandler(void* user_data) {
300 XmlPullParser* parser = reinterpret_cast<XmlPullParser*>(user_data);
301
302 parser->event_queue_.push(EventData{Event::kCdataEnd,
303 XML_GetCurrentLineNumber(parser->parser_),
304 parser->depth_ });
305}
306
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700307Maybe<StringPiece> FindAttribute(const XmlPullParser* parser,
308 const StringPiece& name) {
309 auto iter = parser->FindAttribute("", name);
310 if (iter != parser->end_attributes()) {
311 return StringPiece(util::TrimWhitespace(iter->value));
312 }
313 return {};
314}
315
316Maybe<StringPiece> FindNonEmptyAttribute(const XmlPullParser* parser,
317 const StringPiece& name) {
318 auto iter = parser->FindAttribute("", name);
319 if (iter != parser->end_attributes()) {
320 StringPiece trimmed = util::TrimWhitespace(iter->value);
321 if (!trimmed.empty()) {
322 return trimmed;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800323 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700324 }
325 return {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800326}
327
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700328} // namespace xml
329} // namespace aapt