summaryrefslogtreecommitdiffstats
path: root/nsframework/framework_unified/client/NS_XMLConfigeParser/library/src/ns_xml_writer.cpp
blob: 2876e2280c7dc81d5988ab599a9b27341a9efc0b (plain)
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
/*
 * @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 CXMLWriter class.
///
////////////////////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////////////////////
// Include Files
////////////////////////////////////////////////////////////////////////////////////////////////////
#include <native_service/ns_xml_writer.h>
#include <string>
#include "ns_xmlconfig_parser_frameworkunifiedlog.h"

#ifdef __cplusplus
extern "C" {
#endif

CXMLWriter *GetCXMLWriterObject(CHAR *f_cfilepath) {
  CXMLWriter *l_p_xml_writer = NULL;
  if (NULL != f_cfilepath) {
    l_p_xml_writer = new(std::nothrow) CXMLWriter(f_cfilepath);  // LCOV_EXCL_BR_LINE 11: except branch
  }
  return l_p_xml_writer;
}

CXMLWriter *GetCXMLWriterObjectNoParam() {
  return (new(std::nothrow) CXMLWriter());  // LCOV_EXCL_BR_LINE 11: except branch
}

#ifdef __cplusplus
}
#endif

CXMLWriter::CXMLWriter(): m_pXmlDoc(NULL), m_cFilePath("") {
  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
}

CXMLWriter::CXMLWriter(const std::string &f_cFilePath): m_pXmlDoc(NULL), m_cFilePath(f_cFilePath) {
  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "File Path %s", f_cFilePath.c_str());

  // create document object structure
  m_pXmlDoc = xmlParseFile(m_cFilePath.c_str());  // LCOV_EXCL_BR_LINE 11: except branch

  if (!m_pXmlDoc) {
    FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Pointer to document structure is NULL");
  }
}

CXMLWriter::~CXMLWriter() {
  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "Destructor");

  if (NULL != m_pXmlDoc) {
    xmlFreeDoc(m_pXmlDoc);  // LCOV_EXCL_BR_LINE 11: except branch
    m_pXmlDoc = NULL;
  }
}

EFrameworkunifiedStatus CXMLWriter::ParseFile(const std::string &f_cFilePath) {
  EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;
  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");

  l_eStatus = SetPath(f_cFilePath);

  if (NULL != m_pXmlDoc) {
    xmlFreeDoc(m_pXmlDoc);
    m_pXmlDoc = NULL;
  }

  // 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;
}

EFrameworkunifiedStatus CXMLWriter::SetPath(const std::string &f_cPath) {
  EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;

  if (!f_cPath.empty()) {
    // set the file path
    m_cFilePath.assign(f_cPath);
  } else {
    l_eStatus = eFrameworkunifiedStatusInvldParam;
  }

  return l_eStatus;
}

VOID CXMLWriter::SetDataPtr(PVOID f_pData) {
  m_pXmlDoc = NULL;
  if (NULL != f_pData) {
    // set the doc pointer
    m_pXmlDoc = static_cast<xmlDocPtr>(f_pData);
  } else {
    FRAMEWORKUNIFIEDLOG(ZONE_NS_ERR, __FUNCTION__, "Data Pointer not set in xml writer");  // LCOV_EXCL_BR_LINE 15:marco defined in "native_service/ns_logger_if.h"
  }
}

EFrameworkunifiedStatus CXMLWriter::SetValue(const std::string &f_cKey, std::string f_cValue) {
  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "Key %s", f_cKey.c_str());
  EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;

  if (NULL != m_pXmlDoc && (!f_cKey.empty())) {
    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;
          l_eStatus = XMLSetValue(l_pCurrNode, l_cUserKey, f_cValue, l_bKeyFound);

          if (!l_bKeyFound) {
            l_eStatus = eFrameworkunifiedStatusFail;
          }
        }
      } else {
        l_eStatus = eFrameworkunifiedStatusInvldParam;
      }
    } else {
      l_eStatus = eFrameworkunifiedStatusNullPointer;
    }
  } 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 = eFrameworkunifiedStatusInvldParam;
  }

  return l_eStatus;
}

EFrameworkunifiedStatus CXMLWriter::XMLSetValue(xmlNodePtr f_pCurrNode,
                                   const std::string &f_cUserKey,
                                   std::string &f_cValue,
                                   BOOL &f_bKeyFound) {
  EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;

  if (!f_cUserKey.empty() && NULL != f_pCurrNode) {
    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);
            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)) {
              // set the value
              xmlNodeSetContent(f_pCurrNode, (const xmlChar *)f_cValue.c_str());

              f_bKeyFound = TRUE;
            }
          }
        }

        if (l_bResult) {
          // parse children and next nodes of current node
          l_cKey = f_cUserKey.substr(l_uiLength + 1);  // LCOV_EXCL_BR_LINE 11: except branch
          l_eStatus = XMLSetValue(f_pCurrNode, l_cKey, f_cValue, f_bKeyFound);
        }
      }

      f_pCurrNode = f_pCurrNode->next;
    }
  } else {
    l_eStatus = eFrameworkunifiedStatusNullPointer;
  }

  return l_eStatus;
}

EFrameworkunifiedStatus CXMLWriter::SaveData() {
  FRAMEWORKUNIFIEDLOG(ZONE_FUNC, __FUNCTION__, "+");
  EFrameworkunifiedStatus l_eStatus = eFrameworkunifiedStatusOK;

  // save the doc structure to the file
  if (-1 == xmlSaveFileEnc(m_cFilePath.c_str(), m_pXmlDoc, "UTF-8")) {
    l_eStatus = eFrameworkunifiedStatusFail;
  }

  return l_eStatus;
}