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; }
int XMLQtInterface::isValid() const { QXmlSchema schema; if(_schemaName.length() > 0) schema.load( QUrl::fromLocalFile((QString::fromStdString(_schemaName))) ); if ( schema.isValid() ) { QXmlSchemaValidator validator( schema ); if ( validator.validate( _fileData ) ) return 1; else { INFO("XMLQtInterface::isValid(): XML file %s is invalid (in reference to schema %s).", _fileName.toStdString().c_str(), _schemaName.c_str()); } } else { // The following validator (without constructor arguments) automatically // searches for the xsd given in the xml file. QXmlSchemaValidator validator; if ( validator.validate( _fileData ) ) return 1; else { INFO("XMLQtInterface::isValid(): XML file %s is invalid (in reference to its schema).", _fileName.toStdString().c_str()); } } return 0; }
//! [3] void MainWindow::validate() { const QByteArray schemaData = schemaView->toPlainText().toUtf8(); const QByteArray instanceData = instanceEdit->toPlainText().toUtf8(); MessageHandler messageHandler; QXmlSchema schema; schema.setMessageHandler(&messageHandler); schema.load(schemaData); bool errorOccurred = false; if (!schema.isValid()) { errorOccurred = true; } else { QXmlSchemaValidator validator(schema); if (!validator.validate(instanceData)) errorOccurred = true; } if (errorOccurred) { validationStatus->setText(messageHandler.statusMessage()); moveCursor(messageHandler.line(), messageHandler.column()); } else { validationStatus->setText(tr("validation successful")); } const QString styleSheet = QString("QLabel {background: %1; padding: 3px}") .arg(errorOccurred ? QColor(Qt::red).lighter(160).name() : QColor(Qt::green).lighter(160).name()); validationStatus->setStyleSheet(styleSheet); }
void tst_QXmlSchema::loadSchemaUrlFail() const { const QUrl url("http://notavailable/"); QXmlSchema schema; QVERIFY(!schema.load(url)); }
void TestLanguageFiles::languageSchemeValidationTest() { QUrl languageFile = QUrl::fromLocalFile("schemes/language.xsd"); QXmlSchema languageSchema; QVERIFY(languageSchema.load(languageFile)); QVERIFY(languageSchema.isValid()); }
Object* loadScene(const string& strFileName) { qDebug() << "Loading scene from file" << QString::fromStdString(strFileName); QFile schemaFile(":/Schema/Schema/schema.xsd"); QXmlSchema schema; WispMessageHandler handler; schema.setMessageHandler(&handler); if (!schemaFile.open(QIODevice::ReadOnly)) throw WispException(formatString("Unable to open the XML schema!")); if (!schema.load(schemaFile.readAll())) throw WispException(formatString("Unable to parse the XML schema!")); QXmlSchemaValidator validator(schema); QFile file(QString::fromStdString(strFileName)); if (!file.open(QIODevice::ReadOnly)) throw WispException(formatString("Unable to open the file \"%s\"", strFileName.c_str())); if (!validator.validate(&file)) throw WispException(formatString("Unable to validate the file \"%s\"", strFileName.c_str())); file.seek(0); Parser parser; QXmlSimpleReader reader; reader.setContentHandler(&parser); QXmlInputSource source(&file); if (!reader.parse(source)) throw WispException(formatString("Unable to parse the file \"%s\"", strFileName.c_str())); return parser.getRoot(); }
ErrorResult validateXML(const QString &fileName, const QString &schemaFileName) { QXmlSchema schema; schema.load(QUrl(schemaFileName)); MessageHandler schemaMessageHandler; schema.setMessageHandler(&schemaMessageHandler); if (!schema.isValid()) return ErrorResult(ErrorResultType_Critical, QObject::tr("Schena '%1' is not valid. %2"). arg(schemaFileName). arg(schemaMessageHandler.statusMessage())); QFile file(fileName); file.open(QIODevice::ReadOnly); QXmlSchemaValidator validator(schema); MessageHandler validatorMessageHandler; validator.setMessageHandler(&validatorMessageHandler); //TODO neslo mi nacist soubor se dvema poli // if (!validator.validate(&file, QUrl::fromLocalFile(file.fileName()))) // return ErrorResult(ErrorResultType_Critical, QObject::tr("File '%1' is not valid Agros2D problem file. Error (line %3, column %4): %2"). // arg(fileName). // arg(validatorMessageHandler.statusMessage()). // arg(validatorMessageHandler.line()). // arg(validatorMessageHandler.column())); return ErrorResult(); }
void tst_QXmlSchema::loadSchemaDataFail() const { // empty schema can not be loaded const QByteArray data; QXmlSchema schema; QVERIFY(!schema.load(data)); }
QXmlSchema TestLanguageFiles::loadXmlSchema(const QString &schemeName) const { QString relPath = QString("schemes/%1.xsd").arg(schemeName); QUrl file = QUrl::fromLocalFile(QStandardPaths::locate(QStandardPaths::DataLocation, relPath)); QXmlSchema schema; if (schema.load(file) == false) { qWarning() << "Schema at file " << file.toLocalFile() << " is invalid."; } return schema; }
QXmlSchema ResourceInterface::loadXmlSchema(const QString &schemeName) const { QString relPath = QString("schemes/%1.xsd").arg(schemeName); QUrl file = QUrl::fromLocalFile(QStandardPaths::locate(QStandardPaths::GenericDataLocation, "artikulate/" + relPath)); QXmlSchema schema; if (file.isEmpty() || schema.load(file) == false) { qCWarning(ARTIKULATE_LOG) << "Schema at file " << file.toLocalFile() << " is invalid."; } return schema; }
void Schema::loadFromUrl() const { //! [0] QUrl url("http://www.schema-example.org/myschema.xsd"); QXmlSchema schema; if (schema.load(url) == true) qDebug() << "schema is valid"; else qDebug() << "schema is invalid"; //! [0] }
bool PairsTheme::isValid(const KArchiveFile* file) { KUrl schemaUrl = KUrl::fromLocalFile(KGlobal::dirs()->findResource("appdata", QLatin1String( "themes/game.xsd" ))); QXmlSchema schema; schema.load(schemaUrl); if(!schema.isValid()) { qWarning() << "game Schema not valid"; return false; } QXmlSchemaValidator validator(schema); return validator.validate(file->data()); }
void tst_QXmlSchema::loadSchemaDataSuccess() const { const QByteArray data( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" "<xsd:schema" " xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"" " xmlns=\"http://qt.nokia.com/xmlschematest\"" " targetNamespace=\"http://qt.nokia.com/xmlschematest\"" " version=\"1.0\"" " elementFormDefault=\"qualified\">" "</xsd:schema>" ); QXmlSchema schema; QVERIFY(schema.load(data)); }
void Fenetre::ouvrir() { QString fileName = QFileDialog::getOpenFileName(this, tr("Séléctionner un fichier"), QDir::currentPath(), tr("Fichiers XML (*.xml)")); if (fileName.isEmpty()) return; QFile fileXML(fileName); if (!fileXML.open(QFile::ReadOnly | QFile::Text)) { QMessageBox::warning(this, tr("Erreur lecture"), tr("Impossible de lire le fichier %1:\n%2.") .arg(fileName) .arg(fileXML.errorString())); return; } QFile fileSchema("schemaGraph.xsd"); fileSchema.open(QIODevice::ReadOnly); QXmlSchema schema; MessageHandler messageHandler; schema.setMessageHandler(&messageHandler); schema.load(&fileSchema, QUrl::fromLocalFile(fileSchema.fileName())); if (schema.isValid()) { QFile fileXML2(fileName); fileXML2.open(QIODevice::ReadOnly); QXmlSchemaValidator validator(schema); if (!validator.validate(&fileXML2, QUrl::fromLocalFile(fileXML2.fileName()))){ QMessageBox::warning(this, tr("Fichier malformé"), tr("Impossible d'ouvrir le fichier' %1'.\n Le document ne correspond pas au schéma. \n%2") .arg(fileName) .arg(messageHandler.statusMessage())); return; } } ImportExport import=ImportExport(&graphe,xmltree, affichageSVG); if (import.ouvrir(&fileXML, &domDocument)) ui->statusBar->showMessage(tr("Fichier chargé"), 2000); affichageSVG->drawGraph(); ui->afficheGraphe->adjustSize(); //On pense à remettre la liste des sommets pour créer les arcs redessinerComboArc(); }
void Schema::loadFromFile() const { //! [1] QFile file("myschema.xsd"); file.open(QIODevice::ReadOnly); QXmlSchema schema; schema.load(&file, QUrl::fromLocalFile(file.fileName())); if (schema.isValid()) qDebug() << "schema is valid"; else qDebug() << "schema is invalid"; //! [1] }
MerDevicesXmlReader::MerDevicesXmlReader(const QString &fileName, QObject *parent) : QObject(parent), d(new MerDevicesXmlReaderPrivate) { Utils::FileReader reader; d->error = !reader.fetch(fileName, QIODevice::ReadOnly); if (d->error) { d->errorString = reader.errorString(); return; } QXmlSchema schema; schema.setMessageHandler(&d->messageHandler); Utils::FileReader schemeReader; d->error = !schemeReader.fetch(QString::fromLatin1("%1/mer/devices.xsd").arg(sharedDirPath()), QIODevice::ReadOnly); if (d->error) { d->errorString = schemeReader.errorString(); return; } schema.load(schemeReader.data()); d->error = !schema.isValid(); if (d->error) { d->errorString = d->messageHandler.errorString(); return; } QXmlSchemaValidator validator(schema); validator.setMessageHandler(&d->messageHandler); d->error = !validator.validate(reader.data()); if (d->error) { d->errorString = d->messageHandler.errorString(); return; } d->query.setQuery(QString::fromLatin1("doc('%1')").arg(fileName)); d->query.setMessageHandler(&d->messageHandler); d->error = !d->query.isValid(); if (d->error) { d->errorString = d->messageHandler.errorString(); return; } d->error = !d->query.evaluateTo(d->receiver); if (d->error) d->errorString = d->messageHandler.errorString(); }
void tst_QXmlSchema::loadSchemaDeviceFail() const { QByteArray data( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" "<xsd:schema" " xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"" " xmlns=\"http://qt.nokia.com/xmlschematest\"" " targetNamespace=\"http://qt.nokia.com/xmlschematest\"" " version=\"1.0\"" " elementFormDefault=\"qualified\">" "</xsd:schema>" ); QBuffer buffer(&data); // a closed device can not be loaded QXmlSchema schema; QVERIFY(!schema.load(&buffer)); }
void tst_QXmlSchema::documentUri() const { const QByteArray data( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" "<xsd:schema" " xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"" " xmlns=\"http://qt.nokia.com/xmlschematest\"" " targetNamespace=\"http://qt.nokia.com/xmlschematest\"" " version=\"1.0\"" " elementFormDefault=\"qualified\">" "</xsd:schema>" ); const QUrl documentUri("http://qt.nokia.com/xmlschematest"); QXmlSchema schema; schema.load(data, documentUri); QCOMPARE(documentUri, schema.documentUri()); }
void cActionDefList::validateActionDef( const QString &p_qsActionDefFile, const QString &p_qsSchemaFile ) throw( cSevException ) { cTracer obTracer( &g_obLogger, "cActionList::validateActionDef", p_qsActionDefFile.toStdString() ); QFile obActionsFile( p_qsActionDefFile ); try { if( !obActionsFile.open( QIODevice::ReadOnly ) ) { throw cSevException( cSeverity::ERROR, QString( "Cannot open Actions file: %1" ).arg( p_qsActionDefFile ).toStdString() ); } QXmlSchema obSchema; obSchema.load( p_qsSchemaFile ); if( !obSchema.isValid() ) { throw cSevException( cSeverity::ERROR, QString( "Schema %1 is not valid" ).arg( p_qsSchemaFile ).toStdString() ); } QXmlSchemaValidator obValidator( obSchema ); if( !obValidator.validate( &obActionsFile, QUrl::fromLocalFile( p_qsActionDefFile ) ) ) { throw cSevException( cSeverity::ERROR, QString( "Action definition file %1 is not valid according to Schema %2" ).arg( p_qsActionDefFile ).arg( p_qsSchemaFile ).toStdString() ); } QString qsErrorMsg = ""; int inErrorLine = 0; obActionsFile.seek( 0 ); if( !m_poActionsDoc->setContent( &obActionsFile, &qsErrorMsg, &inErrorLine ) ) { throw cSevException( cSeverity::ERROR, QString( "Parsing Actions file: %1 - Error in line %2: %3" ).arg( p_qsActionDefFile ).arg( inErrorLine ).arg( qsErrorMsg ).toStdString() ); } obActionsFile.close(); } catch( cSevException &e ) { obActionsFile.close(); throw e; } }
void tst_QXmlSchema::loadSchemaDeviceSuccess() const { QByteArray data( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" "<xsd:schema" " xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"" " xmlns=\"http://qt.nokia.com/xmlschematest\"" " targetNamespace=\"http://qt.nokia.com/xmlschematest\"" " version=\"1.0\"" " elementFormDefault=\"qualified\">" "</xsd:schema>" ); QBuffer buffer(&data); buffer.open(QIODevice::ReadOnly); QXmlSchema schema; QVERIFY(schema.load(&buffer)); }
static QXmlSchema createValidSchema() { const QByteArray data( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" "<xsd:schema" " xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"" " xmlns=\"http://qt.nokia.com/xmlschematest\"" " targetNamespace=\"http://qt.nokia.com/xmlschematest\"" " version=\"1.0\"" " elementFormDefault=\"qualified\">" " <xsd:element name=\"myRoot\" type=\"xsd:string\"/>" "</xsd:schema>" ); const QUrl documentUri("http://qt.nokia.com/xmlschematest"); QXmlSchema schema; schema.load(data, documentUri); return schema; }
static PyObject *meth_QXmlSchema_load(PyObject *sipSelf, PyObject *sipArgs, PyObject *sipKwds) { PyObject *sipParseErr = NULL; { const QUrl * a0; QXmlSchema *sipCpp; if (sipParseKwdArgs(&sipParseErr, sipArgs, sipKwds, NULL, NULL, "BJ9", &sipSelf, sipType_QXmlSchema, &sipCpp, sipType_QUrl, &a0)) { bool sipRes; Py_BEGIN_ALLOW_THREADS sipRes = sipCpp->load(*a0); Py_END_ALLOW_THREADS return PyBool_FromLong(sipRes); } }
//---------------------------------------------------------------------------- bool ctkCmdLineModuleXmlValidator::validateInput() { d->ErrorStr.clear(); if (!d->Input) { d->ErrorStr = "No input set for validation."; return false; } QIODevice* inputSchema = d->InputSchema; QScopedPointer<QIODevice> defaultInputSchema(new QFile(":/ctkCmdLineModule.xsd")); if (!inputSchema) { inputSchema = defaultInputSchema.data(); inputSchema->open(QIODevice::ReadOnly); } ctkCmdLineModuleXmlMsgHandler errorHandler; QXmlSchema schema; schema.setMessageHandler(&errorHandler); if (!schema.load(inputSchema)) { QString msg("Invalid input schema at line %1, column %2: %3"); d->ErrorStr = msg.arg(errorHandler.line()).arg(errorHandler.column()).arg(errorHandler.statusMessage()); return false; } QXmlSchemaValidator validator(schema); if (!validator.validate(d->Input)) { QString msg("Error validating CLI XML description, at line %1, column %2: %3"); d->ErrorStr = msg.arg(errorHandler.line()).arg(errorHandler.column()) .arg(errorHandler.statusMessage()); return false; } return true; }
/*! * \brief Utilities::parseMetaModelText * Parses the MetaModel text against the schema. * \param pMessageHandler * \param contents */ void Utilities::parseMetaModelText(MessageHandler *pMessageHandler, QString contents) { QFile schemaFile(QString(":/Resources/XMLSchema/tlmModelDescription.xsd")); schemaFile.open(QIODevice::ReadOnly); const QString schemaText(QString::fromUtf8(schemaFile.readAll())); schemaFile.close(); const QByteArray schemaData = schemaText.toUtf8(); QXmlSchema schema; schema.setMessageHandler(pMessageHandler); schema.load(schemaData); if (!schema.isValid()) { pMessageHandler->setFailed(true); } else { QXmlSchemaValidator validator(schema); if (!validator.validate(contents.toUtf8())) { pMessageHandler->setFailed(true); } } }
void XSDTSTestCase::executeSchemaTest(TestResult::Status &resultStatus, QString &serialized, QAbstractMessageHandler *handler) { QFile file(m_schemaUri.path()); if (!file.open(QIODevice::ReadOnly)) { resultStatus = TestResult::Fail; serialized = QString(); return; } QXmlSchema schema; schema.setMessageHandler(handler); schema.load(&file, m_schemaUri); if (schema.isValid()) { resultStatus = TestResult::Pass; serialized = QString::fromLatin1("true"); } else { resultStatus = TestResult::Pass; serialized = QString::fromLatin1("false"); } }
void Schema::loadFromData() const { //! [2] QByteArray data( "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" "<xsd:schema" " xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"" " xmlns=\"http://qt.nokia.com/xmlschematest\"" " targetNamespace=\"http://qt.nokia.com/xmlschematest\"" " version=\"1.0\"" " elementFormDefault=\"qualified\">" "</xsd:schema>" ); QXmlSchema schema; schema.load(data); if (schema.isValid()) qDebug() << "schema is valid"; else qDebug() << "schema is invalid"; //! [2] }
void XSDTSTestCase::executeInstanceTest(TestResult::Status &resultStatus, QString &serialized, QAbstractMessageHandler *handler) { QFile instanceFile(m_instanceUri.path()); if (!instanceFile.open(QIODevice::ReadOnly)) { resultStatus = TestResult::Fail; serialized = QString(); return; } QXmlSchema schema; if (m_schemaUri.isValid()) { QFile file(m_schemaUri.path()); if (!file.open(QIODevice::ReadOnly)) { resultStatus = TestResult::Fail; serialized = QString(); return; } schema.setMessageHandler(handler); schema.load(&file, m_schemaUri); if (!schema.isValid()) { resultStatus = TestResult::Pass; serialized = QString::fromLatin1("false"); return; } } QXmlSchemaValidator validator(schema); validator.setMessageHandler(handler); qDebug("check %s", qPrintable(m_instanceUri.path())); if (validator.validate(&instanceFile, m_instanceUri)) { resultStatus = TestResult::Pass; serialized = QString::fromLatin1("true"); } else { resultStatus = TestResult::Pass; serialized = QString::fromLatin1("false"); } }
FileReader::FileReader(QString fileName, QAbstractItemModel* d, MyWidget* p) : file(fileName), model(d), parent(p) { QByteArray data("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" "<xsd:schema xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">" "<xsd:element name=\"chart\" type=\"chartType\"/>" " <xsd:complexType name=\"chartType\">" " <xsd:sequence>" " <xsd:element name=\"title\" type=\"xsd:string\"/>" " <xsd:element name=\"xlabel\" type=\"xsd:string\" minOccurs=\"0\"/>" " <xsd:element name=\"ylabel\" type=\"xsd:string\" minOccurs=\"0\"/>" " <xsd:element name=\"point\" type=\"pointType\" maxOccurs=\"unbounded\"/>" " </xsd:sequence>" " </xsd:complexType>" " <xsd:complexType name=\"pointType\">" " <xsd:sequence>" " <xsd:element name=\"x\" type=\"xsd:string\"/>" " <xsd:element name=\"y\" type=\"xsd:string\"/>" " </xsd:sequence>" " </xsd:complexType>" "</xsd:schema>"); if(!file.open(QFile::ReadOnly | QFile::Text)) { QMessageBox::warning(parent, tr("qCharts"), tr("Cannot read file %1:\n%2.").arg(fileName).arg(file.errorString())); return; } QXmlSchema schema; schema.load(data); if(schema.isValid()) { QXmlSchemaValidator validator(schema); if(validator.validate(&file, QUrl::fromLocalFile(file.fileName()))){ isReadable=true; } else { isReadable=false; QMessageBox::warning(this, tr("qCharts"), tr("The file that you are trying to open isn't valid.")); } } file.close(); }
bool MainWindow::validate() { QUrl url("qrc:/resources/peach.xsd"); const QByteArray instanceData = completingTextEdit->toPlainText().toUtf8(); MessageHandler messageHandler; QXmlSchema schema; schema.setMessageHandler(&messageHandler); schema.load(url); bool errorOccurred = false; if (!schema.isValid()) { errorOccurred = true; } else { QXmlSchemaValidator validator(schema); if (!validator.validate(instanceData)) errorOccurred = true; } if (errorOccurred) { statusLabel->setText(messageHandler.statusMessage()); moveCursor(messageHandler.line(), messageHandler.column()); return false; } else { statusLabel->setText(tr("Validation successful")); return true; } const QString styleSheet = QString("QstatusLabel {background: %1; padding: 3px}") .arg(errorOccurred ? QColor(Qt::red).lighter(160).name() : QColor(Qt::green).lighter(160).name()); statusLabel->setStyleSheet(styleSheet); }
QT_USE_NAMESPACE int main(int argc, char **argv) { enum ExitCode { Valid = 0, Invalid, ParseError }; enum ExecutionMode { InvalidMode, SchemaOnlyMode, InstanceOnlyMode, SchemaAndInstanceMode }; const QCoreApplication app(argc, argv); QCoreApplication::setApplicationName(QLatin1String("xmlpatternsvalidator")); QStringList arguments = QCoreApplication::arguments(); if (arguments.size() != 2 && arguments.size() != 3) { qDebug() << QXmlPatternistCLI::tr("usage: xmlpatternsvalidator (<schema url> | <instance url> <schema url> | <instance url>)"); return ParseError; } // parse command line arguments ExecutionMode mode = InvalidMode; QUrl schemaUri; QUrl instanceUri; { QUrl url = arguments[1]; if (url.isRelative()) url = QUrl::fromLocalFile(arguments[1]); if (arguments.size() == 2) { // either it is a schema or instance document if (arguments[1].toLower().endsWith(QLatin1String(".xsd"))) { schemaUri = url; mode = SchemaOnlyMode; } else { // as we could validate all types of xml documents, don't check the extension here instanceUri = url; mode = InstanceOnlyMode; } } else if (arguments.size() == 3) { instanceUri = url; schemaUri = arguments[2]; if (schemaUri.isRelative()) schemaUri = QUrl::fromLocalFile(schemaUri.toString()); mode = SchemaAndInstanceMode; } } // Validate schema QXmlSchema schema; if (InstanceOnlyMode != mode) { schema.load(schemaUri); if (!schema.isValid()) return Invalid; } if (SchemaOnlyMode == mode) return Valid; // Validate instance QXmlSchemaValidator validator(schema); if (validator.validate(instanceUri)) return Valid; return Invalid; }