|
@@
-1,129
+1,125
|
|
1
|
#include "AmdaParser.h"
|
|
1
|
#include "AmdaParser.h"
|
|
|
|
|
2
|
#include "AmdaDefs.h"
|
|
2
|
|
|
3
|
|
|
3
|
#include <DataSource/DataSourceItem.h>
|
|
4
|
#include <DataSource/DataSourceItem.h>
|
|
4
|
|
|
5
|
|
|
5
|
#include <QFile>
|
|
6
|
#include <QFile>
|
|
6
|
#include <QJsonArray>
|
|
7
|
#include <QJsonArray>
|
|
7
|
#include <QJsonDocument>
|
|
8
|
#include <QJsonDocument>
|
|
8
|
#include <QJsonObject>
|
|
9
|
#include <QJsonObject>
|
|
9
|
|
|
10
|
|
|
10
|
Q_LOGGING_CATEGORY(LOG_AmdaParser, "AmdaParser")
|
|
11
|
Q_LOGGING_CATEGORY(LOG_AmdaParser, "AmdaParser")
|
|
11
|
|
|
12
|
|
|
12
|
namespace {
|
|
13
|
namespace {
|
|
13
|
|
|
14
|
|
|
14
|
// Significant keys of an AMDA's JSON file
|
|
|
|
|
15
|
const auto COMPONENT_KEY = QStringLiteral("component");
|
|
|
|
|
16
|
const auto PRODUCT_KEY = QStringLiteral("parameter");
|
|
|
|
|
17
|
const auto ROOT_KEY = QStringLiteral("dataCenter");
|
|
|
|
|
18
|
|
|
|
|
|
19
|
/// Returns the correct item type according to the key passed in parameter
|
|
15
|
/// Returns the correct item type according to the key passed in parameter
|
|
20
|
DataSourceItemType itemType(const QString &key) noexcept
|
|
16
|
DataSourceItemType itemType(const QString &key) noexcept
|
|
21
|
{
|
|
17
|
{
|
|
22
|
if (key == PRODUCT_KEY) {
|
|
18
|
if (key == AMDA_PRODUCT_KEY) {
|
|
23
|
return DataSourceItemType::PRODUCT;
|
|
19
|
return DataSourceItemType::PRODUCT;
|
|
24
|
}
|
|
20
|
}
|
|
25
|
else if (key == COMPONENT_KEY) {
|
|
21
|
else if (key == AMDA_COMPONENT_KEY) {
|
|
26
|
return DataSourceItemType::COMPONENT;
|
|
22
|
return DataSourceItemType::COMPONENT;
|
|
27
|
}
|
|
23
|
}
|
|
28
|
else {
|
|
24
|
else {
|
|
29
|
return DataSourceItemType::NODE;
|
|
25
|
return DataSourceItemType::NODE;
|
|
30
|
}
|
|
26
|
}
|
|
31
|
}
|
|
27
|
}
|
|
32
|
|
|
28
|
|
|
33
|
/**
|
|
29
|
/**
|
|
34
|
* Processes an entry of the JSON file to populate/create data source items
|
|
30
|
* Processes an entry of the JSON file to populate/create data source items
|
|
35
|
* @param jsonKey the entry's key
|
|
31
|
* @param jsonKey the entry's key
|
|
36
|
* @param jsonValue the entry's value
|
|
32
|
* @param jsonValue the entry's value
|
|
37
|
* @param item the current item for which the entry processing will be applied
|
|
33
|
* @param item the current item for which the entry processing will be applied
|
|
38
|
* @param appendData flag indicating that the entry is part of an array. In the case of an array of
|
|
34
|
* @param appendData flag indicating that the entry is part of an array. In the case of an array of
|
|
39
|
* values, each value will be concatenated to the others (rather than replacing the others)
|
|
35
|
* values, each value will be concatenated to the others (rather than replacing the others)
|
|
40
|
*/
|
|
36
|
*/
|
|
41
|
void parseEntry(const QString &jsonKey, const QJsonValue &jsonValue, DataSourceItem &item,
|
|
37
|
void parseEntry(const QString &jsonKey, const QJsonValue &jsonValue, DataSourceItem &item,
|
|
42
|
bool isArrayEntry = false)
|
|
38
|
bool isArrayEntry = false)
|
|
43
|
{
|
|
39
|
{
|
|
44
|
if (jsonValue.isObject()) {
|
|
40
|
if (jsonValue.isObject()) {
|
|
45
|
// Case of an object:
|
|
41
|
// Case of an object:
|
|
46
|
// - a new data source item is created and
|
|
42
|
// - a new data source item is created and
|
|
47
|
// - parsing is called recursively to process the new item
|
|
43
|
// - parsing is called recursively to process the new item
|
|
48
|
// - the new item is then added as a child of the former item
|
|
44
|
// - the new item is then added as a child of the former item
|
|
49
|
auto object = jsonValue.toObject();
|
|
45
|
auto object = jsonValue.toObject();
|
|
50
|
|
|
46
|
|
|
51
|
auto newItem = std::make_unique<DataSourceItem>(itemType(jsonKey));
|
|
47
|
auto newItem = std::make_unique<DataSourceItem>(itemType(jsonKey));
|
|
52
|
|
|
48
|
|
|
53
|
for (auto it = object.constBegin(), end = object.constEnd(); it != end; ++it) {
|
|
49
|
for (auto it = object.constBegin(), end = object.constEnd(); it != end; ++it) {
|
|
54
|
parseEntry(it.key(), it.value(), *newItem);
|
|
50
|
parseEntry(it.key(), it.value(), *newItem);
|
|
55
|
}
|
|
51
|
}
|
|
56
|
|
|
52
|
|
|
57
|
item.appendChild(std::move(newItem));
|
|
53
|
item.appendChild(std::move(newItem));
|
|
58
|
}
|
|
54
|
}
|
|
59
|
else if (jsonValue.isArray()) {
|
|
55
|
else if (jsonValue.isArray()) {
|
|
60
|
// Case of an array: the item is populated with the arrays' content
|
|
56
|
// Case of an array: the item is populated with the arrays' content
|
|
61
|
auto object = jsonValue.toArray();
|
|
57
|
auto object = jsonValue.toArray();
|
|
62
|
|
|
58
|
|
|
63
|
for (auto it = object.constBegin(), end = object.constEnd(); it != end; ++it) {
|
|
59
|
for (auto it = object.constBegin(), end = object.constEnd(); it != end; ++it) {
|
|
64
|
parseEntry(jsonKey, *it, item, true);
|
|
60
|
parseEntry(jsonKey, *it, item, true);
|
|
65
|
}
|
|
61
|
}
|
|
66
|
}
|
|
62
|
}
|
|
67
|
else {
|
|
63
|
else {
|
|
68
|
// Case of a simple value: we add a data to the item. If the simple value is a part of an
|
|
64
|
// Case of a simple value: we add a data to the item. If the simple value is a part of an
|
|
69
|
// array, it is concatenated to the values already existing for this key
|
|
65
|
// array, it is concatenated to the values already existing for this key
|
|
70
|
item.setData(jsonKey, jsonValue.toVariant(), isArrayEntry);
|
|
66
|
item.setData(jsonKey, jsonValue.toVariant(), isArrayEntry);
|
|
71
|
}
|
|
67
|
}
|
|
72
|
}
|
|
68
|
}
|
|
73
|
|
|
69
|
|
|
74
|
} // namespace
|
|
70
|
} // namespace
|
|
75
|
|
|
71
|
|
|
76
|
std::unique_ptr<DataSourceItem> AmdaParser::readJson(const QString &filePath) noexcept
|
|
72
|
std::unique_ptr<DataSourceItem> AmdaParser::readJson(const QString &filePath) noexcept
|
|
77
|
{
|
|
73
|
{
|
|
78
|
QFile jsonFile{filePath};
|
|
74
|
QFile jsonFile{filePath};
|
|
79
|
|
|
75
|
|
|
80
|
if (!jsonFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
|
76
|
if (!jsonFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
|
81
|
qCCritical(LOG_AmdaParser())
|
|
77
|
qCCritical(LOG_AmdaParser())
|
|
82
|
<< QObject::tr("Can't retrieve data source tree from file %1: %2")
|
|
78
|
<< QObject::tr("Can't retrieve data source tree from file %1: %2")
|
|
83
|
.arg(filePath, jsonFile.errorString());
|
|
79
|
.arg(filePath, jsonFile.errorString());
|
|
84
|
return nullptr;
|
|
80
|
return nullptr;
|
|
85
|
}
|
|
81
|
}
|
|
86
|
|
|
82
|
|
|
87
|
auto json = jsonFile.readAll();
|
|
83
|
auto json = jsonFile.readAll();
|
|
88
|
auto jsonDocument = QJsonDocument::fromJson(json);
|
|
84
|
auto jsonDocument = QJsonDocument::fromJson(json);
|
|
89
|
|
|
85
|
|
|
90
|
// Check preconditions for parsing
|
|
86
|
// Check preconditions for parsing
|
|
91
|
if (!jsonDocument.isObject()) {
|
|
87
|
if (!jsonDocument.isObject()) {
|
|
92
|
qCCritical(LOG_AmdaParser())
|
|
88
|
qCCritical(LOG_AmdaParser())
|
|
93
|
<< QObject::tr(
|
|
89
|
<< QObject::tr(
|
|
94
|
"Can't retrieve data source tree from file %1: the file is malformed (there is "
|
|
90
|
"Can't retrieve data source tree from file %1: the file is malformed (there is "
|
|
95
|
"not one and only one root object)")
|
|
91
|
"not one and only one root object)")
|
|
96
|
.arg(filePath);
|
|
92
|
.arg(filePath);
|
|
97
|
return nullptr;
|
|
93
|
return nullptr;
|
|
98
|
}
|
|
94
|
}
|
|
99
|
|
|
95
|
|
|
100
|
auto jsonDocumentObject = jsonDocument.object();
|
|
96
|
auto jsonDocumentObject = jsonDocument.object();
|
|
101
|
if (!jsonDocumentObject.contains(ROOT_KEY)) {
|
|
97
|
if (!jsonDocumentObject.contains(AMDA_ROOT_KEY)) {
|
|
102
|
qCCritical(LOG_AmdaParser())
|
|
98
|
qCCritical(LOG_AmdaParser())
|
|
103
|
<< QObject::tr(
|
|
99
|
<< QObject::tr(
|
|
104
|
"Can't retrieve data source tree from file %1: the file is malformed (the key "
|
|
100
|
"Can't retrieve data source tree from file %1: the file is malformed (the key "
|
|
105
|
"for the root element was not found (%2))")
|
|
101
|
"for the root element was not found (%2))")
|
|
106
|
.arg(filePath, ROOT_KEY);
|
|
102
|
.arg(filePath, AMDA_ROOT_KEY);
|
|
107
|
return nullptr;
|
|
103
|
return nullptr;
|
|
108
|
}
|
|
104
|
}
|
|
109
|
|
|
105
|
|
|
110
|
auto rootValue = jsonDocumentObject.value(ROOT_KEY);
|
|
106
|
auto rootValue = jsonDocumentObject.value(AMDA_ROOT_KEY);
|
|
111
|
if (!rootValue.isObject()) {
|
|
107
|
if (!rootValue.isObject()) {
|
|
112
|
qCCritical(LOG_AmdaParser())
|
|
108
|
qCCritical(LOG_AmdaParser())
|
|
113
|
<< QObject::tr(
|
|
109
|
<< QObject::tr(
|
|
114
|
"Can't retrieve data source tree from file %1: the file is malformed (the root "
|
|
110
|
"Can't retrieve data source tree from file %1: the file is malformed (the root "
|
|
115
|
"element is of the wrong type)")
|
|
111
|
"element is of the wrong type)")
|
|
116
|
.arg(filePath);
|
|
112
|
.arg(filePath);
|
|
117
|
return nullptr;
|
|
113
|
return nullptr;
|
|
118
|
}
|
|
114
|
}
|
|
119
|
|
|
115
|
|
|
120
|
// Makes the parsing
|
|
116
|
// Makes the parsing
|
|
121
|
auto rootObject = rootValue.toObject();
|
|
117
|
auto rootObject = rootValue.toObject();
|
|
122
|
auto rootItem = std::make_unique<DataSourceItem>(DataSourceItemType::NODE);
|
|
118
|
auto rootItem = std::make_unique<DataSourceItem>(DataSourceItemType::NODE);
|
|
123
|
|
|
119
|
|
|
124
|
for (auto it = rootObject.constBegin(), end = rootObject.constEnd(); it != end; ++it) {
|
|
120
|
for (auto it = rootObject.constBegin(), end = rootObject.constEnd(); it != end; ++it) {
|
|
125
|
parseEntry(it.key(), it.value(), *rootItem);
|
|
121
|
parseEntry(it.key(), it.value(), *rootItem);
|
|
126
|
}
|
|
122
|
}
|
|
127
|
|
|
123
|
|
|
128
|
return rootItem;
|
|
124
|
return rootItem;
|
|
129
|
}
|
|
125
|
}
|