1 package com.opencsv;
2
3 import com.opencsv.exceptions.CsvValidationException;
4 import com.opencsv.processor.RowProcessor;
5 import com.opencsv.validators.LineValidatorAggregator;
6 import com.opencsv.validators.RowValidatorAggregator;
7
8 import java.io.IOException;
9 import java.io.Reader;
10 import java.util.HashMap;
11 import java.util.Locale;
12 import java.util.Map;
13 import java.util.ResourceBundle;
14
15
16
17
18
19
20
21
22 public class CSVReaderHeaderAware extends CSVReader {
23
24 private final Map<String, Integer> headerIndex = new HashMap<>();
25
26
27
28
29
30
31
32 public CSVReaderHeaderAware(Reader reader) throws IOException {
33 super(reader);
34 initializeHeader();
35 }
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53 CSVReaderHeaderAware(Reader reader, int skipLines, ICSVParser parser, boolean keepCR, boolean verifyReader,
54 int multilineLimit, Locale errorLocale, LineValidatorAggregator lineValidatorAggregator,
55 RowValidatorAggregator rowValidatorAggregator, RowProcessor rowProcessor) throws IOException {
56 super(reader, skipLines, parser, keepCR, verifyReader, multilineLimit, errorLocale, lineValidatorAggregator, rowValidatorAggregator, rowProcessor);
57 initializeHeader();
58 }
59
60
61
62
63
64
65
66
67
68
69
70 public String[] readNext(String... headerNames) throws IOException, CsvValidationException {
71 if (headerNames == null) {
72 return super.readNextSilently();
73 }
74
75 String[] strings = readNext();
76 if (strings == null) {
77 return null;
78 }
79
80 if (strings.length != headerIndex.size()) {
81 throw new IOException(String.format(
82 ResourceBundle.getBundle(ICSVParser.DEFAULT_BUNDLE_NAME, errorLocale)
83 .getString("header.data.mismatch.with.line.number"),
84 getRecordsRead(), headerIndex.size(), strings.length));
85 }
86
87 String[] response = new String[headerNames.length];
88
89 for (int i = 0; i < headerNames.length; i++) {
90 String headerName = headerNames[i];
91
92 Integer index = headerIndex.get(headerName);
93 if (index == null) {
94 throw new IllegalArgumentException(String.format(
95 ResourceBundle.getBundle(ICSVParser.DEFAULT_BUNDLE_NAME, errorLocale)
96 .getString("header.nonexistant"),
97 headerName));
98 }
99
100 response[i] = strings[index];
101 }
102 return response;
103 }
104
105
106
107
108
109
110
111
112
113 public Map<String, String> readMap() throws IOException, CsvValidationException {
114 String[] strings = readNext();
115 if (strings == null) {
116 return null;
117 }
118 if (strings.length != headerIndex.size()) {
119 throw new IOException(String.format(
120 ResourceBundle.getBundle(ICSVParser.DEFAULT_BUNDLE_NAME, errorLocale)
121 .getString("header.data.mismatch.with.line.number"),
122 getRecordsRead(), headerIndex.size(), strings.length));
123 }
124
125
126
127
128
129 Map<String, String> resultMap = new HashMap<>(headerIndex.size()*2);
130 for(Map.Entry<String, Integer> entry : headerIndex.entrySet()) {
131 if(entry.getValue() < strings.length) {
132 resultMap.put(entry.getKey(), strings[entry.getValue()]);
133 }
134 }
135 return resultMap;
136 }
137
138 private void initializeHeader() throws IOException {
139 String[] headers = super.readNextSilently();
140 for (int i = 0; i < headers.length; i++) {
141 headerIndex.put(headers[i], i);
142 }
143 }
144
145 }