Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / libaddressinput / chromium / cpp / src / country_rules_aggregator.cc
1 // Copyright (C) 2014 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 #include "country_rules_aggregator.h"
16
17 #include <libaddressinput/address_field.h>
18 #include <libaddressinput/callback.h>
19 #include <libaddressinput/util/scoped_ptr.h>
20
21 #include <algorithm>
22 #include <cassert>
23 #include <cstddef>
24 #include <string>
25 #include <vector>
26
27 #include "retriever.h"
28 #include "rule.h"
29 #include "ruleset.h"
30 #include "util/json.h"
31
32 namespace i18n {
33 namespace addressinput {
34
35 CountryRulesAggregator::CountryRulesAggregator(scoped_ptr<Retriever> retriever)
36     : retriever_(retriever.Pass()),
37       country_code_(),
38       key_(),
39       rules_ready_(),
40       json_(),
41       non_default_languages_() {
42   assert(retriever_ != NULL);
43 }
44
45 CountryRulesAggregator::~CountryRulesAggregator() {}
46
47 void CountryRulesAggregator::AggregateRules(const std::string& country_code,
48                                             scoped_ptr<Callback> rules_ready) {
49   Reset();
50   country_code_ = country_code;
51   rules_ready_ = rules_ready.Pass();
52
53   // Key construction:
54   // https://code.google.com/p/libaddressinput/wiki/AddressValidationMetadata
55   // Example of a country-level key: "data/CA".
56   key_ = "data/" + country_code_;
57   retriever_->Retrieve(
58       key_, BuildCallback(this, &CountryRulesAggregator::OnDataReady));
59 }
60
61 void CountryRulesAggregator::OnDataReady(bool success,
62                                          const std::string& key,
63                                          const std::string& data) {
64   if (key != key_) {
65     return;  // An abandoned request.
66   }
67
68   json_ = Json::Build().Pass();
69   if (!success || !json_->ParseObject(data)) {
70     (*rules_ready_)(false, country_code_, scoped_ptr<Ruleset>());
71     Reset();
72     return;
73   }
74
75   scoped_ptr<Ruleset> ruleset = Build(key_, COUNTRY);
76   const bool parse_success = ruleset != NULL;
77   (*rules_ready_)(parse_success, country_code_, ruleset.Pass());
78   Reset();
79 }
80
81 scoped_ptr<Ruleset> CountryRulesAggregator::Build(const std::string& key,
82                                                   AddressField field) {
83   scoped_ptr<Rule> rule = ParseRule(key, field);
84   if (rule == NULL) {
85     return scoped_ptr<Ruleset>();
86   }
87
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(),
96                   rule->GetLanguage());
97     if (default_language_it != non_default_languages_.end()) {
98       non_default_languages_.erase(default_language_it);
99     }
100   }
101
102   scoped_ptr<Ruleset> ruleset(new Ruleset(field, rule.Pass()));
103
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>();
112     }
113     ruleset->AddLanguageCodeRule(*lang_it, lang_rule.Pass());
114   }
115
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>();
125     }
126     ruleset->AddSubRegionRuleset(*subkey_it, sub_ruleset.Pass());
127   }
128
129   return ruleset.Pass();
130 }
131
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>();
137   }
138   scoped_ptr<Rule> rule(new Rule);
139   if (field == COUNTRY) {
140     rule->CopyFrom(Rule::GetDefault());
141   }
142   rule->ParseJsonRule(*value);
143   return rule.Pass();
144 }
145
146 void CountryRulesAggregator::Reset() {
147   country_code_.clear();
148   key_.clear();
149   rules_ready_.reset();
150   json_.reset();
151   non_default_languages_.clear();
152 }
153
154 }  // namespace addressinput
155 }  // namespace i18n