| OLD | NEW |
| 1 // Copyright (C) 2014 Google Inc. | 1 // Copyright (C) 2014 Google Inc. |
| 2 // | 2 // |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); | 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 // you may not use this file except in compliance with 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 | 5 // You may obtain a copy of the License at |
| 6 // | 6 // |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 | 7 // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 // | 8 // |
| 9 // Unless required by applicable law or agreed to in writing, software | 9 // Unless required by applicable law or agreed to in writing, software |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, | 10 // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 // See the License for the specific language governing permissions and | 12 // See the License for the specific language governing permissions and |
| 13 // limitations under the License. | 13 // limitations under the License. |
| 14 | 14 |
| 15 #ifndef I18N_ADDRESSINPUT_COUNTRY_RULES_AGGREGATOR_H_ | 15 #ifndef I18N_ADDRESSINPUT_COUNTRY_RULES_AGGREGATOR_H_ |
| 16 #define I18N_ADDRESSINPUT_COUNTRY_RULES_AGGREGATOR_H_ | 16 #define I18N_ADDRESSINPUT_COUNTRY_RULES_AGGREGATOR_H_ |
| 17 | 17 |
| 18 #include <libaddressinput/address_field.h> |
| 18 #include <libaddressinput/callback.h> | 19 #include <libaddressinput/callback.h> |
| 19 #include <libaddressinput/util/basictypes.h> | 20 #include <libaddressinput/util/basictypes.h> |
| 20 #include <libaddressinput/util/scoped_ptr.h> | 21 #include <libaddressinput/util/scoped_ptr.h> |
| 21 | 22 |
| 22 #include <map> | |
| 23 #include <string> | 23 #include <string> |
| 24 #include <vector> | 24 #include <vector> |
| 25 | 25 |
| 26 namespace i18n { | 26 namespace i18n { |
| 27 namespace addressinput { | 27 namespace addressinput { |
| 28 | 28 |
| 29 class Json; |
| 29 class Retriever; | 30 class Retriever; |
| 31 class Rule; |
| 30 class Ruleset; | 32 class Ruleset; |
| 31 | 33 |
| 32 // Aggregates a ruleset for a country. Sample usage: | 34 // Aggregates a ruleset for a country. Sample usage: |
| 33 // class MyClass { | 35 // class MyClass { |
| 34 // public: | 36 // public: |
| 35 // MyClass() : aggregator_(scoped_ptr<const Retriever>(...)) {} | 37 // MyClass() : aggregator_(scoped_ptr<const Retriever>(...)) {} |
| 36 // | 38 // |
| 37 // ~MyClass() {} | 39 // ~MyClass() {} |
| 38 // | 40 // |
| 39 // void GetRuleset(const std::string& country_code) { | 41 // void GetRuleset(const std::string& country_code) { |
| (...skipping 21 matching lines...) Expand all Loading... |
| 61 ~CountryRulesAggregator(); | 63 ~CountryRulesAggregator(); |
| 62 | 64 |
| 63 // Recursively retrieves all of the rules for |country_code| and its | 65 // Recursively retrieves all of the rules for |country_code| and its |
| 64 // administrative areas, localities, and dependent localities. Also retrieves | 66 // administrative areas, localities, and dependent localities. Also retrieves |
| 65 // the language-specific rules. Abandons previous requests if invoked multiple | 67 // the language-specific rules. Abandons previous requests if invoked multiple |
| 66 // times. Invokes the |rules_ready| callback when finished. | 68 // times. Invokes the |rules_ready| callback when finished. |
| 67 void AggregateRules(const std::string& country_code, | 69 void AggregateRules(const std::string& country_code, |
| 68 scoped_ptr<Callback> rules_ready); | 70 scoped_ptr<Callback> rules_ready); |
| 69 | 71 |
| 70 private: | 72 private: |
| 71 struct RequestData; | |
| 72 | |
| 73 // Callback for Retriever::Retrieve() method. | 73 // Callback for Retriever::Retrieve() method. |
| 74 void OnDataReady(bool success, | 74 void OnDataReady(bool success, |
| 75 const std::string& key, | 75 const std::string& key, |
| 76 const std::string& data); | 76 const std::string& data); |
| 77 | 77 |
| 78 // Builds and returns the ruleset for |key| at |field| level. Returns NULL on |
| 79 // failure, e.g. missing sub-region data in JSON. |
| 80 scoped_ptr<Ruleset> Build(const std::string& key, AddressField field); |
| 81 |
| 82 // Builds and returns the rule for |key| at |field| level. Returns NULL if |
| 83 // |key| is not in JSON. |
| 84 scoped_ptr<Rule> ParseRule(const std::string& key, AddressField field) const; |
| 85 |
| 78 // Abandons all requests and clears all retrieved data. | 86 // Abandons all requests and clears all retrieved data. |
| 79 void Reset(); | 87 void Reset(); |
| 80 | 88 |
| 81 // The data retriever that can download serialized rules for one sub-region at | 89 // The data retriever that can download serialized rules for one sub-region at |
| 82 // a time. | 90 // a time. |
| 83 scoped_ptr<Retriever> retriever_; | 91 scoped_ptr<Retriever> retriever_; |
| 84 | 92 |
| 85 // A mapping of data keys (e.g., "data/CA/AB--fr") to information that helps | |
| 86 // to parse the response data and place it the correct location in the | |
| 87 // rulesets. | |
| 88 std::map<std::string, RequestData> requests_; | |
| 89 | |
| 90 // The country code for which to retrieve the ruleset. Passed to the callback | 93 // The country code for which to retrieve the ruleset. Passed to the callback |
| 91 // method to identify the ruleset. Examples: "US", "CA", "CH", etc. | 94 // method to identify the ruleset. Examples: "US", "CA", "CH", etc. |
| 92 std::string country_code_; | 95 std::string country_code_; |
| 93 | 96 |
| 97 // The key requested from retriever. For example, "data/US". |
| 98 std::string key_; |
| 99 |
| 94 // The callback to invoke when the ruleset has been retrieved. | 100 // The callback to invoke when the ruleset has been retrieved. |
| 95 scoped_ptr<Callback> rules_ready_; | 101 scoped_ptr<Callback> rules_ready_; |
| 96 | 102 |
| 97 // The top-level ruleset for the country code. Passed to the callback method | 103 // The collection of rules for a country code. |
| 98 // as the result of the query. | 104 scoped_ptr<Json> json_; |
| 99 scoped_ptr<Ruleset> root_; | |
| 100 | 105 |
| 101 // The default language for the country code. This value is parsed from the | 106 // The non-default languages that have custom rules. |
| 102 // country-level rule for the country code and is used to filter out the | 107 std::vector<std::string> non_default_languages_; |
| 103 // default language from the list of all supported languages for a country. | |
| 104 // For example, the list of supported languages for Canada is ["en", "fr"], | |
| 105 // but the default language is "en". Data requests for "data/CA/AB--fr" will | |
| 106 // succeed, but "data/CA/AB--en" will not return data. | |
| 107 std::string default_language_; | |
| 108 | |
| 109 // The list of all supported languages for the country code. This value is | |
| 110 // parsed from the country-level rule for the country and is used to download | |
| 111 // language-specific rules. | |
| 112 std::vector<std::string> languages_; | |
| 113 | 108 |
| 114 DISALLOW_COPY_AND_ASSIGN(CountryRulesAggregator); | 109 DISALLOW_COPY_AND_ASSIGN(CountryRulesAggregator); |
| 115 }; | 110 }; |
| 116 | 111 |
| 117 } // namespace addressinput | 112 } // namespace addressinput |
| 118 } // namespace i18n | 113 } // namespace i18n |
| 119 | 114 |
| 120 #endif // I18N_ADDRESSINPUT_COUNTRY_RULES_AGGREGATOR_H_ | 115 #endif // I18N_ADDRESSINPUT_COUNTRY_RULES_AGGREGATOR_H_ |
| OLD | NEW |