| OLD | NEW |
| (Empty) | |
| 1 // Copyright (C) 2013 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_ADDRESS_VALIDATOR_H_ |
| 16 #define I18N_ADDRESSINPUT_ADDRESS_VALIDATOR_H_ |
| 17 |
| 18 #include <libaddressinput/address_field.h> |
| 19 #include <libaddressinput/address_problem.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 Downloader; |
| 31 class LoadRulesDelegate; |
| 32 class RuleRetriever; |
| 33 class Storage; |
| 34 struct AddressData; |
| 35 |
| 36 typedef std::vector<AddressProblem> AddressProblems; |
| 37 typedef std::multimap<AddressField, AddressProblem::Type> AddressProblemFilter; |
| 38 |
| 39 // Validates an AddressData structure. Sample usage: |
| 40 // class MyClass : public LoadRulesDelegate { |
| 41 // public: |
| 42 // MyClass() : validator_(new MyDownloader, new MyStorage, this) { |
| 43 // validator_.LoadRules("US"); |
| 44 // } |
| 45 // |
| 46 // virtual ~MyClass() {} |
| 47 // |
| 48 // virtual void OnAddressValidationRulesLoaded( |
| 49 // const std::string& country_code, |
| 50 // bool success) { |
| 51 // ... |
| 52 // } |
| 53 // |
| 54 // void ValidateAddress() { |
| 55 // AddressData address; |
| 56 // address.country_code = "US"; |
| 57 // address.administrative_area = "CA"; |
| 58 // AddressProblems problems; |
| 59 // AddressProblemFilter filter; |
| 60 // AddressValidator::Result result = |
| 61 // validator_.ValidateAddress(address, filter, &problems); |
| 62 // if (result == AddressValidator::SUCCESS) { |
| 63 // Process(problems); |
| 64 // } |
| 65 // } |
| 66 // |
| 67 // private: |
| 68 // AddressValidator validator_; |
| 69 // }; |
| 70 class AddressValidator { |
| 71 public: |
| 72 // The status of address validation. |
| 73 enum Result { |
| 74 // Address validation completed successfully. Check |problems| to see if any |
| 75 // problems were found. |
| 76 SUCCESS, |
| 77 |
| 78 // Failed to load the validation rules. The user of the library may choose |
| 79 // to reload the rules. |
| 80 RULES_UNAVAILABLE, |
| 81 |
| 82 // The validation rules are being loaded. The user of the library should try |
| 83 // again later. |
| 84 RULES_NOT_READY |
| 85 }; |
| 86 |
| 87 // Takes ownership of |downloader| and |storage|, which cannot be NULL. Does |
| 88 // not take ownership of |load_rules_delegate|, which can be NULL. |
| 89 AddressValidator(const Downloader* downloader, |
| 90 Storage* storage, |
| 91 LoadRulesDelegate* load_rules_delegate); |
| 92 ~AddressValidator(); |
| 93 |
| 94 // Downloads the generic validation rules for |country_code| and specific |
| 95 // rules for the country's administrative areas, localities, and dependent |
| 96 // localities. A typical download size is 10KB. The largest is 250KB. If a |
| 97 // country has language-specific validation rules, then these are also |
| 98 // downloaded. |
| 99 // |
| 100 // If the rules were downloaded successfully before, then does not reload the |
| 101 // rules. Notifies |load_rules_delegate| when the download finishes. |
| 102 void LoadRules(const std::string& country_code); |
| 103 |
| 104 // Validates the |address| and populates |problems| with the validation |
| 105 // problems, filtered according to the |filter| parameter. |
| 106 // |
| 107 // If the |filter| is empty, then all discovered validation problems are |
| 108 // returned. If the |filter| contains problem elements, then only the problems |
| 109 // in the |filter| may be returned. |
| 110 // |
| 111 // LoadRules() must have been called with the |address.country_code| before |
| 112 // invoking this method. |
| 113 Result ValidateAddress(const AddressData& address, |
| 114 const AddressProblemFilter& filter, |
| 115 AddressProblems* problems) const; |
| 116 private: |
| 117 scoped_ptr<RuleRetriever> rule_retriever_; |
| 118 LoadRulesDelegate* load_rules_delegate_; |
| 119 |
| 120 DISALLOW_COPY_AND_ASSIGN(AddressValidator); |
| 121 }; |
| 122 |
| 123 } // namespace addressinput |
| 124 } // namespace i18n |
| 125 |
| 126 #endif // I18N_ADDRESSINPUT_ADDRESS_VALIDATOR_H_ |
| OLD | NEW |