1 /* 2 * Copyright (c) 2024 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 #ifndef META_SRC_LOADERS_CSV_STRING_RESOURCE_LOADER_H 17 #define META_SRC_LOADERS_CSV_STRING_RESOURCE_LOADER_H 18 19 #include <base/containers/unordered_map.h> 20 21 #include <meta/api/property/property_event_handler.h> 22 #include <meta/ext/event_impl.h> 23 #include <meta/ext/object.h> 24 #include <meta/interface/builtin_objects.h> 25 #include <meta/interface/loaders/intf_file_content_loader.h> 26 27 META_BEGIN_NAMESPACE() 28 29 class CsvStringResourceLoader final : public META_NS::ObjectFwd<CsvStringResourceLoader, 30 ClassId::CsvStringResourceLoader, ClassId::Object, IFileContentLoader> { 31 META_IMPLEMENT_INTERFACE_PROPERTY(IFileContentLoader, bool, Cached) 32 public: 33 bool Build(const IMetadata::Ptr& data) override; 34 35 public: // IDynamicContentLoader 36 META_IMPLEMENT_INTERFACE_EVENT(IDynamicContentLoader, IOnChanged, ContentChanged) 37 void Reload() override; 38 39 public: // IFileContentLoader 40 bool SetFile(CORE_NS::IFile::Ptr file) override; 41 IObject::Ptr Create(const IObject::Ptr& params) override; 42 43 private: 44 static constexpr const BASE_NS::string_view KEY_HEADER_COLUMN = "key"; 45 static constexpr const BASE_NS::string_view TARGET_PROPERTY_NAME = "strings"; 46 47 struct ResourceObjectOptions { 48 BASE_NS::string keyHeaderColumnName { KEY_HEADER_COLUMN }; 49 BASE_NS::string targetPropertyName { CsvStringResourceLoader::TARGET_PROPERTY_NAME }; 50 bool keysToLower { true }; 51 }; 52 53 using CsvContentItemType = BASE_NS::pair<BASE_NS::string, BASE_NS::vector<BASE_NS::string>>; 54 using CsvContentType = BASE_NS::vector<CsvContentItemType>; 55 56 void Reset(); 57 IObject::Ptr CreateStringResourceObject(const CsvContentType& content, const ResourceObjectOptions& options); 58 CsvContentType ParseCsv(BASE_NS::string_view csv, const ResourceObjectOptions& options); 59 60 CORE_NS::IFile::Ptr file_; 61 CsvContentType cachedContent_; 62 }; 63 64 META_END_NAMESPACE() 65 66 #endif 67