Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(359)

Side by Side Diff: third_party/libaddressinput/chromium/cpp/src/rule.cc

Issue 261013010: autocomplete: add ability to get full address (hidden behind a flag). (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: self review Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (C) 2013 Google Inc. 1 // Copyright (C) 2013 Google Inc.
2 // 2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); 3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with 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 5 // You may obtain a copy of the License at
6 // 6 //
7 // http://www.apache.org/licenses/LICENSE-2.0 7 // http://www.apache.org/licenses/LICENSE-2.0
8 // 8 //
9 // Unless required by applicable law or agreed to in writing, software 9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, 10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and 12 // See the License for the specific language governing permissions and
13 // limitations under the License. 13 // limitations under the License.
14 14
15 #include "rule.h" 15 #include "rule.h"
16 16
17 #include <libaddressinput/address_field.h> 17 #include <libaddressinput/address_field.h>
18 #include <libaddressinput/util/scoped_ptr.h> 18 #include <libaddressinput/util/scoped_ptr.h>
19 19
20 #include <cassert> 20 #include <cassert>
21 #include <cstddef> 21 #include <cstddef>
22 #include <string> 22 #include <string>
23 #include <vector> 23 #include <vector>
24 24
25 #include "grit.h"
26 #include "grit/libaddressinput_strings.h"
27 #include "region_data_constants.h" 25 #include "region_data_constants.h"
28 #include "util/json.h" 26 #include "util/json.h"
29 #include "util/string_util.h" 27 #include "util/string_util.h"
30 28
31 namespace i18n { 29 namespace i18n {
32 namespace addressinput { 30 namespace addressinput {
33 31
34 namespace { 32 namespace {
35 33
36 bool ParseToken(char c, AddressField* field) { 34 bool ParseToken(char c, AddressField* field) {
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
130 assert(fields != NULL); 128 assert(fields != NULL);
131 fields->clear(); 129 fields->clear();
132 for (size_t i = 0; i < required.length(); ++i) { 130 for (size_t i = 0; i < required.length(); ++i) {
133 AddressField field = COUNTRY; 131 AddressField field = COUNTRY;
134 if (ParseToken(required[i], &field)) { 132 if (ParseToken(required[i], &field)) {
135 fields->push_back(field); 133 fields->push_back(field);
136 } 134 }
137 } 135 }
138 } 136 }
139 137
140 int GetAdminAreaMessageId(const std::string& admin_area_type, bool error) {
141 if (admin_area_type == "area") {
142 return error ? IDS_LIBADDRESSINPUT_I18N_INVALID_AREA
143 : IDS_LIBADDRESSINPUT_I18N_AREA;
144 }
145 if (admin_area_type == "county") {
146 return error ? IDS_LIBADDRESSINPUT_I18N_INVALID_COUNTY_LABEL
147 : IDS_LIBADDRESSINPUT_I18N_COUNTY_LABEL;
148 }
149 if (admin_area_type == "department") {
150 return error ? IDS_LIBADDRESSINPUT_I18N_INVALID_DEPARTMENT
151 : IDS_LIBADDRESSINPUT_I18N_DEPARTMENT;
152 }
153 if (admin_area_type == "district") {
154 return error ? IDS_LIBADDRESSINPUT_I18N_INVALID_DEPENDENT_LOCALITY_LABEL
155 : IDS_LIBADDRESSINPUT_I18N_DEPENDENT_LOCALITY_LABEL;
156 }
157 if (admin_area_type == "do_si") {
158 return error ? IDS_LIBADDRESSINPUT_I18N_INVALID_DO_SI
159 : IDS_LIBADDRESSINPUT_I18N_DO_SI;
160 }
161 if (admin_area_type == "emirate") {
162 return error ? IDS_LIBADDRESSINPUT_I18N_INVALID_EMIRATE
163 : IDS_LIBADDRESSINPUT_I18N_EMIRATE;
164 }
165 if (admin_area_type == "island") {
166 return error ? IDS_LIBADDRESSINPUT_I18N_INVALID_ISLAND
167 : IDS_LIBADDRESSINPUT_I18N_ISLAND;
168 }
169 if (admin_area_type == "parish") {
170 return error ? IDS_LIBADDRESSINPUT_I18N_INVALID_PARISH
171 : IDS_LIBADDRESSINPUT_I18N_PARISH;
172 }
173 if (admin_area_type == "prefecture") {
174 return error ? IDS_LIBADDRESSINPUT_I18N_INVALID_PREFECTURE
175 : IDS_LIBADDRESSINPUT_I18N_PREFECTURE;
176 }
177 if (admin_area_type == "province") {
178 return error ? IDS_LIBADDRESSINPUT_I18N_INVALID_PROVINCE
179 : IDS_LIBADDRESSINPUT_I18N_PROVINCE;
180 }
181 if (admin_area_type == "state") {
182 return error ? IDS_LIBADDRESSINPUT_I18N_INVALID_STATE_LABEL
183 : IDS_LIBADDRESSINPUT_I18N_STATE_LABEL;
184 }
185 return INVALID_MESSAGE_ID;
186 }
187
188 int GetPostalCodeMessageId(const std::string& postal_code_type, bool error) {
189 if (postal_code_type == "postal") {
190 return error ? IDS_LIBADDRESSINPUT_I18N_INVALID_POSTAL_CODE_LABEL
191 : IDS_LIBADDRESSINPUT_I18N_POSTAL_CODE_LABEL;
192 }
193 if (postal_code_type == "zip") {
194 return error ? IDS_LIBADDRESSINPUT_I18N_INVALID_ZIP_CODE_LABEL
195 : IDS_LIBADDRESSINPUT_I18N_ZIP_CODE_LABEL;
196 }
197 return INVALID_MESSAGE_ID;
198 }
199
200 // Finds |target| in |values_to_compare| and sets |selected_value| to the 138 // Finds |target| in |values_to_compare| and sets |selected_value| to the
201 // associated value from |values_to_select|. Returns true if |target| is in 139 // associated value from |values_to_select|. Returns true if |target| is in
202 // |values_to_compare|. |selected_value| should not be NULL. |values_to_compare| 140 // |values_to_compare|. |selected_value| should not be NULL. |values_to_compare|
203 // should not be larger than |values_to_select|. 141 // should not be larger than |values_to_select|.
204 bool GetMatchingValue(const std::string& target, 142 bool GetMatchingValue(const std::string& target,
205 const std::vector<std::string>& values_to_compare, 143 const std::vector<std::string>& values_to_compare,
206 const std::vector<std::string>& values_to_select, 144 const std::vector<std::string>& values_to_select,
207 std::string* selected_value) { 145 std::string* selected_value) {
208 assert(selected_value != NULL); 146 assert(selected_value != NULL);
209 assert(values_to_select.size() >= values_to_compare.size()); 147 assert(values_to_select.size() >= values_to_compare.size());
(...skipping 15 matching lines...) Expand all
225 : field(COUNTRY), literal(literal) { 163 : field(COUNTRY), literal(literal) {
226 assert(!literal.empty()); 164 assert(!literal.empty());
227 } 165 }
228 166
229 FormatElement::~FormatElement() {} 167 FormatElement::~FormatElement() {}
230 168
231 bool FormatElement::operator==(const FormatElement& other) const { 169 bool FormatElement::operator==(const FormatElement& other) const {
232 return field == other.field && literal == other.literal; 170 return field == other.field && literal == other.literal;
233 } 171 }
234 172
235 Rule::Rule() 173 Rule::Rule() {}
236 : key_(),
237 name_(),
238 latin_name_(),
239 format_(),
240 latin_format_(),
241 required_(),
242 sub_keys_(),
243 sub_names_(),
244 sub_lnames_(),
245 languages_(),
246 input_languages_(),
247 language_(),
248 postal_code_format_(),
249 admin_area_name_message_id_(INVALID_MESSAGE_ID),
250 invalid_admin_area_message_id_(INVALID_MESSAGE_ID),
251 postal_code_name_message_id_(INVALID_MESSAGE_ID),
252 invalid_postal_code_message_id_(INVALID_MESSAGE_ID) {}
253 174
254 Rule::~Rule() {} 175 Rule::~Rule() {}
255 176
256 // static 177 // static
257 const Rule& Rule::GetDefault() { 178 const Rule& Rule::GetDefault() {
258 // Allocated once and leaked on shutdown. 179 // Allocated once and leaked on shutdown.
259 static Rule* default_rule = NULL; 180 static Rule* default_rule = NULL;
260 if (default_rule == NULL) { 181 if (default_rule == NULL) {
261 default_rule = new Rule; 182 default_rule = new Rule;
262 default_rule->ParseSerializedRule( 183 default_rule->ParseSerializedRule(
(...skipping 10 matching lines...) Expand all
273 latin_format_ = rule.latin_format_; 194 latin_format_ = rule.latin_format_;
274 required_ = rule.required_; 195 required_ = rule.required_;
275 sub_keys_ = rule.sub_keys_; 196 sub_keys_ = rule.sub_keys_;
276 languages_ = rule.languages_; 197 languages_ = rule.languages_;
277 input_languages_ = rule.input_languages_; 198 input_languages_ = rule.input_languages_;
278 language_ = rule.language_; 199 language_ = rule.language_;
279 sub_keys_ = rule.sub_keys_; 200 sub_keys_ = rule.sub_keys_;
280 sub_names_ = rule.sub_names_; 201 sub_names_ = rule.sub_names_;
281 sub_lnames_ = rule.sub_lnames_; 202 sub_lnames_ = rule.sub_lnames_;
282 postal_code_format_ = rule.postal_code_format_; 203 postal_code_format_ = rule.postal_code_format_;
283 admin_area_name_message_id_ = rule.admin_area_name_message_id_; 204 admin_area_name_type_ = rule.admin_area_name_type_;
284 invalid_admin_area_message_id_ = rule.invalid_admin_area_message_id_; 205 postal_code_name_type_ = rule.postal_code_name_type_;
285 postal_code_name_message_id_ = rule.postal_code_name_message_id_;
286 invalid_postal_code_message_id_ = rule.invalid_postal_code_message_id_;
287 } 206 }
288 207
289 bool Rule::ParseSerializedRule(const std::string& serialized_rule) { 208 bool Rule::ParseSerializedRule(const std::string& serialized_rule) {
290 scoped_ptr<Json> json(Json::Build()); 209 scoped_ptr<Json> json(Json::Build());
291 if (!json->ParseObject(serialized_rule)) { 210 if (!json->ParseObject(serialized_rule)) {
292 return false; 211 return false;
293 } 212 }
294 ParseJsonRule(*json); 213 ParseJsonRule(*json);
295 return true; 214 return true;
296 } 215 }
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
348 267
349 if (json_rule.GetStringValueForKey("lang", &value)) { 268 if (json_rule.GetStringValueForKey("lang", &value)) {
350 language_.swap(value); 269 language_.swap(value);
351 } 270 }
352 271
353 if (json_rule.GetStringValueForKey("zip", &value)) { 272 if (json_rule.GetStringValueForKey("zip", &value)) {
354 postal_code_format_.swap(value); 273 postal_code_format_.swap(value);
355 } 274 }
356 275
357 if (json_rule.GetStringValueForKey("state_name_type", &value)) { 276 if (json_rule.GetStringValueForKey("state_name_type", &value)) {
358 admin_area_name_message_id_ = GetAdminAreaMessageId(value, false); 277 admin_area_name_type_.swap(value);
359 invalid_admin_area_message_id_ = GetAdminAreaMessageId(value, true);
360 } 278 }
361 279
362 if (json_rule.GetStringValueForKey("zip_name_type", &value)) { 280 if (json_rule.GetStringValueForKey("zip_name_type", &value)) {
363 postal_code_name_message_id_ = GetPostalCodeMessageId(value, false); 281 postal_code_name_type_.swap(value);
364 invalid_postal_code_message_id_ = GetPostalCodeMessageId(value, true);
365 } 282 }
366 } 283 }
367 284
368 const std::string& Rule::GetIdentityField(IdentityField identity_field) const { 285 const std::string& Rule::GetIdentityField(IdentityField identity_field) const {
369 switch (identity_field) { 286 switch (identity_field) {
370 case KEY: 287 case KEY:
371 return key_; 288 return key_;
372 case NAME: 289 case NAME:
373 return name_; 290 return name_;
374 case LATIN_NAME: 291 case LATIN_NAME:
375 return latin_name_; 292 return latin_name_;
376 case IDENTITY_FIELDS_SIZE: 293 case IDENTITY_FIELDS_SIZE:
377 assert(false); 294 assert(false);
378 } 295 }
379 return key_; 296 return key_;
380 } 297 }
381 298
382 int Rule::GetInvalidFieldMessageId(AddressField field) const {
383 switch (field) {
384 case ADMIN_AREA:
385 return invalid_admin_area_message_id_;
386 case LOCALITY:
387 return IDS_LIBADDRESSINPUT_I18N_INVALID_LOCALITY_LABEL;
388 case DEPENDENT_LOCALITY:
389 return IDS_LIBADDRESSINPUT_I18N_INVALID_DEPENDENT_LOCALITY_LABEL;
390 case POSTAL_CODE:
391 return invalid_postal_code_message_id_;
392 default:
393 return IDS_LIBADDRESSINPUT_I18N_INVALID_ENTRY;
394 }
395 }
396
397 bool Rule::CanonicalizeSubKey(const std::string& user_input, 299 bool Rule::CanonicalizeSubKey(const std::string& user_input,
398 bool keep_input_latin, 300 bool keep_input_latin,
399 std::string* sub_key) const { 301 std::string* sub_key) const {
400 assert(sub_key != NULL); 302 assert(sub_key != NULL);
401 303
402 if (sub_keys_.empty()) { 304 if (sub_keys_.empty()) {
403 *sub_key = user_input; 305 *sub_key = user_input;
404 return true; 306 return true;
405 } 307 }
406 308
407 return GetMatchingValue(user_input, sub_keys_, sub_keys_, sub_key) || 309 return GetMatchingValue(user_input, sub_keys_, sub_keys_, sub_key) ||
408 GetMatchingValue(user_input, sub_names_, sub_keys_, sub_key) || 310 GetMatchingValue(user_input, sub_names_, sub_keys_, sub_key) ||
409 (keep_input_latin && 311 (keep_input_latin &&
410 GetMatchingValue(user_input, sub_lnames_, sub_lnames_, sub_key)) || 312 GetMatchingValue(user_input, sub_lnames_, sub_lnames_, sub_key)) ||
411 GetMatchingValue(user_input, sub_lnames_, sub_keys_, sub_key); 313 GetMatchingValue(user_input, sub_lnames_, sub_keys_, sub_key);
412 } 314 }
413 315
414 } // namespace addressinput 316 } // namespace addressinput
415 } // namespace i18n 317 } // namespace i18n
OLDNEW
« no previous file with comments | « third_party/libaddressinput/chromium/cpp/src/rule.h ('k') | third_party/libaddressinput/chromium/cpp/test/address_ui_test.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698