//! [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);
}
Exemplo n.º 2
0
Arquivo: xml.cpp Projeto: karban/field
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();
}
Exemplo n.º 3
0
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();
}
Exemplo n.º 4
0
 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);
 }