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::FormatForDisplay(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 field can contain multiple values. |
| 46 for (size_t k = 0; k < address_lines.size(); ++k) { |
| 47 line += address_lines[k]; |
| 48 if (k < address_lines.size() - 1) { |
| 49 lines->push_back(line); |
| 50 line.clear(); |
| 51 } |
| 52 } |
| 53 } else { |
| 54 line += GetFieldValue(element.field); |
| 55 } |
| 56 } else { |
| 57 line += element.literal; |
| 58 } |
| 59 } |
| 60 |
| 61 if (!line.empty()) { |
| 62 lines->push_back(line); |
| 63 } |
| 64 } |
| 65 } |
| 66 |
| 67 const std::string& AddressData::GetFieldValue(AddressField field) const { |
26 switch (field) { | 68 switch (field) { |
27 case COUNTRY: | 69 case COUNTRY: |
28 return country_code; | 70 return country_code; |
29 case ADMIN_AREA: | 71 case ADMIN_AREA: |
30 return administrative_area; | 72 return administrative_area; |
31 case LOCALITY: | 73 case LOCALITY: |
32 return locality; | 74 return locality; |
33 case DEPENDENT_LOCALITY: | 75 case DEPENDENT_LOCALITY: |
34 return dependent_locality; | 76 return dependent_locality; |
35 case SORTING_CODE: | 77 case SORTING_CODE: |
36 return sorting_code; | 78 return sorting_code; |
37 case POSTAL_CODE: | 79 case POSTAL_CODE: |
38 return postal_code; | 80 return postal_code; |
39 case ORGANIZATION: | 81 case ORGANIZATION: |
40 return organization; | 82 return organization; |
41 case RECIPIENT: | 83 case RECIPIENT: |
42 return recipient; | 84 return recipient; |
43 default: | 85 default: |
44 assert(false); | 86 assert(false); |
45 return recipient; | 87 return recipient; |
46 } | 88 } |
47 } | 89 } |
48 | 90 |
49 } // namespace addressinput | 91 } // namespace addressinput |
50 } // namespace i18n | 92 } // namespace i18n |
OLD | NEW |