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 #ifndef OHOS_GLOBAL_I18N_TABOO_H
17 #define OHOS_GLOBAL_I18N_TABOO_H
18 
19 #include <map>
20 #include <set>
21 #include <string>
22 #include <vector>
23 #include <mutex>
24 
25 namespace OHOS {
26 namespace Global {
27 namespace I18n {
28 enum DataFileType {
29     CONFIG_FILE,
30     DATA_FILE
31 };
32 
33 class Taboo {
34 public:
35     Taboo();
36     Taboo(const std::string& path);
37     ~Taboo();
38     std::string ReplaceCountryName(const std::string& region, const std::string& displayLanguage,
39         const std::string& name);
40     std::string ReplaceLanguageName(const std::string& language, const std::string& displayLanguage,
41         const std::string& name);
42 
43 private:
44     void ParseTabooData(const std::string& path, DataFileType fileType, const std::string& Locale = "");
45     void ProcessTabooConfigData(const std::string& name, const std::string& value);
46     void ProcessTabooLocaleData(const std::string& locale, const std::string& name, const std::string& value);
47     void SplitValue(const std::string& value, std::set<std::string>& collation);
48     std::vector<std::string> QueryKeyFallBack(const std::string& key);
49     std::tuple<std::string, std::string> LanguageFallBack(const std::string& language);
50     void ReadResourceList();
51     std::string GetLanguageFromFileName(const std::string& fileName);
52 
53     // Indicates which regions support name replacement using taboo data.
54     static std::set<std::string> supportedRegions;
55     // Indicates which languages support name replacement using taboo data.
56     static std::set<std::string> supportedLanguages;
57     // cache the name replacement taboo data of different locale.
58     std::map<std::string, std::map<std::string, std::string>> localeTabooData;
59     // Indicates which locales are supported to find taboo data.
60     static std::map<std::string, std::string> RESOURCES;
61 
62     std::string tabooDataPath = "";
63     std::string tabooConfigFileName = "taboo-config.xml";
64     std::string tabooLocaleDataFileName = "taboo-data.xml";
65     std::string filePathSplitor = "/";
66     std::string supportedRegionsTag = "regions"; // supported regions key in taboo-config.xml
67     std::string supportedLanguagesTag = "languages"; // supported languages key in taboo-config.xml
68     std::string regionKey = "region_"; // start part of region name replacement data key.
69     std::string languageKey = "language_"; // start part of language name replacement data key.
70     bool isTabooDataExist = false;
71     static std::mutex TABOO_MUTEX;
72 
73     static const char* ROOT_TAG;
74     static const char* ITEM_TAG;
75     static const char* NAME_TAG;
76     static const char* VALUE_TAG;
77     char tabooDataSplitor = ',';
78 };
79 } // namespace I18n
80 } // namespace Global
81 } // namespace OHOS
82 #endif