blob: 64274d032c61d92d0a5b32c887f4c6f16c1ea5af [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
Yurii Zubrytskyi1d6d8ac2024-07-03 16:10:17 -070022#include <optional>
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080023#include <ostream>
Adam Lesinski1ab598f2015-08-14 14:26:04 -070024#include <queue>
25#include <stack>
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080026#include <string>
27#include <vector>
28
Adam Lesinskice5e56e2016-10-21 17:56:45 -070029#include "Resource.h"
Jeremy Meyerb4f83ff2023-11-30 19:29:50 +000030#include "android-base/macros.h"
31#include "androidfw/Streams.h"
32#include "androidfw/StringPiece.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070033#include "process/IResourceTableConsumer.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070034#include "xml/XmlUtil.h"
35
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080036namespace aapt {
Adam Lesinski467f1712015-11-16 17:35:44 -080037namespace xml {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080038
Adam Lesinski1ab598f2015-08-14 14:26:04 -070039class XmlPullParser : public IPackageDeclStack {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070040 public:
41 enum class Event {
42 kBadDocument,
43 kStartDocument,
44 kEndDocument,
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080045
Adam Lesinskicacb28f2016-10-19 12:18:14 -070046 kStartNamespace,
47 kEndNamespace,
48 kStartElement,
49 kEndElement,
50 kText,
51 kComment,
Ryan Mitchellcb76d732018-06-05 10:15:04 -070052 kCdataStart,
53 kCdataEnd,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070054 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080055
Adam Lesinskicacb28f2016-10-19 12:18:14 -070056 /**
Adam Lesinskice5e56e2016-10-21 17:56:45 -070057 * Skips to the next direct descendant node of the given start_depth,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070058 * skipping namespace nodes.
59 *
Adam Lesinskice5e56e2016-10-21 17:56:45 -070060 * When NextChildNode() returns true, you can expect Comments, Text, and
Adam Lesinskicacb28f2016-10-19 12:18:14 -070061 * StartElement events.
62 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070063 static bool NextChildNode(XmlPullParser* parser, size_t start_depth);
64 static bool SkipCurrentElement(XmlPullParser* parser);
65 static bool IsGoodEvent(Event event);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080066
Jeremy Meyerb4f83ff2023-11-30 19:29:50 +000067 explicit XmlPullParser(android::InputStream* in);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070068 ~XmlPullParser();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080069
Adam Lesinskicacb28f2016-10-19 12:18:14 -070070 /**
71 * Returns the current event that is being processed.
72 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070073 Event event() const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080074
Adam Lesinskice5e56e2016-10-21 17:56:45 -070075 const std::string& error() const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080076
Adam Lesinskicacb28f2016-10-19 12:18:14 -070077 /**
78 * Note, unlike XmlPullParser, the first call to next() will return
79 * StartElement of the first element.
80 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070081 Event Next();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080082
Adam Lesinskicacb28f2016-10-19 12:18:14 -070083 //
84 // These are available for all nodes.
85 //
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080086
Adam Lesinskice5e56e2016-10-21 17:56:45 -070087 const std::string& comment() const;
88 size_t line_number() const;
89 size_t depth() const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080090
Adam Lesinskicacb28f2016-10-19 12:18:14 -070091 /**
92 * Returns the character data for a Text event.
93 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070094 const std::string& text() const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080095
Adam Lesinskicacb28f2016-10-19 12:18:14 -070096 //
97 // Namespace prefix and URI are available for StartNamespace and EndNamespace.
98 //
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080099
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700100 const std::string& namespace_prefix() const;
101 const std::string& namespace_uri() const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800102
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700103 //
104 // These are available for StartElement and EndElement.
105 //
Adam Lesinski467f1712015-11-16 17:35:44 -0800106
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700107 const std::string& element_namespace() const;
108 const std::string& element_name() const;
Adam Lesinski467f1712015-11-16 17:35:44 -0800109
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700110 /*
111 * Uses the current stack of namespaces to resolve the package. Eg:
112 * xmlns:app = "http://schemas.android.com/apk/res/com.android.app"
113 * ...
114 * android:text="@app:string/message"
115 *
116 * In this case, 'app' will be converted to 'com.android.app'.
117 *
118 * If xmlns:app="http://schemas.android.com/apk/res-auto", then
119 * 'package' will be set to 'defaultPackage'.
120 */
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700121 std::optional<ExtractedPackage> TransformPackageAlias(android::StringPiece alias) const override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800122
Ryan Mitchell326e35ff2021-04-12 07:50:42 -0700123 struct PackageDecl {
124 std::string prefix;
125 ExtractedPackage package;
126 };
127
128 const std::vector<PackageDecl>& package_decls() const;
129
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700130 //
131 // Remaining methods are for retrieving information about attributes
132 // associated with a StartElement.
133 //
134 // Attributes must be in sorted order (according to the less than operator
135 // of struct Attribute).
136 //
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800137
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700138 struct Attribute {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700139 std::string namespace_uri;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700140 std::string name;
141 std::string value;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800142
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700143 int compare(const Attribute& rhs) const;
144 bool operator<(const Attribute& rhs) const;
145 bool operator==(const Attribute& rhs) const;
146 bool operator!=(const Attribute& rhs) const;
147 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800148
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700149 using const_iterator = std::vector<Attribute>::const_iterator;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800150
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700151 const_iterator begin_attributes() const;
152 const_iterator end_attributes() const;
153 size_t attribute_count() const;
Adam Lesinskid5083f62017-01-16 15:07:21 -0800154 const_iterator FindAttribute(android::StringPiece namespace_uri, android::StringPiece name) const;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700155
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700156 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700157 DISALLOW_COPY_AND_ASSIGN(XmlPullParser);
158
159 static void XMLCALL StartNamespaceHandler(void* user_data, const char* prefix,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700160 const char* uri);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700161 static void XMLCALL StartElementHandler(void* user_data, const char* name,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700162 const char** attrs);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700163 static void XMLCALL CharacterDataHandler(void* user_data, const char* s,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700164 int len);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700165 static void XMLCALL EndElementHandler(void* user_data, const char* name);
166 static void XMLCALL EndNamespaceHandler(void* user_data, const char* prefix);
167 static void XMLCALL CommentDataHandler(void* user_data, const char* comment);
Ryan Mitchellcb76d732018-06-05 10:15:04 -0700168 static void XMLCALL StartCdataSectionHandler(void* user_data);
169 static void XMLCALL EndCdataSectionHandler(void* user_data);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700170
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700171 struct EventData {
172 Event event;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700173 size_t line_number;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700174 size_t depth;
175 std::string data1;
176 std::string data2;
177 std::vector<Attribute> attributes;
178 };
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700179
Jeremy Meyerb4f83ff2023-11-30 19:29:50 +0000180 android::InputStream* in_;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700181 XML_Parser parser_;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700182 std::queue<EventData> event_queue_;
183 std::string error_;
184 const std::string empty_;
185 size_t depth_;
186 std::stack<std::string> namespace_uris_;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700187 std::vector<PackageDecl> package_aliases_;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800188};
189
Adam Lesinski467f1712015-11-16 17:35:44 -0800190/**
191 * Finds the attribute in the current element within the global namespace.
192 */
Ryan Mitchell4382e442021-07-14 12:53:01 -0700193std::optional<android::StringPiece> FindAttribute(const XmlPullParser* parser,
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700194 android::StringPiece name);
Adam Lesinski467f1712015-11-16 17:35:44 -0800195
196/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700197 * Finds the attribute in the current element within the global namespace. The
198 * attribute's value
Adam Lesinski467f1712015-11-16 17:35:44 -0800199 * must not be the empty string.
200 */
Ryan Mitchell4382e442021-07-14 12:53:01 -0700201std::optional<android::StringPiece> FindNonEmptyAttribute(const XmlPullParser* parser,
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700202 android::StringPiece name);
Adam Lesinski467f1712015-11-16 17:35:44 -0800203
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800204//
205// Implementation
206//
207
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700208inline ::std::ostream& operator<<(::std::ostream& out,
209 XmlPullParser::Event event) {
210 switch (event) {
211 case XmlPullParser::Event::kBadDocument:
212 return out << "BadDocument";
213 case XmlPullParser::Event::kStartDocument:
214 return out << "StartDocument";
215 case XmlPullParser::Event::kEndDocument:
216 return out << "EndDocument";
217 case XmlPullParser::Event::kStartNamespace:
218 return out << "StartNamespace";
219 case XmlPullParser::Event::kEndNamespace:
220 return out << "EndNamespace";
221 case XmlPullParser::Event::kStartElement:
222 return out << "StartElement";
223 case XmlPullParser::Event::kEndElement:
224 return out << "EndElement";
225 case XmlPullParser::Event::kText:
226 return out << "Text";
227 case XmlPullParser::Event::kComment:
228 return out << "Comment";
Ryan Mitchellcb76d732018-06-05 10:15:04 -0700229 case XmlPullParser::Event::kCdataStart:
230 return out << "CdataStart";
231 case XmlPullParser::Event::kCdataEnd:
232 return out << "CdataEnd";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700233 }
234 return out;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800235}
236
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700237inline bool XmlPullParser::NextChildNode(XmlPullParser* parser, size_t start_depth) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700238 Event event;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700239
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700240 // First get back to the start depth.
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700241 while (IsGoodEvent(event = parser->Next()) && parser->depth() > start_depth + 1) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700242 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700243
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700244 // Now look for the first good node.
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700245 while ((event != Event::kEndElement || parser->depth() > start_depth) && IsGoodEvent(event)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700246 switch (event) {
247 case Event::kText:
248 case Event::kComment:
249 case Event::kStartElement:
Ryan Mitchellcb76d732018-06-05 10:15:04 -0700250 case Event::kCdataStart:
251 case Event::kCdataEnd:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700252 return true;
253 default:
254 break;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700255 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700256 event = parser->Next();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700257 }
258 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700259}
260
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700261inline bool XmlPullParser::SkipCurrentElement(XmlPullParser* parser) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700262 int depth = 1;
263 while (depth > 0) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700264 switch (parser->Next()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700265 case Event::kEndDocument:
266 return true;
267 case Event::kBadDocument:
268 return false;
269 case Event::kStartElement:
270 depth++;
271 break;
272 case Event::kEndElement:
273 depth--;
274 break;
275 default:
276 break;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800277 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700278 }
279 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800280}
281
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700282inline bool XmlPullParser::IsGoodEvent(XmlPullParser::Event event) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700283 return event != Event::kBadDocument && event != Event::kEndDocument;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800284}
285
286inline int XmlPullParser::Attribute::compare(const Attribute& rhs) const {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700287 int cmp = namespace_uri.compare(rhs.namespace_uri);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700288 if (cmp != 0) return cmp;
289 return name.compare(rhs.name);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800290}
291
292inline bool XmlPullParser::Attribute::operator<(const Attribute& rhs) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700293 return compare(rhs) < 0;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800294}
295
296inline bool XmlPullParser::Attribute::operator==(const Attribute& rhs) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700297 return compare(rhs) == 0;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800298}
299
300inline bool XmlPullParser::Attribute::operator!=(const Attribute& rhs) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700301 return compare(rhs) != 0;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800302}
303
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700304} // namespace xml
305} // namespace aapt
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800306
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700307#endif // AAPT_XML_PULL_PARSER_H