OLD | NEW |
| (Empty) |
1 // Copyright (C) 2014 Google Inc. | |
2 // | |
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 I18N_ADDRESSINPUT_COUNTRY_RULES_AGGREGATOR_H_ | |
16 #define I18N_ADDRESSINPUT_COUNTRY_RULES_AGGREGATOR_H_ | |
17 | |
18 #include <libaddressinput/address_field.h> | |
19 #include <libaddressinput/callback.h> | |
20 #include <libaddressinput/util/basictypes.h> | |
21 #include <libaddressinput/util/scoped_ptr.h> | |
22 | |
23 #include <map> | |
24 #include <string> | |
25 #include <vector> | |
26 | |
27 namespace i18n { | |
28 namespace addressinput { | |
29 | |
30 class Json; | |
31 class Retriever; | |
32 class Rule; | |
33 class Ruleset; | |
34 | |
35 // Aggregates a ruleset for a country. Sample usage: | |
36 // class MyClass { | |
37 // public: | |
38 // MyClass() : aggregator_(scoped_ptr<const Retriever>(...)) {} | |
39 // | |
40 // ~MyClass() {} | |
41 // | |
42 // void GetRuleset(const std::string& country_code) { | |
43 // aggregator_.AggregateRules( | |
44 // country_code, | |
45 // BuildScopedPtrCallback(this, &MyClass::OnRulesetReady)); | |
46 // } | |
47 // | |
48 // void OnRulesetReady(bool success, | |
49 // const std::string& country_code, | |
50 // scoped_ptr<Ruleset> ruleset) { | |
51 // ... | |
52 // } | |
53 // | |
54 // private: | |
55 // CountryRulesAggregator aggregator_; | |
56 // | |
57 // DISALLOW_COPY_AND_ASSIGN(MyClass); | |
58 // }; | |
59 class CountryRulesAggregator { | |
60 public: | |
61 typedef i18n::addressinput::ScopedPtrCallback<void(std::string, Ruleset)> | |
62 Callback; | |
63 | |
64 explicit CountryRulesAggregator(scoped_ptr<Retriever> retriever); | |
65 ~CountryRulesAggregator(); | |
66 | |
67 // Recursively retrieves all of the rules for |country_code| and its | |
68 // administrative areas, localities, and dependent localities. Also retrieves | |
69 // the language-specific rules. Abandons previous requests if invoked multiple | |
70 // times. Invokes the |rules_ready| callback when finished. | |
71 void AggregateRules(const std::string& country_code, | |
72 scoped_ptr<Callback> rules_ready); | |
73 | |
74 private: | |
75 // Callback for Retriever::Retrieve() method. | |
76 bool OnDataReady(bool success, | |
77 const std::string& key, | |
78 const std::string& data); | |
79 | |
80 // Builds and returns the ruleset for |key| at |field| level. Language | |
81 // specific rules are retrieved using |language_specific_keys|, which is a | |
82 // mapping of language to key. | |
83 // | |
84 // Most regions use the same keys for all language variations. For example, | |
85 // "CA/AB--fr" is a French variant of "CA/AB". The notable exception is Hong | |
86 // Kong, where "data/HK/Kowloon--en" is the English variant of "HK/香港島". | |
87 // | |
88 // Returns NULL on failure, e.g. missing sub-region data in JSON. | |
89 scoped_ptr<Ruleset> Build( | |
90 const std::string& key, | |
91 AddressField field, | |
92 const std::map<std::string, std::string>& language_specific_keys); | |
93 | |
94 // Builds and returns the rule for |key| at |field| level. Returns NULL if | |
95 // |key| is not in JSON. | |
96 scoped_ptr<Rule> ParseRule(const std::string& key, AddressField field) const; | |
97 | |
98 // Abandons all requests and clears all retrieved data. | |
99 void Reset(); | |
100 | |
101 // The data retriever that can download serialized rules for one sub-region at | |
102 // a time. | |
103 scoped_ptr<Retriever> retriever_; | |
104 | |
105 // The country code for which to retrieve the ruleset. Passed to the callback | |
106 // method to identify the ruleset. Examples: "US", "CA", "CH", etc. | |
107 std::string country_code_; | |
108 | |
109 // The key requested from retriever. For example, "data/US". | |
110 std::string key_; | |
111 | |
112 // The callback to invoke when the ruleset has been retrieved. | |
113 scoped_ptr<Callback> rules_ready_; | |
114 | |
115 // The collection of rules for a country code. | |
116 scoped_ptr<Json> json_; | |
117 | |
118 // The non-default languages that have custom rules. | |
119 std::vector<std::string> non_default_languages_; | |
120 | |
121 DISALLOW_COPY_AND_ASSIGN(CountryRulesAggregator); | |
122 }; | |
123 | |
124 } // namespace addressinput | |
125 } // namespace i18n | |
126 | |
127 #endif // I18N_ADDRESSINPUT_COUNTRY_RULES_AGGREGATOR_H_ | |
OLD | NEW |