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 #ifndef OHOS_GLOBAL_DATE_TIME_RULE_H
16 #define OHOS_GLOBAL_DATE_TIME_RULE_H
17 
18 #include <cctype>
19 #include <filesystem>
20 #include <string>
21 #include <unicode/regex.h>
22 #include <unordered_map>
23 #include <vector>
24 #include "libxml/globals.h"
25 #include "libxml/tree.h"
26 #include "libxml/xmlstring.h"
27 
28 namespace OHOS {
29 namespace Global {
30 namespace I18n {
31 class DateTimeRule {
32 public:
33     DateTimeRule(std::string& locale);
34     ~DateTimeRule();
35 
36     std::unordered_map<std::string, std::string> GetUniverseRules();
37     std::unordered_map<std::string, std::string> GetLocalesRules();
38     std::unordered_map<std::string, std::string> GetLocalesRulesBackup();
39     std::unordered_map<std::string, std::unordered_map<std::string, std::string>> GetSubRulesMap();
40     std::unordered_map<std::string, std::string> GetSubRules();
41     std::unordered_map<std::string, std::string> GetFilterRules();
42     std::unordered_map<std::string, std::string> GetPastRules();
43     std::unordered_map<std::string, std::string> GetParam();
44     std::unordered_map<std::string, std::string> GetParamBackup();
45     std::unordered_map<std::string, icu::RegexPattern*> GetPatternsMap();
46     int CompareLevel(std::string& key1, std::string& key2);
47     std::string Get(std::unordered_map<std::string, std::string>& param, std::string& ruleName);
48     std::string GetLocale();
49     bool IsRelDates(icu::UnicodeString& hyphen, std::string& locale);
50     static std::string trim(const std::string& src);
51     static bool CompareBeginEnd(const std::string src, const std::string target, bool flag);
52 
53 private:
54     void Init(std::string& locale);
55     void RuleLevel();
56     void InitRules(std::string& xmlPath);
57     void InitRuleBackup(std::string& xmlPathBackup);
58     void LoadStrToStr(std::unordered_map<std::string, std::string>* map, xmlNodePtr cur);
59     void LoadStrToPattern(std::unordered_map<std::string, icu::RegexPattern*>& map, xmlNodePtr cur);
60     int GetLevel(std::string& name);
61     std::string GetWithoutB(const std::string& ruleName);
62     std::unordered_map<std::string, std::string> universeRules;
63     std::unordered_map<std::string, std::string> localesRules;
64     std::unordered_map<std::string, std::string> localesRulesBackup;
65     std::unordered_map<std::string, std::unordered_map<std::string, std::string>> subRulesMap;
66     std::unordered_map<std::string, std::string> subRules;
67     std::unordered_map<std::string, std::string> filterRules;
68     std::unordered_map<std::string, std::string> pastRules;
69     std::unordered_map<std::string, int> levels;
70     std::unordered_map<std::string, std::string> delimiter;
71     std::unordered_map<std::string, std::string> relDates;
72     std::unordered_map<std::string, std::string> param;
73     std::unordered_map<std::string, std::string> paramBackup;
74     std::unordered_map<std::string, std::string> localeMap;
75     std::unordered_map<std::string, std::unordered_map<std::string, std::string>*> loadMap;
76     std::unordered_map<std::string, icu::RegexPattern*> patternsMap;
77     std::string locale;
78     static std::string xmlCommonPath;
79 };
80 } // namespace I18n
81 } // namespace Global
82 } // namespace OHOS
83 #endif