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

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

Issue 106763007: [rac] Parse postal code formats and required fields in libaddressinput. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Move AddressField parsing into Rule. Add more tests for parsing AddressFields. Created 7 years 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,
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>
21 #include <cstddef>
20 #include <map> 22 #include <map>
21 #include <string> 23 #include <string>
22 #include <utility> 24 #include <utility>
25 #include <vector>
23 26
24 #include "address_field_util.h"
25 #include "grit.h" 27 #include "grit.h"
26 #include "messages.h" 28 #include "messages.h"
27 #include "util/json.h" 29 #include "util/json.h"
28 #include "util/string_split.h" 30 #include "util/string_split.h"
29 31
30 namespace i18n { 32 namespace i18n {
31 namespace addressinput { 33 namespace addressinput {
32 34
33 namespace { 35 namespace {
34 36
35 typedef std::map<std::string, int> NameMessageIdMap; 37 typedef std::map<std::string, int> NameMessageIdMap;
36 38
37 const char kAdminAreaNameTypeKey[] = "state_name_type"; 39 const char kAdminAreaNameTypeKey[] = "state_name_type";
38 const char kFormatKey[] = "fmt"; 40 const char kFormatKey[] = "fmt";
39 const char kLanguageKey[] = "lang"; 41 const char kLanguageKey[] = "lang";
40 const char kLanguagesKey[] = "languages"; 42 const char kLanguagesKey[] = "languages";
43 const char kPostalCodeFormatKey[] = "zip";
41 const char kPostalCodeNameTypeKey[] = "zip_name_type"; 44 const char kPostalCodeNameTypeKey[] = "zip_name_type";
45 const char kRequiredKey[] = "require";
42 const char kSubKeysKey[] = "sub_keys"; 46 const char kSubKeysKey[] = "sub_keys";
43 47
44 // Used as a separator in a list of items. For example, the list of supported 48 // Used as a separator in a list of items. For example, the list of supported
45 // languages can be "de~fr~it". 49 // languages can be "de~fr~it".
46 const char kSeparator = '~'; 50 const char kSeparator = '~';
47 51
48 NameMessageIdMap InitAdminAreaMessageIds() { 52 NameMessageIdMap InitAdminAreaMessageIds() {
49 NameMessageIdMap message_ids; 53 NameMessageIdMap message_ids;
50 message_ids.insert(std::make_pair( 54 message_ids.insert(std::make_pair(
51 "area", IDS_LIBADDRESSINPUT_I18N_AREA)); 55 "area", IDS_LIBADDRESSINPUT_I18N_AREA));
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
91 InitPostalCodeMessageIds()); 95 InitPostalCodeMessageIds());
92 return kPostalCodeMessageIds; 96 return kPostalCodeMessageIds;
93 } 97 }
94 98
95 int GetMessageIdFromName(const std::string& name, 99 int GetMessageIdFromName(const std::string& name,
96 const NameMessageIdMap& message_ids) { 100 const NameMessageIdMap& message_ids) {
97 NameMessageIdMap::const_iterator it = message_ids.find(name); 101 NameMessageIdMap::const_iterator it = message_ids.find(name);
98 return it != message_ids.end() ? it->second : INVALID_MESSAGE_ID; 102 return it != message_ids.end() ? it->second : INVALID_MESSAGE_ID;
99 } 103 }
100 104
105 std::map<char, AddressField> InitFields() {
106 std::map<char, AddressField> fields;
107 fields.insert(std::make_pair('R', COUNTRY));
108 fields.insert(std::make_pair('S', ADMIN_AREA));
109 fields.insert(std::make_pair('C', LOCALITY));
110 fields.insert(std::make_pair('D', DEPENDENT_LOCALITY));
111 fields.insert(std::make_pair('x', SORTING_CODE));
112 fields.insert(std::make_pair('Z', POSTAL_CODE));
113 fields.insert(std::make_pair('A', STREET_ADDRESS));
114 fields.insert(std::make_pair('O', ORGANIZATION));
115 fields.insert(std::make_pair('N', RECIPIENT));
116 // An extension of AddressField enum used only internally:
117 fields.insert(std::make_pair(
118 'n', static_cast<AddressField>(NEWLINE)));
119 return fields;
120 }
121
122 const std::map<char, AddressField>& GetFields() {
123 static const std::map<char, AddressField> kFields(InitFields());
124 return kFields;
125 }
126
127 bool IsTokenPrefix(char c) {
128 return c == '%';
129 }
130
131 bool IsToken(char c) {
132 return GetFields().find(c) != GetFields().end();
133 }
134
135 AddressField ParseToken(char c) {
136 std::map<char, AddressField>::const_iterator it = GetFields().find(c);
137 assert(it != GetFields().end());
138 return it->second;
139 }
140
141 // Clears |fields|, parses |format|, and adds the format address fields to
142 // |fields|. The |fields| may also contain NEWLINE elements. For example, parses
143 // "%S%C%n%D%X" into {ADMIN_AREA, LOCALITY, NEWLINE, DEPENDENT_LOCALITY,
144 // SORTING_CODE}.
145 void ParseAddressFieldsFormat(const std::string& format,
146 std::vector<AddressField>* fields) {
147 assert(fields != NULL);
148 fields->clear();
149 for (std::string::const_iterator current = format.begin(),
150 next = format.begin() + 1;
151 current != format.end() && next != format.end();
152 ++current, ++next) {
153 if (IsTokenPrefix(*current) && IsToken(*next)) {
154 fields->push_back(ParseToken(*next));
155 }
156 }
157 }
158
159 // Clears |fields|, parses |required|, and adds the required fields to |fields|.
160 // For example, parses "SCDX" into {ADMIN_AREA, LOCALITY, DEPENDENT_LOCALITY,
161 // SORTING_CODE}.
162 void ParseAddressFieldsRequired(const std::string& required,
163 std::vector<AddressField>* fields) {
164 assert(fields != NULL);
165 fields->clear();
166 for (std::string::const_iterator token = required.begin();
167 token != required.end(); ++token) {
168 if (IsToken(*token)) {
169 fields->push_back(ParseToken(*token));
170 }
171 }
172 }
173
101 } // namespace 174 } // namespace
102 175
103 Rule::Rule() 176 Rule::Rule()
104 : format_(), 177 : format_(),
178 required_(),
105 sub_keys_(), 179 sub_keys_(),
106 languages_(), 180 languages_(),
107 language_(), 181 language_(),
182 postal_code_format_(),
108 admin_area_name_message_id_(INVALID_MESSAGE_ID), 183 admin_area_name_message_id_(INVALID_MESSAGE_ID),
109 postal_code_name_message_id_(INVALID_MESSAGE_ID) {} 184 postal_code_name_message_id_(INVALID_MESSAGE_ID) {}
110 185
111 Rule::~Rule() {} 186 Rule::~Rule() {}
112 187
113 void Rule::CopyFrom(const Rule& rule) { 188 void Rule::CopyFrom(const Rule& rule) {
114 format_ = rule.format_; 189 format_ = rule.format_;
190 required_ = rule.required_;
115 sub_keys_ = rule.sub_keys_; 191 sub_keys_ = rule.sub_keys_;
116 languages_ = rule.languages_; 192 languages_ = rule.languages_;
117 language_ = rule.language_; 193 language_ = rule.language_;
194 postal_code_format_ = rule.postal_code_format_;
118 admin_area_name_message_id_ = rule.admin_area_name_message_id_; 195 admin_area_name_message_id_ = rule.admin_area_name_message_id_;
119 postal_code_name_message_id_ = rule.postal_code_name_message_id_; 196 postal_code_name_message_id_ = rule.postal_code_name_message_id_;
120 } 197 }
121 198
122 bool Rule::ParseSerializedRule(const std::string& serialized_rule) { 199 bool Rule::ParseSerializedRule(const std::string& serialized_rule) {
123 scoped_ptr<Json> json(Json::Build()); 200 scoped_ptr<Json> json(Json::Build());
124 if (!json->ParseObject(serialized_rule)) { 201 if (!json->ParseObject(serialized_rule)) {
125 return false; 202 return false;
126 } 203 }
127 204
128 if (json->HasStringValueForKey(kFormatKey)) { 205 if (json->HasStringValueForKey(kFormatKey)) {
129 ParseAddressFieldsFormat(json->GetStringValueForKey(kFormatKey), &format_); 206 ParseAddressFieldsFormat(json->GetStringValueForKey(kFormatKey), &format_);
130 } 207 }
131 208
209 if (json->HasStringValueForKey(kRequiredKey)) {
210 ParseAddressFieldsRequired(
211 json->GetStringValueForKey(kRequiredKey), &required_);
212 }
213
132 if (json->HasStringValueForKey(kSubKeysKey)) { 214 if (json->HasStringValueForKey(kSubKeysKey)) {
133 SplitString( 215 SplitString(
134 json->GetStringValueForKey(kSubKeysKey), kSeparator, &sub_keys_); 216 json->GetStringValueForKey(kSubKeysKey), kSeparator, &sub_keys_);
135 } 217 }
136 218
137 if (json->HasStringValueForKey(kLanguagesKey)) { 219 if (json->HasStringValueForKey(kLanguagesKey)) {
138 SplitString( 220 SplitString(
139 json->GetStringValueForKey(kLanguagesKey), kSeparator, &languages_); 221 json->GetStringValueForKey(kLanguagesKey), kSeparator, &languages_);
140 } 222 }
141 223
142 if (json->HasStringValueForKey(kLanguageKey)) { 224 if (json->HasStringValueForKey(kLanguageKey)) {
143 language_ = json->GetStringValueForKey(kLanguageKey); 225 language_ = json->GetStringValueForKey(kLanguageKey);
144 } 226 }
145 227
228 if (json->HasStringValueForKey(kPostalCodeFormatKey)) {
229 postal_code_format_ = json->GetStringValueForKey(kPostalCodeFormatKey);
230 }
231
146 if (json->HasStringValueForKey(kAdminAreaNameTypeKey)) { 232 if (json->HasStringValueForKey(kAdminAreaNameTypeKey)) {
147 admin_area_name_message_id_ = 233 admin_area_name_message_id_ =
148 GetMessageIdFromName(json->GetStringValueForKey(kAdminAreaNameTypeKey), 234 GetMessageIdFromName(json->GetStringValueForKey(kAdminAreaNameTypeKey),
149 GetAdminAreaMessageIds()); 235 GetAdminAreaMessageIds());
150 } 236 }
151 237
152 if (json->HasStringValueForKey(kPostalCodeNameTypeKey)) { 238 if (json->HasStringValueForKey(kPostalCodeNameTypeKey)) {
153 postal_code_name_message_id_ = 239 postal_code_name_message_id_ =
154 GetMessageIdFromName(json->GetStringValueForKey(kPostalCodeNameTypeKey), 240 GetMessageIdFromName(json->GetStringValueForKey(kPostalCodeNameTypeKey),
155 GetPostalCodeMessageIds()); 241 GetPostalCodeMessageIds());
156 } 242 }
157 243
158 return true; 244 return true;
159 } 245 }
160 246
161 } // namespace addressinput 247 } // namespace addressinput
162 } // namespace i18n 248 } // namespace i18n
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698