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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
|
/*
* @copyright Copyright (c) 2016-2020 TOYOTA MOTOR CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
////////////////////////////////////////////////////////////////////////////////////////////////////
/// \ingroup tag_NS_ConfigParser
/// \brief This file contains implementation of CXMLReader class.
///
////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
// Include Files
////////////////////////////////////////////////////////////////////////////////////////////////////
#include <native_service/ns_xml_reader.h>
#include <string>
#include "ns_xmlconfig_parser_frameworkunifiedlog.h"
#ifdef __cplusplus
extern "C" {
#endif
CXMLReader *GetCXMLReaderObject(CHAR *f_cfilepath) {
CXMLReader *l_p_xml_reader = NULL;
if (NULL != f_cfilepath) {
l_p_xml_reader = new(std::nothrow) CXMLReader(f_cfilepath); // LCOV_EXCL_BR_LINE 11: except branch
}
return l_p_xml_reader;
}
#ifdef __cplusplus
}
#endif
CXMLReader::CXMLReader(): m_pXmlDoc(NULL) {
}
CXMLReader::CXMLReader(const std::string &f_cFilePath): m_pXmlDoc(NULL) {
FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "File Path %s", f_cFilePath.c_str());
// create document object structure
m_pXmlDoc = xmlParseFile(f_cFilePath.c_str()); // LCOV_EXCL_BR_LINE 11: except branch
if (!m_pXmlDoc) {
FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Pointer to document structure is NULL"); // LCOV_EXCL_BR_LINE 15:marco defined in "native_service/ns_logger_if.h"
}
}
CXMLReader::~CXMLReader() {
FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "Destructor");
if (NULL != m_pXmlDoc) {
xmlFreeDoc(m_pXmlDoc); // LCOV_EXCL_BR_LINE 11: except branch
m_pXmlDoc = NULL;
}
}
EFrameworkunifiedStatus CXMLReader::ParseFile(const std::string &f_cFilePath) {
EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
// create document object structure
m_pXmlDoc = xmlParseFile(f_cFilePath.c_str());
if (NULL == m_pXmlDoc) {
FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Pointer to document structure is NULL");
l_eStatus = eFrameworkunifiedStatusNullPointer;
}
FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
return l_eStatus;
}
PVOID CXMLReader::GetDataPtr() {
return m_pXmlDoc;
}
std::string CXMLReader::GetValue(const std::string &f_cKey) {
FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "Key %s", f_cKey.c_str());
std::string l_cValue = "";
if (NULL != m_pXmlDoc && (!f_cKey.empty())) {
std::string l_cUserKey = ""; // LCOV_EXCL_BR_LINE 11: except branch
l_cUserKey.assign(f_cKey);
// get the root node element
xmlNodePtr l_pCurrNode = xmlDocGetRootElement(m_pXmlDoc); // LCOV_EXCL_BR_LINE 11: except branch
if (NULL != l_pCurrNode) {
// remove the root node name from key
size_t l_uiLength = l_cUserKey.find('.'); // LCOV_EXCL_BR_LINE 11: except branch
if (std::string::npos != l_uiLength) {
l_cUserKey = l_cUserKey.substr(l_uiLength + 1); // LCOV_EXCL_BR_LINE 11: except branch
// if root node name matches with the name in received key
if (!(f_cKey.substr(0, l_uiLength)).compare((PCSTR)l_pCurrNode->name)) {
l_cValue = XMLGetValue(l_pCurrNode,
l_cUserKey);
}
}
}
FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__, "Parsed Value :: %s", l_cValue.c_str()); // LCOV_EXCL_BR_LINE 15:marco defined in "native_service/ns_logger_if.h"
} else {
FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Document structure pointer m_pXmlDoc is NULL"); // LCOV_EXCL_BR_LINE 15:marco defined in "native_service/ns_logger_if.h"
}
return l_cValue;
}
std::string CXMLReader::XMLGetValue(xmlNodePtr f_pCurrNode,
const std::string &f_cUserKey) {
std::string l_cReturnValue = "";
if (NULL != f_pCurrNode && (!f_cUserKey.empty())) {
std::string l_cKey = ""; // LCOV_EXCL_BR_LINE 11: except branch
// result of previous attribute of same node
BOOL l_bResult = TRUE;
// this parameter specifies whether to AND or OR the current result with previous result
// TRUE means AND while FALSE means OR
BOOL l_bOperation = FALSE;
// position of "@" in user key if exists
size_t l_uiAttrLen = 0;
// get the single attribute from list of attributes of node
std::string l_cAttr = ""; // LCOV_EXCL_BR_LINE 11: except branch
std::string l_cNodeName = ""; // LCOV_EXCL_BR_LINE 11: except branch
// contains the single attribute key, value
std::string l_cAttrOpt = ""; // LCOV_EXCL_BR_LINE 11: except branch
// l_cAttrId of the attribute
std::string l_cAttrId = ""; // LCOV_EXCL_BR_LINE 11: except branch
// value of the attribute
std::string l_cAttrValue = ""; // LCOV_EXCL_BR_LINE 11: except branch
size_t l_uiSplitProp = 0;
size_t l_uiAttrSplit = 0;
BOOL l_bCurrentResult = FALSE;
l_cKey.assign(f_cUserKey);
// position of first "." in user key
size_t l_uiLength = l_cKey.find('.'); // LCOV_EXCL_BR_LINE 11: except branch
if (std::string::npos != l_uiLength) {
// get the current node name string with node attributes
l_cKey = l_cKey.substr(0, l_uiLength); // LCOV_EXCL_BR_LINE 11: except branch
}
l_cNodeName.assign(l_cKey);
// get children of current node
f_pCurrNode = f_pCurrNode->children;
while (NULL != f_pCurrNode) {
// check whether current node is an element node.
if (XML_ELEMENT_NODE == f_pCurrNode->type) {
l_bResult = TRUE;
l_bOperation = FALSE;
l_uiAttrLen = 0;
l_cAttr.clear();
// check whether key contains attribute for current node
if (std::string::npos != (l_uiAttrLen = l_cKey.find('@'))) {
// get the attribute string from node key
l_cAttr.assign(l_cKey, (l_uiAttrLen + 1), std::string::npos); // LCOV_EXCL_BR_LINE 11: except branch
// remove the attribute string from key string
l_cKey = l_cKey.substr(0, l_uiAttrLen); // LCOV_EXCL_BR_LINE 11: except branch
}
// check whether node name string matches with the current node name
if (!l_cKey.compare((PCSTR)f_pCurrNode->name)) {
l_cAttrOpt.assign(l_cAttr);
while (0 < l_cAttr.length()) {
// initialize variables
l_cAttrId.clear();
l_cAttrValue.clear();
l_uiSplitProp = 0;
l_uiAttrSplit = 0;
l_bCurrentResult = FALSE;
// check whether node have multiple attributes
if (std::string::npos != (l_uiSplitProp = l_cAttr.find('|'))) {
l_cAttrOpt = l_cAttr.substr(0, l_uiSplitProp);
if (std::string::npos != (l_uiAttrSplit = l_cAttrOpt.find('='))) {
l_cAttrId = l_cAttrOpt.substr(0, l_uiAttrSplit);
l_cAttrValue = l_cAttrOpt.substr(l_uiAttrSplit + 1);
}
} else if (std::string::npos != (l_uiSplitProp = l_cAttr.find('&'))) {
l_cAttrOpt = l_cAttr.substr(0, l_uiSplitProp);
if (std::string::npos != (l_uiAttrSplit = l_cAttrOpt.find('='))) {
l_cAttrId = l_cAttrOpt.substr(0, l_uiAttrSplit);
l_cAttrValue = l_cAttrOpt.substr(l_uiAttrSplit + 1);
}
} else {
l_uiSplitProp = l_cAttr.length() - 1;
if (std::string::npos != (l_uiAttrSplit = l_cAttr.find('='))) {
l_cAttrId = l_cAttr.substr(0, l_uiAttrSplit); // LCOV_EXCL_BR_LINE 11: except branch
l_cAttrValue = l_cAttr.substr(l_uiAttrSplit + 1); // LCOV_EXCL_BR_LINE 11: except branch
}
}
// compare the value of attributes
xmlChar *l_pAttrValue = xmlGetProp(f_pCurrNode, (const xmlChar *)l_cAttrId.c_str()); // LCOV_EXCL_BR_LINE 11: except branch
if (NULL != l_pAttrValue) {
if (!l_cAttrValue.compare((PCSTR)l_pAttrValue)) {
l_bCurrentResult = TRUE;
}
}
xmlFree(l_pAttrValue); // LCOV_EXCL_BR_LINE 11: except branch
l_pAttrValue = NULL;
// combine the result of all attributes
if (!l_bOperation) {
l_bResult = l_bResult && l_bCurrentResult;
} else {
l_bResult = l_bResult || l_bCurrentResult;
}
if ('|' == l_cAttr[l_uiSplitProp]) {
l_bOperation = TRUE;
} else {
l_bOperation = FALSE;
}
if ((!l_bResult) && (!l_bOperation)) {
// break from current while loop
break;
}
l_cAttr = l_cAttr.substr(l_uiSplitProp + 1); // LCOV_EXCL_BR_LINE 11: except branch
}
// if attributes not matched move to next node
if (!l_bResult) {
// search for the same node name in next child nodes
l_cKey.assign(l_cNodeName);
} else {
// read text of current node
if ((NULL != f_pCurrNode->children) &&
(XML_TEXT_NODE == f_pCurrNode->children->type) &&
(NULL == f_pCurrNode->children->next)) {
// append the result in string
l_cReturnValue.append((PCSTR) f_pCurrNode->children->content); // LCOV_EXCL_BR_LINE 11: except branch
}
}
} else {
l_bResult = FALSE;
}
if (l_bResult) {
// parse children and next nodes of current node
if (std::string::npos != l_uiLength) {
l_cKey = f_cUserKey.substr(l_uiLength + 1); // LCOV_EXCL_BR_LINE 11: except branch
std::string l_cValue = XMLGetValue(f_pCurrNode, l_cKey); // LCOV_EXCL_BR_LINE 11: except branch
if (!l_cReturnValue.empty() && !l_cValue.empty()) {
l_cReturnValue.append("\n");
}
// append the received string
l_cReturnValue.append(l_cValue);
}
l_cKey = l_cNodeName;
}
}
f_pCurrNode = f_pCurrNode->next;
}
}
return l_cReturnValue;
}
EFrameworkunifiedStatus CXMLReader::GetValue(const std::string &f_cKey, std::string &f_cValue) {
EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
if (NULL != m_pXmlDoc && (!f_cKey.empty())) {
FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__, "Key is %s", f_cKey.c_str()); // LCOV_EXCL_BR_LINE 15:marco defined in "native_service/ns_logger_if.h"
std::string l_cUserKey = "";
l_cUserKey.assign(f_cKey);
// get the root node element
xmlNodePtr l_pCurrNode = xmlDocGetRootElement(m_pXmlDoc);
if (NULL != l_pCurrNode) {
// remove the root node name from key
size_t l_uiLength = l_cUserKey.find('.'); // LCOV_EXCL_BR_LINE 11: except branch
if (std::string::npos != l_uiLength) {
l_cUserKey = l_cUserKey.substr(l_uiLength + 1); // LCOV_EXCL_BR_LINE 11: except branch
// if root node name matches with the name in received key
if (!(f_cKey.substr(0, l_uiLength)).compare((PCSTR)l_pCurrNode->name)) {
BOOL l_bKeyFound = FALSE;
XMLGetValue(l_pCurrNode, l_cUserKey, f_cValue, l_bKeyFound);
if (!l_bKeyFound) {
l_eStatus = eFrameworkunifiedStatusFail;
}
}
} else {
l_eStatus = eFrameworkunifiedStatusInvldParam;
}
} else {
FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Root Node ptr is NULL"); // LCOV_EXCL_BR_LINE 15:marco defined in "native_service/ns_logger_if.h"
l_eStatus = eFrameworkunifiedStatusNullPointer;
}
FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__, "Parsed Value :: %s", f_cValue.c_str()); // LCOV_EXCL_BR_LINE 15:marco defined in "native_service/ns_logger_if.h"
} else {
FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Document structure pointer m_pXmlDoc is NULL"); // LCOV_EXCL_BR_LINE 15:marco defined in "native_service/ns_logger_if.h"
l_eStatus = eFrameworkunifiedStatusNullPointer;
}
FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
return l_eStatus;
}
EFrameworkunifiedStatus CXMLReader::XMLGetValue(xmlNodePtr f_pCurrNode, const std::string &f_cUserKey, std::string &f_cValue,
BOOL &f_bKeyFound) {
EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusFail;
FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
if (NULL != f_pCurrNode && (!f_cUserKey.empty())) {
FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__, "Key :: %s", f_cUserKey.c_str()); // LCOV_EXCL_BR_LINE 15:marco defined in "native_service/ns_logger_if.h"
std::string l_cKey = "";
// result of previous attribute of same node
BOOL l_bResult = TRUE;
// this parameter specifies whether to AND or OR the current result with previous result
// TRUE means AND while FALSE means OR
BOOL l_bOperation = FALSE;
// position of "@" in user key if exists
size_t l_uiAttrLen = 0;
// get the single attribute from list of attributes of node
std::string l_cAttr = ""; // LCOV_EXCL_BR_LINE 11: except branch
std::string l_cNodeName = ""; // LCOV_EXCL_BR_LINE 11: except branch
// contains the single attribute key, value
std::string l_cAttrOpt = ""; // LCOV_EXCL_BR_LINE 11: except branch
// l_cAttrId of the attribute
std::string l_cAttrId = ""; // LCOV_EXCL_BR_LINE 11: except branch
// value of the attribute
std::string l_cAttrValue = ""; // LCOV_EXCL_BR_LINE 11: except branch
size_t l_uiSplitProp = 0;
size_t l_uiAttrSplit = 0;
BOOL l_bCurrentResult = FALSE;
l_cKey.assign(f_cUserKey);
// position of first "." in user key
size_t l_uiLength = l_cKey.find('.'); // LCOV_EXCL_BR_LINE 11: except branch
if (std::string::npos != l_uiLength) {
// get the current node name string with node attributes
l_cKey = l_cKey.substr(0, l_uiLength); // LCOV_EXCL_BR_LINE 11: except branch
}
l_cNodeName.assign(l_cKey);
// get children of current node
f_pCurrNode = f_pCurrNode->children;
while (NULL != f_pCurrNode) {
// check whether current node is an element node.
if (XML_ELEMENT_NODE == f_pCurrNode->type) {
l_bResult = TRUE;
l_bOperation = FALSE;
l_uiAttrLen = 0;
l_cAttr.clear();
// check whether key contains attribute for current node
if (std::string::npos != (l_uiAttrLen = l_cKey.find('@'))) {
// get the attribute string from node key
l_cAttr.assign(l_cKey, (l_uiAttrLen + 1), std::string::npos); // LCOV_EXCL_BR_LINE 11: except branch
// remove the attribute string from key string
l_cKey = l_cKey.substr(0, l_uiAttrLen); // LCOV_EXCL_BR_LINE 11: except branch
}
// check whether node name string matches with the current node name
if (!l_cKey.compare((PCSTR)f_pCurrNode->name)) {
l_cAttrOpt.assign(l_cAttr);
while (0 < l_cAttr.length()) {
// initialize variables
l_cAttrId.clear();
l_cAttrValue.clear();
l_uiSplitProp = 0;
l_uiAttrSplit = 0;
l_bCurrentResult = FALSE;
// check whether node have multiple attributes
if (std::string::npos != (l_uiSplitProp = l_cAttr.find('|'))) {
l_cAttrOpt = l_cAttr.substr(0, l_uiSplitProp);
if (std::string::npos != (l_uiAttrSplit = l_cAttrOpt.find('='))) {
l_cAttrId = l_cAttrOpt.substr(0, l_uiAttrSplit);
l_cAttrValue = l_cAttrOpt.substr(l_uiAttrSplit + 1);
}
} else if (std::string::npos != (l_uiSplitProp = l_cAttr.find('&'))) {
l_cAttrOpt = l_cAttr.substr(0, l_uiSplitProp);
if (std::string::npos != (l_uiAttrSplit = l_cAttrOpt.find('='))) {
l_cAttrId = l_cAttrOpt.substr(0, l_uiAttrSplit);
l_cAttrValue = l_cAttrOpt.substr(l_uiAttrSplit + 1);
}
} else {
l_uiSplitProp = l_cAttr.length() - 1;
if (std::string::npos != (l_uiAttrSplit = l_cAttr.find('='))) {
l_cAttrId = l_cAttr.substr(0, l_uiAttrSplit); // LCOV_EXCL_BR_LINE 11: except branch
l_cAttrValue = l_cAttr.substr(l_uiAttrSplit + 1); // LCOV_EXCL_BR_LINE 11: except branch
}
}
// compare the value of attributes
xmlChar *l_pAttrValue = xmlGetProp(f_pCurrNode, (const xmlChar *)l_cAttrId.c_str()); // LCOV_EXCL_BR_LINE 11: except branch
if (NULL != l_pAttrValue) {
if (!l_cAttrValue.compare((PCSTR)l_pAttrValue)) {
l_bCurrentResult = TRUE;
}
xmlFree(l_pAttrValue); // LCOV_EXCL_BR_LINE 11: except branch
l_pAttrValue = NULL;
}
// combine the result of all attributes
if (!l_bOperation) {
l_bResult = l_bResult && l_bCurrentResult;
} else {
l_bResult = l_bResult || l_bCurrentResult;
}
if ('|' == l_cAttr[l_uiSplitProp]) {
l_bOperation = TRUE;
} else {
l_bOperation = FALSE;
}
if ((!l_bResult) && (!l_bOperation)) {
// break from current while loop
break;
}
l_cAttr = l_cAttr.substr(l_uiSplitProp + 1); // LCOV_EXCL_BR_LINE 11: except branch
}
// if attributes not matched move to next node
if (!l_bResult) {
// search for the same node name in next child nodes
l_cKey.assign(l_cNodeName);
} else {
// read text of current node
if ((NULL != f_pCurrNode->children) &&
(XML_TEXT_NODE == f_pCurrNode->children->type) &&
(NULL == f_pCurrNode->children->next)) {
if (!f_cValue.empty()) {
f_cValue.append("\n");
}
// append the result in string
f_cValue.append((PCSTR) f_pCurrNode->children->content); // LCOV_EXCL_BR_LINE 11: except branch
f_bKeyFound = TRUE;
}
}
} else {
l_bResult = FALSE;
}
if (l_bResult) {
// parse children and next nodes of current node
if (std::string::npos != l_uiLength) {
l_cKey = f_cUserKey.substr(l_uiLength + 1); // LCOV_EXCL_BR_LINE 11: except branch
l_eStatus = XMLGetValue(f_pCurrNode, l_cKey, f_cValue, f_bKeyFound);
}
l_cKey = l_cNodeName;
}
}
f_pCurrNode = f_pCurrNode->next;
}
} else {
l_eStatus = eFrameworkunifiedStatusNullPointer;
}
FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
return l_eStatus;
}
|