| 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, |
| 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 <map> | 20 #include <map> |
| 21 #include <string> | 21 #include <string> |
| 22 #include <utility> | 22 #include <utility> |
| 23 | 23 |
| 24 #include "address_field_util.h" | 24 #include "address_field_util.h" |
| 25 #include "grit.h" | 25 #include "grit.h" |
| 26 #include "messages.h" | 26 #include "messages.h" |
| 27 #include "region_data_constants.h" |
| 27 #include "util/json.h" | 28 #include "util/json.h" |
| 28 #include "util/string_split.h" | 29 #include "util/string_split.h" |
| 29 | 30 |
| 30 namespace i18n { | 31 namespace i18n { |
| 31 namespace addressinput { | 32 namespace addressinput { |
| 32 | 33 |
| 33 namespace { | 34 namespace { |
| 34 | 35 |
| 35 typedef std::map<std::string, int> NameMessageIdMap; | 36 typedef std::map<std::string, int> NameMessageIdMap; |
| 36 | 37 |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 91 InitPostalCodeMessageIds()); | 92 InitPostalCodeMessageIds()); |
| 92 return kPostalCodeMessageIds; | 93 return kPostalCodeMessageIds; |
| 93 } | 94 } |
| 94 | 95 |
| 95 int GetMessageIdFromName(const std::string& name, | 96 int GetMessageIdFromName(const std::string& name, |
| 96 const NameMessageIdMap& message_ids) { | 97 const NameMessageIdMap& message_ids) { |
| 97 NameMessageIdMap::const_iterator it = message_ids.find(name); | 98 NameMessageIdMap::const_iterator it = message_ids.find(name); |
| 98 return it != message_ids.end() ? it->second : INVALID_MESSAGE_ID; | 99 return it != message_ids.end() ? it->second : INVALID_MESSAGE_ID; |
| 99 } | 100 } |
| 100 | 101 |
| 102 // Parses the default region data into the static Rule object and returns a |
| 103 // constant reference to this object. Cannot return a copy of the object, |
| 104 // because Rule objects are not copyable. |
| 105 const Rule& InitDefaultRule() { |
| 106 static Rule rule; |
| 107 rule.ParseSerializedRule(RegionDataConstants::GetDefaultRegionData()); |
| 108 return rule; |
| 109 } |
| 110 |
| 101 } // namespace | 111 } // namespace |
| 102 | 112 |
| 103 Rule::Rule() | 113 Rule::Rule() |
| 104 : format_(), | 114 : format_(), |
| 105 sub_keys_(), | 115 sub_keys_(), |
| 106 languages_(), | 116 languages_(), |
| 107 language_(), | 117 language_(), |
| 108 admin_area_name_message_id_(INVALID_MESSAGE_ID), | 118 admin_area_name_message_id_(INVALID_MESSAGE_ID), |
| 109 postal_code_name_message_id_(INVALID_MESSAGE_ID) {} | 119 postal_code_name_message_id_(INVALID_MESSAGE_ID) {} |
| 110 | 120 |
| 111 Rule::~Rule() {} | 121 Rule::~Rule() {} |
| 112 | 122 |
| 123 // static |
| 124 const Rule& Rule::GetDefault() { |
| 125 // Returns the constant reference to the Rule object from InitDefaultRule(). |
| 126 // The static object is in InitDefaultRule(), but this function maintains a |
| 127 // constant static reference to it. The constant static reference avoids |
| 128 // re-parsing the default region data. |
| 129 static const Rule& kDefaultRule(InitDefaultRule()); |
| 130 return kDefaultRule; |
| 131 } |
| 132 |
| 113 void Rule::CopyFrom(const Rule& rule) { | 133 void Rule::CopyFrom(const Rule& rule) { |
| 114 format_ = rule.format_; | 134 format_ = rule.format_; |
| 115 sub_keys_ = rule.sub_keys_; | 135 sub_keys_ = rule.sub_keys_; |
| 116 languages_ = rule.languages_; | 136 languages_ = rule.languages_; |
| 117 language_ = rule.language_; | 137 language_ = rule.language_; |
| 118 admin_area_name_message_id_ = rule.admin_area_name_message_id_; | 138 admin_area_name_message_id_ = rule.admin_area_name_message_id_; |
| 119 postal_code_name_message_id_ = rule.postal_code_name_message_id_; | 139 postal_code_name_message_id_ = rule.postal_code_name_message_id_; |
| 120 } | 140 } |
| 121 | 141 |
| 122 bool Rule::ParseSerializedRule(const std::string& serialized_rule) { | 142 bool Rule::ParseSerializedRule(const std::string& serialized_rule) { |
| (...skipping 30 matching lines...) Expand all Loading... |
| 153 postal_code_name_message_id_ = | 173 postal_code_name_message_id_ = |
| 154 GetMessageIdFromName(json->GetStringValueForKey(kPostalCodeNameTypeKey), | 174 GetMessageIdFromName(json->GetStringValueForKey(kPostalCodeNameTypeKey), |
| 155 GetPostalCodeMessageIds()); | 175 GetPostalCodeMessageIds()); |
| 156 } | 176 } |
| 157 | 177 |
| 158 return true; | 178 return true; |
| 159 } | 179 } |
| 160 | 180 |
| 161 } // namespace addressinput | 181 } // namespace addressinput |
| 162 } // namespace i18n | 182 } // namespace i18n |
| OLD | NEW |