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 "plain_text.h"
17
18 namespace OHOS {
19 namespace UDMF {
PlainText()20 PlainText::PlainText() : PlainText("", "")
21 {
22 SetType(PLAIN_TEXT);
23 }
24
PlainText(const std::string & content,const std::string & abstract)25 PlainText::PlainText(const std::string &content, const std::string &abstract)
26 {
27 if (content.length() >= MAX_TEXT_LEN) {
28 return;
29 }
30 SetType(PLAIN_TEXT);
31 this->content_ = content;
32 this->abstract_ = abstract;
33 }
34
PlainText(UDType type,ValueType value)35 PlainText::PlainText(UDType type, ValueType value) : Text(type, value)
36 {
37 SetType(PLAIN_TEXT);
38 if (std::holds_alternative<std::string>(value)) {
39 content_ = 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(TEXT_CONTENT, content_);
43 object->GetValue(ABSTRACT, abstract_);
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 PlainText::GetSize()
53 {
54 return static_cast<int64_t>(UnifiedDataUtils::GetDetailsSize(this->details_) + this->content_.size() +
55 this->abstract_.size()) + GetInnerEntriesSize();
56 }
57
GetContent() const58 std::string PlainText::GetContent() const
59 {
60 return this->content_;
61 }
62
SetContent(const std::string & text)63 void PlainText::SetContent(const std::string &text)
64 {
65 if (text.length() >= MAX_TEXT_LEN) {
66 return;
67 }
68 this->content_ = text;
69 if (std::holds_alternative<std::shared_ptr<Object>>(value_)) {
70 auto object = std::get<std::shared_ptr<Object>>(value_);
71 object->value_[TEXT_CONTENT] = content_;
72 }
73 }
74
GetAbstract() const75 std::string PlainText::GetAbstract() const
76 {
77 return this->abstract_;
78 }
79
SetAbstract(const std::string & abstract)80 void PlainText::SetAbstract(const std::string &abstract)
81 {
82 if (abstract.length() >= MAX_TEXT_LEN) {
83 return;
84 }
85 this->abstract_ = abstract;
86 if (std::holds_alternative<std::shared_ptr<Object>>(value_)) {
87 auto object = std::get<std::shared_ptr<Object>>(value_);
88 object->value_[ABSTRACT] = abstract_;
89 }
90 }
91
InitObject()92 void PlainText::InitObject()
93 {
94 if (!std::holds_alternative<std::shared_ptr<Object>>(value_)) {
95 auto value = value_;
96 value_ = std::make_shared<Object>();
97 auto object = std::get<std::shared_ptr<Object>>(value_);
98 object->value_[UNIFORM_DATA_TYPE] = UtdUtils::GetUtdIdFromUtdEnum(dataType_);
99 object->value_[TEXT_CONTENT] = content_;
100 object->value_[ABSTRACT] = abstract_;
101 object->value_[DETAILS] = ObjectUtils::ConvertToObject(details_);
102 object->value_.insert_or_assign(VALUE_TYPE, std::move(value));
103 }
104 }
105 } // namespace UDMF
106 } // namespace OHOS
107