1 /*
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 #include <iostream>
18 #include <string>
19 
20 #include "util/Util.h"
21 #include "xml/XmlPullParser.h"
22 #include "xml/XmlUtil.h"
23 
24 using ::android::InputStream;
25 using ::android::StringPiece;
26 
27 namespace aapt {
28 namespace xml {
29 
30 constexpr char kXmlNamespaceSep = 1;
31 
XmlPullParser(InputStream * in)32 XmlPullParser::XmlPullParser(InputStream* in) : in_(in), empty_(), depth_(0) {
33   parser_ = XML_ParserCreateNS(nullptr, kXmlNamespaceSep);
34   XML_SetUserData(parser_, this);
35   XML_SetElementHandler(parser_, StartElementHandler, EndElementHandler);
36   XML_SetNamespaceDeclHandler(parser_, StartNamespaceHandler,
37                               EndNamespaceHandler);
38   XML_SetCharacterDataHandler(parser_, CharacterDataHandler);
39   XML_SetCommentHandler(parser_, CommentDataHandler);
40   XML_SetCdataSectionHandler(parser_, StartCdataSectionHandler, EndCdataSectionHandler);
41   event_queue_.push(EventData{Event::kStartDocument, 0, depth_++});
42 }
43 
~XmlPullParser()44 XmlPullParser::~XmlPullParser() {
45   XML_ParserFree(parser_);
46 }
47 
Next()48 XmlPullParser::Event XmlPullParser::Next() {
49   const Event currentEvent = event();
50   if (currentEvent == Event::kBadDocument || currentEvent == Event::kEndDocument) {
51     return currentEvent;
52   }
53 
54   event_queue_.pop();
55   while (event_queue_.empty()) {
56     const char* buffer = nullptr;
57     size_t buffer_size = 0;
58     bool done = false;
59     if (!in_->Next(reinterpret_cast<const void**>(&buffer), &buffer_size)) {
60       if (in_->HadError()) {
61         error_ = in_->GetError();
62         event_queue_.push(EventData{Event::kBadDocument});
63         break;
64       }
65 
66       done = true;
67     }
68 
69     if (XML_Parse(parser_, buffer, buffer_size, done) == XML_STATUS_ERROR) {
70       error_ = XML_ErrorString(XML_GetErrorCode(parser_));
71       event_queue_.push(EventData{Event::kBadDocument});
72       break;
73     }
74 
75     if (done) {
76       event_queue_.push(EventData{Event::kEndDocument, 0, 0});
77     }
78   }
79 
80   Event next_event = event();
81 
82   // Record namespace prefixes and package names so that we can do our own
83   // handling of references that use namespace aliases.
84   if (next_event == Event::kStartNamespace ||
85       next_event == Event::kEndNamespace) {
86     std::optional<ExtractedPackage> result = ExtractPackageFromNamespace(namespace_uri());
87     if (next_event == Event::kStartNamespace) {
88       if (result) {
89         package_aliases_.emplace_back(
90             PackageDecl{namespace_prefix(), std::move(result.value())});
91       }
92     } else {
93       if (result) {
94         package_aliases_.pop_back();
95       }
96     }
97   }
98 
99   return next_event;
100 }
101 
event() const102 XmlPullParser::Event XmlPullParser::event() const {
103   return event_queue_.front().event;
104 }
105 
error() const106 const std::string& XmlPullParser::error() const { return error_; }
107 
comment() const108 const std::string& XmlPullParser::comment() const {
109   return event_queue_.front().data1;
110 }
111 
line_number() const112 size_t XmlPullParser::line_number() const {
113   return event_queue_.front().line_number;
114 }
115 
depth() const116 size_t XmlPullParser::depth() const { return event_queue_.front().depth; }
117 
text() const118 const std::string& XmlPullParser::text() const {
119   if (event() != Event::kText) {
120     return empty_;
121   }
122   return event_queue_.front().data1;
123 }
124 
namespace_prefix() const125 const std::string& XmlPullParser::namespace_prefix() const {
126   const Event current_event = event();
127   if (current_event != Event::kStartNamespace &&
128       current_event != Event::kEndNamespace) {
129     return empty_;
130   }
131   return event_queue_.front().data1;
132 }
133 
namespace_uri() const134 const std::string& XmlPullParser::namespace_uri() const {
135   const Event current_event = event();
136   if (current_event != Event::kStartNamespace &&
137       current_event != Event::kEndNamespace) {
138     return empty_;
139   }
140   return event_queue_.front().data2;
141 }
142 
TransformPackageAlias(StringPiece alias) const143 std::optional<ExtractedPackage> XmlPullParser::TransformPackageAlias(StringPiece alias) const {
144   if (alias.empty()) {
145     return ExtractedPackage{{}, false /*private*/};
146   }
147 
148   const auto end_iter = package_aliases_.rend();
149   for (auto iter = package_aliases_.rbegin(); iter != end_iter; ++iter) {
150     if (alias == iter->prefix) {
151       if (iter->package.package.empty()) {
152         return ExtractedPackage{{}, iter->package.private_namespace};
153       }
154       return iter->package;
155     }
156   }
157   return {};
158 }
159 
element_namespace() const160 const std::string& XmlPullParser::element_namespace() const {
161   const Event current_event = event();
162   if (current_event != Event::kStartElement &&
163       current_event != Event::kEndElement) {
164     return empty_;
165   }
166   return event_queue_.front().data1;
167 }
168 
element_name() const169 const std::string& XmlPullParser::element_name() const {
170   const Event current_event = event();
171   if (current_event != Event::kStartElement &&
172       current_event != Event::kEndElement) {
173     return empty_;
174   }
175   return event_queue_.front().data2;
176 }
177 
package_decls() const178 const std::vector<XmlPullParser::PackageDecl>& XmlPullParser::package_decls() const {
179   return package_aliases_;
180 }
181 
begin_attributes() const182 XmlPullParser::const_iterator XmlPullParser::begin_attributes() const {
183   return event_queue_.front().attributes.begin();
184 }
185 
end_attributes() const186 XmlPullParser::const_iterator XmlPullParser::end_attributes() const {
187   return event_queue_.front().attributes.end();
188 }
189 
attribute_count() const190 size_t XmlPullParser::attribute_count() const {
191   if (event() != Event::kStartElement) {
192     return 0;
193   }
194   return event_queue_.front().attributes.size();
195 }
196 
197 /**
198  * Extracts the namespace and name of an expanded element or attribute name.
199  */
SplitName(const char * name,std::string * out_ns,std::string * out_name)200 static void SplitName(const char* name, std::string* out_ns, std::string* out_name) {
201   const char* p = name;
202   while (*p != 0 && *p != kXmlNamespaceSep) {
203     p++;
204   }
205 
206   if (*p == 0) {
207     out_ns->clear();
208     out_name->assign(name);
209   } else {
210     out_ns->assign(name, (p - name));
211     out_name->assign(p + 1);
212   }
213 }
214 
StartNamespaceHandler(void * user_data,const char * prefix,const char * uri)215 void XMLCALL XmlPullParser::StartNamespaceHandler(void* user_data,
216                                                   const char* prefix,
217                                                   const char* uri) {
218   XmlPullParser* parser = reinterpret_cast<XmlPullParser*>(user_data);
219   std::string namespace_uri = uri != nullptr ? uri : std::string();
220   parser->namespace_uris_.push(namespace_uri);
221   parser->event_queue_.push(
222       EventData{Event::kStartNamespace,
223                 XML_GetCurrentLineNumber(parser->parser_), parser->depth_++,
224                 prefix != nullptr ? prefix : std::string(), namespace_uri});
225 }
226 
StartElementHandler(void * user_data,const char * name,const char ** attrs)227 void XMLCALL XmlPullParser::StartElementHandler(void* user_data,
228                                                 const char* name,
229                                                 const char** attrs) {
230   XmlPullParser* parser = reinterpret_cast<XmlPullParser*>(user_data);
231 
232   EventData data = {Event::kStartElement,
233                     XML_GetCurrentLineNumber(parser->parser_),
234                     parser->depth_++};
235   SplitName(name, &data.data1, &data.data2);
236 
237   while (*attrs) {
238     Attribute attribute;
239     SplitName(*attrs++, &attribute.namespace_uri, &attribute.name);
240     attribute.value = *attrs++;
241 
242     // Insert in sorted order.
243     auto iter = std::lower_bound(data.attributes.begin(), data.attributes.end(),
244                                  attribute);
245     data.attributes.insert(iter, std::move(attribute));
246   }
247 
248   // Move the structure into the queue (no copy).
249   parser->event_queue_.push(std::move(data));
250 }
251 
CharacterDataHandler(void * user_data,const char * s,int len)252 void XMLCALL XmlPullParser::CharacterDataHandler(void* user_data, const char* s,
253                                                  int len) {
254   XmlPullParser* parser = reinterpret_cast<XmlPullParser*>(user_data);
255 
256   parser->event_queue_.push(EventData{Event::kText, XML_GetCurrentLineNumber(parser->parser_),
257                                       parser->depth_, std::string(s, len)});
258 }
259 
EndElementHandler(void * user_data,const char * name)260 void XMLCALL XmlPullParser::EndElementHandler(void* user_data,
261                                               const char* name) {
262   XmlPullParser* parser = reinterpret_cast<XmlPullParser*>(user_data);
263 
264   EventData data = {Event::kEndElement,
265                     XML_GetCurrentLineNumber(parser->parser_),
266                     --(parser->depth_)};
267   SplitName(name, &data.data1, &data.data2);
268 
269   // Move the data into the queue (no copy).
270   parser->event_queue_.push(std::move(data));
271 }
272 
EndNamespaceHandler(void * user_data,const char * prefix)273 void XMLCALL XmlPullParser::EndNamespaceHandler(void* user_data,
274                                                 const char* prefix) {
275   XmlPullParser* parser = reinterpret_cast<XmlPullParser*>(user_data);
276 
277   parser->event_queue_.push(
278       EventData{Event::kEndNamespace, XML_GetCurrentLineNumber(parser->parser_),
279                 --(parser->depth_), prefix != nullptr ? prefix : std::string(),
280                 parser->namespace_uris_.top()});
281   parser->namespace_uris_.pop();
282 }
283 
CommentDataHandler(void * user_data,const char * comment)284 void XMLCALL XmlPullParser::CommentDataHandler(void* user_data,
285                                                const char* comment) {
286   XmlPullParser* parser = reinterpret_cast<XmlPullParser*>(user_data);
287 
288   parser->event_queue_.push(EventData{Event::kComment,
289                                       XML_GetCurrentLineNumber(parser->parser_),
290                                       parser->depth_, comment});
291 }
292 
StartCdataSectionHandler(void * user_data)293 void XMLCALL XmlPullParser::StartCdataSectionHandler(void* user_data) {
294   XmlPullParser* parser = reinterpret_cast<XmlPullParser*>(user_data);
295 
296   parser->event_queue_.push(EventData{Event::kCdataStart,
297                                       XML_GetCurrentLineNumber(parser->parser_),
298                                       parser->depth_ });
299 }
300 
EndCdataSectionHandler(void * user_data)301 void XMLCALL XmlPullParser::EndCdataSectionHandler(void* user_data) {
302   XmlPullParser* parser = reinterpret_cast<XmlPullParser*>(user_data);
303 
304   parser->event_queue_.push(EventData{Event::kCdataEnd,
305                                       XML_GetCurrentLineNumber(parser->parser_),
306                                       parser->depth_ });
307 }
308 
FindAttribute(const XmlPullParser * parser,StringPiece name)309 std::optional<StringPiece> FindAttribute(const XmlPullParser* parser, StringPiece name) {
310   auto iter = parser->FindAttribute("", name);
311   if (iter != parser->end_attributes()) {
312     return StringPiece(util::TrimWhitespace(iter->value));
313   }
314   return {};
315 }
316 
FindNonEmptyAttribute(const XmlPullParser * parser,StringPiece name)317 std::optional<StringPiece> FindNonEmptyAttribute(const XmlPullParser* parser, StringPiece name) {
318   auto iter = parser->FindAttribute("", name);
319   if (iter != parser->end_attributes()) {
320     StringPiece trimmed = util::TrimWhitespace(iter->value);
321     if (!trimmed.empty()) {
322       return trimmed;
323     }
324   }
325   return {};
326 }
327 
328 }  // namespace xml
329 }  // namespace aapt
330