Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 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 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 17 | #include <iostream> |
| 18 | #include <string> |
| 19 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 20 | #include "util/Maybe.h" |
| 21 | #include "util/Util.h" |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 22 | #include "xml/XmlPullParser.h" |
| 23 | #include "xml/XmlUtil.h" |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 24 | |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 25 | using ::aapt::io::InputStream; |
| 26 | using ::android::StringPiece; |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 27 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 28 | namespace aapt { |
Adam Lesinski | 467f171 | 2015-11-16 17:35:44 -0800 | [diff] [blame] | 29 | namespace xml { |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 30 | |
| 31 | constexpr char kXmlNamespaceSep = 1; |
| 32 | |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 33 | XmlPullParser::XmlPullParser(InputStream* in) : in_(in), empty_(), depth_(0) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 34 | 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 Mitchell | cb76d73 | 2018-06-05 10:15:04 -0700 | [diff] [blame] | 41 | XML_SetCdataSectionHandler(parser_, StartCdataSectionHandler, EndCdataSectionHandler); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 42 | event_queue_.push(EventData{Event::kStartDocument, 0, depth_++}); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 43 | } |
| 44 | |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 45 | XmlPullParser::~XmlPullParser() { |
| 46 | XML_ParserFree(parser_); |
| 47 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 48 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 49 | XmlPullParser::Event XmlPullParser::Next() { |
| 50 | const Event currentEvent = event(); |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 51 | if (currentEvent == Event::kBadDocument || currentEvent == Event::kEndDocument) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 52 | return currentEvent; |
| 53 | } |
| 54 | |
| 55 | event_queue_.pop(); |
| 56 | while (event_queue_.empty()) { |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 57 | 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 Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 66 | |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 67 | done = true; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 68 | } |
| 69 | |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 70 | if (XML_Parse(parser_, buffer, buffer_size, done) == XML_STATUS_ERROR) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 71 | error_ = XML_ErrorString(XML_GetErrorCode(parser_)); |
| 72 | event_queue_.push(EventData{Event::kBadDocument}); |
Adam Lesinski | efeb7af | 2017-08-02 14:57:43 -0700 | [diff] [blame] | 73 | break; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 74 | } |
| 75 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 76 | if (done) { |
| 77 | event_queue_.push(EventData{Event::kEndDocument, 0, 0}); |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 78 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 79 | } |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 80 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 81 | Event next_event = event(); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 82 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 83 | // 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 Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 98 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | return next_event; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 102 | } |
| 103 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 104 | XmlPullParser::Event XmlPullParser::event() const { |
| 105 | return event_queue_.front().event; |
| 106 | } |
| 107 | |
| 108 | const std::string& XmlPullParser::error() const { return error_; } |
| 109 | |
| 110 | const std::string& XmlPullParser::comment() const { |
| 111 | return event_queue_.front().data1; |
| 112 | } |
| 113 | |
| 114 | size_t XmlPullParser::line_number() const { |
| 115 | return event_queue_.front().line_number; |
| 116 | } |
| 117 | |
| 118 | size_t XmlPullParser::depth() const { return event_queue_.front().depth; } |
| 119 | |
| 120 | const std::string& XmlPullParser::text() const { |
| 121 | if (event() != Event::kText) { |
| 122 | return empty_; |
| 123 | } |
| 124 | return event_queue_.front().data1; |
| 125 | } |
| 126 | |
| 127 | const 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 | |
| 136 | const 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 Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 145 | Maybe<ExtractedPackage> XmlPullParser::TransformPackageAlias(const StringPiece& alias) const { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 146 | if (alias.empty()) { |
Adam Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 147 | return ExtractedPackage{{}, false /*private*/}; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 148 | } |
| 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 Lesinski | 1ef0fa9 | 2017-08-15 21:32:49 -0700 | [diff] [blame] | 154 | return ExtractedPackage{{}, iter->package.private_namespace}; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 155 | } |
| 156 | return iter->package; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 157 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 158 | } |
| 159 | return {}; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 160 | } |
| 161 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 162 | const 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 Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 169 | } |
| 170 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 171 | const 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 Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 178 | } |
| 179 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 180 | XmlPullParser::const_iterator XmlPullParser::begin_attributes() const { |
| 181 | return event_queue_.front().attributes.begin(); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 182 | } |
| 183 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 184 | XmlPullParser::const_iterator XmlPullParser::end_attributes() const { |
| 185 | return event_queue_.front().attributes.end(); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 186 | } |
| 187 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 188 | size_t XmlPullParser::attribute_count() const { |
| 189 | if (event() != Event::kStartElement) { |
| 190 | return 0; |
| 191 | } |
| 192 | return event_queue_.front().attributes.size(); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | /** |
| 196 | * Extracts the namespace and name of an expanded element or attribute name. |
| 197 | */ |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 198 | static void SplitName(const char* name, std::string* out_ns, std::string* out_name) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 199 | const char* p = name; |
| 200 | while (*p != 0 && *p != kXmlNamespaceSep) { |
| 201 | p++; |
| 202 | } |
| 203 | |
| 204 | if (*p == 0) { |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 205 | out_ns->clear(); |
| 206 | out_name->assign(name); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 207 | } else { |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 208 | out_ns->assign(name, (p - name)); |
| 209 | out_name->assign(p + 1); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 210 | } |
| 211 | } |
| 212 | |
| 213 | void 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 | |
| 225 | void 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 Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 233 | SplitName(name, &data.data1, &data.data2); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 234 | |
| 235 | while (*attrs) { |
| 236 | Attribute attribute; |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 237 | SplitName(*attrs++, &attribute.namespace_uri, &attribute.name); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 238 | 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 | |
| 250 | void XMLCALL XmlPullParser::CharacterDataHandler(void* user_data, const char* s, |
| 251 | int len) { |
| 252 | XmlPullParser* parser = reinterpret_cast<XmlPullParser*>(user_data); |
| 253 | |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 254 | parser->event_queue_.push(EventData{Event::kText, XML_GetCurrentLineNumber(parser->parser_), |
| 255 | parser->depth_, std::string(s, len)}); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | void 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 Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 265 | SplitName(name, &data.data1, &data.data2); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 266 | |
| 267 | // Move the data into the queue (no copy). |
| 268 | parser->event_queue_.push(std::move(data)); |
| 269 | } |
| 270 | |
| 271 | void 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 | |
| 282 | void 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 Mitchell | cb76d73 | 2018-06-05 10:15:04 -0700 | [diff] [blame] | 291 | void 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 | |
| 299 | void 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 Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 307 | Maybe<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 | |
| 316 | Maybe<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 Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 323 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 324 | } |
| 325 | return {}; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 326 | } |
| 327 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 328 | } // namespace xml |
| 329 | } // namespace aapt |