OLD | NEW |
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" | 25 #include "grit.h" |
26 #include "grit/libaddressinput_strings.h" | 26 #include "grit/libaddressinput_strings.h" |
27 #include "region_data_constants.h" | 27 #include "region_data_constants.h" |
28 #include "util/json.h" | 28 #include "util/json.h" |
29 #include "util/string_compare.h" | 29 #include "util/string_util.h" |
30 #include "util/string_split.h" | |
31 | 30 |
32 namespace i18n { | 31 namespace i18n { |
33 namespace addressinput { | 32 namespace addressinput { |
34 | 33 |
35 namespace { | 34 namespace { |
36 | 35 |
37 bool ParseToken(char c, AddressField* field) { | 36 bool ParseToken(char c, AddressField* field) { |
38 assert(field != NULL); | 37 assert(field != NULL); |
39 switch (c) { | 38 switch (c) { |
40 case 'R': | 39 case 'R': |
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
212 | 211 |
213 bool FormatElement::operator==(const FormatElement& other) const { | 212 bool FormatElement::operator==(const FormatElement& other) const { |
214 return field == other.field && literal == other.literal; | 213 return field == other.field && literal == other.literal; |
215 } | 214 } |
216 | 215 |
217 Rule::Rule() | 216 Rule::Rule() |
218 : key_(), | 217 : key_(), |
219 name_(), | 218 name_(), |
220 latin_name_(), | 219 latin_name_(), |
221 format_(), | 220 format_(), |
| 221 latin_format_(), |
222 required_(), | 222 required_(), |
223 sub_keys_(), | 223 sub_keys_(), |
224 sub_names_(), | 224 sub_names_(), |
225 sub_lnames_(), | 225 sub_lnames_(), |
226 languages_(), | 226 languages_(), |
| 227 input_languages_(), |
227 language_(), | 228 language_(), |
228 postal_code_format_(), | 229 postal_code_format_(), |
229 admin_area_name_message_id_(INVALID_MESSAGE_ID), | 230 admin_area_name_message_id_(INVALID_MESSAGE_ID), |
230 invalid_admin_area_message_id_(INVALID_MESSAGE_ID), | 231 invalid_admin_area_message_id_(INVALID_MESSAGE_ID), |
231 postal_code_name_message_id_(INVALID_MESSAGE_ID), | 232 postal_code_name_message_id_(INVALID_MESSAGE_ID), |
232 invalid_postal_code_message_id_(INVALID_MESSAGE_ID) {} | 233 invalid_postal_code_message_id_(INVALID_MESSAGE_ID) {} |
233 | 234 |
234 Rule::~Rule() {} | 235 Rule::~Rule() {} |
235 | 236 |
236 // static | 237 // static |
237 const Rule& Rule::GetDefault() { | 238 const Rule& Rule::GetDefault() { |
238 // Allocated once and leaked on shutdown. | 239 // Allocated once and leaked on shutdown. |
239 static Rule* default_rule = NULL; | 240 static Rule* default_rule = NULL; |
240 if (default_rule == NULL) { | 241 if (default_rule == NULL) { |
241 default_rule = new Rule; | 242 default_rule = new Rule; |
242 default_rule->ParseSerializedRule( | 243 default_rule->ParseSerializedRule( |
243 RegionDataConstants::GetDefaultRegionData()); | 244 RegionDataConstants::GetDefaultRegionData()); |
244 } | 245 } |
245 return *default_rule; | 246 return *default_rule; |
246 } | 247 } |
247 | 248 |
248 void Rule::CopyFrom(const Rule& rule) { | 249 void Rule::CopyFrom(const Rule& rule) { |
249 key_ = rule.key_; | 250 key_ = rule.key_; |
250 name_ = rule.name_; | 251 name_ = rule.name_; |
251 latin_name_ = rule.latin_name_; | 252 latin_name_ = rule.latin_name_; |
252 format_ = rule.format_; | 253 format_ = rule.format_; |
| 254 latin_format_ = rule.latin_format_; |
253 required_ = rule.required_; | 255 required_ = rule.required_; |
254 sub_keys_ = rule.sub_keys_; | 256 sub_keys_ = rule.sub_keys_; |
255 languages_ = rule.languages_; | 257 languages_ = rule.languages_; |
| 258 input_languages_ = rule.input_languages_; |
256 language_ = rule.language_; | 259 language_ = rule.language_; |
257 sub_keys_ = rule.sub_keys_; | 260 sub_keys_ = rule.sub_keys_; |
258 sub_names_ = rule.sub_names_; | 261 sub_names_ = rule.sub_names_; |
259 sub_lnames_ = rule.sub_lnames_; | 262 sub_lnames_ = rule.sub_lnames_; |
260 postal_code_format_ = rule.postal_code_format_; | 263 postal_code_format_ = rule.postal_code_format_; |
261 admin_area_name_message_id_ = rule.admin_area_name_message_id_; | 264 admin_area_name_message_id_ = rule.admin_area_name_message_id_; |
262 invalid_admin_area_message_id_ = rule.invalid_admin_area_message_id_; | 265 invalid_admin_area_message_id_ = rule.invalid_admin_area_message_id_; |
263 postal_code_name_message_id_ = rule.postal_code_name_message_id_; | 266 postal_code_name_message_id_ = rule.postal_code_name_message_id_; |
264 invalid_postal_code_message_id_ = rule.invalid_postal_code_message_id_; | 267 invalid_postal_code_message_id_ = rule.invalid_postal_code_message_id_; |
265 } | 268 } |
(...skipping 18 matching lines...) Expand all Loading... |
284 } | 287 } |
285 | 288 |
286 if (json_rule.GetStringValueForKey("lname", &value)) { | 289 if (json_rule.GetStringValueForKey("lname", &value)) { |
287 latin_name_.swap(value); | 290 latin_name_.swap(value); |
288 } | 291 } |
289 | 292 |
290 if (json_rule.GetStringValueForKey("fmt", &value)) { | 293 if (json_rule.GetStringValueForKey("fmt", &value)) { |
291 ParseAddressFieldsFormat(value, &format_); | 294 ParseAddressFieldsFormat(value, &format_); |
292 } | 295 } |
293 | 296 |
| 297 if (json_rule.GetStringValueForKey("lfmt", &value)) { |
| 298 ParseAddressFieldsFormat(value, &latin_format_); |
| 299 } |
| 300 |
294 if (json_rule.GetStringValueForKey("require", &value)) { | 301 if (json_rule.GetStringValueForKey("require", &value)) { |
295 ParseAddressFieldsRequired(value, &required_); | 302 ParseAddressFieldsRequired(value, &required_); |
296 } | 303 } |
297 | 304 |
298 // Used as a separator in a list of items. For example, the list of supported | 305 // Used as a separator in a list of items. For example, the list of supported |
299 // languages can be "de~fr~it". | 306 // languages can be "de~fr~it". |
300 static const char kSeparator = '~'; | 307 static const char kSeparator = '~'; |
301 if (json_rule.GetStringValueForKey("sub_keys", &value)) { | 308 if (json_rule.GetStringValueForKey("sub_keys", &value)) { |
302 SplitString(value, kSeparator, &sub_keys_); | 309 SplitString(value, kSeparator, &sub_keys_); |
303 } | 310 } |
304 | 311 |
305 if (json_rule.GetStringValueForKey("sub_names", &value)) { | 312 if (json_rule.GetStringValueForKey("sub_names", &value)) { |
306 SplitString(value, kSeparator, &sub_names_); | 313 SplitString(value, kSeparator, &sub_names_); |
307 assert(sub_names_.size() == sub_keys_.size()); | 314 assert(sub_names_.size() == sub_keys_.size()); |
308 } | 315 } |
309 | 316 |
310 if (json_rule.GetStringValueForKey("sub_lnames", &value)) { | 317 if (json_rule.GetStringValueForKey("sub_lnames", &value)) { |
311 SplitString(value, kSeparator, &sub_lnames_); | 318 SplitString(value, kSeparator, &sub_lnames_); |
312 assert(sub_lnames_.size() == sub_keys_.size()); | 319 assert(sub_lnames_.size() == sub_keys_.size()); |
313 } | 320 } |
314 | 321 |
315 if (json_rule.GetStringValueForKey("languages", &value)) { | 322 if (json_rule.GetStringValueForKey("languages", &value)) { |
316 SplitString(value, kSeparator, &languages_); | 323 SplitString(value, kSeparator, &languages_); |
317 } | 324 } |
318 | 325 |
| 326 if (json_rule.GetStringValueForKey("input_languages", &value)) { |
| 327 SplitString(value, kSeparator, &input_languages_); |
| 328 } |
| 329 |
319 if (json_rule.GetStringValueForKey("lang", &value)) { | 330 if (json_rule.GetStringValueForKey("lang", &value)) { |
320 language_.swap(value); | 331 language_.swap(value); |
321 } | 332 } |
322 | 333 |
323 if (json_rule.GetStringValueForKey("zip", &value)) { | 334 if (json_rule.GetStringValueForKey("zip", &value)) { |
324 postal_code_format_.swap(value); | 335 postal_code_format_.swap(value); |
325 } | 336 } |
326 | 337 |
327 if (json_rule.GetStringValueForKey("state_name_type", &value)) { | 338 if (json_rule.GetStringValueForKey("state_name_type", &value)) { |
328 admin_area_name_message_id_ = GetAdminAreaMessageId(value, false); | 339 admin_area_name_message_id_ = GetAdminAreaMessageId(value, false); |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
383 if (LooseStringCompare(values[i], target)) { | 394 if (LooseStringCompare(values[i], target)) { |
384 *sub_key = sub_keys_[i]; | 395 *sub_key = sub_keys_[i]; |
385 return true; | 396 return true; |
386 } | 397 } |
387 } | 398 } |
388 return false; | 399 return false; |
389 } | 400 } |
390 | 401 |
391 } // namespace addressinput | 402 } // namespace addressinput |
392 } // namespace i18n | 403 } // namespace i18n |
OLD | NEW |