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 // An object to store validation rules. | 15 // An object to store validation rules. |
16 | 16 |
17 #ifndef I18N_ADDRESSINPUT_RULE_H_ | 17 #ifndef I18N_ADDRESSINPUT_RULE_H_ |
18 #define I18N_ADDRESSINPUT_RULE_H_ | 18 #define I18N_ADDRESSINPUT_RULE_H_ |
19 | 19 |
20 #include <libaddressinput/address_field.h> | 20 #include <libaddressinput/address_field.h> |
21 #include <libaddressinput/util/basictypes.h> | 21 #include <libaddressinput/util/basictypes.h> |
22 | 22 |
23 #include <string> | 23 #include <string> |
24 #include <vector> | 24 #include <vector> |
25 | 25 |
26 namespace i18n { | 26 namespace i18n { |
27 namespace addressinput { | 27 namespace addressinput { |
28 | 28 |
29 // Stores the validation rules. Sample usage: | 29 // Stores an element in the format of an address as it should be displayed on an |
| 30 // envelope. The element can be either a literal string, like " ", or a field, |
| 31 // like ADMIN_AREA. |
| 32 struct FormatElement { |
| 33 // Builds an element of address format for |field|. |
| 34 explicit FormatElement(AddressField field); |
| 35 |
| 36 // Builds an element of address format for |literal|. The literal should not |
| 37 // be empty. |
| 38 explicit FormatElement(const std::string& literal); |
| 39 |
| 40 ~FormatElement(); |
| 41 |
| 42 // Returns true if this is a field element. |
| 43 bool IsField() const { return literal.empty(); } |
| 44 |
| 45 bool operator==(const FormatElement& other) const; |
| 46 |
| 47 // The field for this element in address format. Should be used only if |
| 48 // |literal| is an empty string. |
| 49 AddressField field; |
| 50 |
| 51 // The literal string for this element in address format. If empty, then this |
| 52 // is a field element. |
| 53 std::string literal; |
| 54 }; |
| 55 |
| 56 // Stores the validation, input, and display rules for an address. Sample usage: |
30 // Rule rule; | 57 // Rule rule; |
31 // if (rule.ParseSerializedRule("{\"fmt\": \"%A%n%C%S %Z\"}")) { | 58 // if (rule.ParseSerializedRule("{\"fmt\": \"%A%n%C%S %Z\"}")) { |
32 // Process(rule.GetFormat()); | 59 // Process(rule.GetFormat()); |
33 // } | 60 // } |
34 class Rule { | 61 class Rule { |
35 public: | 62 public: |
36 Rule(); | 63 Rule(); |
37 ~Rule(); | 64 ~Rule(); |
38 | 65 |
39 // Returns the default rule at a country level. If a country does not specify | 66 // Returns the default rule at a country level. If a country does not specify |
40 // address format, for example, then the format from this rule should be used | 67 // address format, for example, then the format from this rule should be used |
41 // instead. | 68 // instead. |
42 static const Rule& GetDefault(); | 69 static const Rule& GetDefault(); |
43 | 70 |
44 // Copies all data from |rule|. | 71 // Copies all data from |rule|. |
45 void CopyFrom(const Rule& rule); | 72 void CopyFrom(const Rule& rule); |
46 | 73 |
47 // Parses |serialized_rule|. Returns |true| if the |serialized_rule| has valid | 74 // Parses |serialized_rule|. Returns |true| if the |serialized_rule| has valid |
48 // format (JSON dictionary). | 75 // format (JSON dictionary). |
49 bool ParseSerializedRule(const std::string& serialized_rule); | 76 bool ParseSerializedRule(const std::string& serialized_rule); |
50 | 77 |
51 // Returns the address format for this rule. | 78 // Returns the format of the address as it should appear on an envelope. |
52 const std::vector<std::vector<AddressField> >& GetFormat() const { | 79 const std::vector<std::vector<FormatElement> >& GetFormat() const { |
53 return format_; | 80 return format_; |
54 } | 81 } |
55 | 82 |
56 // Returns the required fields for this rule. | 83 // Returns the required fields for this rule. |
57 const std::vector<AddressField>& GetRequired() const { return required_; } | 84 const std::vector<AddressField>& GetRequired() const { return required_; } |
58 | 85 |
59 // Returns the sub-keys for this rule, which are the administrative areas of a | 86 // Returns the sub-keys for this rule, which are the administrative areas of a |
60 // country, the localities of an administrative area, or the dependent | 87 // country, the localities of an administrative area, or the dependent |
61 // localities of a locality. For example, the rules for "US" have sub-keys of | 88 // localities of a locality. For example, the rules for "US" have sub-keys of |
62 // "CA", "NY", "TX", etc. | 89 // "CA", "NY", "TX", etc. |
(...skipping 28 matching lines...) Expand all Loading... |
91 // The error message string identifier for an invalid postal code. If not set, | 118 // The error message string identifier for an invalid postal code. If not set, |
92 // then INVALID_MESSAGE_ID. | 119 // then INVALID_MESSAGE_ID. |
93 int GetInvalidPostalCodeMessageId() const { | 120 int GetInvalidPostalCodeMessageId() const { |
94 return invalid_postal_code_message_id_; | 121 return invalid_postal_code_message_id_; |
95 } | 122 } |
96 | 123 |
97 // Returns the error message string identifier for an invalid |field|. | 124 // Returns the error message string identifier for an invalid |field|. |
98 int GetInvalidFieldMessageId(AddressField field) const; | 125 int GetInvalidFieldMessageId(AddressField field) const; |
99 | 126 |
100 private: | 127 private: |
101 std::vector<std::vector<AddressField> > format_; | 128 std::vector<std::vector<FormatElement> > format_; |
102 std::vector<AddressField> required_; | 129 std::vector<AddressField> required_; |
103 std::vector<std::string> sub_keys_; | 130 std::vector<std::string> sub_keys_; |
104 std::vector<std::string> languages_; | 131 std::vector<std::string> languages_; |
105 std::string language_; | 132 std::string language_; |
106 std::string postal_code_format_; | 133 std::string postal_code_format_; |
107 int admin_area_name_message_id_; | 134 int admin_area_name_message_id_; |
108 int invalid_admin_area_message_id_; | 135 int invalid_admin_area_message_id_; |
109 int postal_code_name_message_id_; | 136 int postal_code_name_message_id_; |
110 int invalid_postal_code_message_id_; | 137 int invalid_postal_code_message_id_; |
111 | 138 |
112 DISALLOW_COPY_AND_ASSIGN(Rule); | 139 DISALLOW_COPY_AND_ASSIGN(Rule); |
113 }; | 140 }; |
114 | 141 |
115 } // namespace addressinput | 142 } // namespace addressinput |
116 } // namespace i18n | 143 } // namespace i18n |
117 | 144 |
118 #endif // I18N_ADDRESSINPUT_RULE_H_ | 145 #endif // I18N_ADDRESSINPUT_RULE_H_ |
OLD | NEW |