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 : #include "xmlstreamwriter.h"
23 :
24 : #include <vector>
25 :
26 : #include <utf8.h>
27 :
28 : #include "global/containers.h"
29 : #include "textstream.h"
30 :
31 : #include "global/log.h"
32 :
33 : using namespace std::literals;
34 : using namespace muse;
35 :
36 : struct XmlStreamWriter::Impl {
37 : std::vector<std::string> stack;
38 : TextStream stream;
39 :
40 716650 : void putLevel()
41 : {
42 716650 : size_t level = stack.size();
43 8100844 : for (size_t i = 0; i < level * 2; ++i) {
44 7384194 : stream << ' ';
45 : }
46 716650 : }
47 : };
48 :
49 293 : XmlStreamWriter::XmlStreamWriter()
50 : {
51 293 : m_impl = new Impl();
52 293 : }
53 :
54 1956 : XmlStreamWriter::XmlStreamWriter(io::IODevice* dev)
55 : {
56 1956 : m_impl = new Impl();
57 1956 : m_impl->stream.setDevice(dev);
58 1956 : }
59 :
60 2249 : XmlStreamWriter::~XmlStreamWriter()
61 : {
62 2249 : flush();
63 2249 : delete m_impl;
64 2249 : }
65 :
66 293 : void XmlStreamWriter::setDevice(io::IODevice* dev)
67 : {
68 293 : m_impl->stream.setDevice(dev);
69 293 : }
70 :
71 4495 : void XmlStreamWriter::flush()
72 : {
73 4495 : m_impl->stream.flush();
74 4495 : }
75 :
76 2233 : void XmlStreamWriter::startDocument()
77 : {
78 2233 : m_impl->stream << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
79 2233 : }
80 :
81 293 : void XmlStreamWriter::writeDoctype(const String& type)
82 : {
83 293 : m_impl->stream << "<!DOCTYPE " << type << '>' << '\n';
84 293 : }
85 :
86 1387179 : std::string XmlStreamWriter::escapeCodePoint(const char32_t c)
87 : {
88 1387179 : switch (c) {
89 1148 : case U'<':
90 1148 : return "<"s;
91 1148 : case U'>':
92 1148 : return ">"s;
93 6 : case U'&':
94 6 : return "&"s;
95 287 : case U'\"':
96 287 : return """s;
97 1384590 : default:
98 : // ignore invalid characters in xml 1.0
99 1384590 : if ((c < 0x20 && c != 0x09 && c != 0x0A && c != 0x0D)) {
100 0 : return std::string();
101 : }
102 1384590 : return utf8::utf32to8(std::u32string_view(&c, 1));
103 : }
104 : }
105 :
106 248492 : std::string XmlStreamWriter::escapeString(const std::string_view str)
107 : {
108 248492 : std::string escaped{};
109 248492 : auto it = str.begin();
110 1635671 : while (it != str.end()) {
111 1387179 : const char32_t c = utf8::next(it, str.end());
112 1387179 : escaped += escapeCodePoint(c);
113 : }
114 :
115 248492 : return escaped;
116 0 : }
117 :
118 419854 : void XmlStreamWriter::writeValue(const Value& v)
119 : {
120 : // std::monostate, int, unsigned int, signed long int, unsigned long int, signed long long, unsigned long long,
121 : // double, const char*, std::string_view, String
122 419854 : switch (v.index()) {
123 0 : case 0:
124 0 : break;
125 155414 : case 1: m_impl->stream << int32_t{ std::get<int>(v) };
126 155414 : break;
127 0 : case 2: m_impl->stream << uint32_t{ std::get<unsigned int>(v) };
128 0 : break;
129 0 : case 3: m_impl->stream << int64_t{ std::get<signed long int>(v) };
130 0 : break;
131 2809 : case 4: m_impl->stream << uint64_t{ std::get<unsigned long int>(v) };
132 2809 : break;
133 0 : case 5: m_impl->stream << int64_t{ std::get<signed long long>(v) };
134 0 : break;
135 0 : case 6: m_impl->stream << uint64_t{ std::get<unsigned long long>(v) };
136 0 : break;
137 13139 : case 7: m_impl->stream << std::get<double>(v);
138 13139 : break;
139 12509 : case 8: m_impl->stream << escapeString(AsciiStringView(std::get<const char*>(v)));
140 12509 : break;
141 157476 : case 9: m_impl->stream << escapeString(std::get<std::string_view>(v));
142 157476 : break;
143 78507 : case 10: m_impl->stream << escapeString(std::get<String>(v).toStdString());
144 78507 : break;
145 0 : default:
146 0 : LOGI() << "index: " << v.index();
147 0 : UNREACHABLE;
148 : break;
149 : }
150 419854 : }
151 :
152 151767 : void XmlStreamWriter::startElement(const std::string_view& name, const Attributes& attrs)
153 : {
154 151767 : IF_ASSERT_FAILED(name.find(' ') == std::string_view::npos) {
155 : }
156 :
157 151767 : m_impl->putLevel();
158 151767 : m_impl->stream << '<' << name;
159 173249 : for (const Attribute& a : attrs) {
160 21482 : m_impl->stream << ' ' << a.first << '=' << '\"';
161 21482 : writeValue(a.second);
162 21482 : m_impl->stream << '\"';
163 : }
164 151767 : m_impl->stream << '>' << '\n';
165 151767 : m_impl->stack.emplace_back(name);
166 151767 : }
167 :
168 0 : void XmlStreamWriter::startElement(const String& name, const Attributes& attrs)
169 : {
170 0 : ByteArray ba = name.toAscii();
171 0 : startElement(AsciiStringView(ba.constChar(), ba.size()), attrs);
172 0 : }
173 :
174 12536 : void XmlStreamWriter::startElementRaw(const String& name)
175 : {
176 12536 : m_impl->putLevel();
177 12536 : m_impl->stream << '<' << name << '>' << '\n';
178 12536 : m_impl->stack.push_back(name.split(' ')[0].toStdString());
179 12536 : }
180 :
181 164303 : void XmlStreamWriter::endElement()
182 : {
183 164303 : m_impl->putLevel();
184 164303 : m_impl->stream << "</" << muse::takeLast(m_impl->stack) << '>' << '\n';
185 164303 : }
186 :
187 : // <element attr="value" />
188 8303 : void XmlStreamWriter::element(const std::string_view& name, const Attributes& attrs)
189 : {
190 8303 : IF_ASSERT_FAILED(name.find(' ') == std::string_view::npos) {
191 : }
192 :
193 8303 : m_impl->putLevel();
194 8303 : m_impl->stream << '<' << name;
195 21269 : for (const Attribute& a : attrs) {
196 12966 : m_impl->stream << ' ' << a.first << '=' << '\"';
197 12966 : writeValue(a.second);
198 12966 : m_impl->stream << '\"';
199 : }
200 8303 : m_impl->stream << "/>\n";
201 8303 : }
202 :
203 348027 : void XmlStreamWriter::element(const std::string_view& name, const Value& body)
204 : {
205 348027 : IF_ASSERT_FAILED(name.find(' ') == std::string_view::npos) {
206 : }
207 :
208 348027 : m_impl->putLevel();
209 348027 : m_impl->stream << '<' << name << '>';
210 348027 : writeValue(body);
211 348027 : m_impl->stream << "</" << name << '>' << '\n';
212 348027 : }
213 :
214 17756 : void XmlStreamWriter::element(const std::string_view& name, const Attributes& attrs, const Value& body)
215 : {
216 17756 : IF_ASSERT_FAILED(name.find(' ') == std::string_view::npos) {
217 : }
218 :
219 17756 : m_impl->putLevel();
220 17756 : m_impl->stream << '<' << name;
221 32218 : for (const Attribute& a : attrs) {
222 14462 : m_impl->stream << ' ' << a.first << '=' << '\"';
223 14462 : writeValue(a.second);
224 14462 : m_impl->stream << '\"';
225 : }
226 17756 : m_impl->stream << ">";
227 17756 : writeValue(body);
228 17756 : m_impl->stream << "</" << name << '>' << '\n';
229 17756 : }
230 :
231 8295 : void XmlStreamWriter::elementRaw(const String& nameWithAttributes, const Value& body)
232 : {
233 8295 : m_impl->putLevel();
234 8295 : if (body.index() == 0) {
235 3134 : m_impl->stream << '<' << nameWithAttributes << "/>\n";
236 : } else {
237 5161 : String ename(String(nameWithAttributes).split(' ')[0]);
238 5161 : m_impl->stream << '<' << nameWithAttributes << '>';
239 5161 : writeValue(body);
240 5161 : m_impl->stream << "</" << ename << '>' << '\n';
241 5161 : }
242 8295 : }
243 :
244 5662 : void XmlStreamWriter::elementStringRaw(const String& nameWithAttributes, const String& body)
245 : {
246 5662 : m_impl->putLevel();
247 5662 : if (body.isEmpty()) {
248 41 : m_impl->stream << '<' << nameWithAttributes << "/>\n";
249 : } else {
250 5621 : String ename(String(nameWithAttributes).split(' ')[0]);
251 5621 : m_impl->stream << '<' << nameWithAttributes << '>';
252 5621 : m_impl->stream << body;
253 5621 : m_impl->stream << "</" << ename << '>' << '\n';
254 5621 : }
255 5662 : }
256 :
257 1 : void XmlStreamWriter::comment(const String& text)
258 : {
259 1 : m_impl->putLevel();
260 1 : m_impl->stream << "<!-- " << text << " -->\n";
261 1 : }
|