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