blob: ff58d604e35efcdae3270450149502c88429ac25 [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"
31
32#include "Resource.h"
33#include "process/IResourceTableConsumer.h"
34#include "util/Maybe.h"
35#include "util/StringPiece.h"
36#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,
54 };
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
Adam Lesinskicacb28f2016-10-19 12:18:14 -070067 explicit XmlPullParser(std::istream& in);
68 ~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 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700121 Maybe<ExtractedPackage> TransformPackageAlias(
122 const StringPiece& alias,
123 const 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;
149 const_iterator FindAttribute(StringPiece namespace_uri,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700150 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);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700164
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700165 struct EventData {
166 Event event;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700167 size_t line_number;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700168 size_t depth;
169 std::string data1;
170 std::string data2;
171 std::vector<Attribute> attributes;
172 };
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700173
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700174 std::istream& in_;
175 XML_Parser parser_;
176 char buffer_[16384];
177 std::queue<EventData> event_queue_;
178 std::string error_;
179 const std::string empty_;
180 size_t depth_;
181 std::stack<std::string> namespace_uris_;
Adam Lesinski467f1712015-11-16 17:35:44 -0800182
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700183 struct PackageDecl {
184 std::string prefix;
185 ExtractedPackage package;
186 };
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 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700193Maybe<StringPiece> FindAttribute(const XmlPullParser* parser,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700194 const 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 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700201Maybe<StringPiece> FindNonEmptyAttribute(const XmlPullParser* parser,
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700202 const 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";
229 }
230 return out;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800231}
232
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700233inline bool XmlPullParser::NextChildNode(XmlPullParser* parser,
234 size_t start_depth) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700235 Event event;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700236
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700237 // First get back to the start depth.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700238 while (IsGoodEvent(event = parser->Next()) &&
239 parser->depth() > start_depth + 1) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700240 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700241
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700242 // Now look for the first good node.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700243 while ((event != Event::kEndElement || parser->depth() > start_depth) &&
244 IsGoodEvent(event)) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700245 switch (event) {
246 case Event::kText:
247 case Event::kComment:
248 case Event::kStartElement:
249 return true;
250 default:
251 break;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700252 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700253 event = parser->Next();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700254 }
255 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700256}
257
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700258inline bool XmlPullParser::SkipCurrentElement(XmlPullParser* parser) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700259 int depth = 1;
260 while (depth > 0) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700261 switch (parser->Next()) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700262 case Event::kEndDocument:
263 return true;
264 case Event::kBadDocument:
265 return false;
266 case Event::kStartElement:
267 depth++;
268 break;
269 case Event::kEndElement:
270 depth--;
271 break;
272 default:
273 break;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800274 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700275 }
276 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800277}
278
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700279inline bool XmlPullParser::IsGoodEvent(XmlPullParser::Event event) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700280 return event != Event::kBadDocument && event != Event::kEndDocument;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800281}
282
283inline int XmlPullParser::Attribute::compare(const Attribute& rhs) const {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700284 int cmp = namespace_uri.compare(rhs.namespace_uri);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700285 if (cmp != 0) return cmp;
286 return name.compare(rhs.name);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800287}
288
289inline bool XmlPullParser::Attribute::operator<(const Attribute& rhs) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700290 return compare(rhs) < 0;
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
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700301inline XmlPullParser::const_iterator XmlPullParser::FindAttribute(
302 StringPiece namespace_uri, StringPiece name) const {
303 const auto end_iter = end_attributes();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700304 const auto iter = std::lower_bound(
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700305 begin_attributes(), end_iter,
306 std::pair<StringPiece, StringPiece>(namespace_uri, name),
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700307 [](const Attribute& attr,
308 const std::pair<StringPiece, StringPiece>& rhs) -> bool {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700309 int cmp = attr.namespace_uri.compare(
310 0, attr.namespace_uri.size(), rhs.first.data(), rhs.first.size());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700311 if (cmp < 0) return true;
312 if (cmp > 0) return false;
313 cmp = attr.name.compare(0, attr.name.size(), rhs.second.data(),
314 rhs.second.size());
315 if (cmp < 0) return true;
316 return false;
317 });
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800318
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700319 if (iter != end_iter && namespace_uri == iter->namespace_uri &&
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700320 name == iter->name) {
321 return iter;
322 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700323 return end_iter;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800324}
325
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700326} // namespace xml
327} // namespace aapt
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800328
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700329#endif // AAPT_XML_PULL_PARSER_H