8135b3be2d7fb463f19b47fe1f38f0b74b500543
[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_.reset(rules_ready.release());
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   (*rules_ready_)(ruleset != NULL, country_code_, ruleset.Pass());
77   Reset();
78 }
79
80 scoped_ptr<Ruleset> CountryRulesAggregator::Build(const std::string& key,
81                                                   AddressField field) {
82   scoped_ptr<Rule> rule = ParseRule(key, field);
83   if (rule == NULL) {
84     return scoped_ptr<Ruleset>();
85   }
86
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(),
95                   rule->GetLanguage());
96     if (default_language_it != non_default_languages_.end()) {
97       non_default_languages_.erase(default_language_it);
98     }
99   }
100
101   scoped_ptr<Ruleset> ruleset(new Ruleset(field, rule.Pass()));
102
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>();
111     }
112     ruleset->AddLanguageCodeRule(*lang_it, lang_rule.Pass());
113   }
114
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>();
124     }
125     ruleset->AddSubRegionRuleset(*subkey_it, sub_ruleset.Pass());
126   }
127
128   return ruleset.Pass();
129 }
130
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>();
136   }
137   scoped_ptr<Rule> rule(new Rule);
138   if (field == COUNTRY) {
139     rule->CopyFrom(Rule::GetDefault());
140   }
141   rule->ParseJsonRule(*value);
142   return rule.Pass();
143 }
144
145 void CountryRulesAggregator::Reset() {
146   country_code_.clear();
147   key_.clear();
148   rules_ready_.reset();
149   json_.reset();
150   non_default_languages_.clear();
151 }
152
153 }  // namespace addressinput
154 }  // namespace i18n