Remove unused directories and files in video_in_hal
[staging/basesystem.git] / nsframework / framework_unified / client / NS_XMLConfigeParser / library / src / ns_xmlparser.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_XmlParser
19 /// \brief    This file contains implementation of CXmlParser class.
20 ///
21 ////////////////////////////////////////////////////////////////////////////////////////////////////
22
23 ////////////////////////////////////////////////////////////////////////////////////////////////////
24 // Include Files
25 ////////////////////////////////////////////////////////////////////////////////////////////////////
26 #include <native_service/ns_xmlparser_if.h>
27 #include <fstream>
28 #include <string>
29 #include "ns_xmlconfig_parser_frameworkunifiedlog.h"
30
31 ////////////////////////////////////////////////////////////////////////////////////////////////
32 /// CXmlParser
33 /// Constructor
34 ////////////////////////////////////////////////////////////////////////////////////////////////
35 CXmlParser::CXmlParser(): m_pXmlDoc(NULL), m_cFileName(""), m_pRootNode(NULL) {
36   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
37
38   // for maintaining indentation
39   xmlKeepBlanksDefault(0);  // LCOV_EXCL_BR_LINE 11: except branch
40
41   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
42 }
43
44 ////////////////////////////////////////////////////////////////////////////////////////////////
45 /// ~CXmlParser
46 /// Destructor
47 ////////////////////////////////////////////////////////////////////////////////////////////////
48 CXmlParser::~CXmlParser() {
49   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
50
51   // clears the document structure
52   ClearDocument();  // LCOV_EXCL_BR_LINE 11: except branch
53
54   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
55 }
56
57 ////////////////////////////////////////////////////////////////////////////////////////////////
58 /// ParseXml
59 /// Parses the xml file and creates a document structure
60 ////////////////////////////////////////////////////////////////////////////////////////////////
61 EFrameworkunifiedStatus CXmlParser::ParseXml(std::string f_cFileName) {
62   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
63   EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
64
65   // clears the document structure
66   ClearDocument();  // LCOV_EXCL_BR_LINE 11: except branch
67
68   m_cFileName = f_cFileName;
69
70   if (IsReadable(m_cFileName)) {
71     // create document object structure
72     m_pXmlDoc = xmlParseFile(m_cFileName.c_str());
73
74     if (NULL != m_pXmlDoc) {
75       // sets the root node in class
76       m_pRootNode.SetXmlNodePtr(xmlDocGetRootElement(m_pXmlDoc));
77     } else {
78       FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Document not parsed successfully :: %s", f_cFileName.c_str());  // LCOV_EXCL_BR_LINE 15:marco defined in "native_service/ns_logger_if.h"
79       l_eStatus = eFrameworkunifiedStatusNullPointer;
80     }
81   } else {
82     l_eStatus = eFrameworkunifiedStatusInvldParam;
83     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "File not exists :: %s", f_cFileName.c_str());  // LCOV_EXCL_BR_LINE 15:marco defined in "native_service/ns_logger_if.h"
84   }
85
86   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
87   return l_eStatus;
88 }
89
90 ////////////////////////////////////////////////////////////////////////////////////////////////
91 /// CreateNewXmlDoc
92 /// Creates a new xml document with f_cRootNodeName as root node
93 ////////////////////////////////////////////////////////////////////////////////////////////////
94 EFrameworkunifiedStatus CXmlParser::CreateNewXmlDoc(std::string f_cRootNodeName) {
95   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
96   EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
97
98   xmlNodePtr l_pRootNodePtr = NULL;
99
100   // clears the document structure
101   ClearDocument();
102
103   if (0 < f_cRootNodeName.length()) {
104     // creates new document structure
105     m_pXmlDoc = xmlNewDoc((const xmlChar *)"1.0");
106     if (NULL != m_pXmlDoc) {
107       // create new node(root)
108       l_pRootNodePtr = xmlNewNode(NULL,
109                                   (const xmlChar *)f_cRootNodeName.c_str());
110       if (NULL != l_pRootNodePtr) {
111         // sets the root node in document structure
112         xmlDocSetRootElement(m_pXmlDoc, l_pRootNodePtr);
113
114         // sets the root node in class
115         m_pRootNode.SetXmlNodePtr(l_pRootNodePtr);
116       } else {
117         l_eStatus = eFrameworkunifiedStatusNullPointer;
118         FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Error creating new root node");  // LCOV_EXCL_BR_LINE 15:marco defined in "native_service/ns_logger_if.h"
119       }
120     } else {
121       l_eStatus = eFrameworkunifiedStatusNullPointer;
122       FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Unable to create new xml document structure");  // LCOV_EXCL_BR_LINE 15:marco defined in "native_service/ns_logger_if.h"
123     }
124   } else {
125     l_eStatus = eFrameworkunifiedStatusInvldParam;
126     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Invalid root node name :: %s", f_cRootNodeName.c_str());  // LCOV_EXCL_BR_LINE 15:marco defined in "native_service/ns_logger_if.h"
127   }
128
129   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
130   return l_eStatus;
131 }
132
133 ////////////////////////////////////////////////////////////////////////////////////////////////
134 /// SaveXml
135 /// Saves the document structure in an xml file
136 ////////////////////////////////////////////////////////////////////////////////////////////////
137 EFrameworkunifiedStatus CXmlParser::SaveXml(std::string f_cFileName) {
138   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
139   EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
140   std::string l_cFileName = "";
141
142   // if user explicitly provides file name
143   if (0 < f_cFileName.length()) {
144     l_cFileName.assign(f_cFileName);
145   } else {  // takes the file name from the CXmlParser object
146     l_cFileName.assign(m_cFileName);
147   }
148
149   if (NULL != m_pXmlDoc) {
150     if (IsReadable(l_cFileName)) {
151       // save the doc structure to the file
152       if (-1 == xmlSaveFormatFileEnc(l_cFileName.c_str(), m_pXmlDoc, "UTF-8", 1)) {
153         l_eStatus = eFrameworkunifiedStatusFail;
154       }
155     } else {
156       l_eStatus = eFrameworkunifiedStatusInvldParam;
157       FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "File not exists :: %s", f_cFileName.c_str());  // LCOV_EXCL_BR_LINE 15:marco defined in "native_service/ns_logger_if.h"
158     }
159   } else {
160     l_eStatus = eFrameworkunifiedStatusNullPointer;
161     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "m_pXmlDoc is NULL");   // LCOV_EXCL_BR_LINE 15:marco defined in "native_service/ns_logger_if.h"
162   }
163
164
165   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
166   return l_eStatus;
167 }
168
169 ////////////////////////////////////////////////////////////////////////////////////////////////
170 /// GetRootNode
171 /// Gets the root node object of xml
172 ////////////////////////////////////////////////////////////////////////////////////////////////
173 CXmlNode CXmlParser::GetRootNode() {
174   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
175
176   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
177   return m_pRootNode;
178 }
179
180 ////////////////////////////////////////////////////////////////////////////////////////////////
181 /// AddNewNode
182 /// Adds the new node to the parent node
183 ////////////////////////////////////////////////////////////////////////////////////////////////
184 CXmlNode CXmlParser::AddNewNode(CXmlNode m_pParentNode, std::string f_cNewNodeName, std::string f_cContent) {
185   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
186   CXmlNode l_pXmlNode;
187
188   if (!m_pParentNode.IsNull()) {
189     l_pXmlNode = m_pParentNode.AddChildNode(f_cNewNodeName, f_cContent);  // LCOV_EXCL_BR_LINE 11: except branch
190   }
191
192   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
193   return l_pXmlNode;
194 }
195
196 ////////////////////////////////////////////////////////////////////////////////////////////////
197 /// RemoveNode
198 /// Removes the node from the xml
199 ////////////////////////////////////////////////////////////////////////////////////////////////
200 EFrameworkunifiedStatus CXmlParser::RemoveNode(CXmlNode m_pNode) {
201   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
202   EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
203
204   if (!m_pNode.IsNull()) {
205     xmlUnlinkNode(m_pNode.m_pXmlNodePtr);
206     xmlFreeNode(m_pNode.m_pXmlNodePtr);
207   } else {
208     l_eStatus = eFrameworkunifiedStatusNullPointer;
209   }
210
211   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
212   return l_eStatus;
213 }
214
215 ////////////////////////////////////////////////////////////////////////////////////////////////
216 /// FindNode
217 /// Finds the first matching node, by tag name or path
218 ////////////////////////////////////////////////////////////////////////////////////////////////
219 CXmlNode CXmlParser::FindNode(std::string f_cNodePath, CXmlNode f_pCurrentNode) {
220   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
221   CXmlNode l_pXmlNode;
222
223   xmlXPathObjectPtr l_pXpathObj = GetNodeSet(f_cNodePath, f_pCurrentNode);  // LCOV_EXCL_BR_LINE 11: except branch
224
225   if (NULL != l_pXpathObj) {
226     if (NULL != l_pXpathObj->nodesetval && 0 < l_pXpathObj->nodesetval->nodeNr) {
227       l_pXmlNode.SetXmlNodePtr(l_pXpathObj->nodesetval->nodeTab[0]);  // LCOV_EXCL_BR_LINE 11: except branch
228     } else {
229       FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "NodeSetVal is NULL");  // LCOV_EXCL_BR_LINE 15:marco defined in "native_service/ns_logger_if.h"
230     }
231
232     xmlXPathFreeObject(l_pXpathObj);  // LCOV_EXCL_BR_LINE 11: except branch
233   } else {
234     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "xmlXPathObjectPtr is NULL");  // LCOV_EXCL_BR_LINE 15:marco defined in "native_service/ns_logger_if.h"
235   }
236
237   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
238   return l_pXmlNode;
239 }
240
241 ////////////////////////////////////////////////////////////////////////////////////////////////
242 /// FindAllNodes
243 /// Finds all the matching node, by tag name or path
244 ////////////////////////////////////////////////////////////////////////////////////////////////
245 TNodeList CXmlParser::FindAllNodes(std::string f_cNodePath, CXmlNode f_pCurrentNode) {
246   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
247   TNodeList l_pNodeList;
248
249   xmlXPathObjectPtr l_pXpathObj = GetNodeSet(f_cNodePath, f_pCurrentNode);  // LCOV_EXCL_BR_LINE 11: except branch
250
251   if (NULL != l_pXpathObj) {
252     if (NULL != l_pXpathObj->nodesetval && 0 < l_pXpathObj->nodesetval->nodeNr) {
253       for (UI_8 l_uiSize = 0;
254            l_uiSize < l_pXpathObj->nodesetval->nodeNr;
255            l_uiSize++) {
256         if (NULL != l_pXpathObj->nodesetval->nodeTab[l_uiSize]) {
257           CXmlNode l_pNode(l_pXpathObj->nodesetval->nodeTab[l_uiSize]);  // LCOV_EXCL_BR_LINE 11: except branch
258           l_pNodeList.push_back(l_pNode);
259         } else {
260           FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "node pointer is NULL");
261         }
262       }
263     } else {
264       FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "NodeSetVal is NULL");  // LCOV_EXCL_BR_LINE 15:marco defined in "native_service/ns_logger_if.h"
265     }
266
267     xmlXPathFreeObject(l_pXpathObj);  // LCOV_EXCL_BR_LINE 11: except branch
268   } else {
269     FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "xmlXPathObjectPtr is NULL");  // LCOV_EXCL_BR_LINE 15:marco defined in "native_service/ns_logger_if.h"
270   }
271
272   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
273   return l_pNodeList;
274 }
275
276 ////////////////////////////////////////////////////////////////////////////////////////////////
277 /// GetNodeSet
278 /// Gets the node set resulting from search of nodepath using XPath
279 ////////////////////////////////////////////////////////////////////////////////////////////////
280 xmlXPathObjectPtr CXmlParser::GetNodeSet(std::string f_cNodePath, CXmlNode f_pCurrentNode) {
281   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
282   xmlXPathObjectPtr l_pXpathObj = NULL;
283
284   xmlXPathContextPtr l_pXpathCtx = NULL;
285   CXmlNode l_pXmlNode;
286
287   if (NULL != m_pXmlDoc) {
288     if (!f_pCurrentNode.IsNull()) {
289       l_pXmlNode = f_pCurrentNode;
290     }
291
292     // Create xpath evaluation context
293     l_pXpathCtx = xmlXPathNewContext(m_pXmlDoc);  // LCOV_EXCL_BR_LINE 11: except branch
294
295     if (NULL != l_pXpathCtx) {
296       // set the current node in path context
297       l_pXpathCtx->node = l_pXmlNode.m_pXmlNodePtr;
298
299       // Evaluate xpath expression
300       l_pXpathObj = xmlXPathEvalExpression((const xmlChar *)f_cNodePath.c_str(), l_pXpathCtx);  // LCOV_EXCL_BR_LINE 11: except branch
301
302       if (NULL == l_pXpathObj) {
303         FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Unable to evaluate nodepath :: \"%s\"", f_cNodePath.c_str());   // LCOV_EXCL_BR_LINE 15:marco defined in "native_service/ns_logger_if.h"
304       }
305
306       // Cleanup of XPath data
307       xmlXPathFreeContext(l_pXpathCtx);  // LCOV_EXCL_BR_LINE 11: except branch
308     } else {
309       FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Unable to create new XPath context");   // LCOV_EXCL_BR_LINE 15:marco defined in "native_service/ns_logger_if.h"
310     }
311   } else {
312     FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "document object is NULL");   // LCOV_EXCL_BR_LINE 15:marco defined in "native_service/ns_logger_if.h"
313   }
314
315   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
316   return l_pXpathObj;
317 }
318
319 ////////////////////////////////////////////////////////////////////////////////////////////////
320 /// ClearDocument
321 /// Clears the document structure and resets the root node
322 ////////////////////////////////////////////////////////////////////////////////////////////////
323 VOID CXmlParser::ClearDocument() {
324   if (NULL != m_pXmlDoc) {
325     // free the document
326     xmlFreeDoc(m_pXmlDoc);
327     m_pXmlDoc = NULL;
328
329     // free the global variables that may have been allocated by the parser
330     xmlCleanupParser();
331   }
332
333   m_pRootNode = NULL;
334   m_cFileName.clear();
335 }
336
337 ////////////////////////////////////////////////////////////////////////////////////////////////
338 /// IsReadable
339 /// Checks whether the file exists or not
340 ////////////////////////////////////////////////////////////////////////////////////////////////
341 BOOL CXmlParser::IsReadable(const std::string &filename) {
342   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
343   BOOL l_bReadable = FALSE;
344
345   std::ifstream l_ifstream(filename.c_str());
346
347   if (l_ifstream.good()) {
348     l_bReadable = TRUE;
349     l_ifstream.close();  // LCOV_EXCL_BR_LINE 11: except branch
350   }
351
352   FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "-");
353   return l_bReadable;
354 }