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