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