OLD | NEW |
1 // Copyright (C) 2013 Google Inc. | 1 // Copyright (C) 2013 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, |
(...skipping 16 matching lines...) Expand all Loading... |
27 #include <cstddef> | 27 #include <cstddef> |
28 #include <map> | 28 #include <map> |
29 #include <set> | 29 #include <set> |
30 #include <string> | 30 #include <string> |
31 #include <utility> | 31 #include <utility> |
32 #include <vector> | 32 #include <vector> |
33 | 33 |
34 #include <re2/re2.h> | 34 #include <re2/re2.h> |
35 | 35 |
36 #include "country_rules_aggregator.h" | 36 #include "country_rules_aggregator.h" |
| 37 #include "grit.h" |
37 #include "grit/libaddressinput_strings.h" | 38 #include "grit/libaddressinput_strings.h" |
38 #include "region_data_constants.h" | 39 #include "region_data_constants.h" |
39 #include "retriever.h" | 40 #include "retriever.h" |
40 #include "rule.h" | 41 #include "rule.h" |
41 #include "ruleset.h" | 42 #include "ruleset.h" |
42 #include "util/stl_util.h" | 43 #include "util/stl_util.h" |
43 #include "util/string_util.h" | 44 #include "util/string_util.h" |
44 | 45 |
45 namespace i18n { | 46 namespace i18n { |
46 namespace addressinput { | 47 namespace addressinput { |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
90 bool IsEmptyStreetAddress(const std::vector<std::string>& street_address) { | 91 bool IsEmptyStreetAddress(const std::vector<std::string>& street_address) { |
91 for (std::vector<std::string>::const_iterator it = street_address.begin(); | 92 for (std::vector<std::string>::const_iterator it = street_address.begin(); |
92 it != street_address.end(); ++it) { | 93 it != street_address.end(); ++it) { |
93 if (!it->empty()) { | 94 if (!it->empty()) { |
94 return false; | 95 return false; |
95 } | 96 } |
96 } | 97 } |
97 return true; | 98 return true; |
98 } | 99 } |
99 | 100 |
| 101 // Returns the ID of the string that should be displayed when the given field |
| 102 // is invalid in the context of |country_rule|. |
| 103 int GetInvalidFieldMessageId(const Rule& country_rule, AddressField field) { |
| 104 switch (field) { |
| 105 case LOCALITY: |
| 106 return IDS_LIBADDRESSINPUT_I18N_INVALID_LOCALITY_LABEL; |
| 107 case DEPENDENT_LOCALITY: |
| 108 return IDS_LIBADDRESSINPUT_I18N_INVALID_DEPENDENT_LOCALITY_LABEL; |
| 109 |
| 110 case ADMIN_AREA: { |
| 111 const std::string& admin_area_name_type = |
| 112 country_rule.GetAdminAreaNameType(); |
| 113 if (admin_area_name_type == "area") { |
| 114 return IDS_LIBADDRESSINPUT_I18N_INVALID_AREA; |
| 115 } |
| 116 if (admin_area_name_type == "county") { |
| 117 return IDS_LIBADDRESSINPUT_I18N_INVALID_COUNTY_LABEL; |
| 118 } |
| 119 if (admin_area_name_type == "department") { |
| 120 return IDS_LIBADDRESSINPUT_I18N_INVALID_DEPARTMENT; |
| 121 } |
| 122 if (admin_area_name_type == "district") { |
| 123 return IDS_LIBADDRESSINPUT_I18N_INVALID_DEPENDENT_LOCALITY_LABEL; |
| 124 } |
| 125 if (admin_area_name_type == "do_si") { |
| 126 return IDS_LIBADDRESSINPUT_I18N_INVALID_DO_SI; |
| 127 } |
| 128 if (admin_area_name_type == "emirate") { |
| 129 return IDS_LIBADDRESSINPUT_I18N_INVALID_EMIRATE; |
| 130 } |
| 131 if (admin_area_name_type == "island") { |
| 132 return IDS_LIBADDRESSINPUT_I18N_INVALID_ISLAND; |
| 133 } |
| 134 if (admin_area_name_type == "parish") { |
| 135 return IDS_LIBADDRESSINPUT_I18N_INVALID_PARISH; |
| 136 } |
| 137 if (admin_area_name_type == "prefecture") { |
| 138 return IDS_LIBADDRESSINPUT_I18N_INVALID_PREFECTURE; |
| 139 } |
| 140 if (admin_area_name_type == "province") { |
| 141 return IDS_LIBADDRESSINPUT_I18N_INVALID_PROVINCE; |
| 142 } |
| 143 if (admin_area_name_type == "state") { |
| 144 return IDS_LIBADDRESSINPUT_I18N_INVALID_STATE_LABEL; |
| 145 } |
| 146 return INVALID_MESSAGE_ID; |
| 147 } |
| 148 |
| 149 case POSTAL_CODE: { |
| 150 const std::string& postal_code_name_type = |
| 151 country_rule.GetPostalCodeNameType(); |
| 152 if (postal_code_name_type == "postal") { |
| 153 return IDS_LIBADDRESSINPUT_I18N_INVALID_POSTAL_CODE_LABEL; |
| 154 } |
| 155 if (postal_code_name_type == "zip") { |
| 156 return IDS_LIBADDRESSINPUT_I18N_INVALID_ZIP_CODE_LABEL; |
| 157 } |
| 158 return INVALID_MESSAGE_ID; |
| 159 } |
| 160 |
| 161 default: |
| 162 return IDS_LIBADDRESSINPUT_I18N_INVALID_ENTRY; |
| 163 } |
| 164 } |
| 165 |
100 // Collects rulesets based on whether they have a parent in the given list. | 166 // Collects rulesets based on whether they have a parent in the given list. |
101 class ParentedRulesetCollector { | 167 class ParentedRulesetCollector { |
102 public: | 168 public: |
103 // Retains a reference to both of the parameters. Does not make a copy of | 169 // Retains a reference to both of the parameters. Does not make a copy of |
104 // |parent_rulesets|. Does not take ownership of |rulesets_with_parents|. The | 170 // |parent_rulesets|. Does not take ownership of |rulesets_with_parents|. The |
105 // |rulesets_with_parents| parameter should not be NULL. | 171 // |rulesets_with_parents| parameter should not be NULL. |
106 ParentedRulesetCollector(const Rulesets& parent_rulesets, | 172 ParentedRulesetCollector(const Rulesets& parent_rulesets, |
107 Rulesets* rulesets_with_parents) | 173 Rulesets* rulesets_with_parents) |
108 : parent_rulesets_(parent_rulesets), | 174 : parent_rulesets_(parent_rulesets), |
109 rulesets_with_parents_(rulesets_with_parents) { | 175 rulesets_with_parents_(rulesets_with_parents) { |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
200 if (!address.postal_code.empty() && | 266 if (!address.postal_code.empty() && |
201 !country_rule.GetPostalCodeFormat().empty() && | 267 !country_rule.GetPostalCodeFormat().empty() && |
202 FilterAllows(filter, | 268 FilterAllows(filter, |
203 POSTAL_CODE, | 269 POSTAL_CODE, |
204 AddressProblem::UNRECOGNIZED_FORMAT) && | 270 AddressProblem::UNRECOGNIZED_FORMAT) && |
205 !RE2::FullMatch( | 271 !RE2::FullMatch( |
206 address.postal_code, country_rule.GetPostalCodeFormat())) { | 272 address.postal_code, country_rule.GetPostalCodeFormat())) { |
207 problems->push_back(AddressProblem( | 273 problems->push_back(AddressProblem( |
208 POSTAL_CODE, | 274 POSTAL_CODE, |
209 AddressProblem::UNRECOGNIZED_FORMAT, | 275 AddressProblem::UNRECOGNIZED_FORMAT, |
210 country_rule.GetInvalidPostalCodeMessageId())); | 276 GetInvalidFieldMessageId(country_rule, POSTAL_CODE))); |
211 } | 277 } |
212 | 278 |
213 while (ruleset != NULL) { | 279 while (ruleset != NULL) { |
214 const Rule& rule = ruleset->GetLanguageCodeRule(address.language_code); | 280 const Rule& rule = ruleset->GetLanguageCodeRule(address.language_code); |
215 | 281 |
216 // Validate the field values, e.g. state names in US. | 282 // Validate the field values, e.g. state names in US. |
217 AddressField sub_field_type = | 283 AddressField sub_field_type = |
218 static_cast<AddressField>(ruleset->field() + 1); | 284 static_cast<AddressField>(ruleset->field() + 1); |
219 std::string sub_key; | 285 std::string sub_key; |
220 const std::string& user_input = address.GetFieldValue(sub_field_type); | 286 const std::string& user_input = address.GetFieldValue(sub_field_type); |
221 if (!user_input.empty() && | 287 if (!user_input.empty() && |
222 FilterAllows(filter, sub_field_type, AddressProblem::UNKNOWN_VALUE) && | 288 FilterAllows(filter, sub_field_type, AddressProblem::UNKNOWN_VALUE) && |
223 !rule.CanonicalizeSubKey(user_input, false, &sub_key)) { | 289 !rule.CanonicalizeSubKey(user_input, false, &sub_key)) { |
224 problems->push_back(AddressProblem( | 290 problems->push_back(AddressProblem( |
225 sub_field_type, | 291 sub_field_type, |
226 AddressProblem::UNKNOWN_VALUE, | 292 AddressProblem::UNKNOWN_VALUE, |
227 country_rule.GetInvalidFieldMessageId(sub_field_type))); | 293 GetInvalidFieldMessageId(country_rule, sub_field_type))); |
228 } | 294 } |
229 | 295 |
230 // Validate sub-region specific postal code format. A sub-region specifies | 296 // Validate sub-region specific postal code format. A sub-region specifies |
231 // the regular expression for a prefix of the postal code. | 297 // the regular expression for a prefix of the postal code. |
232 if (ruleset->field() > COUNTRY && | 298 if (ruleset->field() > COUNTRY && |
233 !address.postal_code.empty() && | 299 !address.postal_code.empty() && |
234 !rule.GetPostalCodeFormat().empty() && | 300 !rule.GetPostalCodeFormat().empty() && |
235 FilterAllows(filter, | 301 FilterAllows(filter, |
236 POSTAL_CODE, | 302 POSTAL_CODE, |
237 AddressProblem::MISMATCHING_VALUE) && | 303 AddressProblem::MISMATCHING_VALUE) && |
238 !ValueMatchesPrefixRegex( | 304 !ValueMatchesPrefixRegex( |
239 address.postal_code, rule.GetPostalCodeFormat())) { | 305 address.postal_code, rule.GetPostalCodeFormat())) { |
240 problems->push_back(AddressProblem( | 306 problems->push_back(AddressProblem( |
241 POSTAL_CODE, | 307 POSTAL_CODE, |
242 AddressProblem::MISMATCHING_VALUE, | 308 AddressProblem::MISMATCHING_VALUE, |
243 country_rule.GetInvalidPostalCodeMessageId())); | 309 GetInvalidFieldMessageId(country_rule, POSTAL_CODE))); |
244 } | 310 } |
245 | 311 |
246 ruleset = ruleset->GetSubRegionRuleset(sub_key); | 312 ruleset = ruleset->GetSubRegionRuleset(sub_key); |
247 } | 313 } |
248 | 314 |
249 return SUCCESS; | 315 return SUCCESS; |
250 } | 316 } |
251 | 317 |
252 // AddressValidator implementation. | 318 // AddressValidator implementation. |
253 virtual Status GetSuggestions(const AddressData& user_input, | 319 virtual Status GetSuggestions(const AddressData& user_input, |
(...skipping 270 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
524 scoped_ptr<Downloader> downloader, | 590 scoped_ptr<Downloader> downloader, |
525 scoped_ptr<Storage> storage, | 591 scoped_ptr<Storage> storage, |
526 LoadRulesDelegate* load_rules_delegate) { | 592 LoadRulesDelegate* load_rules_delegate) { |
527 return scoped_ptr<AddressValidator>(new AddressValidatorImpl( | 593 return scoped_ptr<AddressValidator>(new AddressValidatorImpl( |
528 validation_data_url, downloader.Pass(), storage.Pass(), | 594 validation_data_url, downloader.Pass(), storage.Pass(), |
529 load_rules_delegate)); | 595 load_rules_delegate)); |
530 } | 596 } |
531 | 597 |
532 } // namespace addressinput | 598 } // namespace addressinput |
533 } // namespace i18n | 599 } // namespace i18n |
OLD | NEW |