• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/base/namespace.h>
23 #include <meta/ext/event_impl.h>
24 #include <meta/ext/implementation_macros.h>
25 #include <meta/ext/object_fwd.h>
26 #include <meta/interface/builtin_objects.h>
27 #include <meta/interface/loaders/intf_file_content_loader.h>
28 
META_BEGIN_NAMESPACE()29 META_BEGIN_NAMESPACE()
30 
31 class CsvStringResourceLoader final : public IntroduceInterfaces<ObjectFwd, IFileContentLoader> {
32     META_OBJECT(CsvStringResourceLoader, ClassId::CsvStringResourceLoader, IntroduceInterfaces)
33 public:
34     bool Build(const IMetadata::Ptr& data) override;
35 
36     META_BEGIN_STATIC_DATA()
37     META_STATIC_PROPERTY_DATA(IFileContentLoader, bool, Cached)
38     META_STATIC_EVENT_DATA(IDynamicContentLoader, IOnChanged, ContentChanged)
39     META_END_STATIC_DATA()
40     META_IMPLEMENT_PROPERTY(bool, Cached)
41     META_IMPLEMENT_EVENT(IOnChanged, ContentChanged)
42 public: // IDynamicContentLoader
43     void Reload() override;
44 
45 public: // IFileContentLoader
46     bool SetFile(CORE_NS::IFile::Ptr file) override;
47     IObject::Ptr Create(const IObject::Ptr& params) override;
48 
49 private:
50     static constexpr const BASE_NS::string_view KEY_HEADER_COLUMN = "key";
51     static constexpr const BASE_NS::string_view TARGET_PROPERTY_NAME = "strings";
52 
53     struct ResourceObjectOptions {
54         BASE_NS::string keyHeaderColumnName { KEY_HEADER_COLUMN };
55         BASE_NS::string targetPropertyName { CsvStringResourceLoader::TARGET_PROPERTY_NAME };
56         bool keysToLower { true };
57     };
58 
59     using CsvContentItemType = BASE_NS::pair<BASE_NS::string, BASE_NS::vector<BASE_NS::string>>;
60     using CsvContentType = BASE_NS::vector<CsvContentItemType>;
61 
62     void Reset();
63     IObject::Ptr CreateStringResourceObject(const CsvContentType& content, const ResourceObjectOptions& options);
64     CsvContentType ParseCsv(BASE_NS::string_view csv, const ResourceObjectOptions& options);
65 
66     CORE_NS::IFile::Ptr file_;
67     CsvContentType cachedContent_;
68 };
69 
70 META_END_NAMESPACE()
71 
72 #endif
73