Remove PlatformFile from profile_browsertest
[chromium-blink-merge.git] / third_party / libaddressinput / chromium / cpp / src / rule.h
blobcc0a3d7a3b34596505bfa1213d872493a4fc020c
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.
15 // An object to store validation rules.
17 #ifndef I18N_ADDRESSINPUT_RULE_H_
18 #define I18N_ADDRESSINPUT_RULE_H_
20 #include <libaddressinput/address_field.h>
21 #include <libaddressinput/util/basictypes.h>
23 #include <string>
24 #include <vector>
26 namespace i18n {
27 namespace addressinput {
29 class Json;
31 // Stores an element in the format of an address as it should be displayed on an
32 // envelope. The element can be either a literal string, like " ", or a field,
33 // like ADMIN_AREA.
34 struct FormatElement {
35 // Builds an element of address format for |field|.
36 explicit FormatElement(AddressField field);
38 // Builds an element of address format for |literal|. The literal should not
39 // be empty.
40 explicit FormatElement(const std::string& literal);
42 ~FormatElement();
44 // Returns true if this is a field element.
45 bool IsField() const { return literal.empty(); }
47 bool operator==(const FormatElement& other) const;
49 // The field for this element in address format. Should be used only if
50 // |literal| is an empty string.
51 AddressField field;
53 // The literal string for this element in address format. If empty, then this
54 // is a field element.
55 std::string literal;
58 // Stores the validation, input, and display rules for an address. Sample usage:
59 // Rule rule;
60 // if (rule.ParseSerializedRule("{\"fmt\": \"%A%n%C%S %Z\"}")) {
61 // Process(rule.GetFormat());
62 // }
63 class Rule {
64 public:
65 // The types of fields that describe the rule.
66 enum IdentityField {
67 KEY,
68 NAME,
69 LATIN_NAME,
70 IDENTITY_FIELDS_SIZE
73 Rule();
74 ~Rule();
76 // Returns the default rule at a country level. If a country does not specify
77 // address format, for example, then the format from this rule should be used
78 // instead.
79 static const Rule& GetDefault();
81 // Copies all data from |rule|.
82 void CopyFrom(const Rule& rule);
84 // Parses |serialized_rule|. Returns |true| if the |serialized_rule| has valid
85 // format (JSON dictionary).
86 bool ParseSerializedRule(const std::string& serialized_rule);
88 // Parses |json_rule|, which must contain parsed serialized rule.
89 void ParseJsonRule(const Json& json_rule);
91 // Returns the value of the |identity_field| for this rule, for example, can
92 // return "TX" or "Texas". The |identity_field| parameter should not be
93 // IDENTITY_FIELDS_SIZE.
94 const std::string& GetIdentityField(IdentityField identity_field) const;
96 // Returns the key for this rule. For example, can return "TX".
97 const std::string& GetKey() const { return key_; }
99 // Returns the name for this rule. For example, the name for "TX" is "Texas".
100 const std::string& GetName() const { return name_; }
102 // Returns the latinized version of the name. For example, the latinized
103 // version of "北京市" is "Beijing Shi".
104 const std::string& GetLatinName() const { return latin_name_; }
106 // Returns the format of the address as it should appear on an envelope.
107 const std::vector<std::vector<FormatElement> >& GetFormat() const {
108 return format_;
111 // Returns the latinized format of the address as it should appear on an
112 // envelope.
113 const std::vector<std::vector<FormatElement> >& GetLatinFormat() const {
114 return latin_format_;
117 // Returns the required fields for this rule.
118 const std::vector<AddressField>& GetRequired() const { return required_; }
120 // Returns the sub-keys for this rule, which are the administrative areas of a
121 // country, the localities of an administrative area, or the dependent
122 // localities of a locality. For example, the rules for "US" have sub-keys of
123 // "CA", "NY", "TX", etc.
124 const std::vector<std::string>& GetSubKeys() const { return sub_keys_; }
126 // Returns all of the language codes for which this rule has custom rules, for
127 // example ["de", "fr", "it"].
128 const std::vector<std::string>& GetLanguages() const { return languages_; }
130 // Returns all of the languages codes for addresses that adhere to this rule,
131 // for example ["de", "fr", "gsw", "it"].
132 const std::vector<std::string>& GetInputLanguages() const {
133 return input_languages_;
136 // Returns the language code of this rule, for example "de".
137 const std::string& GetLanguage() const { return language_; }
139 // Returns the postal code format, for example "\\d{5}([ \\-]\\d{4})?".
140 const std::string& GetPostalCodeFormat() const { return postal_code_format_; }
142 // The message string identifier for admin area name. If not set, then
143 // INVALID_MESSAGE_ID.
144 int GetAdminAreaNameMessageId() const { return admin_area_name_message_id_; }
146 // The error message string identifier for an invalid admin area. If not set,
147 // then INVALID_MESSAGE_ID.
148 int GetInvalidAdminAreaMessageId() const {
149 return invalid_admin_area_message_id_;
152 // The message string identifier for postal code name. If not set, then
153 // INVALID_MESSAGE_ID.
154 int GetPostalCodeNameMessageId() const {
155 return postal_code_name_message_id_;
158 // The error message string identifier for an invalid postal code. If not set,
159 // then INVALID_MESSAGE_ID.
160 int GetInvalidPostalCodeMessageId() const {
161 return invalid_postal_code_message_id_;
164 // Returns the error message string identifier for an invalid |field|.
165 int GetInvalidFieldMessageId(AddressField field) const;
167 // Outputs the sub key for a given user input. For example, Texas will map to
168 // TX.
169 bool CanonicalizeSubKey(const std::string& user_input,
170 std::string* sub_key) const;
172 private:
173 // Finds |target| in |values| and sets |sub_key| to the associated value from
174 // |sub_keys_|, or returns false if |target| is not in |values|.
175 bool GetMatchingSubKey(const std::string& target,
176 const std::vector<std::string>& values,
177 std::string* sub_key) const;
179 std::string key_;
180 std::string name_;
181 std::string latin_name_;
182 std::vector<std::vector<FormatElement> > format_;
183 std::vector<std::vector<FormatElement> > latin_format_;
184 std::vector<AddressField> required_;
185 std::vector<std::string> sub_keys_;
186 std::vector<std::string> sub_names_;
187 // The Latin names (when |sub_names_| is not in Latin characters).
188 std::vector<std::string> sub_lnames_;
189 std::vector<std::string> languages_;
190 std::vector<std::string> input_languages_;
191 std::string language_;
192 std::string postal_code_format_;
193 int admin_area_name_message_id_;
194 int invalid_admin_area_message_id_;
195 int postal_code_name_message_id_;
196 int invalid_postal_code_message_id_;
198 DISALLOW_COPY_AND_ASSIGN(Rule);
201 } // namespace addressinput
202 } // namespace i18n
204 #endif // I18N_ADDRESSINPUT_RULE_H_