1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "form_xml_parser.h"
17 
18 #include <string>
19 
20 #include "fms_log_wrapper.h"
21 #include "form_mgr_errors.h"
22 #include "form_constants.h"
23 
24 namespace OHOS::AppExecFwk {
~FormXMLParser()25 FormXMLParser::~FormXMLParser()
26 {
27     HILOG_DEBUG("XMLParser Destroying the parser");
28     std::lock_guard<std::mutex> lock(xmlDocumentMutex_);
29     if (xmlDocument_ != nullptr) {
30         xmlFreeDoc(xmlDocument_);
31         xmlDocument_ = nullptr;
32     }
33 }
34 
Parse()35 int32_t FormXMLParser::Parse()
36 {
37     HILOG_DEBUG("FormXMLParser Parse");
38     std::lock_guard<std::mutex> lock(xmlDocumentMutex_);
39     xmlDocument_ = xmlReadFile(CONFIG_FILE, nullptr, XML_PARSE_NOBLANKS);
40     if (xmlDocument_ == nullptr) {
41         HILOG_ERROR("null xmlDocument_");
42         return ERR_APPEXECFWK_FORM_COMMON_CODE;
43     }
44 
45     xmlNodePtr root = xmlDocGetRootElement(xmlDocument_);
46     if (root == nullptr) {
47         HILOG_ERROR("xmlDocGetRootElement failed");
48         return ERR_APPEXECFWK_FORM_COMMON_CODE;
49     }
50 
51     if (!CheckRootNode(root)) {
52         HILOG_ERROR("CheckRootNode failed");
53         return ERR_APPEXECFWK_FORM_COMMON_CODE;
54     }
55     if (!ParseInternal(root)) {
56         HILOG_ERROR("ParseInternal failed");
57         return ERR_APPEXECFWK_FORM_COMMON_CODE;
58     }
59     return ERR_OK;
60 }
61 
CheckRootNode(xmlNodePtr & node)62 bool FormXMLParser::CheckRootNode(xmlNodePtr &node)
63 {
64     HILOG_DEBUG("CheckRootNode start");
65     if (node == nullptr || node->name == nullptr ||
66         xmlStrcmp(node->name, reinterpret_cast<const xmlChar*>("FORM"))) {
67         HILOG_ERROR("get root element failed");
68         return false;
69     }
70     HILOG_DEBUG("success");
71     return true;
72 }
73 
ParseInternal(xmlNodePtr & node)74 bool FormXMLParser::ParseInternal(xmlNodePtr &node)
75 {
76     HILOG_DEBUG("ParseInternal start");
77     xmlNodePtr curNodePtr = node->xmlChildrenNode;
78     std::string quantityConfig = "quantityConfig";
79     std::string nodeName = reinterpret_cast<const char*>(curNodePtr->name);
80     if (nodeName != quantityConfig) {
81         HILOG_ERROR("invalid node");
82         return false;
83     }
84     for (xmlNodePtr curChildNodePtr = curNodePtr->xmlChildrenNode; curChildNodePtr != nullptr;
85         curChildNodePtr = curChildNodePtr->next) {
86         if (curChildNodePtr->name == nullptr || curChildNodePtr->type == XML_COMMENT_NODE) {
87             HILOG_ERROR("invalid child node");
88             continue;
89         }
90         std::string childNodeName = reinterpret_cast<const char*>(curChildNodePtr->name);
91         if (childNodeName != std::string(Constants::MAX_NORMAL_FORM_SIZE)
92             && childNodeName != std::string(Constants::MAX_TEMP_FORM_SIZE)
93             && childNodeName != std::string(Constants::HOST_MAX_FORM_SIZE)
94             && childNodeName != std::string(Constants::VISIBLE_NOTIFY_DELAY)) {
95             continue;
96         }
97         xmlChar* content = xmlNodeGetContent(curChildNodePtr);
98         if (content == nullptr) {
99             HILOG_ERROR("read xml node error: nodeName:(%{public}s)", curChildNodePtr->name);
100             continue;
101         }
102         std::string contentStr = reinterpret_cast<const char*>(content);
103         configMap_.emplace(std::make_pair(childNodeName, static_cast<int32_t>(std::stoi(contentStr))));
104         xmlFree(content);
105     }
106     return true;
107 }
108 
GetConfigMap() const109 const std::map<std::string, int32_t>& FormXMLParser::GetConfigMap() const
110 {
111     HILOG_DEBUG("GetConfigMap start");
112     return configMap_;
113 }
114 } // namespace OHOS::AppExecFwk