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