LCOV - code coverage report
Current view: top level - src/importexport/musicxml/internal/import - musicxmlvalidation.cpp Coverage Total Hit
Test: filtered_coverage.info Lines: 100.0 % 2 2
Test Date: 2026-11-19 04:14:36

            Line data    Source code
       1              : /*
       2              :  * SPDX-License-Identifier: GPL-3.0-only
       3              :  * MuseScore-Studio-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              : 
      23              : #include "musicxmlvalidation.h"
      24              : 
      25              : #ifdef MUSICXML_NO_VALIDATION
      26              : using namespace mu;
      27              : using namespace mu::iex::musicxml;
      28              : using namespace mu::engraving;
      29              : 
      30          309 : muse::Ret MusicXmlValidation::validate(const muse::String&, const muse::ByteArray&)
      31              : {
      32          309 :     return muse::make_ok();
      33              : }
      34              : 
      35              : #else
      36              : 
      37              : #include <QAbstractMessageHandler>
      38              : #include <QXmlSchema>
      39              : #include <QXmlSchemaValidator>
      40              : #include <QDomDocument>
      41              : 
      42              : #include "engraving/dom/mscore.h"
      43              : 
      44              : #include "musicxmlsupport.h"
      45              : 
      46              : #include "log.h"
      47              : 
      48              : using namespace mu;
      49              : using namespace mu::iex::musicxml;
      50              : using namespace mu::engraving;
      51              : 
      52              : //---------------------------------------------------------
      53              : //   ValidatorMessageHandler
      54              : //---------------------------------------------------------
      55              : 
      56              : /**
      57              :  Message handler for the MusicXML schema validator QXmlSchemaValidator.
      58              :  */
      59              : 
      60              : class ValidatorMessageHandler : public QAbstractMessageHandler
      61              : {
      62              : public:
      63              :     ValidatorMessageHandler()
      64              :         : QAbstractMessageHandler(0) {}
      65              :     QString getErrors() const { return m_errors; }
      66              : protected:
      67              :     virtual void handleMessage(QtMsgType type, const QString& description, const QUrl& identifier, const QSourceLocation& sourceLocation);
      68              : private:
      69              :     QString m_errors;
      70              : };
      71              : 
      72              : void ValidatorMessageHandler::handleMessage(QtMsgType type, const QString& description,
      73              :                                             const QUrl& /* identifier */, const QSourceLocation& sourceLocation)
      74              : {
      75              :     // convert description from html to text
      76              :     QDomDocument desc;
      77              :     QString contentError;
      78              :     int contentLine;
      79              :     int contentColumn;
      80              :     if (!desc.setContent(description, false, &contentError, &contentLine,
      81              :                          &contentColumn)) {
      82              :         LOGD("ValidatorMessageHandler: could not parse validation error line %d column %d: %s",
      83              :              contentLine, contentColumn, qPrintable(contentError));
      84              :         return;
      85              :     }
      86              : 
      87              :     QDomElement e = desc.documentElement();
      88              :     if (e.tagName() != "html") {
      89              :         LOGD("ValidatorMessageHandler: description is not html");
      90              :         return;
      91              :     }
      92              : 
      93              :     QString typeStr;
      94              :     switch (type) {
      95              :     case 0:  typeStr = muse::qtrc("iex_musicxml", "Debug message:");
      96              :         break;
      97              :     case 1:  typeStr = muse::qtrc("iex_musicxml", "Warning:");
      98              :         break;
      99              :     case 2:  typeStr = muse::qtrc("iex_musicxml", "Critical error:");
     100              :         break;
     101              :     case 3:  typeStr = muse::qtrc("iex_musicxml", "Fatal error:");
     102              :         break;
     103              :     default: typeStr = muse::qtrc("iex_musicxml", "Unknown error:");
     104              :         break;
     105              :     }
     106              : 
     107              :     QString errorStr = typeStr + " " + errorStringWithLocation(sourceLocation.byteOffset(), e.text());
     108              : 
     109              :     // append error, separated by newline if necessary
     110              :     if (!m_errors.isEmpty()) {
     111              :         m_errors += "\n";
     112              :     }
     113              :     m_errors += errorStr;
     114              : }
     115              : 
     116              : //---------------------------------------------------------
     117              : //   initMusicXmlSchema
     118              : //    return false on error
     119              : //---------------------------------------------------------
     120              : 
     121              : static bool initMusicXmlSchema(QXmlSchema& schema)
     122              : {
     123              :     // read the MusicXML schema from the application resources
     124              :     QFile schemaFile(":/schema/musicxml.xsd");
     125              :     if (!schemaFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
     126              :         LOGE("initMusicXmlSchema() could not open resource musicxml.xsd");
     127              :         return false;
     128              :     }
     129              : 
     130              :     // copy the schema into a QByteArray and fixup xs:imports,
     131              :     // using a path to the application resources instead of to www.musicxml.org
     132              :     // to prevent downloading from the net
     133              :     QByteArray schemaBa;
     134              :     QTextStream schemaStream(&schemaFile);
     135              :     while (!schemaStream.atEnd()) {
     136              :         QString line = schemaStream.readLine();
     137              :         if (line.contains("xs:import")) {
     138              :             line.replace("http://www.musicxml.org/xsd", "qrc:///schema");
     139              :         }
     140              :         schemaBa += line.toUtf8();
     141              :         schemaBa += "\n";
     142              :     }
     143              : 
     144              :     // load and validate the schema
     145              :     schema.load(schemaBa);
     146              :     if (!schema.isValid()) {
     147              :         LOGE("initMusicXmlSchema() internal error: MusicXML schema is invalid");
     148              :         return false;
     149              :     }
     150              : 
     151              :     return true;
     152              : }
     153              : 
     154              : muse::Ret MusicXmlValidation::validate(const String& name, const muse::ByteArray& data)
     155              : {
     156              :     //QElapsedTimer t;
     157              :     //t.start();
     158              : 
     159              :     // initialize the schema
     160              :     ValidatorMessageHandler messageHandler;
     161              :     QXmlSchema schema;
     162              :     schema.setMessageHandler(&messageHandler);
     163              :     if (!initMusicXmlSchema(schema)) {
     164              :         return make_ret(Err::FileBadFormat, muse::io::path_t(name));      // appropriate error message has been printed by initMusicXmlSchema
     165              :     }
     166              :     // validate the data
     167              :     QXmlSchemaValidator validator(schema);
     168              :     const QByteArray qdata = data.toQByteArrayNoCopy();
     169              :     bool valid = validator.validate(qdata, QUrl::fromLocalFile(name));
     170              :     //LOGD("Validation time elapsed: %d ms", t.elapsed());
     171              : 
     172              :     if (!valid) {
     173              :         LOGD("importMusicXml() file '%s' is not a valid MusicXML file", muPrintable(name));
     174              :         if (MScore::noGui) {
     175              :             return muse::make_ok();         // might as well try anyhow in converter mode
     176              :         }
     177              : 
     178              :         //! NOTE The user decides whether to load the file anyway, then it is loaded again in the force mode, without the validation.
     179              :         //! The caller tells the user about it, the errors are the details
     180              :         return make_ret(Err::FileCorrupted, muse::String::fromQString(messageHandler.getErrors()));
     181              :     }
     182              : 
     183              :     return muse::make_ok();
     184              : }
     185              : 
     186              : #endif // MUSICXML_NO_VALIDATION
        

Generated by: LCOV version 2.0-1