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_.reset(rules_ready.release());
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 (*rules_ready_)(ruleset != NULL, country_code_, ruleset.Pass());
80 scoped_ptr<Ruleset> CountryRulesAggregator::Build(const std::string& key,
82 scoped_ptr<Rule> rule = ParseRule(key, field);
84 return scoped_ptr<Ruleset>();
87 // Determine the languages that have language-specific rules. For example,
88 // the default language in Switzerland is "de", but "fr" and "it" have
89 // language specific rules.
90 if (field == COUNTRY) {
91 non_default_languages_ = rule->GetLanguages();
92 std::vector<std::string>::iterator default_language_it =
93 std::find(non_default_languages_.begin(),
94 non_default_languages_.end(),
96 if (default_language_it != non_default_languages_.end()) {
97 non_default_languages_.erase(default_language_it);
101 scoped_ptr<Ruleset> ruleset(new Ruleset(field, rule.Pass()));
103 // Parse the language-specific rules. For example, parse the rules for "fr"
104 // and "it" languages in Switzerland.
105 for (std::vector<std::string>::const_iterator
106 lang_it = non_default_languages_.begin();
107 lang_it != non_default_languages_.end(); ++lang_it) {
108 scoped_ptr<Rule> lang_rule = ParseRule(key + "--" + *lang_it, field);
109 if (lang_rule == NULL) {
110 return scoped_ptr<Ruleset>();
112 ruleset->AddLanguageCodeRule(*lang_it, lang_rule.Pass());
115 // Parse the sub-keys. For example, parse the rules for all of the states in
116 // US: "CA", "TX", "NY", etc.
117 for (std::vector<std::string>::const_iterator
118 subkey_it = ruleset->rule().GetSubKeys().begin();
119 subkey_it != ruleset->rule().GetSubKeys().end(); ++subkey_it) {
120 scoped_ptr<Ruleset> sub_ruleset =
121 Build(key + "/" + *subkey_it, static_cast<AddressField>(field + 1));
122 if (sub_ruleset == NULL) {
123 return scoped_ptr<Ruleset>();
125 ruleset->AddSubRegionRuleset(*subkey_it, sub_ruleset.Pass());
128 return ruleset.Pass();
131 scoped_ptr<Rule> CountryRulesAggregator::ParseRule(const std::string& key,
132 AddressField field) const {
133 scoped_ptr<Json> value;
134 if (!json_->GetJsonValueForKey(key, &value) || value == NULL) {
135 return scoped_ptr<Rule>();
137 scoped_ptr<Rule> rule(new Rule);
138 if (field == COUNTRY) {
139 rule->CopyFrom(Rule::GetDefault());
141 rule->ParseJsonRule(*value);
145 void CountryRulesAggregator::Reset() {
146 country_code_.clear();
148 rules_ready_.reset();
150 non_default_languages_.clear();
153 } // namespace addressinput