1 // Copyright (C) 2014 Google Inc.
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
15 #include "country_rules_aggregator.h"
17 #include <libaddressinput/address_field.h>
18 #include <libaddressinput/callback.h>
19 #include <libaddressinput/util/scoped_ptr.h>
27 #include "retriever.h"
30 #include "util/json.h"
33 namespace addressinput {
35 CountryRulesAggregator::CountryRulesAggregator(scoped_ptr<Retriever> retriever)
36 : retriever_(retriever.Pass()),
41 non_default_languages_() {
42 assert(retriever_ != NULL);
45 CountryRulesAggregator::~CountryRulesAggregator() {}
47 void CountryRulesAggregator::AggregateRules(const std::string& country_code,
48 scoped_ptr<Callback> rules_ready) {
50 country_code_ = country_code;
51 rules_ready_ = rules_ready.Pass();
54 // https://code.google.com/p/libaddressinput/wiki/AddressValidationMetadata
55 // Example of a country-level key: "data/CA".
56 key_ = "data/" + country_code_;
58 key_, BuildCallback(this, &CountryRulesAggregator::OnDataReady));
61 void CountryRulesAggregator::OnDataReady(bool success,
62 const std::string& key,
63 const std::string& data) {
65 return; // An abandoned request.
68 json_ = Json::Build().Pass();
69 if (!success || !json_->ParseObject(data)) {
70 (*rules_ready_)(false, country_code_, scoped_ptr<Ruleset>());
75 scoped_ptr<Ruleset> ruleset = Build(key_, COUNTRY);
76 const bool parse_success = ruleset != NULL;
77 (*rules_ready_)(parse_success, country_code_, ruleset.Pass());
81 scoped_ptr<Ruleset> CountryRulesAggregator::Build(const std::string& key,
83 scoped_ptr<Rule> rule = ParseRule(key, field);
85 return scoped_ptr<Ruleset>();
88 // Determine the languages that have language-specific rules. For example,
89 // the default language in Switzerland is "de", but "fr" and "it" have
90 // language specific rules.
91 if (field == COUNTRY) {
92 non_default_languages_ = rule->GetLanguages();
93 std::vector<std::string>::iterator default_language_it =
94 std::find(non_default_languages_.begin(),
95 non_default_languages_.end(),
97 if (default_language_it != non_default_languages_.end()) {
98 non_default_languages_.erase(default_language_it);
102 scoped_ptr<Ruleset> ruleset(new Ruleset(field, rule.Pass()));
104 // Parse the language-specific rules. For example, parse the rules for "fr"
105 // and "it" languages in Switzerland.
106 for (std::vector<std::string>::const_iterator
107 lang_it = non_default_languages_.begin();
108 lang_it != non_default_languages_.end(); ++lang_it) {
109 scoped_ptr<Rule> lang_rule = ParseRule(key + "--" + *lang_it, field);
110 if (lang_rule == NULL) {
111 return scoped_ptr<Ruleset>();
113 ruleset->AddLanguageCodeRule(*lang_it, lang_rule.Pass());
116 // Parse the sub-keys. For example, parse the rules for all of the states in
117 // US: "CA", "TX", "NY", etc.
118 for (std::vector<std::string>::const_iterator
119 subkey_it = ruleset->rule().GetSubKeys().begin();
120 subkey_it != ruleset->rule().GetSubKeys().end(); ++subkey_it) {
121 scoped_ptr<Ruleset> sub_ruleset =
122 Build(key + "/" + *subkey_it, static_cast<AddressField>(field + 1));
123 if (sub_ruleset == NULL) {
124 return scoped_ptr<Ruleset>();
126 ruleset->AddSubRegionRuleset(*subkey_it, sub_ruleset.Pass());
129 return ruleset.Pass();
132 scoped_ptr<Rule> CountryRulesAggregator::ParseRule(const std::string& key,
133 AddressField field) const {
134 scoped_ptr<Json> value;
135 if (!json_->GetJsonValueForKey(key, &value) || value == NULL) {
136 return scoped_ptr<Rule>();
138 scoped_ptr<Rule> rule(new Rule);
139 if (field == COUNTRY) {
140 rule->CopyFrom(Rule::GetDefault());
142 rule->ParseJsonRule(*value);
146 void CountryRulesAggregator::Reset() {
147 country_code_.clear();
149 rules_ready_.reset();
151 non_default_languages_.clear();
154 } // namespace addressinput