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 #include <libaddressinput/address_data.h> | 15 #include <libaddressinput/address_data.h> |
16 | 16 |
17 #include <libaddressinput/address_field.h> | 17 #include <libaddressinput/address_field.h> |
18 | 18 |
19 #include <cassert> | 19 #include <cassert> |
| 20 #include <cstddef> |
20 #include <string> | 21 #include <string> |
| 22 #include <vector> |
| 23 |
| 24 #include "region_data_constants.h" |
| 25 #include "rule.h" |
21 | 26 |
22 namespace i18n { | 27 namespace i18n { |
23 namespace addressinput { | 28 namespace addressinput { |
24 | 29 |
25 const std::string& AddressData::GetField(AddressField field) const { | 30 void AddressData::BuildDisplayLines(std::vector<std::string>* lines) const { |
| 31 assert(lines != NULL); |
| 32 lines->clear(); |
| 33 |
| 34 Rule rule; |
| 35 rule.CopyFrom(Rule::GetDefault()); |
| 36 rule.ParseSerializedRule(RegionDataConstants::GetRegionData(country_code)); |
| 37 |
| 38 const std::vector<std::vector<FormatElement> >& format = rule.GetFormat(); |
| 39 for (size_t i = 0; i < format.size(); ++i) { |
| 40 std::string line; |
| 41 for (size_t j = 0; j < format[i].size(); ++j) { |
| 42 const FormatElement& element = format[i][j]; |
| 43 if (element.IsField()) { |
| 44 if (element.field == STREET_ADDRESS) { |
| 45 // Street address is a special case, because it is a multivalued |
| 46 // field. |
| 47 if (format[i].size() == 1) { |
| 48 // The street address is surrounded by newlines. |
| 49 lines->insert(lines->end(), |
| 50 address_lines.begin(), |
| 51 address_lines.end()); |
| 52 } else { |
| 53 // The street address is surrounded by spaces. |
| 54 for (size_t k = 0; k < address_lines.size(); ++k) { |
| 55 line += address_lines[k]; |
| 56 if (k < address_lines.size() - 1) { |
| 57 line += " "; |
| 58 } |
| 59 } |
| 60 } |
| 61 } else { |
| 62 const std::string& field = GetFieldValue(element.field); |
| 63 if (!field.empty()) { |
| 64 line += field; |
| 65 } |
| 66 } |
| 67 } else { |
| 68 line += element.literal; |
| 69 } |
| 70 } |
| 71 |
| 72 if (!line.empty()) { |
| 73 lines->push_back(line); |
| 74 } |
| 75 } |
| 76 } |
| 77 |
| 78 const std::string& AddressData::GetFieldValue(AddressField field) const { |
26 switch (field) { | 79 switch (field) { |
27 case COUNTRY: | 80 case COUNTRY: |
28 return country_code; | 81 return country_code; |
29 case ADMIN_AREA: | 82 case ADMIN_AREA: |
30 return administrative_area; | 83 return administrative_area; |
31 case LOCALITY: | 84 case LOCALITY: |
32 return locality; | 85 return locality; |
33 case DEPENDENT_LOCALITY: | 86 case DEPENDENT_LOCALITY: |
34 return dependent_locality; | 87 return dependent_locality; |
35 case SORTING_CODE: | 88 case SORTING_CODE: |
36 return sorting_code; | 89 return sorting_code; |
37 case POSTAL_CODE: | 90 case POSTAL_CODE: |
38 return postal_code; | 91 return postal_code; |
39 case ORGANIZATION: | 92 case ORGANIZATION: |
40 return organization; | 93 return organization; |
41 case RECIPIENT: | 94 case RECIPIENT: |
42 return recipient; | 95 return recipient; |
43 default: | 96 default: |
44 assert(false); | 97 assert(false); |
45 return recipient; | 98 return recipient; |
46 } | 99 } |
47 } | 100 } |
48 | 101 |
49 } // namespace addressinput | 102 } // namespace addressinput |
50 } // namespace i18n | 103 } // namespace i18n |
OLD | NEW |