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

@@ -21,8 +21,8 @@ public:
static int load(const QString& schema, const QString& config, QJsonObject& json)
{
// Load the schema and the config trees
QJsonObject schemaTree = readJson(schema);
QJsonObject configTree = readJson(config);
QJsonObject schemaTree = readSchema(schema);
QJsonObject configTree = readConfig(config);
// create the validator
QJsonSchemaChecker schemaChecker;
@@ -45,15 +45,15 @@ public:
return 0;
}
static QJsonObject readJson(const QString& path)
static QJsonObject readConfig(const QString& path)
{
QFile file(path);
QJsonParseError error;
if (!file.open(QIODevice::ReadOnly))
{
std::stringstream sstream;
sstream << "Configuration file not found: " << file.errorString().toStdString();
sstream << "Configuration file not found: '" << path.toStdString() << "' (" << file.errorString().toStdString() << ")";
throw std::runtime_error(sstream.str());
}
@@ -61,12 +61,13 @@ public:
config.remove(QRegularExpression("([^:]?\\/\\/.*)"));
QJsonDocument doc = QJsonDocument::fromJson(config.toUtf8(), &error);
file.close();
if (error.error != QJsonParseError::NoError)
{
// 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,config.size()); i<count; ++i )
{
++errorColumn;
@@ -76,13 +77,51 @@ public:
++errorLine;
}
}
std::stringstream sstream;
sstream << "Failed to parse configuration: " << error.errorString().toStdString() << " at Line: " << errorLine << ", Column: " << errorColumn;
throw std::runtime_error(sstream.str());
}
return doc.object();
}
static QJsonObject readSchema(const QString& path)
{
QFile schemaData(path);
QJsonParseError error;
if (!schemaData.open(QIODevice::ReadOnly))
{
std::stringstream sstream;
sstream << "Schema not found: '" << path.toStdString() << "' (" << schemaData.errorString().toStdString() << ")";
throw std::runtime_error(sstream.str());
}
QByteArray schema = schemaData.readAll();
QJsonDocument doc = QJsonDocument::fromJson(schema, &error);
schemaData.close();
file.close();
if (error.error != QJsonParseError::NoError)
{
// 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 << "ERROR: Json schema wrong: " << error.errorString().toStdString() << " at Line: " << errorLine << ", Column: " << errorColumn;
throw std::runtime_error(sstream.str());
}
return doc.object();
}

View File

@@ -67,14 +67,6 @@ private:
///
void setMessage(const std::string & message);
///
/// Retrieves all references from the json-value as specified by the schema
///
/// @param[in] value The json-value
/// @param[in] schema The schema
///
void collectDependencies(const QJsonValue & value, const QJsonObject &schema);
private:
// attribute check functions
///
@@ -106,15 +98,6 @@ private:
///
void checkAdditionalProperties(const QJsonObject & value, const QJsonValue & schema, const QStringList & ignoredProperties);
///
/// Checks if references are configued and used correctly. If this is not the case _error is set
/// to true and an error-message is added to the message-queue.
///
/// @param value The given json-object
/// @param schemaLink The schema of the json-object
///
void checkDependencies(const QJsonValue & value, const QJsonValue & schemaLink);
///
/// Checks if the given value is larger or equal to the specified value. If this is not the case
/// _error is set to true and an error-message is added to the message-queue.