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 18namespace OHOS { 19namespace UDMF { 20PlainText::PlainText() : PlainText("", "") 21{ 22} 23 24PlainText::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 34PlainText::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(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 51int64_t PlainText::GetSize() 52{ 53 return UnifiedDataUtils::GetDetailsSize(this->details_) + this->content_.size() + this->abstract_.size(); 54} 55 56std::string PlainText::GetContent() const 57{ 58 return this->content_; 59} 60 61void 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_[CONTENT] = content_; 70 } 71} 72 73std::string PlainText::GetAbstract() const 74{ 75 return this->abstract_; 76} 77 78void 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 90void 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_[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