This source file includes following definitions.
- non_default_languages_
- AggregateRules
- OnDataReady
- Build
- ParseRule
- Reset
#include "country_rules_aggregator.h"
#include <libaddressinput/address_field.h>
#include <libaddressinput/callback.h>
#include <libaddressinput/util/scoped_ptr.h>
#include <algorithm>
#include <cassert>
#include <cstddef>
#include <map>
#include <string>
#include <vector>
#include "retriever.h"
#include "rule.h"
#include "ruleset.h"
#include "util/json.h"
namespace i18n {
namespace addressinput {
CountryRulesAggregator::CountryRulesAggregator(scoped_ptr<Retriever> retriever)
: retriever_(retriever.Pass()),
country_code_(),
key_(),
rules_ready_(),
json_(),
non_default_languages_() {
assert(retriever_ != NULL);
}
CountryRulesAggregator::~CountryRulesAggregator() {}
void CountryRulesAggregator::AggregateRules(const std::string& country_code,
scoped_ptr<Callback> rules_ready) {
Reset();
country_code_ = country_code;
rules_ready_ = rules_ready.Pass();
key_ = "data/" + country_code_;
retriever_->Retrieve(
key_, BuildCallback(this, &CountryRulesAggregator::OnDataReady));
}
bool CountryRulesAggregator::OnDataReady(bool success,
const std::string& key,
const std::string& data) {
if (key != key_) {
return true;
}
json_ = Json::Build().Pass();
if (!success || !json_->ParseObject(data)) {
(*rules_ready_)(false, country_code_, scoped_ptr<Ruleset>());
Reset();
return false;
}
std::map<std::string, std::string> language_keys;
scoped_ptr<Ruleset> ruleset = Build(key_, COUNTRY, language_keys);
const bool parse_success = ruleset != NULL;
(*rules_ready_)(parse_success, country_code_, ruleset.Pass());
Reset();
return parse_success;
}
scoped_ptr<Ruleset> CountryRulesAggregator::Build(
const std::string& key,
AddressField field,
const std::map<std::string, std::string>& language_specific_keys) {
scoped_ptr<Rule> rule = ParseRule(key, field);
if (rule == NULL) {
return scoped_ptr<Ruleset>();
}
if (field == COUNTRY) {
non_default_languages_ = rule->GetLanguages();
std::vector<std::string>::iterator default_language_it =
std::find(non_default_languages_.begin(),
non_default_languages_.end(),
rule->GetLanguage());
if (default_language_it != non_default_languages_.end()) {
non_default_languages_.erase(default_language_it);
}
}
scoped_ptr<Ruleset> ruleset(new Ruleset(field, rule.Pass()));
std::map<std::string, std::map<std::string, std::string> >
language_specific_subkeys;
for (std::vector<std::string>::const_iterator
non_default_language_it = non_default_languages_.begin();
non_default_language_it != non_default_languages_.end();
++non_default_language_it) {
std::map<std::string, std::string>::const_iterator
language_specific_key_it =
language_specific_keys.find(*non_default_language_it);
std::string language_specific_key =
language_specific_key_it != language_specific_keys.end()
? language_specific_key_it->second
: key;
scoped_ptr<Rule> language_specific_rule =
ParseRule(language_specific_key + "--" + *non_default_language_it,
field);
if (language_specific_rule == NULL ||
language_specific_rule->GetSubKeys().size() !=
ruleset->rule().GetSubKeys().size()) {
return scoped_ptr<Ruleset>();
}
for (size_t i = 0; i < ruleset->rule().GetSubKeys().size(); ++i) {
const std::string& subkey = key + "/" + ruleset->rule().GetSubKeys()[i];
const std::string& language_specific_subkey =
key + "/" + language_specific_rule->GetSubKeys()[i];
language_specific_subkeys[subkey][*non_default_language_it] =
language_specific_subkey;
}
ruleset->AddLanguageCodeRule(
*non_default_language_it, language_specific_rule.Pass());
}
for (std::vector<std::string>::const_iterator
subkey_it = ruleset->rule().GetSubKeys().begin();
subkey_it != ruleset->rule().GetSubKeys().end(); ++subkey_it) {
std::string subkey = key + "/" + *subkey_it;
scoped_ptr<Ruleset> sub_ruleset =
Build(subkey,
static_cast<AddressField>(field + 1),
language_specific_subkeys[subkey]);
if (sub_ruleset == NULL) {
return scoped_ptr<Ruleset>();
}
ruleset->AddSubRegionRuleset(*subkey_it, sub_ruleset.Pass());
}
return ruleset.Pass();
}
scoped_ptr<Rule> CountryRulesAggregator::ParseRule(const std::string& key,
AddressField field) const {
scoped_ptr<Json> value;
if (!json_->GetJsonValueForKey(key, &value) || value == NULL) {
return scoped_ptr<Rule>();
}
scoped_ptr<Rule> rule(new Rule);
if (field == COUNTRY) {
rule->CopyFrom(Rule::GetDefault());
}
rule->ParseJsonRule(*value);
return rule.Pass();
}
void CountryRulesAggregator::Reset() {
country_code_.clear();
key_.clear();
rules_ready_.reset();
json_.reset();
non_default_languages_.clear();
}
}
}