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

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

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

Powered by Google App Engine
This is Rietveld 408576698