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 "html.h"
17 
18 namespace OHOS {
19 namespace UDMF {
Html()20 Html::Html()
21 {
22     SetType(HTML);
23 }
24 
Html(const std::string & htmlContent,const std::string & plainContent)25 Html::Html(const std::string &htmlContent, const std::string &plainContent)
26 {
27     if (plainContent.length() >= MAX_TEXT_LEN || htmlContent.length() >= MAX_TEXT_LEN) {
28         return;
29     }
30     SetType(HTML);
31     this->htmlContent_ = htmlContent;
32     this->plainContent_ = plainContent;
33 }
34 
Html(UDType type,ValueType value)35 Html::Html(UDType type, ValueType value) : Text(type, value)
36 {
37     SetType(HTML);
38     if (std::holds_alternative<std::string>(value)) {
39         htmlContent_ = std::get<std::string>(value);
40     } else if (std::holds_alternative<std::shared_ptr<Object>>(value)) {
41         auto object = std::get<std::shared_ptr<Object>>(value);
42         object->GetValue(HTML_CONTENT, htmlContent_);
43         object->GetValue(PLAINT_CONTENT, plainContent_);
44         std::shared_ptr<Object> detailObj = nullptr;
45         if (object->GetValue(DETAILS, detailObj)) {
46             details_ = ObjectUtils::ConvertToUDDetails(detailObj);
47         }
48         hasObject_ = true;
49     }
50 }
51 
GetSize()52 int64_t Html::GetSize()
53 {
54     return UnifiedDataUtils::GetDetailsSize(this->details_) + this->htmlContent_.size() + this->plainContent_.size();
55 }
56 
GetHtmlContent() const57 std::string Html::GetHtmlContent() const
58 {
59     return this->htmlContent_;
60 }
61 
SetHtmlContent(const std::string & htmlContent)62 void Html::SetHtmlContent(const std::string &htmlContent)
63 {
64     if (htmlContent.length() >= MAX_TEXT_LEN) {
65         return;
66     }
67     this->htmlContent_ = htmlContent;
68     if (std::holds_alternative<std::shared_ptr<Object>>(value_)) {
69         std::get<std::shared_ptr<Object>>(value_)->value_[HTML_CONTENT] = htmlContent_;
70     }
71 }
72 
GetPlainContent() const73 std::string Html::GetPlainContent() const
74 {
75     return this->plainContent_;
76 }
77 
SetPlainContent(const std::string & plainContent)78 void Html::SetPlainContent(const std::string &plainContent)
79 {
80     if (plainContent.length() >= MAX_TEXT_LEN) {
81         return;
82     }
83     this->plainContent_ = plainContent;
84     if (std::holds_alternative<std::shared_ptr<Object>>(value_)) {
85         auto object = std::get<std::shared_ptr<Object>>(value_);
86         object->value_[PLAINT_CONTENT] = plainContent_;
87     }
88 }
89 
InitObject()90 void Html::InitObject()
91 {
92     if (!std::holds_alternative<std::shared_ptr<Object>>(value_)) {
93         auto value = value_;
94         value_ = std::make_shared<Object>();
95         auto object = std::get<std::shared_ptr<Object>>(value_);
96         object->value_[UNIFORM_DATA_TYPE] = UtdUtils::GetUtdIdFromUtdEnum(dataType_);
97         object->value_[HTML_CONTENT] = htmlContent_;
98         object->value_[PLAINT_CONTENT] = plainContent_;
99         object->value_[DETAILS] = ObjectUtils::ConvertToObject(details_);
100         object->value_["VALUE_TYPE"] = value;
101     }
102 }
103 } // namespace UDMF
104 } // namespace OHOS