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(PLAIN_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 static_cast<int64_t>(UnifiedDataUtils::GetDetailsSize(this->details_) + this->htmlContent_.size() +
55 this->plainContent_.size()) + GetInnerEntriesSize();
56 }
57
GetHtmlContent() const58 std::string Html::GetHtmlContent() const
59 {
60 return this->htmlContent_;
61 }
62
SetHtmlContent(const std::string & htmlContent)63 void Html::SetHtmlContent(const std::string &htmlContent)
64 {
65 if (htmlContent.length() >= MAX_TEXT_LEN) {
66 return;
67 }
68 this->htmlContent_ = htmlContent;
69 if (std::holds_alternative<std::shared_ptr<Object>>(value_)) {
70 std::get<std::shared_ptr<Object>>(value_)->value_[HTML_CONTENT] = htmlContent_;
71 }
72 }
73
GetPlainContent() const74 std::string Html::GetPlainContent() const
75 {
76 return this->plainContent_;
77 }
78
SetPlainContent(const std::string & plainContent)79 void Html::SetPlainContent(const std::string &plainContent)
80 {
81 if (plainContent.length() >= MAX_TEXT_LEN) {
82 return;
83 }
84 this->plainContent_ = plainContent;
85 if (std::holds_alternative<std::shared_ptr<Object>>(value_)) {
86 auto object = std::get<std::shared_ptr<Object>>(value_);
87 object->value_[PLAIN_CONTENT] = plainContent_;
88 }
89 }
90
InitObject()91 void Html::InitObject()
92 {
93 if (!std::holds_alternative<std::shared_ptr<Object>>(value_)) {
94 auto value = value_;
95 value_ = std::make_shared<Object>();
96 auto object = std::get<std::shared_ptr<Object>>(value_);
97 object->value_[UNIFORM_DATA_TYPE] = UtdUtils::GetUtdIdFromUtdEnum(dataType_);
98 object->value_[HTML_CONTENT] = htmlContent_;
99 object->value_[PLAIN_CONTENT] = plainContent_;
100 object->value_[DETAILS] = ObjectUtils::ConvertToObject(details_);
101 object->value_.insert_or_assign(VALUE_TYPE, std::move(value));
102 }
103 }
104 } // namespace UDMF
105 } // namespace OHOS