Home | History | Annotate | Download | only in src
      1 // Copyright (C) 2013 Google Inc.
      2 //
      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
      6 //
      7 // http://www.apache.org/licenses/LICENSE-2.0
      8 //
      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.
     14 
     15 #include "rule.h"
     16 
     17 #include <libaddressinput/address_field.h>
     18 #include <libaddressinput/util/scoped_ptr.h>
     19 
     20 #include <map>
     21 #include <string>
     22 #include <utility>
     23 
     24 #include "address_field_util.h"
     25 #include "grit.h"
     26 #include "messages.h"
     27 #include "util/json.h"
     28 #include "util/string_split.h"
     29 
     30 namespace i18n {
     31 namespace addressinput {
     32 
     33 namespace {
     34 
     35 typedef std::map<std::string, int> NameMessageIdMap;
     36 
     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";
     43 
     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 = '~';
     47 
     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));
     72   return message_ids;
     73 }
     74 
     75 const NameMessageIdMap& GetAdminAreaMessageIds() {
     76   static const NameMessageIdMap kAdminAreaMessageIds(InitAdminAreaMessageIds());
     77   return kAdminAreaMessageIds;
     78 }
     79 
     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));
     86   return message_ids;
     87 }
     88 
     89 const NameMessageIdMap& GetPostalCodeMessageIds() {
     90   static const NameMessageIdMap kPostalCodeMessageIds(
     91       InitPostalCodeMessageIds());
     92   return kPostalCodeMessageIds;
     93 }
     94 
     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;
     99 }
    100 
    101 }  // namespace
    102 
    103 Rule::Rule()
    104     : format_(),
    105       sub_keys_(),
    106       languages_(),
    107       language_(),
    108       admin_area_name_message_id_(INVALID_MESSAGE_ID),
    109       postal_code_name_message_id_(INVALID_MESSAGE_ID) {}
    110 
    111 Rule::~Rule() {}
    112 
    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_;
    120 }
    121 
    122 bool Rule::ParseSerializedRule(const std::string& serialized_rule) {
    123   scoped_ptr<Json> json(Json::Build());
    124   if (!json->ParseObject(serialized_rule)) {
    125     return false;
    126   }
    127 
    128   if (json->HasStringValueForKey(kFormatKey)) {
    129     ParseAddressFieldsFormat(json->GetStringValueForKey(kFormatKey), &format_);
    130   }
    131 
    132   if (json->HasStringValueForKey(kSubKeysKey)) {
    133     SplitString(
    134         json->GetStringValueForKey(kSubKeysKey), kSeparator, &sub_keys_);
    135   }
    136 
    137   if (json->HasStringValueForKey(kLanguagesKey)) {
    138     SplitString(
    139         json->GetStringValueForKey(kLanguagesKey), kSeparator, &languages_);
    140   }
    141 
    142   if (json->HasStringValueForKey(kLanguageKey)) {
    143     language_ = json->GetStringValueForKey(kLanguageKey);
    144   }
    145 
    146   if (json->HasStringValueForKey(kAdminAreaNameTypeKey)) {
    147     admin_area_name_message_id_ =
    148         GetMessageIdFromName(json->GetStringValueForKey(kAdminAreaNameTypeKey),
    149                              GetAdminAreaMessageIds());
    150   }
    151 
    152   if (json->HasStringValueForKey(kPostalCodeNameTypeKey)) {
    153     postal_code_name_message_id_ =
    154         GetMessageIdFromName(json->GetStringValueForKey(kPostalCodeNameTypeKey),
    155                              GetPostalCodeMessageIds());
    156   }
    157 
    158   return true;
    159 }
    160 
    161 }  // namespace addressinput
    162 }  // namespace i18n
    163