• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <string>
20 #include <unordered_map>
21 #include <unordered_set>
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(const std::string& path);
36     ~Taboo();
37     std::string ReplaceCountryName(const std::string& region, const std::string& displayLanguage,
38         const std::string& name);
39     std::string ReplaceLanguageName(const std::string& language, const std::string& displayLanguage,
40         const std::string& name);
41     std::string ReplaceTimeZoneName(const std::string& tzId, const std::string& displayLanguage,
42         const std::string& name);
43     std::string ReplaceCityName(const std::string& cityId, const std::string& displayLanguage,
44         const std::string& name);
45     std::string ReplacePhoneLocationName(const std::string& phoneNumber, const std::string& displayLanguage,
46         const std::string& name);
47     std::unordered_set<std::string> GetBlockedLanguages() const;
48     std::unordered_set<std::string> GetBlockedRegions(const std::string& language) const;
49     std::unordered_set<std::string> GetBlockedCities() const;
50     std::unordered_set<std::string> GetBlockedPhoneNumbers() const;
51 
52 private:
53     void ParseTabooData(const std::string& path, DataFileType fileType, const std::string& Locale = "");
54     void ProcessTabooConfigData(const std::string& name, const std::string& value);
55     void ProcessTabooLocaleData(const std::string& locale, const std::string& name, const std::string& value);
56     void HandleTabooData(DataFileType fileType, const std::string& locale, const std::string& name,
57         const std::string& value);
58     std::vector<std::string> QueryKeyFallBack(const std::string& key) const;
59     std::tuple<std::string, std::string> LanguageFallBack(const std::string& language);
60     void ReadResourceList();
61     std::string GetLanguageFromFileName(const std::string& fileName);
62     std::unordered_set<std::string> GetBlockedRegions() const;
63     void ParseBlockedLanguagesAndRegions(const std::string& key, const std::string& value);
64     void ParseBlockedCities(const std::string& key, const std::string& value);
65     void ParseBlockedPhoneNumbers(const std::string& key, const std::string& value);
66     void InitLocaleTabooData(const std::string& fallbackLanguage, const std::string& fileName);
67     void InitFallBackSuffixes();
68     // Indicates which regions support name replacement using taboo data.
69     std::unordered_set<std::string> supportedRegions;
70     // Indicates which languages support name replacement using taboo data.
71     std::unordered_set<std::string> supportedLanguages;
72     // Indicates which time zones support name replacement using taboo data.
73     std::unordered_set<std::string> supportedTimeZones;
74     // Indicates which cities support name replacement using taboo data.
75     std::unordered_set<std::string> supportedcities;
76     // Indicates which phone numbers support location name replacement using taboo data.
77     std::unordered_set<std::string> supportedPhoneNumbers;
78     // cache the name replacement taboo data of different locale.
79     std::unordered_map<std::string, std::unordered_map<std::string, std::string>> localeTabooData;
80     // Indicates which locales are supported to find taboo data.
81     std::unordered_map<std::string, std::string> resources;
82     std::unordered_set<std::string> initResourcesList;
83 
84     std::unordered_map<std::string, std::unordered_set<std::string>> blockedLanguages;
85     std::unordered_map<std::string, std::unordered_set<std::string>> blockedRegions;
86     std::unordered_map<std::string, std::unordered_map<std::string,
87         std::unordered_set<std::string>>> languageBlockedRegions;
88     std::unordered_map<std::string, std::unordered_set<std::string>> blockedCities;
89     std::unordered_map<std::string, std::unordered_set<std::string>> blockedPhoneNumbers;
90 
91     std::vector<std::string> fallBackSuffixes;
92 
93     static const std::string TABOO_CONFIG_FILE;
94     static const std::string TABOO_DATA_FILE;
95     static const std::string FILE_PATH_SPLITOR;
96     static const std::string LANGUAGE_REGION_SPLITOR;
97     static const std::string TABOO_DATA_SPLITOR;
98     static const std::string OMITTED_SYMBOL;
99     static const std::string ROOT_TAG;
100     static const std::string ITEM_TAG;
101     static const std::string NAME_TAG;
102     static const std::string VALUE_TAG;
103 
104     static const std::string SUPPORTED_LANGUAGE_TAG; // supported languages key in taboo-config.xml
105     static const std::string SUPPORTED_REGION_TAG; // supported regions key in taboo-config.xml
106     static const std::string SUPPORTED_TIME_ZONE_TAG; // supported time zones key in taboo-config.xml
107     static const std::string SUPPORTED_CITY_TAG; // supported cities key in taboo-config.xml
108     static const std::string SUPPORTED_PHONE_NUMBER_TAG; // supported phone numbers key in taboo-config.xml
109     static const std::string LANGUAGE_KEY; // start part of language name replacement data key.
110     static const std::string REGION_KEY; // start part of region name replacement data key.
111     static const std::string TIME_ZONE_KEY; // start part of time zone name replacement data key.
112     static const std::string CITY_KEY;  // start part of city name replacement data key.
113     static const std::string PHONE_NUMBER_KEY; // start part of phone number location name replacement data key.
114     static const std::string BLOCKED_LANG_TAG;
115     static const std::string BLOCKED_CITY_TAG;
116     static const std::string BLOCKED_PHONE_NUMBER_TAG;
117 
118     static const std::string MCC_MNC_FIRST_KEY;
119     static const std::string MCC_MNC_SECOND_KEY;
120     static const std::string CUST_REGION_KEY;
121     static const std::string MCC_MNC_TAG;
122     static const std::string MCC_TAG;
123     static const std::string CUST_REGION_TAG;
124     static const std::string DEFAULT_REGION_TAG;
125     static const std::string COMMON_SUFFIX;
126     static const size_t MCC_MNC_LENGTH;
127     static const size_t MCC_LENGTH;
128 
129     std::string tabooDataPath;
130     bool isTabooDataExist = false;
131     std::mutex tabooMutex;
132 };
133 } // namespace I18n
134 } // namespace Global
135 } // namespace OHOS
136 #endif