OLD | NEW |
(Empty) | |
| 1 // Copyright (C) 2013 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 "rule.h" |
| 16 |
| 17 #include <libaddressinput/address_field.h> |
| 18 #include <libaddressinput/util/scoped_ptr.h> |
| 19 |
| 20 #include <map> |
| 21 #include <string> |
| 22 #include <utility> |
| 23 |
| 24 #include "address_field_util.h" |
| 25 #include "grit.h" |
| 26 #include "messages.h" |
| 27 #include "util/json.h" |
| 28 |
| 29 namespace i18n { |
| 30 namespace addressinput { |
| 31 |
| 32 namespace { |
| 33 |
| 34 typedef std::map<std::string, int> NameMessageIdMap; |
| 35 |
| 36 const char kAdminAreaNameTypeKey[] = "state_name_type"; |
| 37 const char kFormatKey[] = "fmt"; |
| 38 const char kPostalCodeNameTypeKey[] = "zip_name_type"; |
| 39 |
| 40 NameMessageIdMap InitAdminAreaMessageIds() { |
| 41 NameMessageIdMap message_ids; |
| 42 message_ids.insert(std::make_pair( |
| 43 "area", IDS_LIBADDRESSINPUT_I18N_AREA)); |
| 44 message_ids.insert(std::make_pair( |
| 45 "county", IDS_LIBADDRESSINPUT_I18N_COUNTY_LABEL)); |
| 46 message_ids.insert(std::make_pair( |
| 47 "department", IDS_LIBADDRESSINPUT_I18N_DEPARTMENT)); |
| 48 message_ids.insert(std::make_pair( |
| 49 "district", IDS_LIBADDRESSINPUT_I18N_DEPENDENT_LOCALITY_LABEL)); |
| 50 message_ids.insert(std::make_pair( |
| 51 "do_si", IDS_LIBADDRESSINPUT_I18N_DO_SI)); |
| 52 message_ids.insert(std::make_pair( |
| 53 "emirate", IDS_LIBADDRESSINPUT_I18N_EMIRATE)); |
| 54 message_ids.insert(std::make_pair( |
| 55 "island", IDS_LIBADDRESSINPUT_I18N_ISLAND)); |
| 56 message_ids.insert(std::make_pair( |
| 57 "parish", IDS_LIBADDRESSINPUT_I18N_PARISH)); |
| 58 message_ids.insert(std::make_pair( |
| 59 "prefecture", IDS_LIBADDRESSINPUT_I18N_PREFECTURE)); |
| 60 message_ids.insert(std::make_pair( |
| 61 "province", IDS_LIBADDRESSINPUT_I18N_PROVINCE)); |
| 62 message_ids.insert(std::make_pair( |
| 63 "state", IDS_LIBADDRESSINPUT_I18N_STATE_LABEL)); |
| 64 return message_ids; |
| 65 } |
| 66 |
| 67 const NameMessageIdMap& GetAdminAreaMessageIds() { |
| 68 static const NameMessageIdMap kAdminAreaMessageIds(InitAdminAreaMessageIds()); |
| 69 return kAdminAreaMessageIds; |
| 70 } |
| 71 |
| 72 NameMessageIdMap InitPostalCodeMessageIds() { |
| 73 NameMessageIdMap message_ids; |
| 74 message_ids.insert(std::make_pair( |
| 75 "postal", IDS_LIBADDRESSINPUT_I18N_POSTAL_CODE_LABEL)); |
| 76 message_ids.insert(std::make_pair( |
| 77 "zip", IDS_LIBADDRESSINPUT_I18N_ZIP_CODE_LABEL)); |
| 78 return message_ids; |
| 79 } |
| 80 |
| 81 const NameMessageIdMap& GetPostalCodeMessageIds() { |
| 82 static const NameMessageIdMap kPostalCodeMessageIds( |
| 83 InitPostalCodeMessageIds()); |
| 84 return kPostalCodeMessageIds; |
| 85 } |
| 86 |
| 87 int GetMessageIdFromName(const std::string& name, |
| 88 const NameMessageIdMap& message_ids) { |
| 89 NameMessageIdMap::const_iterator it = message_ids.find(name); |
| 90 return it != message_ids.end() ? it->second : INVALID_MESSAGE_ID; |
| 91 } |
| 92 |
| 93 } // namespace |
| 94 |
| 95 Rule::Rule() |
| 96 : format_(), |
| 97 admin_area_name_message_id_(INVALID_MESSAGE_ID), |
| 98 postal_code_name_message_id_(INVALID_MESSAGE_ID) {} |
| 99 |
| 100 Rule::~Rule() {} |
| 101 |
| 102 void Rule::CopyFrom(const Rule& rule) { |
| 103 format_ = rule.format_; |
| 104 admin_area_name_message_id_ = rule.admin_area_name_message_id_; |
| 105 postal_code_name_message_id_ = rule.postal_code_name_message_id_; |
| 106 } |
| 107 |
| 108 bool Rule::ParseSerializedRule(const std::string& serialized_rule) { |
| 109 scoped_ptr<Json> json(Json::Build()); |
| 110 if (!json->ParseObject(serialized_rule)) { |
| 111 return false; |
| 112 } |
| 113 |
| 114 if (json->HasStringValueForKey(kFormatKey)) { |
| 115 ParseAddressFieldsFormat(json->GetStringValueForKey(kFormatKey), &format_); |
| 116 } |
| 117 |
| 118 if (json->HasStringValueForKey(kAdminAreaNameTypeKey)) { |
| 119 admin_area_name_message_id_ = |
| 120 GetMessageIdFromName(json->GetStringValueForKey(kAdminAreaNameTypeKey), |
| 121 GetAdminAreaMessageIds()); |
| 122 } |
| 123 |
| 124 if (json->HasStringValueForKey(kPostalCodeNameTypeKey)) { |
| 125 postal_code_name_message_id_ = |
| 126 GetMessageIdFromName(json->GetStringValueForKey(kPostalCodeNameTypeKey), |
| 127 GetPostalCodeMessageIds()); |
| 128 } |
| 129 |
| 130 return true; |
| 131 } |
| 132 |
| 133 } // namespace addressinput |
| 134 } // namespace i18n |
OLD | NEW |