blob: ee51b360feb42295a9c87e33fa0418485f1b55bd [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 Lesinski6f6ceb72014-11-14 14:48:12 -080026#include <algorithm>
Adam Lesinski1ab598f2015-08-14 14:26:04 -070027#include <expat.h>
28#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 Lesinski6f6ceb72014-11-14 14:48:12 -080039public:
40 enum class Event {
41 kBadDocument,
42 kStartDocument,
43 kEndDocument,
44
45 kStartNamespace,
46 kEndNamespace,
47 kStartElement,
48 kEndElement,
49 kText,
50 kComment,
51 };
52
Adam Lesinski1ab598f2015-08-14 14:26:04 -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 StartElement events.
58 */
59 static bool nextChildNode(XmlPullParser* parser, size_t startDepth);
60 static bool skipCurrentElement(XmlPullParser* parser);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080061 static bool isGoodEvent(Event event);
62
Adam Lesinski1ab598f2015-08-14 14:26:04 -070063 XmlPullParser(std::istream& in);
Adam Lesinski467f1712015-11-16 17:35:44 -080064 ~XmlPullParser();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080065
66 /**
67 * Returns the current event that is being processed.
68 */
Adam Lesinski1ab598f2015-08-14 14:26:04 -070069 Event getEvent() const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080070
Adam Lesinski1ab598f2015-08-14 14:26:04 -070071 const std::string& getLastError() const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080072
73 /**
74 * Note, unlike XmlPullParser, the first call to next() will return
75 * StartElement of the first element.
76 */
Adam Lesinski1ab598f2015-08-14 14:26:04 -070077 Event next();
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080078
79 //
80 // These are available for all nodes.
81 //
82
Adam Lesinskid0f116b2016-07-08 15:00:32 -070083 const std::string& getComment() const;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070084 size_t getLineNumber() const;
85 size_t getDepth() const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080086
87 /**
88 * Returns the character data for a Text event.
89 */
Adam Lesinskid0f116b2016-07-08 15:00:32 -070090 const std::string& getText() const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080091
Adam Lesinski24aad162015-04-24 19:19:30 -070092 //
93 // Namespace prefix and URI are available for StartNamespace and EndNamespace.
94 //
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080095
Adam Lesinskid0f116b2016-07-08 15:00:32 -070096 const std::string& getNamespacePrefix() const;
97 const std::string& getNamespaceUri() const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080098
Adam Lesinski467f1712015-11-16 17:35:44 -080099 //
100 // These are available for StartElement and EndElement.
101 //
102
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700103 const std::string& getElementNamespace() const;
104 const std::string& getElementName() const;
Adam Lesinski467f1712015-11-16 17:35:44 -0800105
Adam Lesinski24aad162015-04-24 19:19:30 -0700106 /*
107 * Uses the current stack of namespaces to resolve the package. Eg:
108 * xmlns:app = "http://schemas.android.com/apk/res/com.android.app"
109 * ...
110 * android:text="@app:string/message"
111 *
112 * In this case, 'app' will be converted to 'com.android.app'.
113 *
114 * If xmlns:app="http://schemas.android.com/apk/res-auto", then
115 * 'package' will be set to 'defaultPackage'.
116 */
Adam Lesinski467f1712015-11-16 17:35:44 -0800117 Maybe<ExtractedPackage> transformPackageAlias(
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700118 const StringPiece& alias, const StringPiece& localPackage) const override;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800119
120 //
121 // Remaining methods are for retrieving information about attributes
122 // associated with a StartElement.
123 //
124 // Attributes must be in sorted order (according to the less than operator
125 // of struct Attribute).
126 //
127
128 struct Attribute {
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700129 std::string namespaceUri;
130 std::string name;
131 std::string value;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800132
133 int compare(const Attribute& rhs) const;
134 bool operator<(const Attribute& rhs) const;
135 bool operator==(const Attribute& rhs) const;
136 bool operator!=(const Attribute& rhs) const;
137 };
138
139 using const_iterator = std::vector<Attribute>::const_iterator;
140
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700141 const_iterator beginAttributes() const;
142 const_iterator endAttributes() const;
143 size_t getAttributeCount() const;
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700144 const_iterator findAttribute(StringPiece namespaceUri, StringPiece name) const;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700145
146private:
147 static void XMLCALL startNamespaceHandler(void* userData, const char* prefix, const char* uri);
148 static void XMLCALL startElementHandler(void* userData, const char* name, const char** attrs);
149 static void XMLCALL characterDataHandler(void* userData, const char* s, int len);
150 static void XMLCALL endElementHandler(void* userData, const char* name);
151 static void XMLCALL endNamespaceHandler(void* userData, const char* prefix);
152 static void XMLCALL commentDataHandler(void* userData, const char* comment);
153
154 struct EventData {
155 Event event;
156 size_t lineNumber;
157 size_t depth;
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700158 std::string data1;
159 std::string data2;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700160 std::vector<Attribute> attributes;
161 };
162
163 std::istream& mIn;
164 XML_Parser mParser;
165 char mBuffer[16384];
166 std::queue<EventData> mEventQueue;
167 std::string mLastError;
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700168 const std::string mEmpty;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700169 size_t mDepth;
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700170 std::stack<std::string> mNamespaceUris;
Adam Lesinski467f1712015-11-16 17:35:44 -0800171
172 struct PackageDecl {
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700173 std::string prefix;
Adam Lesinski467f1712015-11-16 17:35:44 -0800174 ExtractedPackage package;
175 };
176 std::vector<PackageDecl> mPackageAliases;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800177};
178
Adam Lesinski467f1712015-11-16 17:35:44 -0800179/**
180 * Finds the attribute in the current element within the global namespace.
181 */
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700182Maybe<StringPiece> findAttribute(const XmlPullParser* parser, const StringPiece& name);
Adam Lesinski467f1712015-11-16 17:35:44 -0800183
184/**
185 * Finds the attribute in the current element within the global namespace. The attribute's value
186 * must not be the empty string.
187 */
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700188Maybe<StringPiece> findNonEmptyAttribute(const XmlPullParser* parser, const StringPiece& name);
Adam Lesinski467f1712015-11-16 17:35:44 -0800189
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800190//
191// Implementation
192//
193
194inline ::std::ostream& operator<<(::std::ostream& out, XmlPullParser::Event event) {
195 switch (event) {
196 case XmlPullParser::Event::kBadDocument: return out << "BadDocument";
197 case XmlPullParser::Event::kStartDocument: return out << "StartDocument";
198 case XmlPullParser::Event::kEndDocument: return out << "EndDocument";
199 case XmlPullParser::Event::kStartNamespace: return out << "StartNamespace";
200 case XmlPullParser::Event::kEndNamespace: return out << "EndNamespace";
201 case XmlPullParser::Event::kStartElement: return out << "StartElement";
202 case XmlPullParser::Event::kEndElement: return out << "EndElement";
203 case XmlPullParser::Event::kText: return out << "Text";
204 case XmlPullParser::Event::kComment: return out << "Comment";
205 }
206 return out;
207}
208
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700209inline bool XmlPullParser::nextChildNode(XmlPullParser* parser, size_t startDepth) {
210 Event event;
211
212 // First get back to the start depth.
213 while (isGoodEvent(event = parser->next()) && parser->getDepth() > startDepth + 1) {}
214
215 // Now look for the first good node.
216 while ((event != Event::kEndElement || parser->getDepth() > startDepth) && isGoodEvent(event)) {
217 switch (event) {
218 case Event::kText:
219 case Event::kComment:
220 case Event::kStartElement:
221 return true;
222 default:
223 break;
224 }
225 event = parser->next();
226 }
227 return false;
228}
229
230inline bool XmlPullParser::skipCurrentElement(XmlPullParser* parser) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800231 int depth = 1;
232 while (depth > 0) {
233 switch (parser->next()) {
234 case Event::kEndDocument:
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700235 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800236 case Event::kBadDocument:
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700237 return false;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800238 case Event::kStartElement:
239 depth++;
240 break;
241 case Event::kEndElement:
242 depth--;
243 break;
244 default:
245 break;
246 }
247 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700248 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800249}
250
251inline bool XmlPullParser::isGoodEvent(XmlPullParser::Event event) {
252 return event != Event::kBadDocument && event != Event::kEndDocument;
253}
254
255inline int XmlPullParser::Attribute::compare(const Attribute& rhs) const {
256 int cmp = namespaceUri.compare(rhs.namespaceUri);
257 if (cmp != 0) return cmp;
258 return name.compare(rhs.name);
259}
260
261inline bool XmlPullParser::Attribute::operator<(const Attribute& rhs) const {
262 return compare(rhs) < 0;
263}
264
265inline bool XmlPullParser::Attribute::operator==(const Attribute& rhs) const {
266 return compare(rhs) == 0;
267}
268
269inline bool XmlPullParser::Attribute::operator!=(const Attribute& rhs) const {
270 return compare(rhs) != 0;
271}
272
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700273inline XmlPullParser::const_iterator XmlPullParser::findAttribute(StringPiece namespaceUri,
274 StringPiece name) const {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800275 const auto endIter = endAttributes();
276 const auto iter = std::lower_bound(beginAttributes(), endIter,
Adam Lesinskid0f116b2016-07-08 15:00:32 -0700277 std::pair<StringPiece, StringPiece>(namespaceUri, name),
278 [](const Attribute& attr, const std::pair<StringPiece, StringPiece>& rhs) -> bool {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800279 int cmp = attr.namespaceUri.compare(0, attr.namespaceUri.size(),
280 rhs.first.data(), rhs.first.size());
281 if (cmp < 0) return true;
282 if (cmp > 0) return false;
283 cmp = attr.name.compare(0, attr.name.size(), rhs.second.data(), rhs.second.size());
284 if (cmp < 0) return true;
285 return false;
286 }
287 );
288
289 if (iter != endIter && namespaceUri == iter->namespaceUri && name == iter->name) {
290 return iter;
291 }
292 return endIter;
293}
294
Adam Lesinski467f1712015-11-16 17:35:44 -0800295} // namespace xml
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800296} // namespace aapt
297
298#endif // AAPT_XML_PULL_PARSER_H