1 // Copyright (C) 2013 Google Inc.
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <libaddressinput/address_field.h>
18 #include <libaddressinput/util/scoped_ptr.h>
24 #include "address_field_util.h"
27 #include "util/json.h"
28 #include "util/string_split.h"
31 namespace addressinput
{
35 typedef std::map
<std::string
, int> NameMessageIdMap
;
37 const char kAdminAreaNameTypeKey
[] = "state_name_type";
38 const char kFormatKey
[] = "fmt";
39 const char kLanguageKey
[] = "lang";
40 const char kLanguagesKey
[] = "languages";
41 const char kPostalCodeNameTypeKey
[] = "zip_name_type";
42 const char kSubKeysKey
[] = "sub_keys";
44 // Used as a separator in a list of items. For example, the list of supported
45 // languages can be "de~fr~it".
46 const char kSeparator
= '~';
48 NameMessageIdMap
InitAdminAreaMessageIds() {
49 NameMessageIdMap message_ids
;
50 message_ids
.insert(std::make_pair(
51 "area", IDS_LIBADDRESSINPUT_I18N_AREA
));
52 message_ids
.insert(std::make_pair(
53 "county", IDS_LIBADDRESSINPUT_I18N_COUNTY_LABEL
));
54 message_ids
.insert(std::make_pair(
55 "department", IDS_LIBADDRESSINPUT_I18N_DEPARTMENT
));
56 message_ids
.insert(std::make_pair(
57 "district", IDS_LIBADDRESSINPUT_I18N_DEPENDENT_LOCALITY_LABEL
));
58 message_ids
.insert(std::make_pair(
59 "do_si", IDS_LIBADDRESSINPUT_I18N_DO_SI
));
60 message_ids
.insert(std::make_pair(
61 "emirate", IDS_LIBADDRESSINPUT_I18N_EMIRATE
));
62 message_ids
.insert(std::make_pair(
63 "island", IDS_LIBADDRESSINPUT_I18N_ISLAND
));
64 message_ids
.insert(std::make_pair(
65 "parish", IDS_LIBADDRESSINPUT_I18N_PARISH
));
66 message_ids
.insert(std::make_pair(
67 "prefecture", IDS_LIBADDRESSINPUT_I18N_PREFECTURE
));
68 message_ids
.insert(std::make_pair(
69 "province", IDS_LIBADDRESSINPUT_I18N_PROVINCE
));
70 message_ids
.insert(std::make_pair(
71 "state", IDS_LIBADDRESSINPUT_I18N_STATE_LABEL
));
75 const NameMessageIdMap
& GetAdminAreaMessageIds() {
76 static const NameMessageIdMap
kAdminAreaMessageIds(InitAdminAreaMessageIds());
77 return kAdminAreaMessageIds
;
80 NameMessageIdMap
InitPostalCodeMessageIds() {
81 NameMessageIdMap message_ids
;
82 message_ids
.insert(std::make_pair(
83 "postal", IDS_LIBADDRESSINPUT_I18N_POSTAL_CODE_LABEL
));
84 message_ids
.insert(std::make_pair(
85 "zip", IDS_LIBADDRESSINPUT_I18N_ZIP_CODE_LABEL
));
89 const NameMessageIdMap
& GetPostalCodeMessageIds() {
90 static const NameMessageIdMap
kPostalCodeMessageIds(
91 InitPostalCodeMessageIds());
92 return kPostalCodeMessageIds
;
95 int GetMessageIdFromName(const std::string
& name
,
96 const NameMessageIdMap
& message_ids
) {
97 NameMessageIdMap::const_iterator it
= message_ids
.find(name
);
98 return it
!= message_ids
.end() ? it
->second
: INVALID_MESSAGE_ID
;
108 admin_area_name_message_id_(INVALID_MESSAGE_ID
),
109 postal_code_name_message_id_(INVALID_MESSAGE_ID
) {}
113 void Rule::CopyFrom(const Rule
& rule
) {
114 format_
= rule
.format_
;
115 sub_keys_
= rule
.sub_keys_
;
116 languages_
= rule
.languages_
;
117 language_
= rule
.language_
;
118 admin_area_name_message_id_
= rule
.admin_area_name_message_id_
;
119 postal_code_name_message_id_
= rule
.postal_code_name_message_id_
;
122 bool Rule::ParseSerializedRule(const std::string
& serialized_rule
) {
123 scoped_ptr
<Json
> json(Json::Build());
124 if (!json
->ParseObject(serialized_rule
)) {
128 if (json
->HasStringValueForKey(kFormatKey
)) {
129 ParseAddressFieldsFormat(json
->GetStringValueForKey(kFormatKey
), &format_
);
132 if (json
->HasStringValueForKey(kSubKeysKey
)) {
134 json
->GetStringValueForKey(kSubKeysKey
), kSeparator
, &sub_keys_
);
137 if (json
->HasStringValueForKey(kLanguagesKey
)) {
139 json
->GetStringValueForKey(kLanguagesKey
), kSeparator
, &languages_
);
142 if (json
->HasStringValueForKey(kLanguageKey
)) {
143 language_
= json
->GetStringValueForKey(kLanguageKey
);
146 if (json
->HasStringValueForKey(kAdminAreaNameTypeKey
)) {
147 admin_area_name_message_id_
=
148 GetMessageIdFromName(json
->GetStringValueForKey(kAdminAreaNameTypeKey
),
149 GetAdminAreaMessageIds());
152 if (json
->HasStringValueForKey(kPostalCodeNameTypeKey
)) {
153 postal_code_name_message_id_
=
154 GetMessageIdFromName(json
->GetStringValueForKey(kPostalCodeNameTypeKey
),
155 GetPostalCodeMessageIds());
161 } // namespace addressinput