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 }
23 
PlainText(const std::string & content,const std::string & abstract)24 PlainText::PlainText(const std::string &content, const std::string &abstract)
25 {
26     if (content.length() >= MAX_TEXT_LEN) {
27         return;
28     }
29     SetType(PLAIN_TEXT);
30     this->content_ = content;
31     this->abstract_ = abstract;
32 }
33 
PlainText(UDType type,ValueType value)34 PlainText::PlainText(UDType type, ValueType value) : Text(type, value)
35 {
36     SetType(PLAIN_TEXT);
37     if (std::holds_alternative<std::string>(value)) {
38         content_ = std::get<std::string>(value);
39     } else if (std::holds_alternative<std::shared_ptr<Object>>(value)) {
40         auto object = std::get<std::shared_ptr<Object>>(value);
41         object->GetValue(TEXT_CONTENT, content_);
42         object->GetValue(ABSTRACT, abstract_);
43         std::shared_ptr<Object> detailObj = nullptr;
44         if (object->GetValue(DETAILS, detailObj)) {
45             details_ = ObjectUtils::ConvertToUDDetails(detailObj);
46         }
47         hasObject_ = true;
48     }
49 }
50 
GetSize()51 int64_t PlainText::GetSize()
52 {
53     return UnifiedDataUtils::GetDetailsSize(this->details_) + this->content_.size() + this->abstract_.size();
54 }
55 
GetContent() const56 std::string PlainText::GetContent() const
57 {
58     return this->content_;
59 }
60 
SetContent(const std::string & text)61 void PlainText::SetContent(const std::string &text)
62 {
63     if (text.length() >= MAX_TEXT_LEN) {
64         return;
65     }
66     this->content_ = text;
67     if (std::holds_alternative<std::shared_ptr<Object>>(value_)) {
68         auto object = std::get<std::shared_ptr<Object>>(value_);
69         object->value_[TEXT_CONTENT] = content_;
70     }
71 }
72 
GetAbstract() const73 std::string PlainText::GetAbstract() const
74 {
75     return this->abstract_;
76 }
77 
SetAbstract(const std::string & abstract)78 void PlainText::SetAbstract(const std::string &abstract)
79 {
80     if (abstract.length() >= MAX_TEXT_LEN) {
81         return;
82     }
83     this->abstract_ = abstract;
84     if (std::holds_alternative<std::shared_ptr<Object>>(value_)) {
85         auto object = std::get<std::shared_ptr<Object>>(value_);
86         object->value_[ABSTRACT] = abstract_;
87     }
88 }
89 
InitObject()90 void PlainText::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_[TEXT_CONTENT] = content_;
98         object->value_[ABSTRACT] = abstract_;
99         object->value_[DETAILS] = ObjectUtils::ConvertToObject(details_);
100         object->value_["VALUE_TYPE"] = value;
101     }
102 }
103 } // namespace UDMF
104 } // namespace OHOS
105