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 #ifndef AAPT_XML_PULL_PARSER_H
18 #define AAPT_XML_PULL_PARSER_H
19 
20 #include <expat.h>
21 
22 #include <algorithm>
23 #include <istream>
24 #include <ostream>
25 #include <queue>
26 #include <stack>
27 #include <string>
28 #include <vector>
29 
30 #include "android-base/macros.h"
31 #include "androidfw/StringPiece.h"
32 
33 #include "Resource.h"
34 #include "io/Io.h"
35 #include "process/IResourceTableConsumer.h"
36 #include "xml/XmlUtil.h"
37 
38 namespace aapt {
39 namespace xml {
40 
41 class XmlPullParser : public IPackageDeclStack {
42  public:
43   enum class Event {
44     kBadDocument,
45     kStartDocument,
46     kEndDocument,
47 
48     kStartNamespace,
49     kEndNamespace,
50     kStartElement,
51     kEndElement,
52     kText,
53     kComment,
54     kCdataStart,
55     kCdataEnd,
56   };
57 
58   /**
59    * Skips to the next direct descendant node of the given start_depth,
60    * skipping namespace nodes.
61    *
62    * When NextChildNode() returns true, you can expect Comments, Text, and
63    * StartElement events.
64    */
65   static bool NextChildNode(XmlPullParser* parser, size_t start_depth);
66   static bool SkipCurrentElement(XmlPullParser* parser);
67   static bool IsGoodEvent(Event event);
68 
69   explicit XmlPullParser(io::InputStream* in);
70   ~XmlPullParser();
71 
72   /**
73    * Returns the current event that is being processed.
74    */
75   Event event() const;
76 
77   const std::string& error() const;
78 
79   /**
80    * Note, unlike XmlPullParser, the first call to next() will return
81    * StartElement of the first element.
82    */
83   Event Next();
84 
85   //
86   // These are available for all nodes.
87   //
88 
89   const std::string& comment() const;
90   size_t line_number() const;
91   size_t depth() const;
92 
93   /**
94    * Returns the character data for a Text event.
95    */
96   const std::string& text() const;
97 
98   //
99   // Namespace prefix and URI are available for StartNamespace and EndNamespace.
100   //
101 
102   const std::string& namespace_prefix() const;
103   const std::string& namespace_uri() const;
104 
105   //
106   // These are available for StartElement and EndElement.
107   //
108 
109   const std::string& element_namespace() const;
110   const std::string& element_name() const;
111 
112   /*
113    * Uses the current stack of namespaces to resolve the package. Eg:
114    * xmlns:app = "http://schemas.android.com/apk/res/com.android.app"
115    * ...
116    * android:text="@app:string/message"
117    *
118    * In this case, 'app' will be converted to 'com.android.app'.
119    *
120    * If xmlns:app="http://schemas.android.com/apk/res-auto", then
121    * 'package' will be set to 'defaultPackage'.
122    */
123   std::optional<ExtractedPackage> TransformPackageAlias(android::StringPiece alias) const override;
124 
125   struct PackageDecl {
126     std::string prefix;
127     ExtractedPackage package;
128   };
129 
130   const std::vector<PackageDecl>& package_decls() const;
131 
132   //
133   // Remaining methods are for retrieving information about attributes
134   // associated with a StartElement.
135   //
136   // Attributes must be in sorted order (according to the less than operator
137   // of struct Attribute).
138   //
139 
140   struct Attribute {
141     std::string namespace_uri;
142     std::string name;
143     std::string value;
144 
145     int compare(const Attribute& rhs) const;
146     bool operator<(const Attribute& rhs) const;
147     bool operator==(const Attribute& rhs) const;
148     bool operator!=(const Attribute& rhs) const;
149   };
150 
151   using const_iterator = std::vector<Attribute>::const_iterator;
152 
153   const_iterator begin_attributes() const;
154   const_iterator end_attributes() const;
155   size_t attribute_count() const;
156   const_iterator FindAttribute(android::StringPiece namespace_uri, android::StringPiece name) const;
157 
158  private:
159   DISALLOW_COPY_AND_ASSIGN(XmlPullParser);
160 
161   static void XMLCALL StartNamespaceHandler(void* user_data, const char* prefix,
162                                             const char* uri);
163   static void XMLCALL StartElementHandler(void* user_data, const char* name,
164                                           const char** attrs);
165   static void XMLCALL CharacterDataHandler(void* user_data, const char* s,
166                                            int len);
167   static void XMLCALL EndElementHandler(void* user_data, const char* name);
168   static void XMLCALL EndNamespaceHandler(void* user_data, const char* prefix);
169   static void XMLCALL CommentDataHandler(void* user_data, const char* comment);
170   static void XMLCALL StartCdataSectionHandler(void* user_data);
171   static void XMLCALL EndCdataSectionHandler(void* user_data);
172 
173   struct EventData {
174     Event event;
175     size_t line_number;
176     size_t depth;
177     std::string data1;
178     std::string data2;
179     std::vector<Attribute> attributes;
180   };
181 
182   io::InputStream* in_;
183   XML_Parser parser_;
184   std::queue<EventData> event_queue_;
185   std::string error_;
186   const std::string empty_;
187   size_t depth_;
188   std::stack<std::string> namespace_uris_;
189   std::vector<PackageDecl> package_aliases_;
190 };
191 
192 /**
193  * Finds the attribute in the current element within the global namespace.
194  */
195 std::optional<android::StringPiece> FindAttribute(const XmlPullParser* parser,
196                                                   android::StringPiece name);
197 
198 /**
199  * Finds the attribute in the current element within the global namespace. The
200  * attribute's value
201  * must not be the empty string.
202  */
203 std::optional<android::StringPiece> FindNonEmptyAttribute(const XmlPullParser* parser,
204                                                           android::StringPiece name);
205 
206 //
207 // Implementation
208 //
209 
210 inline ::std::ostream& operator<<(::std::ostream& out,
211                                   XmlPullParser::Event event) {
212   switch (event) {
213     case XmlPullParser::Event::kBadDocument:
214       return out << "BadDocument";
215     case XmlPullParser::Event::kStartDocument:
216       return out << "StartDocument";
217     case XmlPullParser::Event::kEndDocument:
218       return out << "EndDocument";
219     case XmlPullParser::Event::kStartNamespace:
220       return out << "StartNamespace";
221     case XmlPullParser::Event::kEndNamespace:
222       return out << "EndNamespace";
223     case XmlPullParser::Event::kStartElement:
224       return out << "StartElement";
225     case XmlPullParser::Event::kEndElement:
226       return out << "EndElement";
227     case XmlPullParser::Event::kText:
228       return out << "Text";
229     case XmlPullParser::Event::kComment:
230       return out << "Comment";
231     case XmlPullParser::Event::kCdataStart:
232       return out << "CdataStart";
233     case XmlPullParser::Event::kCdataEnd:
234       return out << "CdataEnd";
235   }
236   return out;
237 }
238 
NextChildNode(XmlPullParser * parser,size_t start_depth)239 inline bool XmlPullParser::NextChildNode(XmlPullParser* parser, size_t start_depth) {
240   Event event;
241 
242   // First get back to the start depth.
243   while (IsGoodEvent(event = parser->Next()) && parser->depth() > start_depth + 1) {
244   }
245 
246   // Now look for the first good node.
247   while ((event != Event::kEndElement || parser->depth() > start_depth) && IsGoodEvent(event)) {
248     switch (event) {
249       case Event::kText:
250       case Event::kComment:
251       case Event::kStartElement:
252       case Event::kCdataStart:
253       case Event::kCdataEnd:
254         return true;
255       default:
256         break;
257     }
258     event = parser->Next();
259   }
260   return false;
261 }
262 
SkipCurrentElement(XmlPullParser * parser)263 inline bool XmlPullParser::SkipCurrentElement(XmlPullParser* parser) {
264   int depth = 1;
265   while (depth > 0) {
266     switch (parser->Next()) {
267       case Event::kEndDocument:
268         return true;
269       case Event::kBadDocument:
270         return false;
271       case Event::kStartElement:
272         depth++;
273         break;
274       case Event::kEndElement:
275         depth--;
276         break;
277       default:
278         break;
279     }
280   }
281   return true;
282 }
283 
IsGoodEvent(XmlPullParser::Event event)284 inline bool XmlPullParser::IsGoodEvent(XmlPullParser::Event event) {
285   return event != Event::kBadDocument && event != Event::kEndDocument;
286 }
287 
compare(const Attribute & rhs)288 inline int XmlPullParser::Attribute::compare(const Attribute& rhs) const {
289   int cmp = namespace_uri.compare(rhs.namespace_uri);
290   if (cmp != 0) return cmp;
291   return name.compare(rhs.name);
292 }
293 
294 inline bool XmlPullParser::Attribute::operator<(const Attribute& rhs) const {
295   return compare(rhs) < 0;
296 }
297 
298 inline bool XmlPullParser::Attribute::operator==(const Attribute& rhs) const {
299   return compare(rhs) == 0;
300 }
301 
302 inline bool XmlPullParser::Attribute::operator!=(const Attribute& rhs) const {
303   return compare(rhs) != 0;
304 }
305 
FindAttribute(android::StringPiece namespace_uri,android::StringPiece name)306 inline XmlPullParser::const_iterator XmlPullParser::FindAttribute(
307     android::StringPiece namespace_uri, android::StringPiece name) const {
308   const auto end_iter = end_attributes();
309   const auto iter = std::lower_bound(
310       begin_attributes(), end_iter,
311       std::pair<android::StringPiece, android::StringPiece>(namespace_uri, name),
312       [](const Attribute& attr,
313          const std::pair<android::StringPiece, android::StringPiece>& rhs) -> bool {
314         int cmp = attr.namespace_uri.compare(
315             0, attr.namespace_uri.size(), rhs.first.data(), rhs.first.size());
316         if (cmp < 0) return true;
317         if (cmp > 0) return false;
318         cmp = attr.name.compare(0, attr.name.size(), rhs.second.data(),
319                                 rhs.second.size());
320         if (cmp < 0) return true;
321         return false;
322       });
323 
324   if (iter != end_iter && namespace_uri == iter->namespace_uri &&
325       name == iter->name) {
326     return iter;
327   }
328   return end_iter;
329 }
330 
331 }  // namespace xml
332 }  // namespace aapt
333 
334 #endif  // AAPT_XML_PULL_PARSER_H
335