blob: 6ebaa285745b4428732a9786a60c6b78cc31aed5 [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,
Ryan Mitchellcb76d732018-06-05 10:15:04 -070055 kCdataStart,
56 kCdataEnd,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070057 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080058
Adam Lesinskicacb28f2016-10-19 12:18:14 -070059 /**
Adam Lesinskice5e56e2016-10-21 17:56:45 -070060 * Skips to the next direct descendant node of the given start_depth,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070061 * skipping namespace nodes.
62 *
Adam Lesinskice5e56e2016-10-21 17:56:45 -070063 * When NextChildNode() returns true, you can expect Comments, Text, and
Adam Lesinskicacb28f2016-10-19 12:18:14 -070064 * StartElement events.
65 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070066 static bool NextChildNode(XmlPullParser* parser, size_t start_depth);
67 static bool SkipCurrentElement(XmlPullParser* parser);
68 static bool IsGoodEvent(Event event);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080069
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070070 explicit XmlPullParser(io::InputStream* in);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070071 ~XmlPullParser();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080072
Adam Lesinskicacb28f2016-10-19 12:18:14 -070073 /**
74 * Returns the current event that is being processed.
75 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070076 Event event() const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080077
Adam Lesinskice5e56e2016-10-21 17:56:45 -070078 const std::string& error() const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080079
Adam Lesinskicacb28f2016-10-19 12:18:14 -070080 /**
81 * Note, unlike XmlPullParser, the first call to next() will return
82 * StartElement of the first element.
83 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070084 Event Next();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080085
Adam Lesinskicacb28f2016-10-19 12:18:14 -070086 //
87 // These are available for all nodes.
88 //
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080089
Adam Lesinskice5e56e2016-10-21 17:56:45 -070090 const std::string& comment() const;
91 size_t line_number() const;
92 size_t depth() const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080093
Adam Lesinskicacb28f2016-10-19 12:18:14 -070094 /**
95 * Returns the character data for a Text event.
96 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070097 const std::string& text() const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080098
Adam Lesinskicacb28f2016-10-19 12:18:14 -070099 //
100 // Namespace prefix and URI are available for StartNamespace and EndNamespace.
101 //
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800102
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700103 const std::string& namespace_prefix() const;
104 const std::string& namespace_uri() const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800105
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700106 //
107 // These are available for StartElement and EndElement.
108 //
Adam Lesinski467f1712015-11-16 17:35:44 -0800109
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700110 const std::string& element_namespace() const;
111 const std::string& element_name() const;
Adam Lesinski467f1712015-11-16 17:35:44 -0800112
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700113 /*
114 * Uses the current stack of namespaces to resolve the package. Eg:
115 * xmlns:app = "http://schemas.android.com/apk/res/com.android.app"
116 * ...
117 * android:text="@app:string/message"
118 *
119 * In this case, 'app' will be converted to 'com.android.app'.
120 *
121 * If xmlns:app="http://schemas.android.com/apk/res-auto", then
122 * 'package' will be set to 'defaultPackage'.
123 */
Adam Lesinski1ef0fa92017-08-15 21:32:49 -0700124 Maybe<ExtractedPackage> TransformPackageAlias(const android::StringPiece& alias) const override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800125
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700126 //
127 // Remaining methods are for retrieving information about attributes
128 // associated with a StartElement.
129 //
130 // Attributes must be in sorted order (according to the less than operator
131 // of struct Attribute).
132 //
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800133
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700134 struct Attribute {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700135 std::string namespace_uri;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700136 std::string name;
137 std::string value;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800138
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700139 int compare(const Attribute& rhs) const;
140 bool operator<(const Attribute& rhs) const;
141 bool operator==(const Attribute& rhs) const;
142 bool operator!=(const Attribute& rhs) const;
143 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800144
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700145 using const_iterator = std::vector<Attribute>::const_iterator;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800146
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700147 const_iterator begin_attributes() const;
148 const_iterator end_attributes() const;
149 size_t attribute_count() const;
Adam Lesinskid5083f62017-01-16 15:07:21 -0800150 const_iterator FindAttribute(android::StringPiece namespace_uri, android::StringPiece name) const;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700151
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700152 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700153 DISALLOW_COPY_AND_ASSIGN(XmlPullParser);
154
155 static void XMLCALL StartNamespaceHandler(void* user_data, const char* prefix,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700156 const char* uri);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700157 static void XMLCALL StartElementHandler(void* user_data, const char* name,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700158 const char** attrs);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700159 static void XMLCALL CharacterDataHandler(void* user_data, const char* s,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700160 int len);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700161 static void XMLCALL EndElementHandler(void* user_data, const char* name);
162 static void XMLCALL EndNamespaceHandler(void* user_data, const char* prefix);
163 static void XMLCALL CommentDataHandler(void* user_data, const char* comment);
Ryan Mitchellcb76d732018-06-05 10:15:04 -0700164 static void XMLCALL StartCdataSectionHandler(void* user_data);
165 static void XMLCALL EndCdataSectionHandler(void* user_data);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700166
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700167 struct EventData {
168 Event event;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700169 size_t line_number;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700170 size_t depth;
171 std::string data1;
172 std::string data2;
173 std::vector<Attribute> attributes;
174 };
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700175
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700176 io::InputStream* in_;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700177 XML_Parser parser_;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700178 std::queue<EventData> event_queue_;
179 std::string error_;
180 const std::string empty_;
181 size_t depth_;
182 std::stack<std::string> namespace_uris_;
Adam Lesinski467f1712015-11-16 17:35:44 -0800183
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700184 struct PackageDecl {
185 std::string prefix;
186 ExtractedPackage package;
187 };
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700188 std::vector<PackageDecl> package_aliases_;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800189};
190
Adam Lesinski467f1712015-11-16 17:35:44 -0800191/**
192 * Finds the attribute in the current element within the global namespace.
193 */
Adam Lesinskid5083f62017-01-16 15:07:21 -0800194Maybe<android::StringPiece> FindAttribute(const XmlPullParser* parser,
195 const android::StringPiece& name);
Adam Lesinski467f1712015-11-16 17:35:44 -0800196
197/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700198 * Finds the attribute in the current element within the global namespace. The
199 * attribute's value
Adam Lesinski467f1712015-11-16 17:35:44 -0800200 * must not be the empty string.
201 */
Adam Lesinskid5083f62017-01-16 15:07:21 -0800202Maybe<android::StringPiece> FindNonEmptyAttribute(const XmlPullParser* parser,
203 const android::StringPiece& name);
Adam Lesinski467f1712015-11-16 17:35:44 -0800204
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800205//
206// Implementation
207//
208
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700209inline ::std::ostream& operator<<(::std::ostream& out,
210 XmlPullParser::Event event) {
211 switch (event) {
212 case XmlPullParser::Event::kBadDocument:
213 return out << "BadDocument";
214 case XmlPullParser::Event::kStartDocument:
215 return out << "StartDocument";
216 case XmlPullParser::Event::kEndDocument:
217 return out << "EndDocument";
218 case XmlPullParser::Event::kStartNamespace:
219 return out << "StartNamespace";
220 case XmlPullParser::Event::kEndNamespace:
221 return out << "EndNamespace";
222 case XmlPullParser::Event::kStartElement:
223 return out << "StartElement";
224 case XmlPullParser::Event::kEndElement:
225 return out << "EndElement";
226 case XmlPullParser::Event::kText:
227 return out << "Text";
228 case XmlPullParser::Event::kComment:
229 return out << "Comment";
Ryan Mitchellcb76d732018-06-05 10:15:04 -0700230 case XmlPullParser::Event::kCdataStart:
231 return out << "CdataStart";
232 case XmlPullParser::Event::kCdataEnd:
233 return out << "CdataEnd";
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700234 }
235 return out;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800236}
237
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700238inline bool XmlPullParser::NextChildNode(XmlPullParser* parser, size_t start_depth) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700239 Event event;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700240
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700241 // First get back to the start depth.
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700242 while (IsGoodEvent(event = parser->Next()) && parser->depth() > start_depth + 1) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700243 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700244
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700245 // Now look for the first good node.
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700246 while ((event != Event::kEndElement || parser->depth() > start_depth) && IsGoodEvent(event)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700247 switch (event) {
248 case Event::kText:
249 case Event::kComment:
250 case Event::kStartElement:
Ryan Mitchellcb76d732018-06-05 10:15:04 -0700251 case Event::kCdataStart:
252 case Event::kCdataEnd:
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700253 return true;
254 default:
255 break;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700256 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700257 event = parser->Next();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700258 }
259 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700260}
261
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700262inline bool XmlPullParser::SkipCurrentElement(XmlPullParser* parser) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700263 int depth = 1;
264 while (depth > 0) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700265 switch (parser->Next()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700266 case Event::kEndDocument:
267 return true;
268 case Event::kBadDocument:
269 return false;
270 case Event::kStartElement:
271 depth++;
272 break;
273 case Event::kEndElement:
274 depth--;
275 break;
276 default:
277 break;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800278 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700279 }
280 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800281}
282
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700283inline bool XmlPullParser::IsGoodEvent(XmlPullParser::Event event) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700284 return event != Event::kBadDocument && event != Event::kEndDocument;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800285}
286
287inline int XmlPullParser::Attribute::compare(const Attribute& rhs) const {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700288 int cmp = namespace_uri.compare(rhs.namespace_uri);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700289 if (cmp != 0) return cmp;
290 return name.compare(rhs.name);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800291}
292
293inline bool XmlPullParser::Attribute::operator<(const Attribute& rhs) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700294 return compare(rhs) < 0;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800295}
296
297inline bool XmlPullParser::Attribute::operator==(const Attribute& rhs) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700298 return compare(rhs) == 0;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800299}
300
301inline bool XmlPullParser::Attribute::operator!=(const Attribute& rhs) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700302 return compare(rhs) != 0;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800303}
304
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700305inline XmlPullParser::const_iterator XmlPullParser::FindAttribute(
Adam Lesinskid5083f62017-01-16 15:07:21 -0800306 android::StringPiece namespace_uri, android::StringPiece name) const {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700307 const auto end_iter = end_attributes();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700308 const auto iter = std::lower_bound(
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700309 begin_attributes(), end_iter,
Adam Lesinskid5083f62017-01-16 15:07:21 -0800310 std::pair<android::StringPiece, android::StringPiece>(namespace_uri, name),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700311 [](const Attribute& attr,
Adam Lesinskid5083f62017-01-16 15:07:21 -0800312 const std::pair<android::StringPiece, android::StringPiece>& rhs) -> bool {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700313 int cmp = attr.namespace_uri.compare(
314 0, attr.namespace_uri.size(), rhs.first.data(), rhs.first.size());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700315 if (cmp < 0) return true;
316 if (cmp > 0) return false;
317 cmp = attr.name.compare(0, attr.name.size(), rhs.second.data(),
318 rhs.second.size());
319 if (cmp < 0) return true;
320 return false;
321 });
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800322
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700323 if (iter != end_iter && namespace_uri == iter->namespace_uri &&
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700324 name == iter->name) {
325 return iter;
326 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700327 return end_iter;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800328}
329
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700330} // namespace xml
331} // namespace aapt
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800332
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700333#endif // AAPT_XML_PULL_PARSER_H