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 // An object to store address metadata, describing the addressing rules for
     16 // regions and sub-regions. The address metadata format is documented here:
     17 //
     18 // https://code.google.com/p/libaddressinput/wiki/AddressValidationMetadata
     19 
     20 #ifndef I18N_ADDRESSINPUT_RULE_H_
     21 #define I18N_ADDRESSINPUT_RULE_H_
     22 
     23 #include <libaddressinput/address_field.h>
     24 #include <libaddressinput/util/basictypes.h>
     25 #include <libaddressinput/util/scoped_ptr.h>
     26 
     27 #include <string>
     28 #include <vector>
     29 
     30 #include "format_element.h"
     31 
     32 namespace i18n {
     33 namespace addressinput {
     34 
     35 class Json;
     36 class RE2ptr;
     37 
     38 // Stores address metadata addressing rules, to be used for determining the
     39 // layout of an address input widget or for address validation. Sample usage:
     40 //    Rule rule;
     41 //    if (rule.ParseSerializedRule("{\"fmt\": \"%A%n%C%S %Z\"}")) {
     42 //      Process(rule.GetFormat());
     43 //    }
     44 class Rule {
     45  public:
     46   Rule();
     47   ~Rule();
     48 
     49   // Returns the default rule at a country level. If a country does not specify
     50   // address format, for example, then the format from this rule should be used
     51   // instead.
     52   static const Rule& GetDefault();
     53 
     54   // Copies all data from |rule|.
     55   void CopyFrom(const Rule& rule);
     56 
     57   // Parses |serialized_rule|. Returns |true| if the |serialized_rule| has valid
     58   // format (JSON dictionary).
     59   bool ParseSerializedRule(const std::string& serialized_rule);
     60 
     61   // Reads data from |json|, which must already have parsed a serialized rule.
     62   void ParseJsonRule(const Json& json);
     63 
     64   // Returns the ID string for this rule.
     65   const std::string& GetId() const { return id_; }
     66 
     67   // Returns the format elements for this rule. The format can include the
     68   // relevant address fields, but also strings used for formatting, or newline
     69   // information.
     70   const std::vector<FormatElement>& GetFormat() const { return format_; }
     71 
     72   // Returns the approximate address format with the Latin order of fields. The
     73   // format can include the relevant address fields, but also strings used for
     74   // formatting, or newline information.
     75   const std::vector<FormatElement>& GetLatinFormat() const {
     76     return latin_format_;
     77   }
     78 
     79   // Returns the required fields for this rule.
     80   const std::vector<AddressField>& GetRequired() const { return required_; }
     81 
     82   // Returns the sub-keys for this rule, which are the administrative areas of a
     83   // country, the localities of an administrative area, or the dependent
     84   // localities of a locality. For example, the rules for "US" have sub-keys of
     85   // "CA", "NY", "TX", etc.
     86   const std::vector<std::string>& GetSubKeys() const { return sub_keys_; }
     87 
     88   // Returns all of the language tags supported by this rule, for example ["de",
     89   // "fr", "it"].
     90   const std::vector<std::string>& GetLanguages() const { return languages_; }
     91 
     92   // Returns a pointer to a RE2 regular expression object created from the
     93   // postal code format string, if specified, or NULL otherwise. The regular
     94   // expression is anchored to the beginning of the string so that it can be
     95   // used either with RE2::PartialMatch() to perform prefix matching or else
     96   // with RE2::FullMatch() to perform matching against the entire string.
     97   const RE2ptr* GetPostalCodeMatcher() const {
     98     return postal_code_matcher_.get();
     99   }
    100 
    101   // Returns the sole postal code for this rule, if there is one.
    102   const std::string& GetSolePostalCode() const {
    103     return sole_postal_code_;
    104   }
    105 
    106   // The message string identifier for admin area name. If not set, then
    107   // INVALID_MESSAGE_ID.
    108   int GetAdminAreaNameMessageId() const { return admin_area_name_message_id_; }
    109 
    110   // The message string identifier for postal code name. If not set, then
    111   // INVALID_MESSAGE_ID.
    112   int GetPostalCodeNameMessageId() const {
    113     return postal_code_name_message_id_;
    114   }
    115 
    116   // Returns the name for the most specific place described by this rule, if
    117   // there is one. This is typically set when it differs from the key.
    118   const std::string& GetName() const { return name_; }
    119 
    120   // Returns the Latin-script name for the most specific place described by this
    121   // rule, if there is one.
    122   const std::string& GetLatinName() const { return latin_name_; }
    123 
    124   // Returns the postal code example string for this rule.
    125   const std::string& GetPostalCodeExample() const {
    126     return postal_code_example_;
    127   }
    128 
    129   // Returns the post service URL string for this rule.
    130   const std::string& GetPostServiceUrl() const {
    131     return post_service_url_;
    132   }
    133 
    134  private:
    135   std::string id_;
    136   std::vector<FormatElement> format_;
    137   std::vector<FormatElement> latin_format_;
    138   std::vector<AddressField> required_;
    139   std::vector<std::string> sub_keys_;
    140   std::vector<std::string> languages_;
    141   scoped_ptr<const RE2ptr> postal_code_matcher_;
    142   std::string sole_postal_code_;
    143   int admin_area_name_message_id_;
    144   int postal_code_name_message_id_;
    145   std::string name_;
    146   std::string latin_name_;
    147   std::string postal_code_example_;
    148   std::string post_service_url_;
    149 
    150   DISALLOW_COPY_AND_ASSIGN(Rule);
    151 };
    152 
    153 }  // namespace addressinput
    154 }  // namespace i18n
    155 
    156 #endif  // I18N_ADDRESSINPUT_RULE_H_
    157