Example #1
0
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;
      }
Example #2
0
bool XmlResApp::Validate()
{
    if ( flagVerbose )
        wxPuts("validating XRC files...");

    wxString schemaURI;

    if ( !parSchemaFile.empty() )
    {
        schemaURI = parSchemaFile;
    }
    else
    {
        schemaURI = "http://www.wxwidgets.org/wxxrc";

        // Normally, we'd use an OASIS XML catalog to map the URI to a local copy,
        // but Jing's catalog support (-C catalogFile) requires additional
        // dependency, resolver.jar, that is not commonly installed alongside Jing
        // by systems that package Jing. So do the (trivial) mapping manually here:
        wxString wxWinRoot;
        if ( wxGetEnv("WXWIN", &wxWinRoot) )
        {
            wxString schemaFile(wxWinRoot + "/misc/schema/xrc_schema.rnc");
            if ( wxFileExists(schemaFile) )
                schemaURI = schemaFile;
        }
    }

    wxString cmdline = wxString::Format("jing -c \"%s\"", schemaURI);
    for ( size_t i = 0; i < parFiles.GetCount(); i++ )
        cmdline << wxString::Format(" \"%s\"", parFiles[i]);

    int res = wxExecute(cmdline, wxEXEC_BLOCK);
    if (res == -1)
    {
        wxLogError("Running RELAX NG validator failed.");
        wxLogError("Please install Jing (http://www.thaiopensource.com/relaxng/jing.html).");
        wxLogError("See http://svn.wxwidgets.org/svn/wx/wxWidgets/trunk/misc/schema/README for more information.");
        return false;
    }

    if ( flagVerbose )
    {
        if ( res == 0 )
            wxPuts("XRC validation passed without errors.");
        else
            wxPuts("XRC validation failed, there are errors.");
    }

    return res == 0;
}
Example #3
0
/*!
 * \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);
    }
  }
}
//! [1]
void MainWindow::schemaSelected(int index)
{
    instanceSelection->clear();
    if (index == 0) {
        instanceSelection->addItem(tr("Valid Contact Instance"));
        instanceSelection->addItem(tr("Invalid Contact Instance"));
    } else if (index == 1) {
        instanceSelection->addItem(tr("Valid Recipe Instance"));
        instanceSelection->addItem(tr("Invalid Recipe Instance"));
    } else if (index == 2) {
        instanceSelection->addItem(tr("Valid Order Instance"));
        instanceSelection->addItem(tr("Invalid Order Instance"));
    }
    textChanged();

    QFile schemaFile(QString(":/schema_%1.xsd").arg(index));
    schemaFile.open(QIODevice::ReadOnly);
    const QString schemaText(QString::fromUtf8(schemaFile.readAll()));
    schemaView->setPlainText(schemaText);

    validate();
}
Example #5
0
void UveXmlValidator::setXML(QString fileName)
{
    QFile xmlFile(fileName);
    xmlFile.open(QIODevice::ReadOnly);
    const QString xmlText(QString::fromUtf8(xmlFile.readAll()));
    ui->XMLFileBrowser->setPlainText(xmlText);

    QString XSDfile;

    if(fileName.endsWith("uve"))
    {
        XSDfile = UVESettings::getInstance()->value("UVEXSD").toString();
    }
    if(fileName.endsWith("uvc"))
    {
        XSDfile = UVESettings::getInstance()->value("VIPXSD").toString();
    }
    QFile schemaFile(XSDfile);
    schemaFile.open(QIODevice::ReadOnly);
    const QString schemaText(QString::fromUtf8(schemaFile.readAll()));
    ui->XSDFileBrowser->setPlainText(schemaText);

    validate();
}
Example #6
0
int main(int argc, char **argv)
{
	// we need the -schema <schema>, -input <json file or directory containing $(basename schema)*.json> and -pass <bool>
	QStringList arguments;
	std::string schema, input;
	bool pass = true;

	for (int i = 0; i < argc; i++) {
		if (argv[i][0] == '-') {
			switch (argv[i][1]) {
				case 's':
					if (strcmp(argv[i], "-schema") == 0) {
						schema = argv[++i];
						continue;
					}
					break;
				case 'i':
					if (strcmp(argv[i], "-input") == 0) {
						input = argv[++i];
						continue;
					}
					break;
				case 'p':
					if (strcmp(argv[i], "-pass") == 0) {
						pass = strcmp(argv[++i], "0") != 0;
						continue;
					}
					break;
				case '-':
					if (strcmp(argv[i], "--help") == 0) {
						printUsage(stdout, 1, argc, argv);
					}
			}
		}
		arguments += argv[i];
	}

	raw_buffer schemaData;
	QFileInfo schemaFileInfo(QString::fromStdString(schema));
	FilePtr schemaFile(map(schemaFileInfo.absoluteFilePath(), schemaData));
	if (schemaFile.isNull())
		printUsage(stderr, 2, argc, argv);
	resolutionDir = schemaFileInfo.dir();
	qDebug() << "Looking for schemas in" << resolutionDir.absolutePath();

	QList<FilePtr> fileHandles;
	pjson::testc::Inputs jsonInput;

	QString inputQStr = QString::fromStdString(input);
	QFileInfo inputInfo(inputQStr);
	QDir inputsDir(inputInfo.dir());
	if (inputInfo.isDir()) {
		QStringList nameFilters;
		nameFilters += QFileInfo(QString::fromStdString(schema)).completeBaseName() + "*.json";
		QFileInfoList tests = inputsDir.entryInfoList(nameFilters, QDir::Files | QDir::Readable, QDir::Name);
		for (int i = 0; i < tests.size(); i++) {
			jsonInput.fileNames << tests.at(i).absoluteFilePath();
		}
	} else {
		jsonInput.fileNames += inputQStr;
	}

	for (int i = 0; i < jsonInput.fileNames.size(); i++) {
		raw_buffer fileData;
		FilePtr fileHandle(map(jsonInput.fileNames.at(i), fileData));
		if (fileHandle == NULL) {
			destroyFiles(fileHandles);
			printUsage(stderr, 3, argc, argv);
		}

		fileHandles += fileHandle;
		jsonInput.fileData += fileData;
	}

	if (jsonInput.fileData.size() == 0) {
		fprintf(stderr, "No json inputs found to validate against in %s\n", input.c_str());
		printUsage(stderr, 4, argc, argv);
	}

	Q_ASSERT(jsonInput.fileData.size() == jsonInput.fileNames.size());

	pjson::testc::TestSchema schemaTester(schemaData, jsonInput, pass);

	return QTest::qExec(&schemaTester, arguments);
}
RodsMetadataSchema::RodsMetadataSchema(QString filePath)
{
    // if no schema file path specified, use platform default
    if (!filePath.length())
        filePath = SCHEMA_PATH;

    QFile schemaFile(filePath);
    QDomDocument schema;

    // TODO: error handling, refactor away from the constructor...
    if (!schemaFile.open(QIODevice::ReadOnly) || !schema.setContent(&schemaFile))
        return;

    // initialize conversion table
    this->nsConv["."] = "Global Namespace";

    // process namespaces
    QDomNodeList namespaces = schema.elementsByTagName("irods:namespace");

    for (int i = 0; i < namespaces.size(); i++)
    {
        QDomNode ns = namespaces.item(i);
        QDomNamedNodeMap nsAttr = ns.attributes();

        // for now, we use prefix and label from the schema
        QString nsPrefix = nsAttr.namedItem("prefix").nodeValue();
        QString nsLabel = nsAttr.namedItem("label").nodeValue();

        this->nsConv[nsPrefix.toStdString()] = nsLabel.toStdString();

        // process namespace attributes
        QDomNodeList attrElemList = ns.childNodes();

        for (int j = 0; j < attrElemList.size(); j++)
        {
            QDomNode attrElem = attrElemList.item(j);

            // we only process irods:attribute nodes
            if (!attrElem.nodeName().compare("irods:attribute"))
            {
                // attribute name and label are read from the DOM
                QDomNamedNodeMap attrElemAttrs = attrElem.attributes();
                QString attrName = nsPrefix + attrElemAttrs.namedItem("name").nodeValue();

                QDomElement attrLabelElem = attrElem.firstChildElement("irods:label");
                QString attrLabel = attrLabelElem.text();

                this->attrConv[attrName.toStdString()] = attrLabel.toStdString();

                // process attribute subnodes
                QDomNodeList attrSubNodes = attrElem.childNodes();

                for (int k = 0; k < attrSubNodes.size(); k++)
                {
                    QDomNode node = attrSubNodes.item(k);

                    // for now, we only process irods:displayFilter nodes
                    if (!node.nodeName().compare("irods:displayFilter"))
                    {
                        QDomNamedNodeMap filterAttrs = node.attributes();
                        if (!filterAttrs.namedItem("type").nodeValue().compare("regExp"))
                        {
                            QDomElement regExpRule = node.firstChildElement("irods:regExpRule");
                            QDomElement regExpFilter = node.firstChildElement("irods:regExpFilter");

                            // if we have a proper rule
                            if (regExpRule.text().length() > 0)
                            {
                                regExpRules[attrName.toStdString()] = regExpRule.text().toStdString();
                                regExpFilters[attrName.toStdString()] = regExpFilter.text().toStdString();
                            }
                        }
                    }
                }
            }
        }
    }
}