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 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
107 assert(fields != NULL); | 107 assert(fields != NULL); |
108 fields->clear(); | 108 fields->clear(); |
109 for (size_t i = 0; i < required.length(); ++i) { | 109 for (size_t i = 0; i < required.length(); ++i) { |
110 AddressField field = COUNTRY; | 110 AddressField field = COUNTRY; |
111 if (ParseToken(required[i], &field)) { | 111 if (ParseToken(required[i], &field)) { |
112 fields->push_back(field); | 112 fields->push_back(field); |
113 } | 113 } |
114 } | 114 } |
115 } | 115 } |
116 | 116 |
117 int GetAdminAreaMessageId(const std::string& admin_area_type) { | 117 int GetAdminAreaMessageId(const std::string& admin_area_type, bool error) { |
118 if (admin_area_type == "area") { | 118 if (admin_area_type == "area") { |
119 return IDS_LIBADDRESSINPUT_I18N_AREA; | 119 return error ? IDS_LIBADDRESSINPUT_I18N_INVALID_AREA |
| 120 : IDS_LIBADDRESSINPUT_I18N_AREA; |
120 } | 121 } |
121 if (admin_area_type == "county") { | 122 if (admin_area_type == "county") { |
122 return IDS_LIBADDRESSINPUT_I18N_COUNTY_LABEL; | 123 return error ? IDS_LIBADDRESSINPUT_I18N_INVALID_COUNTY_LABEL |
| 124 : IDS_LIBADDRESSINPUT_I18N_COUNTY_LABEL; |
123 } | 125 } |
124 if (admin_area_type == "department") { | 126 if (admin_area_type == "department") { |
125 return IDS_LIBADDRESSINPUT_I18N_DEPARTMENT; | 127 return error ? IDS_LIBADDRESSINPUT_I18N_INVALID_DEPARTMENT |
| 128 : IDS_LIBADDRESSINPUT_I18N_DEPARTMENT; |
126 } | 129 } |
127 if (admin_area_type == "district") { | 130 if (admin_area_type == "district") { |
128 return IDS_LIBADDRESSINPUT_I18N_DEPENDENT_LOCALITY_LABEL; | 131 return error ? IDS_LIBADDRESSINPUT_I18N_INVALID_DEPENDENT_LOCALITY_LABEL |
| 132 : IDS_LIBADDRESSINPUT_I18N_DEPENDENT_LOCALITY_LABEL; |
129 } | 133 } |
130 if (admin_area_type == "do_si") { | 134 if (admin_area_type == "do_si") { |
131 return IDS_LIBADDRESSINPUT_I18N_DO_SI; | 135 return error ? IDS_LIBADDRESSINPUT_I18N_INVALID_DO_SI |
| 136 : IDS_LIBADDRESSINPUT_I18N_DO_SI; |
132 } | 137 } |
133 if (admin_area_type == "emirate") { | 138 if (admin_area_type == "emirate") { |
134 return IDS_LIBADDRESSINPUT_I18N_EMIRATE; | 139 return error ? IDS_LIBADDRESSINPUT_I18N_INVALID_EMIRATE |
| 140 : IDS_LIBADDRESSINPUT_I18N_EMIRATE; |
135 } | 141 } |
136 if (admin_area_type == "island") { | 142 if (admin_area_type == "island") { |
137 return IDS_LIBADDRESSINPUT_I18N_ISLAND; | 143 return error ? IDS_LIBADDRESSINPUT_I18N_INVALID_ISLAND |
| 144 : IDS_LIBADDRESSINPUT_I18N_ISLAND; |
138 } | 145 } |
139 if (admin_area_type == "parish") { | 146 if (admin_area_type == "parish") { |
140 return IDS_LIBADDRESSINPUT_I18N_PARISH; | 147 return error ? IDS_LIBADDRESSINPUT_I18N_INVALID_PARISH |
| 148 : IDS_LIBADDRESSINPUT_I18N_PARISH; |
141 } | 149 } |
142 if (admin_area_type == "prefecture") { | 150 if (admin_area_type == "prefecture") { |
143 return IDS_LIBADDRESSINPUT_I18N_PREFECTURE; | 151 return error ? IDS_LIBADDRESSINPUT_I18N_INVALID_PREFECTURE |
| 152 : IDS_LIBADDRESSINPUT_I18N_PREFECTURE; |
144 } | 153 } |
145 if (admin_area_type == "province") { | 154 if (admin_area_type == "province") { |
146 return IDS_LIBADDRESSINPUT_I18N_PROVINCE; | 155 return error ? IDS_LIBADDRESSINPUT_I18N_INVALID_PROVINCE |
| 156 : IDS_LIBADDRESSINPUT_I18N_PROVINCE; |
147 } | 157 } |
148 if (admin_area_type == "state") { | 158 if (admin_area_type == "state") { |
149 return IDS_LIBADDRESSINPUT_I18N_STATE_LABEL; | 159 return error ? IDS_LIBADDRESSINPUT_I18N_INVALID_STATE_LABEL |
| 160 : IDS_LIBADDRESSINPUT_I18N_STATE_LABEL; |
150 } | 161 } |
151 return INVALID_MESSAGE_ID; | 162 return INVALID_MESSAGE_ID; |
152 } | 163 } |
153 | 164 |
154 int GetPostalCodeMessageId(const std::string& postal_code_type) { | 165 int GetPostalCodeMessageId(const std::string& postal_code_type, bool error) { |
155 if (postal_code_type == "postal") { | 166 if (postal_code_type == "postal") { |
156 return IDS_LIBADDRESSINPUT_I18N_POSTAL_CODE_LABEL; | 167 return error ? IDS_LIBADDRESSINPUT_I18N_INVALID_POSTAL_CODE_LABEL |
| 168 : IDS_LIBADDRESSINPUT_I18N_POSTAL_CODE_LABEL; |
157 } | 169 } |
158 if (postal_code_type == "zip") { | 170 if (postal_code_type == "zip") { |
159 return IDS_LIBADDRESSINPUT_I18N_ZIP_CODE_LABEL; | 171 return error ? IDS_LIBADDRESSINPUT_I18N_INVALID_ZIP_CODE_LABEL |
| 172 : IDS_LIBADDRESSINPUT_I18N_ZIP_CODE_LABEL; |
160 } | 173 } |
161 return INVALID_MESSAGE_ID; | 174 return INVALID_MESSAGE_ID; |
162 } | 175 } |
163 | 176 |
164 } // namespace | 177 } // namespace |
165 | 178 |
166 Rule::Rule() | 179 Rule::Rule() |
167 : format_(), | 180 : format_(), |
168 required_(), | 181 required_(), |
169 sub_keys_(), | 182 sub_keys_(), |
(...skipping 16 matching lines...) Expand all Loading... |
186 } | 199 } |
187 | 200 |
188 void Rule::CopyFrom(const Rule& rule) { | 201 void Rule::CopyFrom(const Rule& rule) { |
189 format_ = rule.format_; | 202 format_ = rule.format_; |
190 required_ = rule.required_; | 203 required_ = rule.required_; |
191 sub_keys_ = rule.sub_keys_; | 204 sub_keys_ = rule.sub_keys_; |
192 languages_ = rule.languages_; | 205 languages_ = rule.languages_; |
193 language_ = rule.language_; | 206 language_ = rule.language_; |
194 postal_code_format_ = rule.postal_code_format_; | 207 postal_code_format_ = rule.postal_code_format_; |
195 admin_area_name_message_id_ = rule.admin_area_name_message_id_; | 208 admin_area_name_message_id_ = rule.admin_area_name_message_id_; |
| 209 invalid_admin_area_message_id_ = rule.invalid_admin_area_message_id_; |
196 postal_code_name_message_id_ = rule.postal_code_name_message_id_; | 210 postal_code_name_message_id_ = rule.postal_code_name_message_id_; |
| 211 invalid_postal_code_message_id_ = rule.invalid_postal_code_message_id_; |
197 } | 212 } |
198 | 213 |
199 bool Rule::ParseSerializedRule(const std::string& serialized_rule) { | 214 bool Rule::ParseSerializedRule(const std::string& serialized_rule) { |
200 scoped_ptr<Json> json(Json::Build()); | 215 scoped_ptr<Json> json(Json::Build()); |
201 if (!json->ParseObject(serialized_rule)) { | 216 if (!json->ParseObject(serialized_rule)) { |
202 return false; | 217 return false; |
203 } | 218 } |
204 | 219 |
205 std::string value; | 220 std::string value; |
206 if (json->GetStringValueForKey("fmt", &value)) { | 221 if (json->GetStringValueForKey("fmt", &value)) { |
(...skipping 17 matching lines...) Expand all Loading... |
224 | 239 |
225 if (json->GetStringValueForKey("lang", &value)) { | 240 if (json->GetStringValueForKey("lang", &value)) { |
226 language_ = value; | 241 language_ = value; |
227 } | 242 } |
228 | 243 |
229 if (json->GetStringValueForKey("zip", &value)) { | 244 if (json->GetStringValueForKey("zip", &value)) { |
230 postal_code_format_ = value; | 245 postal_code_format_ = value; |
231 } | 246 } |
232 | 247 |
233 if (json->GetStringValueForKey("state_name_type", &value)) { | 248 if (json->GetStringValueForKey("state_name_type", &value)) { |
234 admin_area_name_message_id_ = GetAdminAreaMessageId(value); | 249 admin_area_name_message_id_ = GetAdminAreaMessageId(value, false); |
| 250 invalid_admin_area_message_id_ = GetAdminAreaMessageId(value, true); |
235 } | 251 } |
236 | 252 |
237 if (json->GetStringValueForKey("zip_name_type", &value)) { | 253 if (json->GetStringValueForKey("zip_name_type", &value)) { |
238 postal_code_name_message_id_ = GetPostalCodeMessageId(value); | 254 postal_code_name_message_id_ = GetPostalCodeMessageId(value, false); |
| 255 invalid_postal_code_message_id_ = GetPostalCodeMessageId(value, true); |
239 } | 256 } |
240 | 257 |
241 return true; | 258 return true; |
242 } | 259 } |
243 | 260 |
| 261 int Rule::GetInvalidFieldMessageId(AddressField field) const { |
| 262 switch (field) { |
| 263 case ADMIN_AREA: |
| 264 return invalid_postal_code_message_id_; |
| 265 case LOCALITY: |
| 266 return IDS_LIBADDRESSINPUT_I18N_INVALID_LOCALITY_LABEL; |
| 267 case DEPENDENT_LOCALITY: |
| 268 return IDS_LIBADDRESSINPUT_I18N_INVALID_DEPENDENT_LOCALITY_LABEL; |
| 269 case POSTAL_CODE: |
| 270 return invalid_postal_code_message_id_; |
| 271 default: |
| 272 return IDS_LIBADDRESSINPUT_I18N_INVALID_ENTRY; |
| 273 } |
| 274 } |
| 275 |
244 } // namespace addressinput | 276 } // namespace addressinput |
245 } // namespace i18n | 277 } // namespace i18n |
OLD | NEW |