1 /*
2 * Copyright (c) 2025 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 "auto_layout.h"
18 #include "component/component_factory.h"
19 #include "component/component_register.h"
20 #include "components/ui_view.h"
21 #include "json_visitor.h"
22 #include "log/log.h"
23 #include "view_api.h"
24
25 namespace Updater {
26 namespace {
27 constexpr auto DEFAULT_MODULE = "default";
28 constexpr auto COMPONENT_MODULE = "coms";
29 constexpr auto COMMON_LABEL = "Common";
30 constexpr auto COMMON_TYPE = "type";
31 }
32
33 class LayoutParser::Impl {
34 using cJSONPtr = std::unique_ptr<cJSON, decltype(&cJSON_Delete)>;
35 AutoLayout &layout = AutoLayout::GetInstance();
36 public:
LoadLayout(const std::vector<std::string> & layoutFiles,std::vector<UxPageInfo> & vec) const37 bool LoadLayout(const std::vector<std::string> &layoutFiles, std::vector<UxPageInfo> &vec) const
38 {
39 RegisterComponents();
40 layout.Init();
41 std::vector<UxPageInfo>().swap(vec);
42 UxPageInfo pageInfo = {};
43 for (const auto &file : layoutFiles) {
44 if (!LoadLayout(file, pageInfo)) {
45 LOG(ERROR) << file << " load failed";
46 return false;
47 }
48 vec.push_back(std::move(pageInfo));
49 pageInfo = {};
50 }
51 return true;
52 }
53
54 private:
LoadLayout(const std::string & filename,UxPageInfo & pageInfo) const55 bool LoadLayout(const std::string &filename, UxPageInfo &pageInfo) const
56 {
57 JsonNode node {std::filesystem::path {filename}};
58 layout.SetJsonLocation(node);
59 // parse color, id, subpages
60 if (!Visit<SETVAL>(node, pageInfo)) {
61 LOG(ERROR) << "get page info (id, color, subpages) failed";
62 return false;
63 }
64
65 // parse view info
66 if (!ParseViewInfo(node, pageInfo.viewInfos)) {
67 LOG(ERROR) << "component Node parse failed";
68 return false;
69 }
70
71 return true;
72 }
73
ParseViewInfo(const JsonNode & root,std::vector<UxViewInfo> & vec) const74 bool ParseViewInfo(const JsonNode &root, std::vector<UxViewInfo> &vec) const
75 {
76 UxViewInfo info {};
77 std::vector<UxViewInfo>().swap(vec);
78 const JsonNode &defaultNode = root[DEFAULT_MODULE];
79 const JsonNode &componentNodes = root[COMPONENT_MODULE];
80 if (componentNodes.Type() != NodeType::ARRAY) {
81 LOG(ERROR) << "Please check whether json file has a coms field";
82 return false;
83 }
84 for (const auto &componentNode : componentNodes) {
85 const JsonNode &comNode = componentNode.get();
86 auto viewType = comNode[COMMON_TYPE].As<std::string>();
87 if (viewType == std::nullopt) {
88 LOG(ERROR) << "Component don't have a type field";
89 return false;
90 }
91 const JsonNode &commonDefault = defaultNode[COMMON_LABEL];
92 const JsonNode &specificDefault = defaultNode[*viewType];
93
94 // for common info
95 if (!Visit<SETVAL>(componentNode, commonDefault, info.commonInfo)) {
96 LOG(ERROR) << "set common info failed";
97 return false;
98 }
99 // for specific info
100 info.specificInfo = ComponentFactory::CreateSpecificInfo(*viewType);
101 if (info.specificInfo == nullptr) {
102 LOG(ERROR) << "create specific info failed, COMMON_TYPE: " << *viewType;
103 return false;
104 }
105 if (!info.specificInfo->DeserializeFromJson(comNode, specificDefault)) {
106 LOG(ERROR) << "parse specific info failed, COMMON_TYPE: " << *viewType;
107 return false;
108 }
109
110 vec.emplace_back(std::move(info));
111 info = {};
112 }
113 return true;
114 }
115 };
116
117 LayoutParser::~LayoutParser() = default;
118
LayoutParser()119 LayoutParser::LayoutParser() : pImpl_(std::make_unique<Impl>()) { }
120
GetInstance()121 LayoutParser &LayoutParser::GetInstance()
122 {
123 static LayoutParser layoutParser;
124 return layoutParser;
125 }
126
LoadLayout(const std::vector<std::string> & layoutFiles,std::vector<UxPageInfo> & vec) const127 bool LayoutParser::LoadLayout(const std::vector<std::string> &layoutFiles, std::vector<UxPageInfo> &vec) const
128 {
129 return pImpl_->LoadLayout(layoutFiles, vec);
130 }
131 } // namespace Updater
132