• 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 #ifndef OHOS_GLOBAL_RULES_ENGINE_H
16 #define OHOS_GLOBAL_RULES_ENGINE_H
17 
18 #include <string>
19 #include <unicode/regex.h>
20 #include <unordered_map>
21 #include <vector>
22 #include "date_time_rule.h"
23 #include "matched_date_time_info.h"
24 
25 namespace OHOS {
26 namespace Global {
27 namespace I18n {
28 struct RulesSet {
29 public:
RulesSetRulesSet30     RulesSet (std::unordered_map<std::string, std::string> rulesMap,
31         std::unordered_map<std::string, std::string> subRules,
32         std::unordered_map<std::string, std::string> param,
33         std::unordered_map<std::string, std::string> paramBackup)
34     {
35         this->rulesMap = rulesMap;
36         this->subRules = subRules;
37         this->param = param;
38         this->paramBackup = paramBackup;
39     }
40     std::unordered_map<std::string, std::string> rulesMap;
41     std::unordered_map<std::string, std::string> subRules;
42     std::unordered_map<std::string, std::string> param;
43     std::unordered_map<std::string, std::string> paramBackup;
44 };
45 
46 class RulesEngine {
47 public:
48     RulesEngine();
49     RulesEngine(DateTimeRule* dateTimeRule, RulesSet& rulesSet);
50     virtual ~RulesEngine();
51     std::vector<MatchedDateTimeInfo> Match(icu::UnicodeString& message);
52 
53 protected:
54     virtual bool IsRegexPatternInvalid(icu::RegexPattern* pattern);
55     virtual bool IsRegexMatcherInvalid(icu::RegexMatcher* matcher);
56 
57 private:
58     void Init();
59     std::string InitOptRules(std::string& rule);
60     std::string InitSubRules(std::string& rule);
61     bool InitRules(std::string& rulesValue);
62     DateTimeRule* dateTimeRule = nullptr;
63     std::unordered_map<std::string, std::string> rulesMap;
64     std::unordered_map<std::string, std::string> subRules;
65     std::unordered_map<std::string, std::string> param;
66     std::unordered_map<std::string, std::string> paramBackup;
67     std::unordered_map<std::string, icu::UnicodeString> patterns;
68 };
69 } // namespace I18n
70 } // namespace Global
71 } // namespace OHOS
72 #endif