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 #define LOG_TAG "TypeDescriptor"
16 #include "type_descriptor.h"
17 #include "logger.h"
18 #include "utd_graph.h"
19 #include "flexible_type.h"
20 namespace OHOS {
21 namespace UDMF {
TypeDescriptor(const std::string & typeId,const std::vector<std::string> & belongingToTypes,const std::vector<std::string> & filenameExtensions,const std::vector<std::string> & mimeTypes,const std::string & description,const std::string & referenceURL,const std::string & iconFile)22 TypeDescriptor::TypeDescriptor(const std::string &typeId, const std::vector<std::string> &belongingToTypes,
23     const std::vector<std::string> &filenameExtensions, const std::vector<std::string> &mimeTypes,
24     const std::string &description, const std::string &referenceURL, const std::string &iconFile) : typeId_(typeId),
25     belongingToTypes_(belongingToTypes), filenameExtensions_(filenameExtensions), mimeTypes_(mimeTypes),
26     description_(description), referenceURL_(referenceURL), iconFile_(iconFile)
27 {
28 }
29 
TypeDescriptor(const TypeDescriptorCfg & typeDescriptorCfg)30 TypeDescriptor::TypeDescriptor(const TypeDescriptorCfg& typeDescriptorCfg)
31 {
32     typeId_ = typeDescriptorCfg.typeId;
33     belongingToTypes_ = typeDescriptorCfg.belongingToTypes;
34     filenameExtensions_ = typeDescriptorCfg.filenameExtensions;
35     mimeTypes_ = typeDescriptorCfg.mimeTypes;
36     description_ = typeDescriptorCfg.description;
37     referenceURL_ = typeDescriptorCfg.referenceURL;
38     iconFile_ = typeDescriptorCfg.iconFile;
39     isFlexibleType_ = (typeId_.find(FLEXIBLE_TYPE_FLAG) != typeId_.npos);
40 }
41 
~TypeDescriptor()42 TypeDescriptor::~TypeDescriptor()
43 {
44 }
45 
CmpFlexibleTypeLevel(const std::string higherLevelTypeId,bool isFlexibleType)46 bool TypeDescriptor::CmpFlexibleTypeLevel(const std::string higherLevelTypeId, bool isFlexibleType)
47 {
48     if (belongingToTypes_.empty()) {
49         LOG_WARN(UDMF_CLIENT, "The current utd belongings is empty. %{public}s,", typeId_.c_str());
50         return false;
51     }
52     if (std::find(belongingToTypes_.begin(), belongingToTypes_.end(), higherLevelTypeId) != belongingToTypes_.end()) {
53         return true;
54     }
55     if (!isFlexibleType) {
56         for (auto belong : belongingToTypes_) {
57             if (UtdGraph::GetInstance().IsLowerLevelType(higherLevelTypeId, belong)) {
58                 return true;
59             }
60         }
61     }
62     LOG_INFO(UDMF_CLIENT, "The current utd[%{public}s] belongings is not belong[] %{public}s,",
63              typeId_.c_str(), higherLevelTypeId.c_str());
64     return false;
65 }
66 
BelongsTo(const std::string & typeId,bool & checkResult)67 Status TypeDescriptor::BelongsTo(const std::string &typeId, bool &checkResult)
68 {
69     checkResult = false;
70     bool isFlexibleType = typeId.find(FLEXIBLE_TYPE_FLAG) != typeId_.npos;
71     if (!UtdGraph::GetInstance().IsValidType(typeId) && !isFlexibleType) {
72         LOG_ERROR(UDMF_CLIENT, "invalid para. %{public}s,", typeId.c_str());
73         return Status::E_INVALID_PARAMETERS;
74     }
75 
76     if (isFlexibleType_) {
77         if (typeId_ == typeId) {
78             checkResult = true;
79             return Status::E_OK;
80         };
81         checkResult = CmpFlexibleTypeLevel(typeId, isFlexibleType);
82         return Status::E_OK;
83     }
84 
85     if (typeId_ == typeId) {
86         checkResult = true;
87         return Status::E_OK;
88     };
89     checkResult = UtdGraph::GetInstance().IsLowerLevelType(typeId, typeId_);
90     return Status::E_OK;
91 }
92 
IsLowerLevelType(const std::string & typeId,bool & checkResult)93 Status TypeDescriptor::IsLowerLevelType(const std::string &typeId, bool &checkResult)
94 {
95     checkResult = false;
96     bool isFlexibleType = typeId.find(FLEXIBLE_TYPE_FLAG) != typeId_.npos;
97     if (!UtdGraph::GetInstance().IsValidType(typeId) && !isFlexibleType) {
98         LOG_ERROR(UDMF_CLIENT, "invalid para. %{public}s,", typeId.c_str());
99         return Status::E_INVALID_PARAMETERS;
100     }
101     if (isFlexibleType_) {
102         checkResult = CmpFlexibleTypeLevel(typeId, isFlexibleType);
103         return Status::E_OK;
104     }
105 
106     if (typeId_ == typeId) {
107         return Status::E_OK;
108     };
109     checkResult = UtdGraph::GetInstance().IsLowerLevelType(typeId, typeId_);
110     return Status::E_OK;
111 }
112 
IsHigherLevelType(const std::string & typeId,bool & checkResult)113 Status TypeDescriptor::IsHigherLevelType(const std::string &typeId, bool &checkResult)
114 {
115     checkResult = false;
116     bool isFlexibleType = typeId.find(FLEXIBLE_TYPE_FLAG) != typeId_.npos;
117     if (!UtdGraph::GetInstance().IsValidType(typeId) && !isFlexibleType) {
118         LOG_ERROR(UDMF_CLIENT, "invalid para. %{public}s,", typeId.c_str());
119         return Status::E_INVALID_PARAMETERS;
120     }
121     if (isFlexibleType_) {  // flexibleType cannot be other type height level.
122         return Status::E_OK;
123     }
124     if (typeId_ == typeId) {
125         return Status::E_OK;
126     };
127     checkResult = UtdGraph::GetInstance().IsLowerLevelType(typeId_, typeId);
128     return Status::E_OK;
129 }
130 
Equals(std::shared_ptr<TypeDescriptor> descriptor)131 bool TypeDescriptor::Equals(std::shared_ptr<TypeDescriptor> descriptor)
132 {
133     return descriptor->GetTypeId() == this->GetTypeId();
134 }
135 
GetTypeId() const136 const std::string& TypeDescriptor::GetTypeId() const
137 {
138     return typeId_;
139 }
140 
GetBelongingToTypes()141 std::vector<std::string> TypeDescriptor::GetBelongingToTypes()
142 {
143     return belongingToTypes_;
144 }
145 
GetIconFile()146 std::string TypeDescriptor::GetIconFile()
147 {
148     return iconFile_;
149 }
150 
GetDescription()151 std::string TypeDescriptor::GetDescription()
152 {
153     return description_;
154 }
155 
GetReferenceURL()156 std::string TypeDescriptor::GetReferenceURL()
157 {
158     return referenceURL_;
159 }
160 
GetFilenameExtensions()161 std::vector<std::string> TypeDescriptor::GetFilenameExtensions()
162 {
163     return filenameExtensions_;
164 }
165 
GetMimeTypes()166 std::vector<std::string> TypeDescriptor::GetMimeTypes()
167 {
168     return mimeTypes_;
169 }
170 } // namespace UDMF
171 } // namespace OHOS