aboutsummaryrefslogtreecommitdiff
path: root/cpp/src/rule.h
blob: 1286bcb5d2264b2d1dd3f835da354b9e4716c760 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Copyright (C) 2013 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// An object to store address metadata, describing the addressing rules for
// regions and sub-regions. The address metadata format is documented here:
//
// https://code.google.com/p/libaddressinput/wiki/AddressValidationMetadata

#ifndef I18N_ADDRESSINPUT_RULE_H_
#define I18N_ADDRESSINPUT_RULE_H_

#include <libaddressinput/address_field.h>
#include <libaddressinput/util/basictypes.h>
#include <libaddressinput/util/scoped_ptr.h>

#include <string>
#include <vector>

namespace i18n {
namespace addressinput {

class FormatElement;
class Json;
struct RE2ptr;

// Stores address metadata addressing rules, to be used for determining the
// layout of an address input widget or for address validation. Sample usage:
//    Rule rule;
//    if (rule.ParseSerializedRule("{\"fmt\": \"%A%n%C%S %Z\"}")) {
//      Process(rule.GetFormat());
//    }
class Rule {
 public:
  Rule();
  ~Rule();

  // Returns the default rule at a country level. If a country does not specify
  // address format, for example, then the format from this rule should be used
  // instead.
  static const Rule& GetDefault();

  // Copies all data from |rule|.
  void CopyFrom(const Rule& rule);

  // Parses |serialized_rule|. Returns |true| if the |serialized_rule| has valid
  // format (JSON dictionary).
  bool ParseSerializedRule(const std::string& serialized_rule);

  // Reads data from |json|, which must already have parsed a serialized rule.
  void ParseJsonRule(const Json& json);

  // Returns the ID string for this rule.
  const std::string& GetId() const { return id_; }

  // Returns the format elements for this rule. The format can include the
  // relevant address fields, but also strings used for formatting, or newline
  // information.
  const std::vector<FormatElement>& GetFormat() const { return format_; }

  // Returns the approximate address format with the Latin order of fields. The
  // format can include the relevant address fields, but also strings used for
  // formatting, or newline information.
  const std::vector<FormatElement>& GetLatinFormat() const {
    return latin_format_;
  }

  // Returns the required fields for this rule.
  const std::vector<AddressField>& GetRequired() const { return required_; }

  // Returns the sub-keys for this rule, which are the administrative areas of a
  // country, the localities of an administrative area, or the dependent
  // localities of a locality. For example, the rules for "US" have sub-keys of
  // "CA", "NY", "TX", etc.
  const std::vector<std::string>& GetSubKeys() const { return sub_keys_; }

  // Returns all of the language tags supported by this rule, for example ["de",
  // "fr", "it"].
  const std::vector<std::string>& GetLanguages() const { return languages_; }

  // Returns a pointer to a RE2 regular expression object created from the
  // postal code format string, if specified, or NULL otherwise. The regular
  // expression is anchored to the beginning of the string so that it can be
  // used either with RE2::PartialMatch() to perform prefix matching or else
  // with RE2::FullMatch() to perform matching against the entire string.
  const RE2ptr* GetPostalCodeMatcher() const {
    return postal_code_matcher_.get();
  }

  // Returns the sole postal code for this rule, if there is one.
  const std::string& GetSolePostalCode() const { return sole_postal_code_; }

  // The message string identifier for admin area name. If not set, then
  // INVALID_MESSAGE_ID.
  int GetAdminAreaNameMessageId() const { return admin_area_name_message_id_; }

  // The message string identifier for postal code name. If not set, then
  // INVALID_MESSAGE_ID.
  int GetPostalCodeNameMessageId() const {
    return postal_code_name_message_id_;
  }

  // Returns the name for the most specific place described by this rule, if
  // there is one. This is typically set when it differs from the key.
  const std::string& GetName() const { return name_; }

  // Returns the Latin-script name for the most specific place described by this
  // rule, if there is one.
  const std::string& GetLatinName() const { return latin_name_; }

  // Returns the postal code example string for this rule.
  const std::string& GetPostalCodeExample() const {
    return postal_code_example_;
  }

  // Returns the post service URL string for this rule.
  const std::string& GetPostServiceUrl() const { return post_service_url_; }

 private:
  std::string id_;
  std::vector<FormatElement> format_;
  std::vector<FormatElement> latin_format_;
  std::vector<AddressField> required_;
  std::vector<std::string> sub_keys_;
  std::vector<std::string> languages_;
  scoped_ptr<const RE2ptr> postal_code_matcher_;
  std::string sole_postal_code_;
  int admin_area_name_message_id_;
  int postal_code_name_message_id_;
  std::string name_;
  std::string latin_name_;
  std::string postal_code_example_;
  std::string post_service_url_;

  DISALLOW_COPY_AND_ASSIGN(Rule);
};

}  // namespace addressinput
}  // namespace i18n

#endif  // I18N_ADDRESSINPUT_RULE_H_