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_SOURCE_XML_PULL_PARSER_H |
| 18 | #define AAPT_SOURCE_XML_PULL_PARSER_H |
| 19 | |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 20 | #include "XmlPullParser.h" |
| 21 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 22 | #include <istream> |
| 23 | #include <libexpat/expat.h> |
| 24 | #include <queue> |
| 25 | #include <stack> |
| 26 | #include <string> |
| 27 | #include <vector> |
| 28 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 29 | namespace aapt { |
| 30 | |
| 31 | class SourceXmlPullParser : public XmlPullParser { |
| 32 | public: |
| 33 | SourceXmlPullParser(std::istream& in); |
| 34 | SourceXmlPullParser(const SourceXmlPullParser& rhs) = delete; |
| 35 | ~SourceXmlPullParser(); |
| 36 | |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 37 | Event getEvent() const override; |
| 38 | const std::string& getLastError() const override ; |
| 39 | Event next() override ; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 40 | |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 41 | const std::u16string& getComment() const override; |
| 42 | size_t getLineNumber() const override; |
| 43 | size_t getDepth() const override; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 44 | |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 45 | const std::u16string& getText() const override; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 46 | |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 47 | const std::u16string& getNamespacePrefix() const override; |
| 48 | const std::u16string& getNamespaceUri() const override; |
| 49 | bool applyPackageAlias(std::u16string* package, |
| 50 | const std::u16string& defaultPackage) const override; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 51 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 52 | |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 53 | const std::u16string& getElementNamespace() const override; |
| 54 | const std::u16string& getElementName() const override; |
| 55 | |
| 56 | const_iterator beginAttributes() const override; |
| 57 | const_iterator endAttributes() const override; |
| 58 | size_t getAttributeCount() const override; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 59 | |
| 60 | private: |
| 61 | static void XMLCALL startNamespaceHandler(void* userData, const char* prefix, const char* uri); |
| 62 | static void XMLCALL startElementHandler(void* userData, const char* name, const char** attrs); |
| 63 | static void XMLCALL characterDataHandler(void* userData, const char* s, int len); |
| 64 | static void XMLCALL endElementHandler(void* userData, const char* name); |
| 65 | static void XMLCALL endNamespaceHandler(void* userData, const char* prefix); |
| 66 | static void XMLCALL commentDataHandler(void* userData, const char* comment); |
| 67 | |
| 68 | struct EventData { |
| 69 | Event event; |
| 70 | size_t lineNumber; |
| 71 | size_t depth; |
| 72 | std::u16string data1; |
| 73 | std::u16string data2; |
| 74 | std::u16string comment; |
| 75 | std::vector<Attribute> attributes; |
| 76 | }; |
| 77 | |
| 78 | std::istream& mIn; |
| 79 | XML_Parser mParser; |
| 80 | char mBuffer[16384]; |
| 81 | std::queue<EventData> mEventQueue; |
| 82 | std::string mLastError; |
| 83 | const std::u16string mEmpty; |
| 84 | size_t mDepth; |
| 85 | std::stack<std::u16string> mNamespaceUris; |
Adam Lesinski | 24aad16 | 2015-04-24 19:19:30 -0700 | [diff] [blame] | 86 | std::vector<std::pair<std::u16string, std::u16string>> mPackageAliases; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 87 | }; |
| 88 | |
| 89 | } // namespace aapt |
| 90 | |
| 91 | #endif // AAPT_SOURCE_XML_PULL_PARSER_H |