blob: ab347728ae4bc663b046cebf162249f05b94fb19 [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
17#ifndef AAPT_XML_PULL_PARSER_H
18#define AAPT_XML_PULL_PARSER_H
19
Adam Lesinski1ab598f2015-08-14 14:26:04 -070020#include <expat.h>
Adam Lesinskice5e56e2016-10-21 17:56:45 -070021
Adam Lesinskicacb28f2016-10-19 12:18:14 -070022#include <algorithm>
Adam Lesinski1ab598f2015-08-14 14:26:04 -070023#include <istream>
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080024#include <ostream>
Adam Lesinski1ab598f2015-08-14 14:26:04 -070025#include <queue>
26#include <stack>
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080027#include <string>
28#include <vector>
29
Adam Lesinskice5e56e2016-10-21 17:56:45 -070030#include "android-base/macros.h"
Adam Lesinskid5083f62017-01-16 15:07:21 -080031#include "androidfw/StringPiece.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070032
33#include "Resource.h"
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070034#include "io/Io.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070035#include "process/IResourceTableConsumer.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070036#include "xml/XmlUtil.h"
37
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080038namespace aapt {
Adam Lesinski467f1712015-11-16 17:35:44 -080039namespace xml {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080040
Adam Lesinski1ab598f2015-08-14 14:26:04 -070041class XmlPullParser : public IPackageDeclStack {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070042 public:
43 enum class Event {
44 kBadDocument,
45 kStartDocument,
46 kEndDocument,
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080047
Adam Lesinskicacb28f2016-10-19 12:18:14 -070048 kStartNamespace,
49 kEndNamespace,
50 kStartElement,
51 kEndElement,
52 kText,
53 kComment,
Ryan Mitchellcb76d732018-06-05 10:15:04 -070054 kCdataStart,
55 kCdataEnd,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070056 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080057
Adam Lesinskicacb28f2016-10-19 12:18:14 -070058 /**
Adam Lesinskice5e56e2016-10-21 17:56:45 -070059 * Skips to the next direct descendant node of the given start_depth,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070060 * skipping namespace nodes.
61 *
Adam Lesinskice5e56e2016-10-21 17:56:45 -070062 * When NextChildNode() returns true, you can expect Comments, Text, and
Adam Lesinskicacb28f2016-10-19 12:18:14 -070063 * StartElement events.
64 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070065 static bool NextChildNode(XmlPullParser* parser, size_t start_depth);
66 static bool SkipCurrentElement(XmlPullParser* parser);
67 static bool IsGoodEvent(Event event);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080068
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070069 explicit XmlPullParser(io::InputStream* in);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070070 ~XmlPullParser();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080071
Adam Lesinskicacb28f2016-10-19 12:18:14 -070072 /**
73 * Returns the current event that is being processed.
74 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070075 Event event() const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080076
Adam Lesinskice5e56e2016-10-21 17:56:45 -070077 const std::string& error() const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080078
Adam Lesinskicacb28f2016-10-19 12:18:14 -070079 /**
80 * Note, unlike XmlPullParser, the first call to next() will return
81 * StartElement of the first element.
82 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070083 Event Next();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080084
Adam Lesinskicacb28f2016-10-19 12:18:14 -070085 //
86 // These are available for all nodes.
87 //
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080088
Adam Lesinskice5e56e2016-10-21 17:56:45 -070089 const std::string& comment() const;
90 size_t line_number() const;
91 size_t depth() const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080092
Adam Lesinskicacb28f2016-10-19 12:18:14 -070093 /**
94 * Returns the character data for a Text event.
95 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070096 const std::string& text() const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080097
Adam Lesinskicacb28f2016-10-19 12:18:14 -070098 //
99 // Namespace prefix and URI are available for StartNamespace and EndNamespace.
100 //
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800101
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700102 const std::string& namespace_prefix() const;
103 const std::string& namespace_uri() const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800104
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700105 //
106 // These are available for StartElement and EndElement.
107 //
Adam Lesinski467f1712015-11-16 17:35:44 -0800108
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700109 const std::string& element_namespace() const;
110 const std::string& element_name() const;
Adam Lesinski467f1712015-11-16 17:35:44 -0800111
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700112 /*
113 * Uses the current stack of namespaces to resolve the package. Eg:
114 * xmlns:app = "http://schemas.android.com/apk/res/com.android.app"
115 * ...
116 * android:text="@app:string/message"
117 *
118 * In this case, 'app' will be converted to 'com.android.app'.
119 *
120 * If xmlns:app="http://schemas.android.com/apk/res-auto", then
121 * 'package' will be set to 'defaultPackage'.
122 */
Ryan Mitchell4382e442021-07-14 12:53:01 -0700123 std::optional<ExtractedPackage> TransformPackageAlias(
124 const android::StringPiece& alias) const override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800125
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700126 struct PackageDecl {
127 std::string prefix;
128 ExtractedPackage package;
129 };
130
131 const std::vector<PackageDecl>& package_decls() const;
132
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700133 //
134 // Remaining methods are for retrieving information about attributes
135 // associated with a StartElement.
136 //
137 // Attributes must be in sorted order (according to the less than operator
138 // of struct Attribute).
139 //
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800140
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700141 struct Attribute {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700142 std::string namespace_uri;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700143 std::string name;
144 std::string value;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800145
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700146 int compare(const Attribute& rhs) const;
147 bool operator<(const Attribute& rhs) const;
148 bool operator==(const Attribute& rhs) const;
149 bool operator!=(const Attribute& rhs) const;
150 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800151
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700152 using const_iterator = std::vector<Attribute>::const_iterator;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800153
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700154 const_iterator begin_attributes() const;
155 const_iterator end_attributes() const;
156 size_t attribute_count() const;
Adam Lesinskid5083f62017-01-16 15:07:21 -0800157 const_iterator FindAttribute(android::StringPiece namespace_uri, android::StringPiece name) const;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700158
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700159 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700160 DISALLOW_COPY_AND_ASSIGN(XmlPullParser);
161
162 static void XMLCALL StartNamespaceHandler(void* user_data, const char* prefix,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700163 const char* uri);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700164 static void XMLCALL StartElementHandler(void* user_data, const char* name,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700165 const char** attrs);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700166 static void XMLCALL CharacterDataHandler(void* user_data, const char* s,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700167 int len);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700168 static void XMLCALL EndElementHandler(void* user_data, const char* name);
169 static void XMLCALL EndNamespaceHandler(void* user_data, const char* prefix);
170 static void XMLCALL CommentDataHandler(void* user_data, const char* comment);
Ryan Mitchellcb76d732018-06-05 10:15:04 -0700171 static void XMLCALL StartCdataSectionHandler(void* user_data);
172 static void XMLCALL EndCdataSectionHandler(void* user_data);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700173
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700174 struct EventData {
175 Event event;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700176 size_t line_number;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700177 size_t depth;
178 std::string data1;
179 std::string data2;
180 std::vector<Attribute> attributes;
181 };
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700182
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700183 io::InputStream* in_;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700184 XML_Parser parser_;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700185 std::queue<EventData> event_queue_;
186 std::string error_;
187 const std::string empty_;
188 size_t depth_;
189 std::stack<std::string> namespace_uris_;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700190 std::vector<PackageDecl> package_aliases_;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800191};
192
Adam Lesinski467f1712015-11-16 17:35:44 -0800193/**
194 * Finds the attribute in the current element within the global namespace.
195 */
Ryan Mitchell4382e442021-07-14 12:53:01 -0700196std::optional<android::StringPiece> FindAttribute(const XmlPullParser* parser,
197 const android::StringPiece& name);
Adam Lesinski467f1712015-11-16 17:35:44 -0800198
199/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700200 * Finds the attribute in the current element within the global namespace. The
201 * attribute's value
Adam Lesinski467f1712015-11-16 17:35:44 -0800202 * must not be the empty string.
203 */
Ryan Mitchell4382e442021-07-14 12:53:01 -0700204std::optional<android::StringPiece> FindNonEmptyAttribute(const XmlPullParser* parser,
205 const android::StringPiece& name);
Adam Lesinski467f1712015-11-16 17:35:44 -0800206
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800207//
208// Implementation
209//
210
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700211inline ::std::ostream& operator<<(::std::ostream& out,
212 XmlPullParser::Event event) {
213 switch (event) {
214 case XmlPullParser::Event::kBadDocument:
215 return out << "BadDocument";
216 case XmlPullParser::Event::kStartDocument:
217 return out << "StartDocument";
218 case XmlPullParser::Event::kEndDocument:
219 return out << "EndDocument";
220 case XmlPullParser::Event::kStartNamespace:
221 return out << "StartNamespace";
222 case XmlPullParser::Event::kEndNamespace:
223 return out << "EndNamespace";
224 case XmlPullParser::Event::kStartElement:
225 return out << "StartElement";
226 case XmlPullParser::Event::kEndElement:
227 return out << "EndElement";
228 case XmlPullParser::Event::kText:
229 return out << "Text";
230 case XmlPullParser::Event::kComment:
231 return out << "Comment";
Ryan Mitchellcb76d732018-06-05 10:15:04 -0700232 case XmlPullParser::Event::kCdataStart:
233 return out << "CdataStart";
234 case XmlPullParser::Event::kCdataEnd:
235 return out << "CdataEnd";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700236 }
237 return out;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800238}
239
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700240inline bool XmlPullParser::NextChildNode(XmlPullParser* parser, size_t start_depth) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700241 Event event;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700242
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700243 // First get back to the start depth.
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700244 while (IsGoodEvent(event = parser->Next()) && parser->depth() > start_depth + 1) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700245 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700246
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700247 // Now look for the first good node.
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700248 while ((event != Event::kEndElement || parser->depth() > start_depth) && IsGoodEvent(event)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700249 switch (event) {
250 case Event::kText:
251 case Event::kComment:
252 case Event::kStartElement:
Ryan Mitchellcb76d732018-06-05 10:15:04 -0700253 case Event::kCdataStart:
254 case Event::kCdataEnd:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700255 return true;
256 default:
257 break;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700258 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700259 event = parser->Next();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700260 }
261 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700262}
263
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700264inline bool XmlPullParser::SkipCurrentElement(XmlPullParser* parser) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700265 int depth = 1;
266 while (depth > 0) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700267 switch (parser->Next()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700268 case Event::kEndDocument:
269 return true;
270 case Event::kBadDocument:
271 return false;
272 case Event::kStartElement:
273 depth++;
274 break;
275 case Event::kEndElement:
276 depth--;
277 break;
278 default:
279 break;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800280 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700281 }
282 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800283}
284
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700285inline bool XmlPullParser::IsGoodEvent(XmlPullParser::Event event) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700286 return event != Event::kBadDocument && event != Event::kEndDocument;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800287}
288
289inline int XmlPullParser::Attribute::compare(const Attribute& rhs) const {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700290 int cmp = namespace_uri.compare(rhs.namespace_uri);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700291 if (cmp != 0) return cmp;
292 return name.compare(rhs.name);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800293}
294
295inline bool XmlPullParser::Attribute::operator<(const Attribute& rhs) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700296 return compare(rhs) < 0;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800297}
298
299inline bool XmlPullParser::Attribute::operator==(const Attribute& rhs) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700300 return compare(rhs) == 0;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800301}
302
303inline bool XmlPullParser::Attribute::operator!=(const Attribute& rhs) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700304 return compare(rhs) != 0;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800305}
306
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700307inline XmlPullParser::const_iterator XmlPullParser::FindAttribute(
Adam Lesinskid5083f62017-01-16 15:07:21 -0800308 android::StringPiece namespace_uri, android::StringPiece name) const {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700309 const auto end_iter = end_attributes();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700310 const auto iter = std::lower_bound(
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700311 begin_attributes(), end_iter,
Adam Lesinskid5083f62017-01-16 15:07:21 -0800312 std::pair<android::StringPiece, android::StringPiece>(namespace_uri, name),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700313 [](const Attribute& attr,
Adam Lesinskid5083f62017-01-16 15:07:21 -0800314 const std::pair<android::StringPiece, android::StringPiece>& rhs) -> bool {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700315 int cmp = attr.namespace_uri.compare(
316 0, attr.namespace_uri.size(), rhs.first.data(), rhs.first.size());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700317 if (cmp < 0) return true;
318 if (cmp > 0) return false;
319 cmp = attr.name.compare(0, attr.name.size(), rhs.second.data(),
320 rhs.second.size());
321 if (cmp < 0) return true;
322 return false;
323 });
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800324
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700325 if (iter != end_iter && namespace_uri == iter->namespace_uri &&
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700326 name == iter->name) {
327 return iter;
328 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700329 return end_iter;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800330}
331
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700332} // namespace xml
333} // namespace aapt
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800334
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700335#endif // AAPT_XML_PULL_PARSER_H