Remove duplicated Code (#377)

* Reduce Duplicate Code

* Reduce Duplicate Code

* No Error Logging when "document_root" in Json config is empty

* Update JsonClientConnection.cpp

* Remove obsolete functions

* create readConfig and readSchema function

* set forgotten _error variable
This commit is contained in:
Paulchen Panther
2017-01-23 23:25:12 +01:00
committed by brindosch
parent 8a4d1c5088
commit ed47852518
7 changed files with 73 additions and 95 deletions

View File

@@ -16,53 +16,30 @@ bool loadConfig(const QString & configFile)
{
// make sure the resources are loaded (they may be left out after static linking)
Q_INIT_RESOURCE(resource);
QJsonParseError error;
////////////////////////////////////////////////////////////
// read and set the json schema from the resource
////////////////////////////////////////////////////////////
QFile schemaData(":/hyperion-schema-"+QString::number(CURRENT_CONFIG_VERSION));
if (!schemaData.open(QIODevice::ReadOnly))
QJsonObject schemaJson;
try
{
std::stringstream error;
error << "Schema not found: " << schemaData.errorString().toStdString();
throw std::runtime_error(error.str());
schemaJson = QJsonFactory::readSchema(":/hyperion-schema");
}
QByteArray schema = schemaData.readAll();
QJsonDocument schemaJson = QJsonDocument::fromJson(schema, &error);
if (error.error != QJsonParseError::NoError)
catch(const std::runtime_error& error)
{
// report to the user the failure and their locations in the document.
int errorLine(0), errorColumn(0);
for( int i=0, count=qMin( error.offset,schema.size()); i<count; ++i )
{
++errorColumn;
if(schema.at(i) == '\n' )
{
errorColumn = 0;
++errorLine;
}
}
std::stringstream sstream;
sstream << "Schema error: " << error.errorString().toStdString() << " at Line: " << errorLine << ", Column: " << errorColumn;
throw std::runtime_error(sstream.str());
throw std::runtime_error(error.what());
}
QJsonSchemaChecker schemaChecker;
schemaChecker.setSchema(schemaJson.object());
schemaChecker.setSchema(schemaJson);
////////////////////////////////////////////////////////////
// read and validate the configuration file from the command line
////////////////////////////////////////////////////////////
const QJsonObject jsonConfig = QJsonFactory::readJson(configFile);
const QJsonObject jsonConfig = QJsonFactory::readConfig(configFile);
if (!schemaChecker.validate(jsonConfig))
{