-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
Copy pathimportxml.cpp
379 lines (318 loc) · 13.6 KB
/
importxml.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
//=============================================================================
// MusE Score
// Linux Music Score Editor
//
// Copyright (C) 2002-2015 Werner Schweer and others
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 2.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//=============================================================================
/**
MusicXML import.
*/
#include "thirdparty/qzip/qzipreader_p.h"
#include "importmxml.h"
namespace Ms {
//---------------------------------------------------------
// tupletAssert -- check assertions for tuplet handling
//---------------------------------------------------------
/**
Check assertions for tuplet handling. If this fails, MusicXML
import will almost certainly break in non-obvious ways.
Should never happen, thus it is OK to quit the application.
*/
static void tupletAssert()
{
if (!(int(TDuration::DurationType::V_BREVE) == int(TDuration::DurationType::V_LONG) + 1
&& int(TDuration::DurationType::V_WHOLE) == int(TDuration::DurationType::V_BREVE) + 1
&& int(TDuration::DurationType::V_HALF) == int(TDuration::DurationType::V_WHOLE) + 1
&& int(TDuration::DurationType::V_QUARTER) == int(TDuration::DurationType::V_HALF) + 1
&& int(TDuration::DurationType::V_EIGHTH) == int(TDuration::DurationType::V_QUARTER) + 1
&& int(TDuration::DurationType::V_16TH) == int(TDuration::DurationType::V_EIGHTH) + 1
&& int(TDuration::DurationType::V_32ND) == int(TDuration::DurationType::V_16TH) + 1
&& int(TDuration::DurationType::V_64TH) == int(TDuration::DurationType::V_32ND) + 1
&& int(TDuration::DurationType::V_128TH) == int(TDuration::DurationType::V_64TH) + 1
&& int(TDuration::DurationType::V_256TH) == int(TDuration::DurationType::V_128TH) + 1
&& int(TDuration::DurationType::V_512TH) == int(TDuration::DurationType::V_256TH) + 1
&& int(TDuration::DurationType::V_1024TH) == int(TDuration::DurationType::V_512TH) + 1
)) {
qFatal("tupletAssert() failed");
}
}
//---------------------------------------------------------
// initMusicXmlSchema
// return false on error
//---------------------------------------------------------
static bool initMusicXmlSchema(QXmlSchema& schema)
{
// read the MusicXML schema from the application resources
QFile schemaFile(":/schema/musicxml.xsd");
if (!schemaFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
qDebug("initMusicXmlSchema() could not open resource musicxml.xsd");
MScore::lastError = QObject::tr("Internal error: Could not open resource musicxml.xsd\n");
return false;
}
// copy the schema into a QByteArray and fixup xs:imports,
// using a path to the application resources instead of to www.musicxml.org
// to prevent downloading from the net
QByteArray schemaBa;
QTextStream schemaStream(&schemaFile);
while (!schemaStream.atEnd()) {
QString line = schemaStream.readLine();
if (line.contains("xs:import"))
line.replace("http://www.musicxml.org/xsd", "qrc:///schema");
schemaBa += line.toUtf8();
schemaBa += "\n";
}
// load and validate the schema
schema.load(schemaBa);
if (!schema.isValid()) {
qDebug("initMusicXmlSchema() internal error: MusicXML schema is invalid");
MScore::lastError = QObject::tr("Internal error: MusicXML schema is invalid\n");
return false;
}
return true;
}
//---------------------------------------------------------
// musicXMLValidationErrorDialog
//---------------------------------------------------------
/**
Show a dialog displaying the MusicXML validation error(s)
and asks the user if he wants to try to load the file anyway.
Return QMessageBox::Yes (try anyway) or QMessageBox::No (don't)
*/
static int musicXMLValidationErrorDialog(QString text, QString detailedText)
{
QMessageBox errorDialog;
errorDialog.setIcon(QMessageBox::Question);
errorDialog.setText(text);
errorDialog.setInformativeText(QObject::tr("Do you want to try to load this file anyway?"));
errorDialog.setDetailedText(detailedText);
errorDialog.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
errorDialog.setDefaultButton(QMessageBox::No);
return errorDialog.exec();
}
//---------------------------------------------------------
// extractRootfile
//---------------------------------------------------------
/**
Extract rootfile from compressed MusicXML file \a qf, return true if OK and false on error.
*/
static bool extractRootfile(QFile* qf, QByteArray& data)
{
MQZipReader f(qf->fileName());
data = f.fileData("META-INF/container.xml");
QDomDocument container;
int line, column;
QString err;
if (!container.setContent(data, false, &err, &line, &column)) {
MScore::lastError = QObject::tr("Error reading container.xml at line %1 column %2: %3\n").arg(line).arg(column).arg(err);
return false;
}
// extract first rootfile
QString rootfile = "";
for (QDomElement e = container.documentElement(); !e.isNull(); e = e.nextSiblingElement()) {
if (e.tagName() == "container") {
for (QDomElement ee = e.firstChildElement(); !ee.isNull(); ee = ee.nextSiblingElement()) {
if (ee.tagName() == "rootfiles") {
for (QDomElement eee = ee.firstChildElement(); !eee.isNull(); eee = eee.nextSiblingElement()) {
if (eee.tagName() == "rootfile") {
if (rootfile == "")
rootfile = eee.attribute(QString("full-path"));
}
else
domError(eee);
}
}
else
domError(ee);
}
}
else
domError(e);
}
if (rootfile == "") {
qDebug("can't find rootfile in: %s", qPrintable(qf->fileName()));
MScore::lastError = QObject::tr("Can't find rootfile\n%1").arg(qf->fileName());
return false;
}
// read the rootfile
data = f.fileData(rootfile);
return true;
}
//---------------------------------------------------------
// doValidate
//---------------------------------------------------------
/**
Validate MusicXML data from file \a name contained in QIODevice \a dev.
*/
static Score::FileError doValidate(const QString& name, QIODevice* dev)
{
//QElapsedTimer t;
//t.start();
// initialize the schema
ValidatorMessageHandler messageHandler;
QXmlSchema schema;
schema.setMessageHandler(&messageHandler);
if (!initMusicXmlSchema(schema))
return Score::FileError::FILE_BAD_FORMAT; // appropriate error message has been printed by initMusicXmlSchema
// validate the data
QXmlSchemaValidator validator(schema);
bool valid = validator.validate(dev, QUrl::fromLocalFile(name));
//qDebug("Validation time elapsed: %d ms", t.elapsed());
if (!valid) {
qDebug("importMusicXml() file '%s' is not a valid MusicXML file", qPrintable(name));
MScore::lastError = QObject::tr("File '%1' is not a valid MusicXML file").arg(name);
if (MScore::noGui)
return Score::FileError::FILE_NO_ERROR; // might as well try anyhow in converter mode
if (musicXMLValidationErrorDialog(MScore::lastError, messageHandler.getErrors()) != QMessageBox::Yes)
return Score::FileError::FILE_USER_ABORT;
}
// return OK
return Score::FileError::FILE_NO_ERROR;
}
//---------------------------------------------------------
// doValidateAndImport
//---------------------------------------------------------
/**
Validate and import MusicXML data from file \a name contained in QIODevice \a dev into score \a score.
*/
static Score::FileError doValidateAndImport(Score* score, const QString& name, QIODevice* dev)
{
// verify tuplet TDuration::DurationType dependencies
tupletAssert();
// validate the file
Score::FileError res;
res = doValidate(name, dev);
if (res != Score::FileError::FILE_NO_ERROR)
return res;
// actually do the import
importMusicXMLfromBuffer(score, name, dev);
//qDebug("importMusicXml() return %d", int(res));
return res;
}
//---------------------------------------------------------
// importMusicXml
// return Score::FileError::FILE_* errors
//---------------------------------------------------------
/**
Import MusicXML file \a name into the Score.
*/
Score::FileError importMusicXml(MasterScore* score, QIODevice* dev, const QString& name)
{
ScoreLoad sl; // suppress warnings for undo push/pop
if (!dev->open(QIODevice::ReadOnly)) {
qDebug("importMusicXml() could not open MusicXML file '%s'", qPrintable(name));
MScore::lastError = QObject::tr("Could not open MusicXML file\n%1").arg(name);
return Score::FileError::FILE_OPEN_ERROR;
}
// and import it
return doValidateAndImport(score, name, dev);
}
Score::FileError importMusicXml(MasterScore* score, const QString& name) {
ScoreLoad sl; // suppress warnings for undo push/pop
//qDebug("importMusicXml(%p, %s)", score, qPrintable(name));
// open the MusicXML file
QFile xmlFile(name);
if (!xmlFile.exists())
return Score::FileError::FILE_NOT_FOUND;
if (!xmlFile.open(QIODevice::ReadOnly)) {
qDebug("importMusicXml() could not open MusicXML file '%s'", qPrintable(name));
MScore::lastError = QObject::tr("Could not open MusicXML file\n%1").arg(name);
return Score::FileError::FILE_OPEN_ERROR;
}
// and import it
return doValidateAndImport(score, name, &xmlFile);
}
//---------------------------------------------------------
// importCompressedMusicXml
// return false on error
//---------------------------------------------------------
/**
Import compressed MusicXML file \a name into the Score.
*/
Score::FileError importCompressedMusicXml(MasterScore* score, const QString& name)
{
//qDebug("importCompressedMusicXml(%p, %s)", score, qPrintable(name));
// open the compressed MusicXML file
QFile mxlFile(name);
if (!mxlFile.exists())
return Score::FileError::FILE_NOT_FOUND;
if (!mxlFile.open(QIODevice::ReadOnly)) {
qDebug("importCompressedMusicXml() could not open compressed MusicXML file '%s'", qPrintable(name));
MScore::lastError = QObject::tr("Could not open compressed MusicXML file\n%1").arg(name);
return Score::FileError::FILE_OPEN_ERROR;
}
// extract the root file
QByteArray data;
if (!extractRootfile(&mxlFile, data))
return Score::FileError::FILE_BAD_FORMAT; // appropriate error message has been printed by extractRootfile
QBuffer buffer(&data);
buffer.open(QIODevice::ReadOnly);
// and import it
return doValidateAndImport(score, name, &buffer);
}
//---------------------------------------------------------
// VoiceDesc
//---------------------------------------------------------
// TODO: move somewhere else
VoiceDesc::VoiceDesc() : _staff(-1), _voice(-1), _overlaps(false)
{
for (int i = 0; i < MAX_STAVES; ++i) {
_chordRests[i] = 0;
_staffAlloc[i] = -1;
_voices[i] = -1;
}
}
void VoiceDesc::incrChordRests(int s)
{
if (0 <= s && s < MAX_STAVES)
_chordRests[s]++;
}
int VoiceDesc::numberChordRests() const
{
int res = 0;
for (int i = 0; i < MAX_STAVES; ++i)
res += _chordRests[i];
return res;
}
int VoiceDesc::preferredStaff() const
{
int max = 0;
int res = 0;
for (int i = 0; i < MAX_STAVES; ++i)
if (_chordRests[i] > max) {
max = _chordRests[i];
res = i;
}
return res;
}
QString VoiceDesc::toString() const
{
QString res = "[";
for (int i = 0; i < MAX_STAVES; ++i)
res += QString(" %1").arg(_chordRests[i]);
res += QString(" ] overlaps %1").arg(_overlaps);
if (_overlaps) {
res += " staffAlloc [";
for (int i = 0; i < MAX_STAVES; ++i)
res += QString(" %1").arg(_staffAlloc[i]);
res += " ] voices [";
for (int i = 0; i < MAX_STAVES; ++i)
res += QString(" %1").arg(_voices[i]);
res += " ]";
}
else
res += QString(" staff %1 voice %2").arg(_staff + 1).arg(_voice + 1);
return res;
}
} // namespace Ms