Line data Source code
1 : /*
2 : * SPDX-License-Identifier: GPL-3.0-only
3 : * MuseScore-CLA-applies
4 : *
5 : * MuseScore Studio
6 : * Music Composition & Notation
7 : *
8 : * Copyright (C) 2021 MuseScore Limited and others
9 : *
10 : * This program is free software: you can redistribute it and/or modify
11 : * it under the terms of the GNU General Public License version 3 as
12 : * published by the Free Software Foundation.
13 : *
14 : * This program is distributed in the hope that it will be useful,
15 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 : * GNU General Public License for more details.
18 : *
19 : * You should have received a copy of the GNU General Public License
20 : * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 : */
22 : #ifndef MUSE_GLOBAL_XMLSTREAMREADER_H
23 : #define MUSE_GLOBAL_XMLSTREAMREADER_H
24 :
25 : #include <vector>
26 : #include <map>
27 :
28 : #include "io/iodevice.h"
29 : #include "types/bytearray.h"
30 : #include "types/string.h"
31 :
32 : #ifndef NO_QT_SUPPORT
33 : #include <QByteArray>
34 : #endif
35 :
36 : namespace muse {
37 : class XmlStreamReader
38 : {
39 : public:
40 :
41 : enum TokenType {
42 : NoToken = 0,
43 : Invalid,
44 : StartDocument,
45 : EndDocument,
46 : StartElement,
47 : EndElement,
48 : Characters,
49 : Comment,
50 : DTD,
51 : Unknown
52 : };
53 :
54 : enum Error {
55 : NoError,
56 : UnexpectedElementError,
57 : CustomError,
58 : NotWellFormedError,
59 : PrematureEndOfDocumentError
60 : };
61 :
62 : struct Attribute
63 : {
64 : AsciiStringView name;
65 : String value;
66 : };
67 :
68 : XmlStreamReader();
69 : explicit XmlStreamReader(io::IODevice* device);
70 : explicit XmlStreamReader(const ByteArray& data);
71 : #ifndef NO_QT_SUPPORT
72 : explicit XmlStreamReader(const QByteArray& data);
73 : #endif
74 : virtual ~XmlStreamReader();
75 :
76 : XmlStreamReader(const XmlStreamReader&) = delete;
77 : XmlStreamReader& operator=(const XmlStreamReader&) = delete;
78 :
79 : void setData(const ByteArray& data);
80 :
81 : bool readNextStartElement();
82 : bool atEnd() const;
83 : void skipCurrentElement();
84 : TokenType readNext();
85 : TokenType tokenType() const;
86 : AsciiStringView tokenString() const;
87 :
88 : inline bool isStartDocument() const { return tokenType() == StartDocument; }
89 : inline bool isEndDocument() const { return tokenType() == EndDocument; }
90 2514297 : inline bool isStartElement() const { return tokenType() == StartElement; }
91 2157602 : inline bool isEndElement() const { return tokenType() == EndElement; }
92 : inline bool isCharacters() const { return tokenType() == Characters; }
93 : bool isWhitespace() const;
94 :
95 : AsciiStringView name() const;
96 :
97 : bool hasAttribute(const char* name) const;
98 : String attribute(const char* name) const;
99 : String attribute(const char* name, const String& def) const;
100 : AsciiStringView asciiAttribute(const char* name) const;
101 : AsciiStringView asciiAttribute(const char* name, const AsciiStringView& def) const;
102 : int intAttribute(const char* name) const;
103 : int intAttribute(const char* name, int def) const;
104 : double doubleAttribute(const char* name) const;
105 : double doubleAttribute(const char* name, double def) const;
106 : std::vector<Attribute> attributes() const;
107 :
108 : bool noChildren() const;
109 :
110 : String readBody() const;
111 :
112 : String text() const;
113 : AsciiStringView asciiText() const;
114 : String readText();
115 : AsciiStringView readAsciiText();
116 : int readInt(bool* ok = nullptr, int base = 10);
117 : double readDouble(bool* ok = nullptr);
118 :
119 : int64_t byteOffset() const;
120 : Error error() const;
121 : bool isError() const;
122 : String errorString() const;
123 : void raiseError(const String& message);
124 :
125 : private:
126 : struct Xml;
127 :
128 : void tryParseEntity(Xml* xml);
129 : String nodeValue(Xml* xml) const;
130 :
131 : Xml* m_xml = nullptr;
132 : TokenType m_token = TokenType::NoToken;
133 :
134 : std::map<String, String> m_entities;
135 : };
136 : }
137 :
138 : #endif // MUSE_GLOBAL_XMLSTREAMREADER_H
|