2 * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR CORPORATION.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 ////////////////////////////////////////////////////////////////////////////////////////////////////
18 /// \ingroup tag_NS_ConfigParser
19 /// \brief This file contains implementation of CXMLReader class.
21 ////////////////////////////////////////////////////////////////////////////////////////////////////
23 ////////////////////////////////////////////////////////////////////////////////////////////////////
25 ////////////////////////////////////////////////////////////////////////////////////////////////////
26 #include <native_service/ns_xml_reader.h>
28 #include "ns_xmlconfig_parser_frameworkunifiedlog.h"
34 CXMLReader *GetCXMLReaderObject(CHAR *f_cfilepath) {
35 CXMLReader *l_p_xml_reader = NULL;
36 if (NULL != f_cfilepath) {
37 l_p_xml_reader = new(std::nothrow) CXMLReader(f_cfilepath); // LCOV_EXCL_BR_LINE 11: except branch
39 return l_p_xml_reader;
46 CXMLReader::CXMLReader(): m_pXmlDoc(NULL) {
49 CXMLReader::CXMLReader(const std::string &f_cFilePath): m_pXmlDoc(NULL) {
50 FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "File Path %s", f_cFilePath.c_str());
52 // create document object structure
53 m_pXmlDoc = xmlParseFile(f_cFilePath.c_str()); // LCOV_EXCL_BR_LINE 11: except branch
56 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"
60 CXMLReader::~CXMLReader() {
61 FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "Destructor");
63 if (NULL != m_pXmlDoc) {
64 xmlFreeDoc(m_pXmlDoc); // LCOV_EXCL_BR_LINE 11: except branch
69 EFrameworkunifiedStatus CXMLReader::ParseFile(const std::string &f_cFilePath) {
70 EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
71 FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
73 // create document object structure
74 m_pXmlDoc = xmlParseFile(f_cFilePath.c_str());
76 if (NULL == m_pXmlDoc) {
77 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Pointer to document structure is NULL");
78 l_eStatus = eFrameworkunifiedStatusNullPointer;
81 FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
85 PVOID CXMLReader::GetDataPtr() {
89 std::string CXMLReader::GetValue(const std::string &f_cKey) {
90 FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "Key %s", f_cKey.c_str());
92 std::string l_cValue = "";
94 if (NULL != m_pXmlDoc && (!f_cKey.empty())) {
95 std::string l_cUserKey = ""; // LCOV_EXCL_BR_LINE 11: except branch
96 l_cUserKey.assign(f_cKey);
98 // get the root node element
99 xmlNodePtr l_pCurrNode = xmlDocGetRootElement(m_pXmlDoc); // LCOV_EXCL_BR_LINE 11: except branch
101 if (NULL != l_pCurrNode) {
102 // remove the root node name from key
103 size_t l_uiLength = l_cUserKey.find('.'); // LCOV_EXCL_BR_LINE 11: except branch
105 if (std::string::npos != l_uiLength) {
106 l_cUserKey = l_cUserKey.substr(l_uiLength + 1); // LCOV_EXCL_BR_LINE 11: except branch
108 // if root node name matches with the name in received key
109 if (!(f_cKey.substr(0, l_uiLength)).compare((PCSTR)l_pCurrNode->name)) {
110 l_cValue = XMLGetValue(l_pCurrNode,
116 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"
118 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"
125 std::string CXMLReader::XMLGetValue(xmlNodePtr f_pCurrNode,
126 const std::string &f_cUserKey) {
127 std::string l_cReturnValue = "";
129 if (NULL != f_pCurrNode && (!f_cUserKey.empty())) {
130 std::string l_cKey = ""; // LCOV_EXCL_BR_LINE 11: except branch
132 // result of previous attribute of same node
133 BOOL l_bResult = TRUE;
135 // this parameter specifies whether to AND or OR the current result with previous result
136 // TRUE means AND while FALSE means OR
137 BOOL l_bOperation = FALSE;
139 // position of "@" in user key if exists
140 size_t l_uiAttrLen = 0;
142 // get the single attribute from list of attributes of node
143 std::string l_cAttr = ""; // LCOV_EXCL_BR_LINE 11: except branch
144 std::string l_cNodeName = ""; // LCOV_EXCL_BR_LINE 11: except branch
146 // contains the single attribute key, value
147 std::string l_cAttrOpt = ""; // LCOV_EXCL_BR_LINE 11: except branch
149 // l_cAttrId of the attribute
150 std::string l_cAttrId = ""; // LCOV_EXCL_BR_LINE 11: except branch
151 // value of the attribute
152 std::string l_cAttrValue = ""; // LCOV_EXCL_BR_LINE 11: except branch
154 size_t l_uiSplitProp = 0;
155 size_t l_uiAttrSplit = 0;
156 BOOL l_bCurrentResult = FALSE;
158 l_cKey.assign(f_cUserKey);
160 // position of first "." in user key
161 size_t l_uiLength = l_cKey.find('.'); // LCOV_EXCL_BR_LINE 11: except branch
162 if (std::string::npos != l_uiLength) {
163 // get the current node name string with node attributes
164 l_cKey = l_cKey.substr(0, l_uiLength); // LCOV_EXCL_BR_LINE 11: except branch
167 l_cNodeName.assign(l_cKey);
169 // get children of current node
170 f_pCurrNode = f_pCurrNode->children;
172 while (NULL != f_pCurrNode) {
173 // check whether current node is an element node.
174 if (XML_ELEMENT_NODE == f_pCurrNode->type) {
176 l_bOperation = FALSE;
180 // check whether key contains attribute for current node
181 if (std::string::npos != (l_uiAttrLen = l_cKey.find('@'))) {
182 // get the attribute string from node key
183 l_cAttr.assign(l_cKey, (l_uiAttrLen + 1), std::string::npos); // LCOV_EXCL_BR_LINE 11: except branch
185 // remove the attribute string from key string
186 l_cKey = l_cKey.substr(0, l_uiAttrLen); // LCOV_EXCL_BR_LINE 11: except branch
189 // check whether node name string matches with the current node name
190 if (!l_cKey.compare((PCSTR)f_pCurrNode->name)) {
191 l_cAttrOpt.assign(l_cAttr);
193 while (0 < l_cAttr.length()) {
194 // initialize variables
196 l_cAttrValue.clear();
199 l_bCurrentResult = FALSE;
201 // check whether node have multiple attributes
202 if (std::string::npos != (l_uiSplitProp = l_cAttr.find('|'))) {
203 l_cAttrOpt = l_cAttr.substr(0, l_uiSplitProp);
205 if (std::string::npos != (l_uiAttrSplit = l_cAttrOpt.find('='))) {
206 l_cAttrId = l_cAttrOpt.substr(0, l_uiAttrSplit);
207 l_cAttrValue = l_cAttrOpt.substr(l_uiAttrSplit + 1);
209 } else if (std::string::npos != (l_uiSplitProp = l_cAttr.find('&'))) {
210 l_cAttrOpt = l_cAttr.substr(0, l_uiSplitProp);
212 if (std::string::npos != (l_uiAttrSplit = l_cAttrOpt.find('='))) {
213 l_cAttrId = l_cAttrOpt.substr(0, l_uiAttrSplit);
214 l_cAttrValue = l_cAttrOpt.substr(l_uiAttrSplit + 1);
217 l_uiSplitProp = l_cAttr.length() - 1;
218 if (std::string::npos != (l_uiAttrSplit = l_cAttr.find('='))) {
219 l_cAttrId = l_cAttr.substr(0, l_uiAttrSplit); // LCOV_EXCL_BR_LINE 11: except branch
220 l_cAttrValue = l_cAttr.substr(l_uiAttrSplit + 1); // LCOV_EXCL_BR_LINE 11: except branch
224 // compare the value of attributes
225 xmlChar *l_pAttrValue = xmlGetProp(f_pCurrNode, (const xmlChar *)l_cAttrId.c_str()); // LCOV_EXCL_BR_LINE 11: except branch
226 if (NULL != l_pAttrValue) {
227 if (!l_cAttrValue.compare((PCSTR)l_pAttrValue)) {
228 l_bCurrentResult = TRUE;
231 xmlFree(l_pAttrValue); // LCOV_EXCL_BR_LINE 11: except branch
234 // combine the result of all attributes
236 l_bResult = l_bResult && l_bCurrentResult;
238 l_bResult = l_bResult || l_bCurrentResult;
241 if ('|' == l_cAttr[l_uiSplitProp]) {
244 l_bOperation = FALSE;
247 if ((!l_bResult) && (!l_bOperation)) {
248 // break from current while loop
252 l_cAttr = l_cAttr.substr(l_uiSplitProp + 1); // LCOV_EXCL_BR_LINE 11: except branch
255 // if attributes not matched move to next node
257 // search for the same node name in next child nodes
258 l_cKey.assign(l_cNodeName);
260 // read text of current node
261 if ((NULL != f_pCurrNode->children) &&
262 (XML_TEXT_NODE == f_pCurrNode->children->type) &&
263 (NULL == f_pCurrNode->children->next)) {
264 // append the result in string
265 l_cReturnValue.append((PCSTR) f_pCurrNode->children->content); // LCOV_EXCL_BR_LINE 11: except branch
273 // parse children and next nodes of current node
274 if (std::string::npos != l_uiLength) {
275 l_cKey = f_cUserKey.substr(l_uiLength + 1); // LCOV_EXCL_BR_LINE 11: except branch
277 std::string l_cValue = XMLGetValue(f_pCurrNode, l_cKey); // LCOV_EXCL_BR_LINE 11: except branch
279 if (!l_cReturnValue.empty() && !l_cValue.empty()) {
280 l_cReturnValue.append("\n");
283 // append the received string
284 l_cReturnValue.append(l_cValue);
287 l_cKey = l_cNodeName;
291 f_pCurrNode = f_pCurrNode->next;
295 return l_cReturnValue;
298 EFrameworkunifiedStatus CXMLReader::GetValue(const std::string &f_cKey, std::string &f_cValue) {
299 EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
300 FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
302 if (NULL != m_pXmlDoc && (!f_cKey.empty())) {
303 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"
305 std::string l_cUserKey = "";
306 l_cUserKey.assign(f_cKey);
308 // get the root node element
309 xmlNodePtr l_pCurrNode = xmlDocGetRootElement(m_pXmlDoc);
311 if (NULL != l_pCurrNode) {
312 // remove the root node name from key
313 size_t l_uiLength = l_cUserKey.find('.'); // LCOV_EXCL_BR_LINE 11: except branch
315 if (std::string::npos != l_uiLength) {
316 l_cUserKey = l_cUserKey.substr(l_uiLength + 1); // LCOV_EXCL_BR_LINE 11: except branch
318 // if root node name matches with the name in received key
319 if (!(f_cKey.substr(0, l_uiLength)).compare((PCSTR)l_pCurrNode->name)) {
320 BOOL l_bKeyFound = FALSE;
322 XMLGetValue(l_pCurrNode, l_cUserKey, f_cValue, l_bKeyFound);
325 l_eStatus = eFrameworkunifiedStatusFail;
329 l_eStatus = eFrameworkunifiedStatusInvldParam;
332 FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Root Node ptr is NULL"); // LCOV_EXCL_BR_LINE 15:marco defined in "native_service/ns_logger_if.h"
333 l_eStatus = eFrameworkunifiedStatusNullPointer;
336 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"
338 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"
339 l_eStatus = eFrameworkunifiedStatusNullPointer;
342 FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
346 EFrameworkunifiedStatus CXMLReader::XMLGetValue(xmlNodePtr f_pCurrNode, const std::string &f_cUserKey, std::string &f_cValue,
348 EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusFail;
349 FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
351 if (NULL != f_pCurrNode && (!f_cUserKey.empty())) {
352 FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__, "Key :: %s", f_cUserKey.c_str()); // LCOV_EXCL_BR_LINE 15:marco defined in "native_service/ns_logger_if.h"
354 std::string l_cKey = "";
356 // result of previous attribute of same node
357 BOOL l_bResult = TRUE;
359 // this parameter specifies whether to AND or OR the current result with previous result
360 // TRUE means AND while FALSE means OR
361 BOOL l_bOperation = FALSE;
363 // position of "@" in user key if exists
364 size_t l_uiAttrLen = 0;
366 // get the single attribute from list of attributes of node
367 std::string l_cAttr = ""; // LCOV_EXCL_BR_LINE 11: except branch
368 std::string l_cNodeName = ""; // LCOV_EXCL_BR_LINE 11: except branch
370 // contains the single attribute key, value
371 std::string l_cAttrOpt = ""; // LCOV_EXCL_BR_LINE 11: except branch
373 // l_cAttrId of the attribute
374 std::string l_cAttrId = ""; // LCOV_EXCL_BR_LINE 11: except branch
375 // value of the attribute
376 std::string l_cAttrValue = ""; // LCOV_EXCL_BR_LINE 11: except branch
378 size_t l_uiSplitProp = 0;
379 size_t l_uiAttrSplit = 0;
380 BOOL l_bCurrentResult = FALSE;
382 l_cKey.assign(f_cUserKey);
384 // position of first "." in user key
385 size_t l_uiLength = l_cKey.find('.'); // LCOV_EXCL_BR_LINE 11: except branch
386 if (std::string::npos != l_uiLength) {
387 // get the current node name string with node attributes
388 l_cKey = l_cKey.substr(0, l_uiLength); // LCOV_EXCL_BR_LINE 11: except branch
390 l_cNodeName.assign(l_cKey);
392 // get children of current node
393 f_pCurrNode = f_pCurrNode->children;
395 while (NULL != f_pCurrNode) {
396 // check whether current node is an element node.
397 if (XML_ELEMENT_NODE == f_pCurrNode->type) {
399 l_bOperation = FALSE;
403 // check whether key contains attribute for current node
404 if (std::string::npos != (l_uiAttrLen = l_cKey.find('@'))) {
405 // get the attribute string from node key
406 l_cAttr.assign(l_cKey, (l_uiAttrLen + 1), std::string::npos); // LCOV_EXCL_BR_LINE 11: except branch
408 // remove the attribute string from key string
409 l_cKey = l_cKey.substr(0, l_uiAttrLen); // LCOV_EXCL_BR_LINE 11: except branch
412 // check whether node name string matches with the current node name
413 if (!l_cKey.compare((PCSTR)f_pCurrNode->name)) {
414 l_cAttrOpt.assign(l_cAttr);
416 while (0 < l_cAttr.length()) {
417 // initialize variables
419 l_cAttrValue.clear();
422 l_bCurrentResult = FALSE;
424 // check whether node have multiple attributes
425 if (std::string::npos != (l_uiSplitProp = l_cAttr.find('|'))) {
426 l_cAttrOpt = l_cAttr.substr(0, l_uiSplitProp);
428 if (std::string::npos != (l_uiAttrSplit = l_cAttrOpt.find('='))) {
429 l_cAttrId = l_cAttrOpt.substr(0, l_uiAttrSplit);
430 l_cAttrValue = l_cAttrOpt.substr(l_uiAttrSplit + 1);
432 } else if (std::string::npos != (l_uiSplitProp = l_cAttr.find('&'))) {
433 l_cAttrOpt = l_cAttr.substr(0, l_uiSplitProp);
435 if (std::string::npos != (l_uiAttrSplit = l_cAttrOpt.find('='))) {
436 l_cAttrId = l_cAttrOpt.substr(0, l_uiAttrSplit);
437 l_cAttrValue = l_cAttrOpt.substr(l_uiAttrSplit + 1);
440 l_uiSplitProp = l_cAttr.length() - 1;
441 if (std::string::npos != (l_uiAttrSplit = l_cAttr.find('='))) {
442 l_cAttrId = l_cAttr.substr(0, l_uiAttrSplit); // LCOV_EXCL_BR_LINE 11: except branch
443 l_cAttrValue = l_cAttr.substr(l_uiAttrSplit + 1); // LCOV_EXCL_BR_LINE 11: except branch
447 // compare the value of attributes
448 xmlChar *l_pAttrValue = xmlGetProp(f_pCurrNode, (const xmlChar *)l_cAttrId.c_str()); // LCOV_EXCL_BR_LINE 11: except branch
449 if (NULL != l_pAttrValue) {
450 if (!l_cAttrValue.compare((PCSTR)l_pAttrValue)) {
451 l_bCurrentResult = TRUE;
454 xmlFree(l_pAttrValue); // LCOV_EXCL_BR_LINE 11: except branch
458 // combine the result of all attributes
460 l_bResult = l_bResult && l_bCurrentResult;
462 l_bResult = l_bResult || l_bCurrentResult;
465 if ('|' == l_cAttr[l_uiSplitProp]) {
468 l_bOperation = FALSE;
471 if ((!l_bResult) && (!l_bOperation)) {
472 // break from current while loop
476 l_cAttr = l_cAttr.substr(l_uiSplitProp + 1); // LCOV_EXCL_BR_LINE 11: except branch
479 // if attributes not matched move to next node
481 // search for the same node name in next child nodes
482 l_cKey.assign(l_cNodeName);
484 // read text of current node
485 if ((NULL != f_pCurrNode->children) &&
486 (XML_TEXT_NODE == f_pCurrNode->children->type) &&
487 (NULL == f_pCurrNode->children->next)) {
488 if (!f_cValue.empty()) {
489 f_cValue.append("\n");
492 // append the result in string
493 f_cValue.append((PCSTR) f_pCurrNode->children->content); // LCOV_EXCL_BR_LINE 11: except branch
503 // parse children and next nodes of current node
504 if (std::string::npos != l_uiLength) {
505 l_cKey = f_cUserKey.substr(l_uiLength + 1); // LCOV_EXCL_BR_LINE 11: except branch
507 l_eStatus = XMLGetValue(f_pCurrNode, l_cKey, f_cValue, f_bKeyFound);
510 l_cKey = l_cNodeName;
514 f_pCurrNode = f_pCurrNode->next;
517 l_eStatus = eFrameworkunifiedStatusNullPointer;
520 FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");