| 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_RULESET_H_ | 15 #ifndef I18N_ADDRESSINPUT_RULESET_H_ |
| 16 #define I18N_ADDRESSINPUT_RULESET_H_ | 16 #define I18N_ADDRESSINPUT_RULESET_H_ |
| 17 | 17 |
| 18 #include <libaddressinput/address_field.h> |
| 18 #include <libaddressinput/util/basictypes.h> | 19 #include <libaddressinput/util/basictypes.h> |
| 19 #include <libaddressinput/util/scoped_ptr.h> | 20 #include <libaddressinput/util/scoped_ptr.h> |
| 20 | 21 |
| 21 #include <map> | 22 #include <map> |
| 22 #include <string> | 23 #include <string> |
| 23 | 24 |
| 24 namespace i18n { | 25 namespace i18n { |
| 25 namespace addressinput { | 26 namespace addressinput { |
| 26 | 27 |
| 27 class Rule; | 28 class Rule; |
| 28 | 29 |
| 29 // A recursive data structure that stores a set of rules for a region. Can store | 30 // A recursive data structure that stores a set of rules for a region. Can store |
| 30 // the rules for a country, its administrative areas, localities, and dependent | 31 // the rules for a country, its administrative areas, localities, and dependent |
| 31 // localities, in addition to the language-specific rules. | 32 // localities, in addition to the language-specific rules. |
| 32 // | 33 // |
| 33 // Example for Canada and some of its provinces: | 34 // Example for Canada and some of its provinces: |
| 34 // CA-->fr | 35 // CA-->fr |
| 35 // | | 36 // | |
| 36 // ------------------------------------- | 37 // ------------------------------------- |
| 37 // | | | | | | 38 // | | | | | |
| 38 // v v v v v | 39 // v v v v v |
| 39 // AB-->fr BC-->fr MB-->fr NB-->fr NL-->fr | 40 // AB-->fr BC-->fr MB-->fr NB-->fr NL-->fr |
| 40 // | 41 // |
| 41 // The rules in Canada are in English by default. Each rule also has a French | 42 // The rules in Canada are in English by default. Each rule also has a French |
| 42 // language version. | 43 // language version. |
| 43 class Ruleset { | 44 class Ruleset { |
| 44 public: | 45 public: |
| 45 // Builds a ruleset with a region-wide |rule| in the default language of the | 46 // Builds a ruleset for |field| with a region-wide |rule| in the default |
| 46 // country. The |rule| should not be NULL. | 47 // language of the country. The |field| should be between COUNTRY and |
| 47 explicit Ruleset(scoped_ptr<Rule> rule); | 48 // DEPENDENT_LOCALITY (inclusively). The |rule| should not be NULL. |
| 49 Ruleset(AddressField field, scoped_ptr<Rule> rule); |
| 48 | 50 |
| 49 ~Ruleset(); | 51 ~Ruleset(); |
| 50 | 52 |
| 51 // Returns the region-wide rule in the default language of the country. | 53 // Returns the field type for this ruleset. |
| 54 AddressField field() const { return field_; } |
| 55 |
| 56 // Returns the region-wide rule for this ruleset in the default language of |
| 57 // the country. |
| 52 const Rule& rule() const { return *rule_.get(); } | 58 const Rule& rule() const { return *rule_.get(); } |
| 53 | 59 |
| 54 // Adds and the |ruleset| for |sub_region|. | 60 // Adds the |ruleset| for |sub_region|. A |sub_region| should be added at most |
| 61 // once. The |ruleset| should not be NULL. |
| 62 // |
| 63 // The field of the |ruleset| parameter must be exactly one smaller than the |
| 64 // field of this ruleset. For example, a COUNTRY ruleset can contain |
| 65 // ADMIN_AREA rulesets, but should not contain COUNTRY or LOCALITY rulesets. |
| 55 void AddSubRegionRuleset(const std::string& sub_region, | 66 void AddSubRegionRuleset(const std::string& sub_region, |
| 56 scoped_ptr<Ruleset> ruleset); | 67 scoped_ptr<Ruleset> ruleset); |
| 57 | 68 |
| 58 // Adds a language-specific |rule| for |language_code| for this region. | 69 // Adds a language-specific |rule| for |language_code| for this region. A |
| 70 // |language_code| should be added at most once. The |rule| should not be |
| 71 // NULL. |
| 59 void AddLanguageCodeRule(const std::string& language_code, | 72 void AddLanguageCodeRule(const std::string& language_code, |
| 60 scoped_ptr<Rule> rule); | 73 scoped_ptr<Rule> rule); |
| 61 | 74 |
| 75 // Returns the set of rules for |sub_region|. The result is NULL if there's no |
| 76 // such |sub_region|. The caller does not own the result. |
| 77 Ruleset* GetSubRegionRuleset(const std::string& sub_region) const; |
| 78 |
| 79 // If there's a language-specific rule for |language_code|, then returns this |
| 80 // rule. Otherwise returns the rule in the default language of the country. |
| 81 const Rule& GetLanguageCodeRule(const std::string& language_code) const; |
| 82 |
| 62 private: | 83 private: |
| 84 // The field of this ruleset. |
| 85 const AddressField field_; |
| 86 |
| 63 // The region-wide rule in the default language of the country. | 87 // The region-wide rule in the default language of the country. |
| 64 scoped_ptr<const Rule> rule_; | 88 const scoped_ptr<const Rule> rule_; |
| 65 | 89 |
| 66 // Owned rulesets for sub-regions. | 90 // Owned rulesets for sub-regions. |
| 67 std::map<std::string, Ruleset*> sub_regions_; | 91 std::map<std::string, Ruleset*> sub_regions_; |
| 68 | 92 |
| 69 // Owned language-specific rules for the region. | 93 // Owned language-specific rules for the region. |
| 70 std::map<std::string, const Rule*> language_codes_; | 94 std::map<std::string, const Rule*> language_codes_; |
| 71 | 95 |
| 72 DISALLOW_COPY_AND_ASSIGN(Ruleset); | 96 DISALLOW_COPY_AND_ASSIGN(Ruleset); |
| 73 }; | 97 }; |
| 74 | 98 |
| 75 } // namespace addressinput | 99 } // namespace addressinput |
| 76 } // namespace i18n | 100 } // namespace i18n |
| 77 | 101 |
| 78 #endif // I18N_ADDRESSINPUT_RULESET_H_ | 102 #endif // I18N_ADDRESSINPUT_RULESET_H_ |
| OLD | NEW |