b8b1566f
Peter M. Groen
Finished setting ...
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
/* ****************************************************************************
* Copyright 2019 Open Systems Development BV *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the "Software"), *
* to deal in the Software without restriction, including without limitation *
* the rights to use, copy, modify, merge, publish, distribute, sublicense, *
* and/or sell copies of the Software, and to permit persons to whom the *
* Software is furnished to do so, subject to the following conditions: *
* *
* The above copyright notice and this permission notice shall be included in *
* all copies or substantial portions of the Software. *
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
* DEALINGS IN THE SOFTWARE. *
* ***************************************************************************/
#include "xmlbase.h"
|
1c528733
Peter M. Groen
Setting up a firs...
|
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
#include "log.h"
/*
* ________________________________________
* / Distance doesn't make you any smaller, \
* | but it does make you part of a larger |
* \ picture. /
* ----------------------------------------
* \
* \
* .--.
* |o_o |
* |:_/ |
* // \ \
* (| | )
* /'\_ _/`\
* \___)=(___/
*
**********************************************************/
using namespace osdev::components::xml;
using namespace osdev::components::log;
void xml_string_writer::write(const void* data, size_t size)
{
result += std::string(static_cast<const char*>(data), size);
}
|
b8b1566f
Peter M. Groen
Finished setting ...
|
52
|
XmlBase::XmlBase(const QString& xmlFile)
|
1c528733
Peter M. Groen
Setting up a firs...
|
53
54
55
56
57
|
: m_xmldoc()
, m_xPathHash()
{
if (!xmlFile.isNull() || !xmlFile.isEmpty()) {
if (parseFile(xmlFile)) {
|
b8b1566f
Peter M. Groen
Finished setting ...
|
58
|
LogDebug("[XmlBase::XmlBase]", QString("File : %1 ..............[OK].").arg(xmlFile).toStdString());
|
1c528733
Peter M. Groen
Setting up a firs...
|
59
60
|
}
else {
|
b8b1566f
Peter M. Groen
Finished setting ...
|
61
62
|
LogError("[XmlBase::XmlBase]", QString("File : %1 ..............[Failed].").arg(xmlFile).toStdString());
throw std::runtime_error("[XmlBase::XmlBase] parseFile failed");
|
1c528733
Peter M. Groen
Setting up a firs...
|
63
64
65
66
|
}
}
}
|
b8b1566f
Peter M. Groen
Finished setting ...
|
67
|
XmlBase::~XmlBase() = default;
|
1c528733
Peter M. Groen
Setting up a firs...
|
68
|
|
b8b1566f
Peter M. Groen
Finished setting ...
|
69
|
bool XmlBase::parseString(const QString& qsXml)
|
1c528733
Peter M. Groen
Setting up a firs...
|
70
71
72
73
74
75
76
77
78
79
80
81
82
|
{
bool bResult = false;
if (!qsXml.isEmpty()) {
pugi::xml_parse_status parseStatus = m_xmldoc.load_buffer(qsXml.toStdString().c_str(),
qsXml.toStdString().size())
.status;
bResult = checkError(parseStatus);
}
return bResult;
}
|
b8b1566f
Peter M. Groen
Finished setting ...
|
83
|
bool XmlBase::parseFile(const QString& qsXml)
|
1c528733
Peter M. Groen
Setting up a firs...
|
84
85
86
87
88
89
90
91
92
93
94
|
{
bool bResult = false;
if (!qsXml.isEmpty()) {
pugi::xml_parse_status parseStatus = m_xmldoc.load_file(qsXml.toStdString().c_str()).status;
bResult = checkError(parseStatus);
}
return bResult;
}
|
b8b1566f
Peter M. Groen
Finished setting ...
|
95
|
bool XmlBase::checkError(pugi::xml_parse_status _parseStatus)
|
1c528733
Peter M. Groen
Setting up a firs...
|
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
|
{
bool bResult = false;
QString sLogMessage = "An unknown error occured.";
/*
* GCC 5.3.1 insits on all enumeration cases for this switch to be specified.
* The enumeration cases that throws -Wswitch warning are not available in pugixml for FC21.
* For backward compatibility with older pugixml version "-Wswitch" warnings are temporarily supressed.
*/
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wswitch"
switch (_parseStatus) {
case pugi::status_ok:
sLogMessage = "File parsed successfully.";
bResult = true;
break;
case pugi::status_file_not_found:
sLogMessage = "File not found.";
break;
case pugi::status_io_error:
sLogMessage = "Some I/O Error occured during reading the file / stream. Check the hardware for errors.";
break;
case pugi::status_out_of_memory:
sLogMessage = "Out of Memory while parsing the file.";
break;
case pugi::status_internal_error:
sLogMessage = "Oh dear... That's different. Something went horribly wrong. It is unrecoverable. We're exiting the whole shabang..";
break;
case pugi::status_unrecognized_tag:
sLogMessage = "Parsing stopping due to a tag with either an empty name or a name which starts with an incorrect character (Left a '#' somewhere?)";
break;
case pugi::status_bad_pi:
sLogMessage = "Parsing stopped due to incorrect document declaration/processing instruction. ";
break;
case pugi::status_bad_comment:
sLogMessage = "Parsing stopped due to the invalid construct of a Comment.";
break;
case pugi::status_bad_cdata:
sLogMessage = "Parsing stopped due to the invalid construct of a CDATA section.";
break;
case pugi::status_bad_doctype:
sLogMessage = "Parsing stopped due to the invalid construct of a DocType.";
break;
case pugi::status_bad_pcdata:
sLogMessage = "Parsing stopped due to the invalid construct of a PCDATA section.";
break;
case pugi::status_bad_attribute:
sLogMessage = "Parsing stopped because there was an incorrect attribute, such as an attribute without value or with value that is not quoted (note that <node attr=1> is incorrect in XML).";
break;
case pugi::status_bad_start_element:
sLogMessage = "Parsing stopped because a starting tag either had no closing > symbol or contained some incorrect symbol.";
break;
case pugi::status_bad_end_element:
sLogMessage = "Parsing stopped because ending tag had incorrect syntax (i.e. extra non-whitespace symbols between tag name and >).";
break;
case pugi::status_end_element_mismatch:
sLogMessage = "parsing stopped because the closing tag did not match the opening one (i.e. <node></nedo>) or because some tag was not closed at all.";
break;
// DISABLED ON FEDORA 21 and CentOS 7. Not present in pugixml 1.0-8.fc21
// case pugi::status_append_invalid_root:
// case pugi::status_no_document_element:
}
#pragma GCC diagnostic pop
|
b8b1566f
Peter M. Groen
Finished setting ...
|
159
|
LogDebug("[XmlBase::checkError]", sLogMessage.toStdString());
|
1c528733
Peter M. Groen
Setting up a firs...
|
160
161
162
|
return bResult;
}
|
b8b1566f
Peter M. Groen
Finished setting ...
|
163
|
void XmlBase::addXPath(const QString& qsName, const QString& qsXPath)
|
1c528733
Peter M. Groen
Setting up a firs...
|
164
165
|
{
if (m_xPathHash.contains(qsName)) {
|
b8b1566f
Peter M. Groen
Finished setting ...
|
166
|
LogWarning("[XmlBase::addXPath]", QString( "XPath already registered : " + qsName).toStdString());
|
1c528733
Peter M. Groen
Setting up a firs...
|
167
168
169
|
}
m_xPathHash.insert(qsName, qsXPath);
|
b8b1566f
Peter M. Groen
Finished setting ...
|
170
|
LogDebug("[XmlBase::addXPath]", QString("XPath" + qsXPath + " registered with key : " + qsName).toStdString());
|
1c528733
Peter M. Groen
Setting up a firs...
|
171
172
|
}
|
b8b1566f
Peter M. Groen
Finished setting ...
|
173
|
QString XmlBase::getXPath(const QString& qsXPathSelect) const
|
1c528733
Peter M. Groen
Setting up a firs...
|
174
175
176
177
|
{
QString qsXPath = m_xPathHash.value(qsXPathSelect);
if (qsXPath.isEmpty()) {
|
b8b1566f
Peter M. Groen
Finished setting ...
|
178
|
LogWarning("[XmlBase::getXPath]", QString("XPath not registered : " + qsXPathSelect).toStdString());
|
1c528733
Peter M. Groen
Setting up a firs...
|
179
180
181
182
183
|
}
return qsXPath;
}
|
b8b1566f
Peter M. Groen
Finished setting ...
|
184
|
QString XmlBase::evaluateXPath(const QString& qsXPathSelect,
|
1c528733
Peter M. Groen
Setting up a firs...
|
185
186
187
188
|
const QList<QVariant>& arguments) const
{
QString qsResult = getXPath(qsXPathSelect);
|
b8b1566f
Peter M. Groen
Finished setting ...
|
189
|
LogDebug( "[XmlBase::evaluateXPath]", QString( "Found XPathExpression : " + qsResult + " for selection : " + qsXPathSelect ).toStdString() );
|
1c528733
Peter M. Groen
Setting up a firs...
|
190
|
|
b8b1566f
Peter M. Groen
Finished setting ...
|
191
192
|
for (auto& value : arguments)
{
|
1c528733
Peter M. Groen
Setting up a firs...
|
193
194
195
|
qsResult = qsResult.arg(value.toString());
}
|
b8b1566f
Peter M. Groen
Finished setting ...
|
196
|
LogInfo("[XmlBase::evaluateXPath]", QString("Resulting XPathExpression : " + qsResult).toStdString());
|
1c528733
Peter M. Groen
Setting up a firs...
|
197
198
199
|
return qsResult;
}
|
b8b1566f
Peter M. Groen
Finished setting ...
|
200
|
void XmlBase::setSimpleData(const QString& qsXPathSelect,
|
1c528733
Peter M. Groen
Setting up a firs...
|
201
202
203
204
205
206
207
|
const QList<QVariant>& arguments,
const QVariant& data)
{
QString qsXPath = evaluateXPath(qsXPathSelect, arguments);
setNodeData(qsXPath, data);
}
|
b8b1566f
Peter M. Groen
Finished setting ...
|
208
|
void XmlBase::setSimpleData(const QString& qsXPathSelect, const QVariant& data)
|
1c528733
Peter M. Groen
Setting up a firs...
|
209
210
211
212
213
214
|
{
QString qsXPath = getXPath(qsXPathSelect);
setNodeData(qsXPath, data);
}
|
b8b1566f
Peter M. Groen
Finished setting ...
|
215
|
QVariant XmlBase::getSimpleData(const QString& qsXPathSelect,
|
1c528733
Peter M. Groen
Setting up a firs...
|
216
217
218
219
220
221
222
223
224
225
|
const QList<QVariant>& arguments) const
{
QString qsXPath = evaluateXPath(qsXPathSelect, arguments);
QVariant qvResult = getNodeData(qsXPath);
return qvResult;
}
// static
|
b8b1566f
Peter M. Groen
Finished setting ...
|
226
|
bool XmlBase::getBoolean(const QVariant& value)
|
1c528733
Peter M. Groen
Setting up a firs...
|
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
|
{
bool b_result = false;
QString l_result = value.toString().toUpper();
if ( // ------------------------
("Y" == l_result) ||
("YES" == l_result) ||
("TRUE" == l_result) ||
("ON" == l_result) ||
("1" == l_result)
// ------------------------
) {
b_result = true;
}
return b_result;
}
// static
|
b8b1566f
Peter M. Groen
Finished setting ...
|
246
|
QString XmlBase::getAttributeValue(const pugi::xml_node& xmlNode, const char* attributeName)
|
1c528733
Peter M. Groen
Setting up a firs...
|
247
248
249
250
251
252
253
254
|
{
const auto attr = xmlNode.attribute(attributeName);
if (attr.empty()) {
return {};
}
return attr.value();
}
|
b8b1566f
Peter M. Groen
Finished setting ...
|
255
|
pugi::xpath_node XmlBase::selectNode(const QString& qsXPath) const
|
1c528733
Peter M. Groen
Setting up a firs...
|
256
257
258
259
|
{
return m_xmldoc.select_node(qsXPath.toStdString().c_str());
}
|
b8b1566f
Peter M. Groen
Finished setting ...
|
260
|
QList<pugi::xpath_node> XmlBase::selectNodes(const QString& qsXPath) const
|
1c528733
Peter M. Groen
Setting up a firs...
|
261
262
263
264
265
266
267
268
269
270
271
|
{
QList<pugi::xpath_node> lstResult;
pugi::xpath_node_set nodes = m_xmldoc.select_nodes(qsXPath.toStdString().c_str());
for (auto& node : nodes) {
lstResult.append(node);
}
return lstResult;
}
|
b8b1566f
Peter M. Groen
Finished setting ...
|
272
|
void XmlBase::setNodeData(const QString& qsXPath, const QVariant& qsData)
|
1c528733
Peter M. Groen
Setting up a firs...
|
273
274
275
276
277
278
279
280
|
{
pugi::xml_node selectedNode;
selectedNode = selectNode(qsXPath).node();
if (!selectedNode.empty()) {
selectedNode.set_value(qsData.toString().toStdString().c_str());
}
else {
|
b8b1566f
Peter M. Groen
Finished setting ...
|
281
|
LogError("[XmlBase::setNodeData]", QString("No node(s) found for XPath expression : '%1'").arg(qsXPath).toStdString());
|
1c528733
Peter M. Groen
Setting up a firs...
|
282
283
284
|
}
}
|
b8b1566f
Peter M. Groen
Finished setting ...
|
285
|
QVariant XmlBase::getNodeData(const QString& qsXPath) const
|
1c528733
Peter M. Groen
Setting up a firs...
|
286
287
288
289
290
291
292
293
294
|
{
QVariant qvResult;
pugi::xml_node selectedNode = selectNode(qsXPath).node();
if (!selectedNode.empty()) {
qvResult = QString(selectedNode.value());
}
return qvResult;
}
|
b8b1566f
Peter M. Groen
Finished setting ...
|
295
|
QString XmlBase::asString() const
|
1c528733
Peter M. Groen
Setting up a firs...
|
296
297
298
299
300
301
|
{
xml_string_writer writer;
m_xmldoc.save(writer);
return QString(writer.result.c_str());
}
|