1 // Copyright (C) 2012 The Libphonenumber Authors
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 // Author: Patrick Mezard
16
17 #include "phonenumbers/geocoding/phonenumber_offline_geocoder.h"
18
19 #include <algorithm>
20 #include <string>
21
22 #include <unicode/unistr.h> // NOLINT(build/include_order)
23
24 #include "phonenumbers/geocoding/area_code_map.h"
25 #include "phonenumbers/geocoding/geocoding_data.h"
26 #include "phonenumbers/geocoding/mapping_file_provider.h"
27 #include "phonenumbers/phonenumberutil.h"
28 #include "phonenumbers/stl_util.h"
29
30 namespace i18n {
31 namespace phonenumbers {
32
33 using icu::UnicodeString;
34 using std::string;
35
36 namespace {
37
38 // Returns true if s1 comes strictly before s2 in lexicographic order.
IsLowerThan(const char * s1,const char * s2)39 bool IsLowerThan(const char* s1, const char* s2) {
40 return strcmp(s1, s2) < 0;
41 }
42
43 } // namespace
44
PhoneNumberOfflineGeocoder()45 PhoneNumberOfflineGeocoder::PhoneNumberOfflineGeocoder() {
46 Init(get_country_calling_codes(), get_country_calling_codes_size(),
47 get_country_languages, get_prefix_language_code_pairs(),
48 get_prefix_language_code_pairs_size(), get_prefix_descriptions);
49 }
50
PhoneNumberOfflineGeocoder(const int * country_calling_codes,int country_calling_codes_size,country_languages_getter get_country_languages,const char ** prefix_language_code_pairs,int prefix_language_code_pairs_size,prefix_descriptions_getter get_prefix_descriptions)51 PhoneNumberOfflineGeocoder::PhoneNumberOfflineGeocoder(
52 const int* country_calling_codes, int country_calling_codes_size,
53 country_languages_getter get_country_languages,
54 const char** prefix_language_code_pairs,
55 int prefix_language_code_pairs_size,
56 prefix_descriptions_getter get_prefix_descriptions) {
57 Init(country_calling_codes, country_calling_codes_size,
58 get_country_languages, prefix_language_code_pairs,
59 prefix_language_code_pairs_size, get_prefix_descriptions);
60 }
61
Init(const int * country_calling_codes,int country_calling_codes_size,country_languages_getter get_country_languages,const char ** prefix_language_code_pairs,int prefix_language_code_pairs_size,prefix_descriptions_getter get_prefix_descriptions)62 void PhoneNumberOfflineGeocoder::Init(
63 const int* country_calling_codes, int country_calling_codes_size,
64 country_languages_getter get_country_languages,
65 const char** prefix_language_code_pairs,
66 int prefix_language_code_pairs_size,
67 prefix_descriptions_getter get_prefix_descriptions) {
68 phone_util_ = PhoneNumberUtil::GetInstance();
69 provider_.reset(new MappingFileProvider(country_calling_codes,
70 country_calling_codes_size,
71 get_country_languages));
72 prefix_language_code_pairs_ = prefix_language_code_pairs;
73 prefix_language_code_pairs_size_ = prefix_language_code_pairs_size;
74 get_prefix_descriptions_ = get_prefix_descriptions;
75 }
76
~PhoneNumberOfflineGeocoder()77 PhoneNumberOfflineGeocoder::~PhoneNumberOfflineGeocoder() {
78 gtl::STLDeleteContainerPairSecondPointers(
79 available_maps_.begin(), available_maps_.end());
80 }
81
GetPhonePrefixDescriptions(int prefix,const string & language,const string & script,const string & region) const82 const AreaCodeMap* PhoneNumberOfflineGeocoder::GetPhonePrefixDescriptions(
83 int prefix, const string& language, const string& script,
84 const string& region) const {
85 string filename;
86 provider_->GetFileName(prefix, language, script, region, &filename);
87 if (filename.empty()) {
88 return NULL;
89 }
90 AreaCodeMaps::const_iterator it = available_maps_.find(filename);
91 if (it == available_maps_.end()) {
92 return LoadAreaCodeMapFromFile(filename);
93 }
94 return it->second;
95 }
96
LoadAreaCodeMapFromFile(const string & filename) const97 const AreaCodeMap* PhoneNumberOfflineGeocoder::LoadAreaCodeMapFromFile(
98 const string& filename) const {
99 const char** const prefix_language_code_pairs_end =
100 prefix_language_code_pairs_ + prefix_language_code_pairs_size_;
101 const char** const prefix_language_code_pair =
102 std::lower_bound(prefix_language_code_pairs_,
103 prefix_language_code_pairs_end,
104 filename.c_str(), IsLowerThan);
105 if (prefix_language_code_pair != prefix_language_code_pairs_end &&
106 filename.compare(*prefix_language_code_pair) == 0) {
107 AreaCodeMap* const m = new AreaCodeMap();
108 m->ReadAreaCodeMap(get_prefix_descriptions_(
109 prefix_language_code_pair - prefix_language_code_pairs_));
110 return available_maps_.insert(AreaCodeMaps::value_type(filename, m))
111 .first->second;
112 }
113 return NULL;
114 }
115
GetCountryNameForNumber(const PhoneNumber & number,const Locale & language) const116 string PhoneNumberOfflineGeocoder::GetCountryNameForNumber(
117 const PhoneNumber& number, const Locale& language) const {
118 string region_code;
119 phone_util_->GetRegionCodeForNumber(number, ®ion_code);
120 return GetRegionDisplayName(®ion_code, language);
121 }
122
GetRegionDisplayName(const string * region_code,const Locale & language) const123 string PhoneNumberOfflineGeocoder::GetRegionDisplayName(
124 const string* region_code, const Locale& language) const {
125 if (region_code == NULL || region_code->compare("ZZ") == 0 ||
126 region_code->compare(
127 PhoneNumberUtil::kRegionCodeForNonGeoEntity) == 0) {
128 return "";
129 }
130 UnicodeString udisplay_country;
131 icu::Locale("", region_code->c_str()).getDisplayCountry(
132 language, udisplay_country);
133 string display_country;
134 udisplay_country.toUTF8String(display_country);
135 return display_country;
136 }
137
GetDescriptionForValidNumber(const PhoneNumber & number,const Locale & language) const138 string PhoneNumberOfflineGeocoder::GetDescriptionForValidNumber(
139 const PhoneNumber& number, const Locale& language) const {
140 const char* const description = GetAreaDescription(
141 number, language.getLanguage(), "", language.getCountry());
142 return *description != '\0'
143 ? description
144 : GetCountryNameForNumber(number, language);
145 }
146
GetDescriptionForValidNumber(const PhoneNumber & number,const Locale & language,const string & user_region) const147 string PhoneNumberOfflineGeocoder::GetDescriptionForValidNumber(
148 const PhoneNumber& number, const Locale& language,
149 const string& user_region) const {
150 // If the user region matches the number's region, then we just show the
151 // lower-level description, if one exists - if no description exists, we will
152 // show the region(country) name for the number.
153 string region_code;
154 phone_util_->GetRegionCodeForNumber(number, ®ion_code);
155 if (user_region.compare(region_code) == 0) {
156 return GetDescriptionForValidNumber(number, language);
157 }
158 // Otherwise, we just show the region(country) name for now.
159 return GetRegionDisplayName(®ion_code, language);
160 }
161
GetDescriptionForNumber(const PhoneNumber & number,const Locale & locale) const162 string PhoneNumberOfflineGeocoder::GetDescriptionForNumber(
163 const PhoneNumber& number, const Locale& locale) const {
164 PhoneNumberUtil::PhoneNumberType number_type =
165 phone_util_->GetNumberType(number);
166 if (number_type == PhoneNumberUtil::UNKNOWN) {
167 return "";
168 } else if (!phone_util_->IsNumberGeographical(number_type,
169 number.country_code())) {
170 return GetCountryNameForNumber(number, locale);
171 }
172 return GetDescriptionForValidNumber(number, locale);
173 }
174
GetDescriptionForNumber(const PhoneNumber & number,const Locale & language,const string & user_region) const175 string PhoneNumberOfflineGeocoder::GetDescriptionForNumber(
176 const PhoneNumber& number, const Locale& language,
177 const string& user_region) const {
178 PhoneNumberUtil::PhoneNumberType number_type =
179 phone_util_->GetNumberType(number);
180 if (number_type == PhoneNumberUtil::UNKNOWN) {
181 return "";
182 } else if (!phone_util_->IsNumberGeographical(number_type,
183 number.country_code())) {
184 return GetCountryNameForNumber(number, language);
185 }
186 return GetDescriptionForValidNumber(number, language, user_region);
187 }
188
GetAreaDescription(const PhoneNumber & number,const string & lang,const string & script,const string & region) const189 const char* PhoneNumberOfflineGeocoder::GetAreaDescription(
190 const PhoneNumber& number, const string& lang, const string& script,
191 const string& region) const {
192 const int country_calling_code = number.country_code();
193 // NANPA area is not split in C++ code.
194 const int phone_prefix = country_calling_code;
195 const AreaCodeMap* const descriptions = GetPhonePrefixDescriptions(
196 phone_prefix, lang, script, region);
197 const char* description = descriptions ? descriptions->Lookup(number) : NULL;
198 // When a location is not available in the requested language, fall back to
199 // English.
200 if ((!description || *description == '\0') && MayFallBackToEnglish(lang)) {
201 const AreaCodeMap* default_descriptions = GetPhonePrefixDescriptions(
202 phone_prefix, "en", "", "");
203 if (!default_descriptions) {
204 return "";
205 }
206 description = default_descriptions->Lookup(number);
207 }
208 return description ? description : "";
209 }
210
211 // Don't fall back to English if the requested language is among the following:
212 // - Chinese
213 // - Japanese
214 // - Korean
MayFallBackToEnglish(const string & lang) const215 bool PhoneNumberOfflineGeocoder::MayFallBackToEnglish(
216 const string& lang) const {
217 return lang.compare("zh") && lang.compare("ja") && lang.compare("ko");
218 }
219
220 } // namespace phonenumbers
221 } // namespace i18n
222