| 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, |
| (...skipping 21 matching lines...) Expand all Loading... |
| 32 namespace addressinput { | 32 namespace addressinput { |
| 33 | 33 |
| 34 namespace { | 34 namespace { |
| 35 | 35 |
| 36 typedef std::map<std::string, int> NameMessageIdMap; | 36 typedef std::map<std::string, int> NameMessageIdMap; |
| 37 | 37 |
| 38 const char kAdminAreaNameTypeKey[] = "state_name_type"; | 38 const char kAdminAreaNameTypeKey[] = "state_name_type"; |
| 39 const char kFormatKey[] = "fmt"; | 39 const char kFormatKey[] = "fmt"; |
| 40 const char kLanguageKey[] = "lang"; | 40 const char kLanguageKey[] = "lang"; |
| 41 const char kLanguagesKey[] = "languages"; | 41 const char kLanguagesKey[] = "languages"; |
| 42 const char kPostalCodeFormatKey[] = "zip"; |
| 42 const char kPostalCodeNameTypeKey[] = "zip_name_type"; | 43 const char kPostalCodeNameTypeKey[] = "zip_name_type"; |
| 44 const char kRequiredKey[] = "require"; |
| 43 const char kSubKeysKey[] = "sub_keys"; | 45 const char kSubKeysKey[] = "sub_keys"; |
| 44 | 46 |
| 45 // Used as a separator in a list of items. For example, the list of supported | 47 // Used as a separator in a list of items. For example, the list of supported |
| 46 // languages can be "de~fr~it". | 48 // languages can be "de~fr~it". |
| 47 const char kSeparator = '~'; | 49 const char kSeparator = '~'; |
| 48 | 50 |
| 49 NameMessageIdMap InitAdminAreaMessageIds() { | 51 NameMessageIdMap InitAdminAreaMessageIds() { |
| 50 NameMessageIdMap message_ids; | 52 NameMessageIdMap message_ids; |
| 51 message_ids.insert(std::make_pair( | 53 message_ids.insert(std::make_pair( |
| 52 "area", IDS_LIBADDRESSINPUT_I18N_AREA)); | 54 "area", IDS_LIBADDRESSINPUT_I18N_AREA)); |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 105 const Rule& InitDefaultRule() { | 107 const Rule& InitDefaultRule() { |
| 106 static Rule rule; | 108 static Rule rule; |
| 107 rule.ParseSerializedRule(RegionDataConstants::GetDefaultRegionData()); | 109 rule.ParseSerializedRule(RegionDataConstants::GetDefaultRegionData()); |
| 108 return rule; | 110 return rule; |
| 109 } | 111 } |
| 110 | 112 |
| 111 } // namespace | 113 } // namespace |
| 112 | 114 |
| 113 Rule::Rule() | 115 Rule::Rule() |
| 114 : format_(), | 116 : format_(), |
| 117 required_(), |
| 115 sub_keys_(), | 118 sub_keys_(), |
| 116 languages_(), | 119 languages_(), |
| 117 language_(), | 120 language_(), |
| 121 postal_code_format_(), |
| 118 admin_area_name_message_id_(INVALID_MESSAGE_ID), | 122 admin_area_name_message_id_(INVALID_MESSAGE_ID), |
| 119 postal_code_name_message_id_(INVALID_MESSAGE_ID) {} | 123 postal_code_name_message_id_(INVALID_MESSAGE_ID) {} |
| 120 | 124 |
| 121 Rule::~Rule() {} | 125 Rule::~Rule() {} |
| 122 | 126 |
| 123 // static | 127 // static |
| 124 const Rule& Rule::GetDefault() { | 128 const Rule& Rule::GetDefault() { |
| 125 // Returns the constant reference to the Rule object from InitDefaultRule(). | 129 // Returns the constant reference to the Rule object from InitDefaultRule(). |
| 126 // The static object is in InitDefaultRule(), but this function maintains a | 130 // The static object is in InitDefaultRule(), but this function maintains a |
| 127 // constant static reference to it. The constant static reference avoids | 131 // constant static reference to it. The constant static reference avoids |
| 128 // re-parsing the default region data. | 132 // re-parsing the default region data. |
| 129 static const Rule& kDefaultRule(InitDefaultRule()); | 133 static const Rule& kDefaultRule(InitDefaultRule()); |
| 130 return kDefaultRule; | 134 return kDefaultRule; |
| 131 } | 135 } |
| 132 | 136 |
| 133 void Rule::CopyFrom(const Rule& rule) { | 137 void Rule::CopyFrom(const Rule& rule) { |
| 134 format_ = rule.format_; | 138 format_ = rule.format_; |
| 139 required_ = rule.required_; |
| 135 sub_keys_ = rule.sub_keys_; | 140 sub_keys_ = rule.sub_keys_; |
| 136 languages_ = rule.languages_; | 141 languages_ = rule.languages_; |
| 137 language_ = rule.language_; | 142 language_ = rule.language_; |
| 143 postal_code_format_ = rule.postal_code_format_; |
| 138 admin_area_name_message_id_ = rule.admin_area_name_message_id_; | 144 admin_area_name_message_id_ = rule.admin_area_name_message_id_; |
| 139 postal_code_name_message_id_ = rule.postal_code_name_message_id_; | 145 postal_code_name_message_id_ = rule.postal_code_name_message_id_; |
| 140 } | 146 } |
| 141 | 147 |
| 142 bool Rule::ParseSerializedRule(const std::string& serialized_rule) { | 148 bool Rule::ParseSerializedRule(const std::string& serialized_rule) { |
| 143 scoped_ptr<Json> json(Json::Build()); | 149 scoped_ptr<Json> json(Json::Build()); |
| 144 if (!json->ParseObject(serialized_rule)) { | 150 if (!json->ParseObject(serialized_rule)) { |
| 145 return false; | 151 return false; |
| 146 } | 152 } |
| 147 | 153 |
| 148 if (json->HasStringValueForKey(kFormatKey)) { | 154 if (json->HasStringValueForKey(kFormatKey)) { |
| 149 ParseAddressFieldsFormat(json->GetStringValueForKey(kFormatKey), &format_); | 155 ParseAddressFieldsFormat(json->GetStringValueForKey(kFormatKey), &format_); |
| 150 } | 156 } |
| 151 | 157 |
| 158 if (json->HasStringValueForKey(kRequiredKey)) { |
| 159 ParseAddressFieldsRequired( |
| 160 json->GetStringValueForKey(kRequiredKey), &required_); |
| 161 } |
| 162 |
| 152 if (json->HasStringValueForKey(kSubKeysKey)) { | 163 if (json->HasStringValueForKey(kSubKeysKey)) { |
| 153 SplitString( | 164 SplitString( |
| 154 json->GetStringValueForKey(kSubKeysKey), kSeparator, &sub_keys_); | 165 json->GetStringValueForKey(kSubKeysKey), kSeparator, &sub_keys_); |
| 155 } | 166 } |
| 156 | 167 |
| 157 if (json->HasStringValueForKey(kLanguagesKey)) { | 168 if (json->HasStringValueForKey(kLanguagesKey)) { |
| 158 SplitString( | 169 SplitString( |
| 159 json->GetStringValueForKey(kLanguagesKey), kSeparator, &languages_); | 170 json->GetStringValueForKey(kLanguagesKey), kSeparator, &languages_); |
| 160 } | 171 } |
| 161 | 172 |
| 162 if (json->HasStringValueForKey(kLanguageKey)) { | 173 if (json->HasStringValueForKey(kLanguageKey)) { |
| 163 language_ = json->GetStringValueForKey(kLanguageKey); | 174 language_ = json->GetStringValueForKey(kLanguageKey); |
| 164 } | 175 } |
| 165 | 176 |
| 177 if (json->HasStringValueForKey(kPostalCodeFormatKey)) { |
| 178 postal_code_format_ = json->GetStringValueForKey(kPostalCodeFormatKey); |
| 179 } |
| 180 |
| 166 if (json->HasStringValueForKey(kAdminAreaNameTypeKey)) { | 181 if (json->HasStringValueForKey(kAdminAreaNameTypeKey)) { |
| 167 admin_area_name_message_id_ = | 182 admin_area_name_message_id_ = |
| 168 GetMessageIdFromName(json->GetStringValueForKey(kAdminAreaNameTypeKey), | 183 GetMessageIdFromName(json->GetStringValueForKey(kAdminAreaNameTypeKey), |
| 169 GetAdminAreaMessageIds()); | 184 GetAdminAreaMessageIds()); |
| 170 } | 185 } |
| 171 | 186 |
| 172 if (json->HasStringValueForKey(kPostalCodeNameTypeKey)) { | 187 if (json->HasStringValueForKey(kPostalCodeNameTypeKey)) { |
| 173 postal_code_name_message_id_ = | 188 postal_code_name_message_id_ = |
| 174 GetMessageIdFromName(json->GetStringValueForKey(kPostalCodeNameTypeKey), | 189 GetMessageIdFromName(json->GetStringValueForKey(kPostalCodeNameTypeKey), |
| 175 GetPostalCodeMessageIds()); | 190 GetPostalCodeMessageIds()); |
| 176 } | 191 } |
| 177 | 192 |
| 178 return true; | 193 return true; |
| 179 } | 194 } |
| 180 | 195 |
| 181 } // namespace addressinput | 196 } // namespace addressinput |
| 182 } // namespace i18n | 197 } // namespace i18n |
| OLD | NEW |