• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 "layout_parser.h"
17 #include "components/ui_view.h"
18 #include "json_visitor.h"
19 #include "log/log.h"
20 
21 namespace Updater {
22 namespace {
23 constexpr auto DEFAULT_MODULE = "default";
24 constexpr auto COMPONENT_MODULE = "coms";
25 constexpr auto COMMON_LABEL = "Common";
26 constexpr auto COMMON_TYPE = "type";
27 
GetSpecificInfoMap()28 inline const auto &GetSpecificInfoMap()
29 {
30     using namespace OHOS;
31     const static std::unordered_map<std::string, std::function<SpecificInfo()>> specificInfoMap {
32         {VIEW_TYPE_STRING[UI_IMAGE_VIEW], [] () { return UxImageInfo {}; }},
33         {VIEW_TYPE_STRING[UI_BOX_PROGRESS], [] () { return UxBoxProgressInfo {}; }},
34         {VIEW_TYPE_STRING[UI_LABEL], [] () { return UxLabelInfo {}; }},
35         {VIEW_TYPE_STRING[UI_LABEL_BUTTON], [] () { return UxLabelBtnInfo {}; }}
36     };
37     return specificInfoMap;
38 }
39 }
40 
41 class LayoutParser::Impl {
42     using cJSONPtr = std::unique_ptr<cJSON, decltype(&cJSON_Delete)>;
43 public:
LoadLayout(const std::vector<std::string> & layoutFiles,std::vector<UxPageInfo> & vec) const44     bool LoadLayout(const std::vector<std::string> &layoutFiles, std::vector<UxPageInfo> &vec) const
45     {
46         std::vector<UxPageInfo>().swap(vec);
47         UxPageInfo pageInfo = {};
48         for (const auto &file : layoutFiles) {
49             if (!LoadLayout(file, pageInfo)) {
50                 LOG(ERROR) << file << " load failed";
51                 return false;
52             }
53             vec.push_back(std::move(pageInfo));
54             pageInfo = {};
55         }
56         return true;
57     }
LoadLayout(const std::string & filename,UxPageInfo & pageInfo) const58     bool LoadLayout(const std::string &filename, UxPageInfo &pageInfo) const
59     {
60         JsonNode node {std::filesystem::path {filename}};
61 
62         // parse color, id, subpages
63         if (!Visit<SETVAL>(node, pageInfo)) {
64             LOG(ERROR) << "get page info (id, color, subpages) failed";
65             return false;
66         }
67 
68         // parse view info
69         if (!ParseViewInfo(node, pageInfo.viewInfos)) {
70             LOG(ERROR) << "component Node parse failed";
71             return false;
72         }
73 
74         return true;
75     }
76 private:
ParseViewInfo(const JsonNode & root,std::vector<UxViewInfo> & vec) const77     bool ParseViewInfo(const JsonNode &root, std::vector<UxViewInfo> &vec) const
78     {
79         UxViewInfo info {};
80         std::vector<UxViewInfo>().swap(vec);
81         const JsonNode &defaultNode = root[DEFAULT_MODULE];
82         const JsonNode &componentNodes = root[COMPONENT_MODULE];
83         if (componentNodes.Type() != NodeType::ARRAY) {
84             LOG(ERROR) << "Please check whether json file has a coms field";
85             return false;
86         }
87         for (const auto &componentNode : componentNodes) {
88             const JsonNode &comNode = componentNode.get();
89             auto viewType = comNode[COMMON_TYPE].As<std::string>();
90             if (viewType == std::nullopt) {
91                 LOG(ERROR) << "Component don't have a type field";
92                 return false;
93             }
94             const JsonNode &commonDefault = defaultNode[COMMON_LABEL];
95             if (!Visit<SETVAL>(componentNode, commonDefault, info.commonInfo)) {
96                 LOG(ERROR) << "set common info failed";
97                 return false;
98             }
99 
100             auto it = GetSpecificInfoMap().find(*viewType);
101             if (it == GetSpecificInfoMap().end()) {
102                 LOG(ERROR) << "Can't recognize this type " << *viewType;
103                 return false;
104             }
105             info.specificInfo = it->second();
106             auto visitor = [&comNode, &defaultNode] (auto &args) {
107                 const JsonNode &defaultComNode = defaultNode[Traits<std::decay_t<decltype(args)>>::STRUCT_KEY];
108                 return Visit<SETVAL>(comNode, defaultComNode, args);
109             };
110             if (!std::visit(visitor, info.specificInfo)) {
111                 return false;
112             }
113             vec.push_back(std::move(info));
114             info = {};
115         }
116         return true;
117     }
118 };
119 
120 LayoutParser::~LayoutParser() = default;
121 
LayoutParser()122 LayoutParser::LayoutParser() : pImpl_(std::make_unique<Impl>()) { }
123 
GetInstance()124 LayoutParser &LayoutParser::GetInstance()
125 {
126     static LayoutParser layoutParser;
127     return layoutParser;
128 }
129 
LoadLayout(const std::string & layoutFile,UxPageInfo & pageInfo) const130 bool LayoutParser::LoadLayout(const std::string &layoutFile, UxPageInfo &pageInfo) const
131 {
132     return pImpl_->LoadLayout(layoutFile, pageInfo);
133 }
134 
LoadLayout(const std::vector<std::string> & layoutFiles,std::vector<UxPageInfo> & vec) const135 bool LayoutParser::LoadLayout(const std::vector<std::string> &layoutFiles, std::vector<UxPageInfo> &vec) const
136 {
137     return pImpl_->LoadLayout(layoutFiles, vec);
138 }
139 }  // namespace Updater
140