blob: ce69df669b0c44107f09ca2d77f0b7a7aa20cdf8 [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 "Resource.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070021#include "process/IResourceTableConsumer.h"
Adam Lesinski467f1712015-11-16 17:35:44 -080022#include "util/Maybe.h"
23#include "util/StringPiece.h"
24#include "xml/XmlUtil.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070025
Adam Lesinski1ab598f2015-08-14 14:26:04 -070026#include <expat.h>
Adam Lesinskicacb28f2016-10-19 12:18:14 -070027#include <algorithm>
Adam Lesinski1ab598f2015-08-14 14:26:04 -070028#include <istream>
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080029#include <ostream>
Adam Lesinski1ab598f2015-08-14 14:26:04 -070030#include <queue>
31#include <stack>
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080032#include <string>
33#include <vector>
34
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080035namespace aapt {
Adam Lesinski467f1712015-11-16 17:35:44 -080036namespace xml {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080037
Adam Lesinski1ab598f2015-08-14 14:26:04 -070038class XmlPullParser : public IPackageDeclStack {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070039 public:
40 enum class Event {
41 kBadDocument,
42 kStartDocument,
43 kEndDocument,
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080044
Adam Lesinskicacb28f2016-10-19 12:18:14 -070045 kStartNamespace,
46 kEndNamespace,
47 kStartElement,
48 kEndElement,
49 kText,
50 kComment,
51 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080052
Adam Lesinskicacb28f2016-10-19 12:18:14 -070053 /**
54 * Skips to the next direct descendant node of the given startDepth,
55 * skipping namespace nodes.
56 *
57 * When nextChildNode returns true, you can expect Comments, Text, and
58 * StartElement events.
59 */
60 static bool nextChildNode(XmlPullParser* parser, size_t startDepth);
61 static bool skipCurrentElement(XmlPullParser* parser);
62 static bool isGoodEvent(Event event);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080063
Adam Lesinskicacb28f2016-10-19 12:18:14 -070064 explicit XmlPullParser(std::istream& in);
65 ~XmlPullParser();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080066
Adam Lesinskicacb28f2016-10-19 12:18:14 -070067 /**
68 * Returns the current event that is being processed.
69 */
70 Event getEvent() const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080071
Adam Lesinskicacb28f2016-10-19 12:18:14 -070072 const std::string& getLastError() const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080073
Adam Lesinskicacb28f2016-10-19 12:18:14 -070074 /**
75 * Note, unlike XmlPullParser, the first call to next() will return
76 * StartElement of the first element.
77 */
78 Event next();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080079
Adam Lesinskicacb28f2016-10-19 12:18:14 -070080 //
81 // These are available for all nodes.
82 //
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080083
Adam Lesinskicacb28f2016-10-19 12:18:14 -070084 const std::string& getComment() const;
85 size_t getLineNumber() const;
86 size_t getDepth() const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080087
Adam Lesinskicacb28f2016-10-19 12:18:14 -070088 /**
89 * Returns the character data for a Text event.
90 */
91 const std::string& getText() const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080092
Adam Lesinskicacb28f2016-10-19 12:18:14 -070093 //
94 // Namespace prefix and URI are available for StartNamespace and EndNamespace.
95 //
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080096
Adam Lesinskicacb28f2016-10-19 12:18:14 -070097 const std::string& getNamespacePrefix() const;
98 const std::string& getNamespaceUri() const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080099
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700100 //
101 // These are available for StartElement and EndElement.
102 //
Adam Lesinski467f1712015-11-16 17:35:44 -0800103
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700104 const std::string& getElementNamespace() const;
105 const std::string& getElementName() const;
Adam Lesinski467f1712015-11-16 17:35:44 -0800106
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700107 /*
108 * Uses the current stack of namespaces to resolve the package. Eg:
109 * xmlns:app = "http://schemas.android.com/apk/res/com.android.app"
110 * ...
111 * android:text="@app:string/message"
112 *
113 * In this case, 'app' will be converted to 'com.android.app'.
114 *
115 * If xmlns:app="http://schemas.android.com/apk/res-auto", then
116 * 'package' will be set to 'defaultPackage'.
117 */
118 Maybe<ExtractedPackage> transformPackageAlias(
119 const StringPiece& alias, const StringPiece& localPackage) const override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800120
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700121 //
122 // Remaining methods are for retrieving information about attributes
123 // associated with a StartElement.
124 //
125 // Attributes must be in sorted order (according to the less than operator
126 // of struct Attribute).
127 //
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800128
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700129 struct Attribute {
130 std::string namespaceUri;
131 std::string name;
132 std::string value;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800133
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700134 int compare(const Attribute& rhs) const;
135 bool operator<(const Attribute& rhs) const;
136 bool operator==(const Attribute& rhs) const;
137 bool operator!=(const Attribute& rhs) const;
138 };
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800139
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700140 using const_iterator = std::vector<Attribute>::const_iterator;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800141
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700142 const_iterator beginAttributes() const;
143 const_iterator endAttributes() const;
144 size_t getAttributeCount() const;
145 const_iterator findAttribute(StringPiece namespaceUri,
146 StringPiece name) const;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700147
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700148 private:
149 static void XMLCALL startNamespaceHandler(void* userData, const char* prefix,
150 const char* uri);
151 static void XMLCALL startElementHandler(void* userData, const char* name,
152 const char** attrs);
153 static void XMLCALL characterDataHandler(void* userData, const char* s,
154 int len);
155 static void XMLCALL endElementHandler(void* userData, const char* name);
156 static void XMLCALL endNamespaceHandler(void* userData, const char* prefix);
157 static void XMLCALL commentDataHandler(void* userData, const char* comment);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700158
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700159 struct EventData {
160 Event event;
161 size_t lineNumber;
162 size_t depth;
163 std::string data1;
164 std::string data2;
165 std::vector<Attribute> attributes;
166 };
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700167
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700168 std::istream& mIn;
169 XML_Parser mParser;
170 char mBuffer[16384];
171 std::queue<EventData> mEventQueue;
172 std::string mLastError;
173 const std::string mEmpty;
174 size_t mDepth;
175 std::stack<std::string> mNamespaceUris;
Adam Lesinski467f1712015-11-16 17:35:44 -0800176
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700177 struct PackageDecl {
178 std::string prefix;
179 ExtractedPackage package;
180 };
181 std::vector<PackageDecl> mPackageAliases;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800182};
183
Adam Lesinski467f1712015-11-16 17:35:44 -0800184/**
185 * Finds the attribute in the current element within the global namespace.
186 */
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700187Maybe<StringPiece> findAttribute(const XmlPullParser* parser,
188 const StringPiece& name);
Adam Lesinski467f1712015-11-16 17:35:44 -0800189
190/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700191 * Finds the attribute in the current element within the global namespace. The
192 * attribute's value
Adam Lesinski467f1712015-11-16 17:35:44 -0800193 * must not be the empty string.
194 */
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700195Maybe<StringPiece> findNonEmptyAttribute(const XmlPullParser* parser,
196 const StringPiece& name);
Adam Lesinski467f1712015-11-16 17:35:44 -0800197
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800198//
199// Implementation
200//
201
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700202inline ::std::ostream& operator<<(::std::ostream& out,
203 XmlPullParser::Event event) {
204 switch (event) {
205 case XmlPullParser::Event::kBadDocument:
206 return out << "BadDocument";
207 case XmlPullParser::Event::kStartDocument:
208 return out << "StartDocument";
209 case XmlPullParser::Event::kEndDocument:
210 return out << "EndDocument";
211 case XmlPullParser::Event::kStartNamespace:
212 return out << "StartNamespace";
213 case XmlPullParser::Event::kEndNamespace:
214 return out << "EndNamespace";
215 case XmlPullParser::Event::kStartElement:
216 return out << "StartElement";
217 case XmlPullParser::Event::kEndElement:
218 return out << "EndElement";
219 case XmlPullParser::Event::kText:
220 return out << "Text";
221 case XmlPullParser::Event::kComment:
222 return out << "Comment";
223 }
224 return out;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800225}
226
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700227inline bool XmlPullParser::nextChildNode(XmlPullParser* parser,
228 size_t startDepth) {
229 Event event;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700230
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700231 // First get back to the start depth.
232 while (isGoodEvent(event = parser->next()) &&
233 parser->getDepth() > startDepth + 1) {
234 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700235
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700236 // Now look for the first good node.
237 while ((event != Event::kEndElement || parser->getDepth() > startDepth) &&
238 isGoodEvent(event)) {
239 switch (event) {
240 case Event::kText:
241 case Event::kComment:
242 case Event::kStartElement:
243 return true;
244 default:
245 break;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700246 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700247 event = parser->next();
248 }
249 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700250}
251
252inline bool XmlPullParser::skipCurrentElement(XmlPullParser* parser) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700253 int depth = 1;
254 while (depth > 0) {
255 switch (parser->next()) {
256 case Event::kEndDocument:
257 return true;
258 case Event::kBadDocument:
259 return false;
260 case Event::kStartElement:
261 depth++;
262 break;
263 case Event::kEndElement:
264 depth--;
265 break;
266 default:
267 break;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800268 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700269 }
270 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800271}
272
273inline bool XmlPullParser::isGoodEvent(XmlPullParser::Event event) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700274 return event != Event::kBadDocument && event != Event::kEndDocument;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800275}
276
277inline int XmlPullParser::Attribute::compare(const Attribute& rhs) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700278 int cmp = namespaceUri.compare(rhs.namespaceUri);
279 if (cmp != 0) return cmp;
280 return name.compare(rhs.name);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800281}
282
283inline bool XmlPullParser::Attribute::operator<(const Attribute& rhs) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700284 return compare(rhs) < 0;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800285}
286
287inline bool XmlPullParser::Attribute::operator==(const Attribute& rhs) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700288 return compare(rhs) == 0;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800289}
290
291inline bool XmlPullParser::Attribute::operator!=(const Attribute& rhs) const {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700292 return compare(rhs) != 0;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800293}
294
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700295inline XmlPullParser::const_iterator XmlPullParser::findAttribute(
296 StringPiece namespaceUri, StringPiece name) const {
297 const auto endIter = endAttributes();
298 const auto iter = std::lower_bound(
299 beginAttributes(), endIter,
300 std::pair<StringPiece, StringPiece>(namespaceUri, name),
301 [](const Attribute& attr,
302 const std::pair<StringPiece, StringPiece>& rhs) -> bool {
303 int cmp = attr.namespaceUri.compare(0, attr.namespaceUri.size(),
304 rhs.first.data(), rhs.first.size());
305 if (cmp < 0) return true;
306 if (cmp > 0) return false;
307 cmp = attr.name.compare(0, attr.name.size(), rhs.second.data(),
308 rhs.second.size());
309 if (cmp < 0) return true;
310 return false;
311 });
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800312
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700313 if (iter != endIter && namespaceUri == iter->namespaceUri &&
314 name == iter->name) {
315 return iter;
316 }
317 return endIter;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800318}
319
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700320} // namespace xml
321} // namespace aapt
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800322
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700323#endif // AAPT_XML_PULL_PARSER_H