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