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