Remove unused directories and files in video_in_hal
[staging/basesystem.git] / nsframework / framework_unified / client / NS_XMLConfigeParser / library / src / ns_xml_reader.cpp
1 /*
2  * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR CORPORATION.
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 ////////////////////////////////////////////////////////////////////////////////////////////////////
18 /// \ingroup  tag_NS_ConfigParser
19 /// \brief    This file contains implementation of CXMLReader class.
20 ///
21 ////////////////////////////////////////////////////////////////////////////////////////////////////
22
23 ////////////////////////////////////////////////////////////////////////////////////////////////////
24 // Include Files
25 ////////////////////////////////////////////////////////////////////////////////////////////////////
26 #include <native_service/ns_xml_reader.h>
27 #include <string>
28 #include "ns_xmlconfig_parser_frameworkunifiedlog.h"
29
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33
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
38   }
39   return l_p_xml_reader;
40 }
41
42 #ifdef __cplusplus
43 }
44 #endif
45
46 CXMLReader::CXMLReader(): m_pXmlDoc(NULL) {
47 }
48
49 CXMLReader::CXMLReader(const std::string &f_cFilePath): m_pXmlDoc(NULL) {
50   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "File Path %s", f_cFilePath.c_str());
51
52   // create document object structure
53   m_pXmlDoc = xmlParseFile(f_cFilePath.c_str());  // LCOV_EXCL_BR_LINE 11: except branch
54
55   if (!m_pXmlDoc) {
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"
57   }
58 }
59
60 CXMLReader::~CXMLReader() {
61   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "Destructor");
62
63   if (NULL != m_pXmlDoc) {
64     xmlFreeDoc(m_pXmlDoc);  // LCOV_EXCL_BR_LINE 11: except branch
65     m_pXmlDoc = NULL;
66   }
67 }
68
69 EFrameworkunifiedStatus CXMLReader::ParseFile(const std::string &f_cFilePath) {
70   EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
71   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
72
73   // create document object structure
74   m_pXmlDoc = xmlParseFile(f_cFilePath.c_str());
75
76   if (NULL == m_pXmlDoc) {
77     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Pointer to document structure is NULL");
78     l_eStatus = eFrameworkunifiedStatusNullPointer;
79   }
80
81   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
82   return l_eStatus;
83 }
84
85 PVOID CXMLReader::GetDataPtr() {
86   return m_pXmlDoc;
87 }
88
89 std::string CXMLReader::GetValue(const std::string &f_cKey) {
90   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "Key %s", f_cKey.c_str());
91
92   std::string l_cValue = "";
93
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);
97
98     // get the root node element
99     xmlNodePtr l_pCurrNode = xmlDocGetRootElement(m_pXmlDoc);  // LCOV_EXCL_BR_LINE 11: except branch
100
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
104
105       if (std::string::npos != l_uiLength) {
106         l_cUserKey = l_cUserKey.substr(l_uiLength + 1);  // LCOV_EXCL_BR_LINE 11: except branch
107
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,
111                                  l_cUserKey);
112         }
113       }
114     }
115
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"
117   } else {
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"
119   }
120
121
122   return l_cValue;
123 }
124
125 std::string CXMLReader::XMLGetValue(xmlNodePtr f_pCurrNode,
126                                     const std::string &f_cUserKey) {
127   std::string l_cReturnValue = "";
128
129   if (NULL != f_pCurrNode && (!f_cUserKey.empty())) {
130     std::string l_cKey = "";   // LCOV_EXCL_BR_LINE 11: except branch
131
132     // result of previous attribute of same node
133     BOOL l_bResult = TRUE;
134
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;
138
139     // position of "@" in user key if exists
140     size_t l_uiAttrLen = 0;
141
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
145
146     // contains the single attribute key, value
147     std::string l_cAttrOpt = "";   // LCOV_EXCL_BR_LINE 11: except branch
148
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
153
154     size_t l_uiSplitProp = 0;
155     size_t l_uiAttrSplit = 0;
156     BOOL l_bCurrentResult = FALSE;
157
158     l_cKey.assign(f_cUserKey);
159
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
165     }
166
167     l_cNodeName.assign(l_cKey);
168
169     // get children of current node
170     f_pCurrNode = f_pCurrNode->children;
171
172     while (NULL != f_pCurrNode) {
173       // check whether current node is an element node.
174       if (XML_ELEMENT_NODE == f_pCurrNode->type) {
175         l_bResult = TRUE;
176         l_bOperation = FALSE;
177         l_uiAttrLen = 0;
178         l_cAttr.clear();
179
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
184
185           // remove the attribute string from key string
186           l_cKey = l_cKey.substr(0, l_uiAttrLen);  // LCOV_EXCL_BR_LINE 11: except branch
187         }
188
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);
192
193           while (0 < l_cAttr.length()) {
194             // initialize variables
195             l_cAttrId.clear();
196             l_cAttrValue.clear();
197             l_uiSplitProp = 0;
198             l_uiAttrSplit = 0;
199             l_bCurrentResult = FALSE;
200
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);
204
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);
208               }
209             } else if (std::string::npos != (l_uiSplitProp = l_cAttr.find('&'))) {
210               l_cAttrOpt = l_cAttr.substr(0, l_uiSplitProp);
211
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);
215               }
216             } else {
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
221               }
222             }
223
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;
229               }
230             }
231             xmlFree(l_pAttrValue);  // LCOV_EXCL_BR_LINE 11: except branch
232             l_pAttrValue = NULL;
233
234             // combine the result of all attributes
235             if (!l_bOperation) {
236               l_bResult = l_bResult && l_bCurrentResult;
237             } else {
238               l_bResult = l_bResult || l_bCurrentResult;
239             }
240
241             if ('|' == l_cAttr[l_uiSplitProp]) {
242               l_bOperation = TRUE;
243             } else {
244               l_bOperation = FALSE;
245             }
246
247             if ((!l_bResult) && (!l_bOperation)) {
248               // break from current while loop
249               break;
250             }
251
252             l_cAttr = l_cAttr.substr(l_uiSplitProp + 1);  // LCOV_EXCL_BR_LINE 11: except branch
253           }
254
255           // if attributes not matched move to next node
256           if (!l_bResult) {
257             // search for the same node name in next child nodes
258             l_cKey.assign(l_cNodeName);
259           } else {
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
266             }
267           }
268         } else {
269           l_bResult = FALSE;
270         }
271
272         if (l_bResult) {
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
276
277             std::string l_cValue = XMLGetValue(f_pCurrNode, l_cKey);  // LCOV_EXCL_BR_LINE 11: except branch
278
279             if (!l_cReturnValue.empty() && !l_cValue.empty()) {
280               l_cReturnValue.append("\n");
281             }
282
283             // append the received string
284             l_cReturnValue.append(l_cValue);
285           }
286
287           l_cKey = l_cNodeName;
288         }
289       }
290
291       f_pCurrNode = f_pCurrNode->next;
292     }
293   }
294
295   return l_cReturnValue;
296 }
297
298 EFrameworkunifiedStatus CXMLReader::GetValue(const std::string &f_cKey, std::string &f_cValue) {
299   EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
300   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
301
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"
304
305     std::string l_cUserKey = "";
306     l_cUserKey.assign(f_cKey);
307
308     // get the root node element
309     xmlNodePtr l_pCurrNode = xmlDocGetRootElement(m_pXmlDoc);
310
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
314
315       if (std::string::npos != l_uiLength) {
316         l_cUserKey = l_cUserKey.substr(l_uiLength + 1);  // LCOV_EXCL_BR_LINE 11: except branch
317
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;
321
322           XMLGetValue(l_pCurrNode, l_cUserKey, f_cValue, l_bKeyFound);
323
324           if (!l_bKeyFound) {
325             l_eStatus = eFrameworkunifiedStatusFail;
326           }
327         }
328       } else {
329         l_eStatus = eFrameworkunifiedStatusInvldParam;
330       }
331     } else {
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;
334     }
335
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"
337   } else {
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;
340   }
341
342   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
343   return l_eStatus;
344 }
345
346 EFrameworkunifiedStatus CXMLReader::XMLGetValue(xmlNodePtr f_pCurrNode, const std::string &f_cUserKey, std::string &f_cValue,
347                                    BOOL &f_bKeyFound) {
348   EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusFail;
349   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
350
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"
353
354     std::string l_cKey = "";
355
356     // result of previous attribute of same node
357     BOOL l_bResult = TRUE;
358
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;
362
363     // position of "@" in user key if exists
364     size_t l_uiAttrLen = 0;
365
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
369
370     // contains the single attribute key, value
371     std::string l_cAttrOpt = "";  // LCOV_EXCL_BR_LINE 11: except branch
372
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
377
378     size_t l_uiSplitProp = 0;
379     size_t l_uiAttrSplit = 0;
380     BOOL l_bCurrentResult = FALSE;
381
382     l_cKey.assign(f_cUserKey);
383
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
389     }
390     l_cNodeName.assign(l_cKey);
391
392     // get children of current node
393     f_pCurrNode = f_pCurrNode->children;
394
395     while (NULL != f_pCurrNode) {
396       // check whether current node is an element node.
397       if (XML_ELEMENT_NODE == f_pCurrNode->type) {
398         l_bResult = TRUE;
399         l_bOperation = FALSE;
400         l_uiAttrLen = 0;
401         l_cAttr.clear();
402
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
407
408           // remove the attribute string from key string
409           l_cKey = l_cKey.substr(0, l_uiAttrLen);  // LCOV_EXCL_BR_LINE 11: except branch
410         }
411
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);
415
416           while (0 < l_cAttr.length()) {
417             // initialize variables
418             l_cAttrId.clear();
419             l_cAttrValue.clear();
420             l_uiSplitProp = 0;
421             l_uiAttrSplit = 0;
422             l_bCurrentResult = FALSE;
423
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);
427
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);
431               }
432             } else if (std::string::npos != (l_uiSplitProp = l_cAttr.find('&'))) {
433               l_cAttrOpt = l_cAttr.substr(0, l_uiSplitProp);
434
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);
438               }
439             } else {
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
444               }
445             }
446
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;
452               }
453
454               xmlFree(l_pAttrValue);   // LCOV_EXCL_BR_LINE 11: except branch
455               l_pAttrValue = NULL;
456             }
457
458             // combine the result of all attributes
459             if (!l_bOperation) {
460               l_bResult = l_bResult && l_bCurrentResult;
461             } else {
462               l_bResult = l_bResult || l_bCurrentResult;
463             }
464
465             if ('|' == l_cAttr[l_uiSplitProp]) {
466               l_bOperation = TRUE;
467             } else {
468               l_bOperation = FALSE;
469             }
470
471             if ((!l_bResult) && (!l_bOperation)) {
472               // break from current while loop
473               break;
474             }
475
476             l_cAttr = l_cAttr.substr(l_uiSplitProp + 1);  // LCOV_EXCL_BR_LINE 11: except branch
477           }
478
479           // if attributes not matched move to next node
480           if (!l_bResult) {
481             // search for the same node name in next child nodes
482             l_cKey.assign(l_cNodeName);
483           } else {
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");
490               }
491
492               // append the result in string
493               f_cValue.append((PCSTR) f_pCurrNode->children->content);  // LCOV_EXCL_BR_LINE 11: except branch
494
495               f_bKeyFound = TRUE;
496             }
497           }
498         } else {
499           l_bResult = FALSE;
500         }
501
502         if (l_bResult) {
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
506
507             l_eStatus = XMLGetValue(f_pCurrNode, l_cKey, f_cValue, f_bKeyFound);
508           }
509
510           l_cKey = l_cNodeName;
511         }
512       }
513
514       f_pCurrNode = f_pCurrNode->next;
515     }
516   } else {
517     l_eStatus = eFrameworkunifiedStatusNullPointer;
518   }
519
520   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
521   return l_eStatus;
522 }