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 
20 #include "XmlPullParser.h"
21 
22 #include <istream>
23 #include <libexpat/expat.h>
24 #include <queue>
25 #include <stack>
26 #include <string>
27 #include <vector>
28 
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 
37     Event getEvent() const override;
38     const std::string& getLastError() const override ;
39     Event next() override ;
40 
41     const std::u16string& getComment() const override;
42     size_t getLineNumber() const override;
43     size_t getDepth() const override;
44 
45     const std::u16string& getText() const override;
46 
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;
51 
52 
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;
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;
86     std::vector<std::pair<std::u16string, std::u16string>> mPackageAliases;
87 };
88 
89 } // namespace aapt
90 
91 #endif // AAPT_SOURCE_XML_PULL_PARSER_H
92