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