xmlbase.cpp
10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/* ****************************************************************************
* 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"
#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);
}
XmlBase::XmlBase(const QString& xmlFile)
: m_xmldoc()
, m_xPathHash()
{
if (!xmlFile.isNull() || !xmlFile.isEmpty()) {
if (parseFile(xmlFile)) {
LogDebug("[XmlBase::XmlBase]", QString("File : %1 ..............[OK].").arg(xmlFile).toStdString());
}
else {
LogError("[XmlBase::XmlBase]", QString("File : %1 ..............[Failed].").arg(xmlFile).toStdString());
throw std::runtime_error("[XmlBase::XmlBase] parseFile failed");
}
}
}
XmlBase::~XmlBase() = default;
bool XmlBase::parseString(const QString& qsXml)
{
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;
}
bool XmlBase::parseFile(const QString& qsXml)
{
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;
}
bool XmlBase::checkError(pugi::xml_parse_status _parseStatus)
{
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
LogDebug("[XmlBase::checkError]", sLogMessage.toStdString());
return bResult;
}
void XmlBase::addXPath(const QString& qsName, const QString& qsXPath)
{
if (m_xPathHash.contains(qsName)) {
LogWarning("[XmlBase::addXPath]", QString( "XPath already registered : " + qsName).toStdString());
}
m_xPathHash.insert(qsName, qsXPath);
LogDebug("[XmlBase::addXPath]", QString("XPath" + qsXPath + " registered with key : " + qsName).toStdString());
}
QString XmlBase::getXPath(const QString& qsXPathSelect) const
{
QString qsXPath = m_xPathHash.value(qsXPathSelect);
if (qsXPath.isEmpty()) {
LogWarning("[XmlBase::getXPath]", QString("XPath not registered : " + qsXPathSelect).toStdString());
}
return qsXPath;
}
QString XmlBase::evaluateXPath(const QString& qsXPathSelect,
const QList<QVariant>& arguments) const
{
QString qsResult = getXPath(qsXPathSelect);
LogDebug( "[XmlBase::evaluateXPath]", QString( "Found XPathExpression : " + qsResult + " for selection : " + qsXPathSelect ).toStdString() );
for (auto& value : arguments)
{
qsResult = qsResult.arg(value.toString());
}
LogInfo("[XmlBase::evaluateXPath]", QString("Resulting XPathExpression : " + qsResult).toStdString());
return qsResult;
}
void XmlBase::setSimpleData(const QString& qsXPathSelect,
const QList<QVariant>& arguments,
const QVariant& data)
{
QString qsXPath = evaluateXPath(qsXPathSelect, arguments);
setNodeData(qsXPath, data);
}
void XmlBase::setSimpleData(const QString& qsXPathSelect, const QVariant& data)
{
QString qsXPath = getXPath(qsXPathSelect);
setNodeData(qsXPath, data);
}
QVariant XmlBase::getSimpleData(const QString& qsXPathSelect,
const QList<QVariant>& arguments) const
{
QString qsXPath = evaluateXPath(qsXPathSelect, arguments);
QVariant qvResult = getNodeData(qsXPath);
return qvResult;
}
// static
bool XmlBase::getBoolean(const QVariant& value)
{
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
QString XmlBase::getAttributeValue(const pugi::xml_node& xmlNode, const char* attributeName)
{
const auto attr = xmlNode.attribute(attributeName);
if (attr.empty()) {
return {};
}
return attr.value();
}
pugi::xpath_node XmlBase::selectNode(const QString& qsXPath) const
{
return m_xmldoc.select_node(qsXPath.toStdString().c_str());
}
QList<pugi::xpath_node> XmlBase::selectNodes(const QString& qsXPath) const
{
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;
}
void XmlBase::setNodeData(const QString& qsXPath, const QVariant& qsData)
{
pugi::xml_node selectedNode;
selectedNode = selectNode(qsXPath).node();
if (!selectedNode.empty()) {
selectedNode.set_value(qsData.toString().toStdString().c_str());
}
else {
LogError("[XmlBase::setNodeData]", QString("No node(s) found for XPath expression : '%1'").arg(qsXPath).toStdString());
}
}
QVariant XmlBase::getNodeData(const QString& qsXPath) const
{
QVariant qvResult;
pugi::xml_node selectedNode = selectNode(qsXPath).node();
if (!selectedNode.empty()) {
qvResult = QString(selectedNode.value());
}
return qvResult;
}
QString XmlBase::asString() const
{
xml_string_writer writer;
m_xmldoc.save(writer);
return QString(writer.result.c_str());
}