blob: a00caa139061be0a99aca19dff964377c952224e [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"
36#include "util/Maybe.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070037#include "xml/XmlUtil.h"
38
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080039namespace aapt {
Adam Lesinski467f1712015-11-16 17:35:44 -080040namespace xml {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080041
Adam Lesinski1ab598f2015-08-14 14:26:04 -070042class XmlPullParser : public IPackageDeclStack {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070043 public:
44 enum class Event {
45 kBadDocument,
46 kStartDocument,
47 kEndDocument,
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080048
Adam Lesinskicacb28f2016-10-19 12:18:14 -070049 kStartNamespace,
50 kEndNamespace,
51 kStartElement,
52 kEndElement,
53 kText,
54 kComment,
55 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080056
Adam Lesinskicacb28f2016-10-19 12:18:14 -070057 /**
Adam Lesinskice5e56e2016-10-21 17:56:45 -070058 * Skips to the next direct descendant node of the given start_depth,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070059 * skipping namespace nodes.
60 *
Adam Lesinskice5e56e2016-10-21 17:56:45 -070061 * When NextChildNode() returns true, you can expect Comments, Text, and
Adam Lesinskicacb28f2016-10-19 12:18:14 -070062 * StartElement events.
63 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070064 static bool NextChildNode(XmlPullParser* parser, size_t start_depth);
65 static bool SkipCurrentElement(XmlPullParser* parser);
66 static bool IsGoodEvent(Event event);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080067
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070068 explicit XmlPullParser(io::InputStream* in);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070069 ~XmlPullParser();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080070
Adam Lesinskicacb28f2016-10-19 12:18:14 -070071 /**
72 * Returns the current event that is being processed.
73 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070074 Event event() const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080075
Adam Lesinskice5e56e2016-10-21 17:56:45 -070076 const std::string& error() const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080077
Adam Lesinskicacb28f2016-10-19 12:18:14 -070078 /**
79 * Note, unlike XmlPullParser, the first call to next() will return
80 * StartElement of the first element.
81 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070082 Event Next();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080083
Adam Lesinskicacb28f2016-10-19 12:18:14 -070084 //
85 // These are available for all nodes.
86 //
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080087
Adam Lesinskice5e56e2016-10-21 17:56:45 -070088 const std::string& comment() const;
89 size_t line_number() const;
90 size_t depth() const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080091
Adam Lesinskicacb28f2016-10-19 12:18:14 -070092 /**
93 * Returns the character data for a Text event.
94 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070095 const std::string& text() const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080096
Adam Lesinskicacb28f2016-10-19 12:18:14 -070097 //
98 // Namespace prefix and URI are available for StartNamespace and EndNamespace.
99 //
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800100
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700101 const std::string& namespace_prefix() const;
102 const std::string& namespace_uri() const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800103
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700104 //
105 // These are available for StartElement and EndElement.
106 //
Adam Lesinski467f1712015-11-16 17:35:44 -0800107
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700108 const std::string& element_namespace() const;
109 const std::string& element_name() const;
Adam Lesinski467f1712015-11-16 17:35:44 -0800110
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700111 /*
112 * Uses the current stack of namespaces to resolve the package. Eg:
113 * xmlns:app = "http://schemas.android.com/apk/res/com.android.app"
114 * ...
115 * android:text="@app:string/message"
116 *
117 * In this case, 'app' will be converted to 'com.android.app'.
118 *
119 * If xmlns:app="http://schemas.android.com/apk/res-auto", then
120 * 'package' will be set to 'defaultPackage'.
121 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700122 Maybe<ExtractedPackage> TransformPackageAlias(
Adam Lesinskid5083f62017-01-16 15:07:21 -0800123 const android::StringPiece& alias, const android::StringPiece& local_package) const override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800124
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700125 //
126 // Remaining methods are for retrieving information about attributes
127 // associated with a StartElement.
128 //
129 // Attributes must be in sorted order (according to the less than operator
130 // of struct Attribute).
131 //
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800132
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700133 struct Attribute {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700134 std::string namespace_uri;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700135 std::string name;
136 std::string value;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800137
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700138 int compare(const Attribute& rhs) const;
139 bool operator<(const Attribute& rhs) const;
140 bool operator==(const Attribute& rhs) const;
141 bool operator!=(const Attribute& rhs) const;
142 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800143
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700144 using const_iterator = std::vector<Attribute>::const_iterator;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800145
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700146 const_iterator begin_attributes() const;
147 const_iterator end_attributes() const;
148 size_t attribute_count() const;
Adam Lesinskid5083f62017-01-16 15:07:21 -0800149 const_iterator FindAttribute(android::StringPiece namespace_uri, android::StringPiece name) const;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700150
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700151 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700152 DISALLOW_COPY_AND_ASSIGN(XmlPullParser);
153
154 static void XMLCALL StartNamespaceHandler(void* user_data, const char* prefix,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700155 const char* uri);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700156 static void XMLCALL StartElementHandler(void* user_data, const char* name,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700157 const char** attrs);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700158 static void XMLCALL CharacterDataHandler(void* user_data, const char* s,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700159 int len);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700160 static void XMLCALL EndElementHandler(void* user_data, const char* name);
161 static void XMLCALL EndNamespaceHandler(void* user_data, const char* prefix);
162 static void XMLCALL CommentDataHandler(void* user_data, const char* comment);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700163
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700164 struct EventData {
165 Event event;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700166 size_t line_number;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700167 size_t depth;
168 std::string data1;
169 std::string data2;
170 std::vector<Attribute> attributes;
171 };
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700172
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700173 io::InputStream* in_;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700174 XML_Parser parser_;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700175 std::queue<EventData> event_queue_;
176 std::string error_;
177 const std::string empty_;
178 size_t depth_;
179 std::stack<std::string> namespace_uris_;
Adam Lesinski467f1712015-11-16 17:35:44 -0800180
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700181 struct PackageDecl {
182 std::string prefix;
183 ExtractedPackage package;
184 };
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700185 std::vector<PackageDecl> package_aliases_;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800186};
187
Adam Lesinski467f1712015-11-16 17:35:44 -0800188/**
189 * Finds the attribute in the current element within the global namespace.
190 */
Adam Lesinskid5083f62017-01-16 15:07:21 -0800191Maybe<android::StringPiece> FindAttribute(const XmlPullParser* parser,
192 const android::StringPiece& name);
Adam Lesinski467f1712015-11-16 17:35:44 -0800193
194/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700195 * Finds the attribute in the current element within the global namespace. The
196 * attribute's value
Adam Lesinski467f1712015-11-16 17:35:44 -0800197 * must not be the empty string.
198 */
Adam Lesinskid5083f62017-01-16 15:07:21 -0800199Maybe<android::StringPiece> FindNonEmptyAttribute(const XmlPullParser* parser,
200 const android::StringPiece& name);
Adam Lesinski467f1712015-11-16 17:35:44 -0800201
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800202//
203// Implementation
204//
205
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700206inline ::std::ostream& operator<<(::std::ostream& out,
207 XmlPullParser::Event event) {
208 switch (event) {
209 case XmlPullParser::Event::kBadDocument:
210 return out << "BadDocument";
211 case XmlPullParser::Event::kStartDocument:
212 return out << "StartDocument";
213 case XmlPullParser::Event::kEndDocument:
214 return out << "EndDocument";
215 case XmlPullParser::Event::kStartNamespace:
216 return out << "StartNamespace";
217 case XmlPullParser::Event::kEndNamespace:
218 return out << "EndNamespace";
219 case XmlPullParser::Event::kStartElement:
220 return out << "StartElement";
221 case XmlPullParser::Event::kEndElement:
222 return out << "EndElement";
223 case XmlPullParser::Event::kText:
224 return out << "Text";
225 case XmlPullParser::Event::kComment:
226 return out << "Comment";
227 }
228 return out;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800229}
230
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700231inline bool XmlPullParser::NextChildNode(XmlPullParser* parser, size_t start_depth) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700232 Event event;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700233
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700234 // First get back to the start depth.
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700235 while (IsGoodEvent(event = parser->Next()) && parser->depth() > start_depth + 1) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700236 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700237
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700238 // Now look for the first good node.
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700239 while ((event != Event::kEndElement || parser->depth() > start_depth) && IsGoodEvent(event)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700240 switch (event) {
241 case Event::kText:
242 case Event::kComment:
243 case Event::kStartElement:
244 return true;
245 default:
246 break;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700247 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700248 event = parser->Next();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700249 }
250 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700251}
252
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700253inline bool XmlPullParser::SkipCurrentElement(XmlPullParser* parser) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700254 int depth = 1;
255 while (depth > 0) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700256 switch (parser->Next()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700257 case Event::kEndDocument:
258 return true;
259 case Event::kBadDocument:
260 return false;
261 case Event::kStartElement:
262 depth++;
263 break;
264 case Event::kEndElement:
265 depth--;
266 break;
267 default:
268 break;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800269 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700270 }
271 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800272}
273
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700274inline bool XmlPullParser::IsGoodEvent(XmlPullParser::Event event) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700275 return event != Event::kBadDocument && event != Event::kEndDocument;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800276}
277
278inline int XmlPullParser::Attribute::compare(const Attribute& rhs) const {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700279 int cmp = namespace_uri.compare(rhs.namespace_uri);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700280 if (cmp != 0) return cmp;
281 return name.compare(rhs.name);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800282}
283
284inline bool XmlPullParser::Attribute::operator<(const Attribute& rhs) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700285 return compare(rhs) < 0;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800286}
287
288inline bool XmlPullParser::Attribute::operator==(const Attribute& rhs) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700289 return compare(rhs) == 0;
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
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700296inline XmlPullParser::const_iterator XmlPullParser::FindAttribute(
Adam Lesinskid5083f62017-01-16 15:07:21 -0800297 android::StringPiece namespace_uri, android::StringPiece name) const {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700298 const auto end_iter = end_attributes();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700299 const auto iter = std::lower_bound(
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700300 begin_attributes(), end_iter,
Adam Lesinskid5083f62017-01-16 15:07:21 -0800301 std::pair<android::StringPiece, android::StringPiece>(namespace_uri, name),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700302 [](const Attribute& attr,
Adam Lesinskid5083f62017-01-16 15:07:21 -0800303 const std::pair<android::StringPiece, android::StringPiece>& rhs) -> bool {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700304 int cmp = attr.namespace_uri.compare(
305 0, attr.namespace_uri.size(), rhs.first.data(), rhs.first.size());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700306 if (cmp < 0) return true;
307 if (cmp > 0) return false;
308 cmp = attr.name.compare(0, attr.name.size(), rhs.second.data(),
309 rhs.second.size());
310 if (cmp < 0) return true;
311 return false;
312 });
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800313
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700314 if (iter != end_iter && namespace_uri == iter->namespace_uri &&
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700315 name == iter->name) {
316 return iter;
317 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700318 return end_iter;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800319}
320
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700321} // namespace xml
322} // namespace aapt
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800323
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700324#endif // AAPT_XML_PULL_PARSER_H