| 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 214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 225 BuildScopedPtrCallback(this, &AddressValidatorImpl::OnRulesLoaded)); | 225 BuildScopedPtrCallback(this, &AddressValidatorImpl::OnRulesLoaded)); |
| 226 } | 226 } |
| 227 } | 227 } |
| 228 | 228 |
| 229 // AddressValidator implementation. | 229 // AddressValidator implementation. |
| 230 virtual Status ValidateAddress( | 230 virtual Status ValidateAddress( |
| 231 const AddressData& address, | 231 const AddressData& address, |
| 232 const AddressProblemFilter& filter, | 232 const AddressProblemFilter& filter, |
| 233 AddressProblems* problems) const { | 233 AddressProblems* problems) const { |
| 234 std::map<std::string, Ruleset*>::const_iterator ruleset_it = | 234 std::map<std::string, Ruleset*>::const_iterator ruleset_it = |
| 235 rules_.find(address.country_code); | 235 rules_.find(address.region_code); |
| 236 | 236 |
| 237 // We can still validate the required fields even if the full ruleset isn't | 237 // We can still validate the required fields even if the full ruleset isn't |
| 238 // ready. | 238 // ready. |
| 239 if (ruleset_it == rules_.end()) { | 239 if (ruleset_it == rules_.end()) { |
| 240 if (problems != NULL) { | 240 if (problems != NULL) { |
| 241 Rule rule; | 241 Rule rule; |
| 242 rule.CopyFrom(Rule::GetDefault()); | 242 rule.CopyFrom(Rule::GetDefault()); |
| 243 if (rule.ParseSerializedRule( | 243 if (rule.ParseSerializedRule( |
| 244 RegionDataConstants::GetRegionData(address.country_code))) { | 244 RegionDataConstants::GetRegionData(address.region_code))) { |
| 245 EnforceRequiredFields(rule, address, filter, problems); | 245 EnforceRequiredFields(rule, address, filter, problems); |
| 246 } | 246 } |
| 247 } | 247 } |
| 248 | 248 |
| 249 return loading_rules_.find(address.country_code) != loading_rules_.end() | 249 return loading_rules_.find(address.region_code) != loading_rules_.end() |
| 250 ? RULES_NOT_READY | 250 ? RULES_NOT_READY |
| 251 : RULES_UNAVAILABLE; | 251 : RULES_UNAVAILABLE; |
| 252 } | 252 } |
| 253 | 253 |
| 254 if (problems == NULL) { | 254 if (problems == NULL) { |
| 255 return SUCCESS; | 255 return SUCCESS; |
| 256 } | 256 } |
| 257 | 257 |
| 258 const Ruleset* ruleset = ruleset_it->second; | 258 const Ruleset* ruleset = ruleset_it->second; |
| 259 assert(ruleset != NULL); | 259 assert(ruleset != NULL); |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 314 | 314 |
| 315 return SUCCESS; | 315 return SUCCESS; |
| 316 } | 316 } |
| 317 | 317 |
| 318 // AddressValidator implementation. | 318 // AddressValidator implementation. |
| 319 virtual Status GetSuggestions(const AddressData& user_input, | 319 virtual Status GetSuggestions(const AddressData& user_input, |
| 320 AddressField focused_field, | 320 AddressField focused_field, |
| 321 size_t suggestions_limit, | 321 size_t suggestions_limit, |
| 322 std::vector<AddressData>* suggestions) const { | 322 std::vector<AddressData>* suggestions) const { |
| 323 std::map<std::string, Ruleset*>::const_iterator ruleset_it = | 323 std::map<std::string, Ruleset*>::const_iterator ruleset_it = |
| 324 rules_.find(user_input.country_code); | 324 rules_.find(user_input.region_code); |
| 325 | 325 |
| 326 if (ruleset_it == rules_.end()) { | 326 if (ruleset_it == rules_.end()) { |
| 327 return | 327 return |
| 328 loading_rules_.find(user_input.country_code) != loading_rules_.end() | 328 loading_rules_.find(user_input.region_code) != loading_rules_.end() |
| 329 ? RULES_NOT_READY | 329 ? RULES_NOT_READY |
| 330 : RULES_UNAVAILABLE; | 330 : RULES_UNAVAILABLE; |
| 331 } | 331 } |
| 332 | 332 |
| 333 if (suggestions == NULL) { | 333 if (suggestions == NULL) { |
| 334 return SUCCESS; | 334 return SUCCESS; |
| 335 } | 335 } |
| 336 suggestions->clear(); | 336 suggestions->clear(); |
| 337 | 337 |
| 338 assert(ruleset_it->second != NULL); | 338 assert(ruleset_it->second != NULL); |
| (...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 470 rule_field = Rule::KEY; | 470 rule_field = Rule::KEY; |
| 471 } else if (matching_rule_fields.test(Rule::NAME)) { | 471 } else if (matching_rule_fields.test(Rule::NAME)) { |
| 472 rule_field = Rule::NAME; | 472 rule_field = Rule::NAME; |
| 473 } else if (matching_rule_fields.test(Rule::LATIN_NAME)) { | 473 } else if (matching_rule_fields.test(Rule::LATIN_NAME)) { |
| 474 rule_field = Rule::LATIN_NAME; | 474 rule_field = Rule::LATIN_NAME; |
| 475 } else { | 475 } else { |
| 476 assert(false); | 476 assert(false); |
| 477 } | 477 } |
| 478 | 478 |
| 479 AddressData suggestion; | 479 AddressData suggestion; |
| 480 suggestion.country_code = user_input.country_code; | 480 suggestion.region_code = user_input.region_code; |
| 481 suggestion.postal_code = user_input.postal_code; | 481 suggestion.postal_code = user_input.postal_code; |
| 482 | 482 |
| 483 // Traverse the tree of rulesets from the most specific |ruleset| to the | 483 // Traverse the tree of rulesets from the most specific |ruleset| to the |
| 484 // country-wide "root" of the tree. Use the region names found at each of | 484 // country-wide "root" of the tree. Use the region names found at each of |
| 485 // the levels of the ruleset tree to build the |suggestion|. | 485 // the levels of the ruleset tree to build the |suggestion|. |
| 486 for (const Ruleset* suggestion_ruleset = &ruleset; | 486 for (const Ruleset* suggestion_ruleset = &ruleset; |
| 487 suggestion_ruleset->parent() != NULL; | 487 suggestion_ruleset->parent() != NULL; |
| 488 suggestion_ruleset = suggestion_ruleset->parent()) { | 488 suggestion_ruleset = suggestion_ruleset->parent()) { |
| 489 const Rule& suggestion_rule = | 489 const Rule& suggestion_rule = |
| 490 suggestion_ruleset->GetLanguageCodeRule(user_input.language_code); | 490 suggestion_ruleset->GetLanguageCodeRule(user_input.language_code); |
| 491 suggestion.SetFieldValue(suggestion_ruleset->field(), | 491 suggestion.SetFieldValue(suggestion_ruleset->field(), |
| 492 suggestion_rule.GetIdentityField(rule_field)); | 492 suggestion_rule.GetIdentityField(rule_field)); |
| 493 } | 493 } |
| 494 | 494 |
| 495 suggestions->push_back(suggestion); | 495 suggestions->push_back(suggestion); |
| 496 } | 496 } |
| 497 | 497 |
| 498 return SUCCESS; | 498 return SUCCESS; |
| 499 } | 499 } |
| 500 | 500 |
| 501 // AddressValidator implementation. | 501 // AddressValidator implementation. |
| 502 virtual bool CanonicalizeAdministrativeArea(AddressData* address_data) const { | 502 virtual bool CanonicalizeAdministrativeArea(AddressData* address_data) const { |
| 503 std::map<std::string, Ruleset*>::const_iterator ruleset_it = | 503 std::map<std::string, Ruleset*>::const_iterator ruleset_it = |
| 504 rules_.find(address_data->country_code); | 504 rules_.find(address_data->region_code); |
| 505 if (ruleset_it == rules_.end()) { | 505 if (ruleset_it == rules_.end()) { |
| 506 return false; | 506 return false; |
| 507 } | 507 } |
| 508 const Rule& rule = | 508 const Rule& rule = |
| 509 ruleset_it->second->GetLanguageCodeRule(address_data->language_code); | 509 ruleset_it->second->GetLanguageCodeRule(address_data->language_code); |
| 510 | 510 |
| 511 return rule.CanonicalizeSubKey(address_data->administrative_area, | 511 return rule.CanonicalizeSubKey(address_data->administrative_area, |
| 512 true, // Keep input latin. | 512 true, // Keep input latin. |
| 513 &address_data->administrative_area); | 513 &address_data->administrative_area); |
| 514 } | 514 } |
| (...skipping 22 matching lines...) Expand all Loading... |
| 537 const AddressData& address, | 537 const AddressData& address, |
| 538 const AddressProblemFilter& filter, | 538 const AddressProblemFilter& filter, |
| 539 AddressProblems* problems) const { | 539 AddressProblems* problems) const { |
| 540 assert(problems != NULL); | 540 assert(problems != NULL); |
| 541 for (std::vector<AddressField>::const_iterator | 541 for (std::vector<AddressField>::const_iterator |
| 542 field_it = country_rule.GetRequired().begin(); | 542 field_it = country_rule.GetRequired().begin(); |
| 543 field_it != country_rule.GetRequired().end(); | 543 field_it != country_rule.GetRequired().end(); |
| 544 ++field_it) { | 544 ++field_it) { |
| 545 bool field_empty = *field_it != STREET_ADDRESS | 545 bool field_empty = *field_it != STREET_ADDRESS |
| 546 ? address.GetFieldValue(*field_it).empty() | 546 ? address.GetFieldValue(*field_it).empty() |
| 547 : IsEmptyStreetAddress(address.address_lines); | 547 : IsEmptyStreetAddress(address.address_line); |
| 548 if (field_empty && | 548 if (field_empty && |
| 549 FilterAllows( | 549 FilterAllows( |
| 550 filter, *field_it, AddressProblem::MISSING_REQUIRED_FIELD)) { | 550 filter, *field_it, AddressProblem::MISSING_REQUIRED_FIELD)) { |
| 551 problems->push_back(AddressProblem( | 551 problems->push_back(AddressProblem( |
| 552 *field_it, | 552 *field_it, |
| 553 AddressProblem::MISSING_REQUIRED_FIELD, | 553 AddressProblem::MISSING_REQUIRED_FIELD, |
| 554 IDS_LIBADDRESSINPUT_I18N_MISSING_REQUIRED_FIELD)); | 554 IDS_LIBADDRESSINPUT_I18N_MISSING_REQUIRED_FIELD)); |
| 555 } | 555 } |
| 556 } | 556 } |
| 557 } | 557 } |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 590 scoped_ptr<Downloader> downloader, | 590 scoped_ptr<Downloader> downloader, |
| 591 scoped_ptr<Storage> storage, | 591 scoped_ptr<Storage> storage, |
| 592 LoadRulesDelegate* load_rules_delegate) { | 592 LoadRulesDelegate* load_rules_delegate) { |
| 593 return scoped_ptr<AddressValidator>(new AddressValidatorImpl( | 593 return scoped_ptr<AddressValidator>(new AddressValidatorImpl( |
| 594 validation_data_url, downloader.Pass(), storage.Pass(), | 594 validation_data_url, downloader.Pass(), storage.Pass(), |
| 595 load_rules_delegate)); | 595 load_rules_delegate)); |
| 596 } | 596 } |
| 597 | 597 |
| 598 } // namespace addressinput | 598 } // namespace addressinput |
| 599 } // namespace i18n | 599 } // namespace i18n |
| OLD | NEW |