• 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 locale governing permissions and
13  * limitations under the License.
14  */
15 #include "date_rule_init.h"
16 #include "hilog/log.h"
17 
18 namespace OHOS {
19 namespace Global {
20 namespace I18n {
21 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, 0xD001E00, "DateRuleInit" };
22 using namespace OHOS::HiviewDFX;
DateRuleInit(std::string & locale)23 DateRuleInit::DateRuleInit(std::string& locale)
24 {
25     dateTimeRule = new DateTimeRule(locale);
26     if (dateTimeRule == nullptr) {
27         HiLog::Error(LABEL, "DateTimeRule construct failed.");
28     }
29     this->locale = dateTimeRule->GetLocale();
30     filter = new DateTimeFilter(this->locale, dateTimeRule);
31     if (filter == nullptr) {
32         HiLog::Error(LABEL, "DateTimeFilter construct failed.");
33     }
34     Init();
35 }
36 
~DateRuleInit()37 DateRuleInit::~DateRuleInit()
38 {
39     delete dateTimeRule;
40     delete filter;
41 }
42 
Detect(icu::UnicodeString & message)43 std::vector<MatchedDateTimeInfo> DateRuleInit::Detect(icu::UnicodeString& message)
44 {
45     std::vector<MatchedDateTimeInfo> matches = GetMatches(message);
46     std::vector<MatchedDateTimeInfo> clearMatches = ClearFind(message);
47     std::vector<MatchedDateTimeInfo> pastMatches = PastFind(message);
48     // Filter results that don't meet rules
49     matches = filter->Filter(message, matches, clearMatches, pastMatches);
50     return matches;
51 }
52 
53 // Obtains result that meet the clear rules
ClearFind(icu::UnicodeString & message)54 std::vector<MatchedDateTimeInfo> DateRuleInit::ClearFind(icu::UnicodeString& message)
55 {
56     return this->clearRulesEngine.Match(message);
57 }
58 
59 // Obtains result that meet the past rules
PastFind(icu::UnicodeString & message)60 std::vector<MatchedDateTimeInfo> DateRuleInit::PastFind(icu::UnicodeString& message)
61 {
62     return this->pastRulesEngine.Match(message);
63 }
64 
Init()65 void DateRuleInit::Init()
66 {
67     // create rulesEngine based on the UniverseRules.
68     RulesSet rulesSet1(dateTimeRule->GetUniverseRules(), dateTimeRule->GetSubRules(), dateTimeRule->GetParam(),
69         dateTimeRule->GetParamBackup());
70     RulesEngine rulesEngine1(dateTimeRule, rulesSet1);
71     universalAndLocaleRules.push_back(rulesEngine1);
72 
73     std::unordered_map<std::string, std::string> nullMap;
74     // create rulesEngine based on the LocalesRules.
75     if (dateTimeRule->GetLocalesRules().size() != 0) {
76         RulesSet rulesSet2(dateTimeRule->GetLocalesRules(), dateTimeRule->GetSubRules(), dateTimeRule->GetParam(),
77             nullMap);
78         RulesEngine rulesEngine2(dateTimeRule, rulesSet2);
79         universalAndLocaleRules.push_back(rulesEngine2);
80     }
81 
82     // create rulesEngine based on the LocalesRulesBackup.
83     if (dateTimeRule->GetLocalesRulesBackup().size() != 0) {
84         RulesSet rulesSet3(dateTimeRule->GetLocalesRulesBackup(), dateTimeRule->GetSubRules(),
85             dateTimeRule->GetParamBackup(), nullMap);
86         RulesEngine rulesEngine3(dateTimeRule, rulesSet3);
87         universalAndLocaleRules.push_back(rulesEngine3);
88     }
89 
90     // create rulesEngine based on the SubRules.
91     for (auto& kv : dateTimeRule->GetSubRulesMap()) {
92         RulesSet rulesSet4(kv.second, dateTimeRule->GetSubRules(), dateTimeRule->GetParam(),
93             dateTimeRule->GetParamBackup());
94         RulesEngine rulesEngine4(dateTimeRule, rulesSet4);
95         subDetectsMap[kv.first] = rulesEngine4;
96     }
97 
98     // create rulesEngine based on the FilterRules.
99     RulesSet rulesSet5(dateTimeRule->GetFilterRules(), dateTimeRule->GetSubRules(), dateTimeRule->GetParam(),
100         dateTimeRule->GetParamBackup());
101     RulesEngine rulesEngine5(dateTimeRule, rulesSet5);
102     this->clearRulesEngine = rulesEngine5;
103 
104     // create rulesEngine based on the PastRules.
105     RulesSet rulesSet6(dateTimeRule->GetPastRules(), nullMap, dateTimeRule->GetParam(),
106         dateTimeRule->GetParamBackup());
107     RulesEngine rulesEngine6(dateTimeRule, rulesSet6);
108     this->pastRulesEngine = rulesEngine6;
109 }
110 
111 // obtains the date and time information in the message.
GetMatches(icu::UnicodeString & message)112 std::vector<MatchedDateTimeInfo> DateRuleInit::GetMatches(icu::UnicodeString& message)
113 {
114     std::vector<MatchedDateTimeInfo> matches;
115     for (auto& detect : universalAndLocaleRules) {
116         std::vector<MatchedDateTimeInfo> tempMatches = detect.Match(message);
117         for (MatchedDateTimeInfo& match : tempMatches) {
118             GetMatchedInfo(matches, match, message);
119         }
120     }
121     return matches;
122 }
123 
GetMatchedInfo(std::vector<MatchedDateTimeInfo> & matches,MatchedDateTimeInfo & match,icu::UnicodeString & message)124 void DateRuleInit::GetMatchedInfo(std::vector<MatchedDateTimeInfo>& matches, MatchedDateTimeInfo& match,
125     icu::UnicodeString& message)
126 {
127     // splitting results based on subRules.
128     if (subDetectsMap.find(match.GetRegex()) != subDetectsMap.end()) {
129         RulesEngine subRulesEngine = subDetectsMap[match.GetRegex()];
130         icu::UnicodeString subMessage = message.tempSubString(match.GetBegin(), match.GetEnd() - match.GetBegin());
131         std::vector<MatchedDateTimeInfo> subMatches = subRulesEngine.Match(subMessage);
132         for (auto& subMatch : subMatches) {
133             subMatch.SetBegin(subMatch.GetBegin() + match.GetBegin());
134             subMatch.SetEnd(subMatch.GetEnd() + match.GetBegin());
135         }
136         matches.insert(matches.end(), subMatches.begin(), subMatches.end());
137     } else {
138         matches.push_back(match);
139     }
140 }
141 } // namespace I18n
142 } // namespace Global
143 } // namespace OHOS