1 package com.opencsv;
2
3 import com.opencsv.enums.CSVReaderNullFieldIndicator;
4 import com.opencsv.processor.RowProcessor;
5 import com.opencsv.validators.LineValidator;
6 import com.opencsv.validators.RowValidator;
7 import org.apache.commons.lang3.ObjectUtils;
8
9 import java.io.IOException;
10 import java.io.Reader;
11 import java.util.Locale;
12 import java.util.ResourceBundle;
13
14 /**
15 * Builder for {@link CSVReaderHeaderAware}.
16 *
17 * @author Andre Rosot
18 * @since 4.2
19 */
20 public class CSVReaderHeaderAwareBuilder extends CSVReaderBaseBuilder<CSVReaderHeaderAware> {
21
22 /**
23 * Sets the reader to an underlying CSV source.
24 *
25 * @param reader The reader to an underlying CSV source.
26 */
27 public CSVReaderHeaderAwareBuilder(Reader reader) {
28 super(reader);
29 }
30
31 /**
32 * Sets the number of lines to skip before reading.
33 *
34 * @param skipLines The number of lines to skip before reading.
35 * @return {@code this}
36 */
37 public CSVReaderHeaderAwareBuilder withSkipLines(
38 final int skipLines) {
39 this.skipLines = Math.max(skipLines, 0);
40 return this;
41 }
42
43 /**
44 * Sets the parser to use to parse the input.
45 *
46 * @param icsvParser The parser to use to parse the input.
47 * @return {@code this}
48 */
49 public CSVReaderHeaderAwareBuilder withCSVParser(
50 final ICSVParser icsvParser) {
51 this.icsvParser = icsvParser;
52 return this;
53 }
54
55 /**
56 * Sets if the reader will keep or discard carriage returns.
57 *
58 * @param keepCR True to keep carriage returns, false to discard.
59 * @return {@code this}
60 */
61 public CSVReaderHeaderAwareBuilder withKeepCarriageReturn(boolean keepCR) {
62 this.keepCR = keepCR;
63 return this;
64 }
65
66 /**
67 * Checks to see if the {@link CSVReaderHeaderAware} should verify the reader state before
68 * reads or not.
69 *
70 * <p>This should be set to false if you are using some form of asynchronous
71 * reader (like readers created by the java.nio.* classes).</p>
72 *
73 * <p>The default value is true.</p>
74 *
75 * @param verifyReader True if {@link CSVReaderHeaderAware} should verify reader before each read, false otherwise.
76 * @return {@code this}
77 */
78 public CSVReaderHeaderAwareBuilder withVerifyReader(boolean verifyReader) {
79 this.verifyReader = verifyReader;
80 return this;
81 }
82
83 /**
84 * Checks to see if it should treat a field with two separators, two quotes, or both as a null field.
85 *
86 * @param indicator {@link CSVReaderNullFieldIndicator} set to what should be considered a null field.
87 * @return {@code this}
88 */
89 public CSVReaderHeaderAwareBuilder withFieldAsNull(CSVReaderNullFieldIndicator indicator) {
90 this.nullFieldIndicator = indicator;
91 return this;
92 }
93
94 /**
95 * Sets the maximum number of lines allowed in a multiline record.
96 * More than this number in one record results in an IOException.
97 *
98 * @param multilineLimit No more than this number of lines is allowed in a
99 * single input record. The default is {@link CSVReader#DEFAULT_MULTILINE_LIMIT}.
100 * @return {@code this}
101 */
102 public CSVReaderHeaderAwareBuilder withMultilineLimit(int multilineLimit) {
103 this.multilineLimit = multilineLimit;
104 return this;
105 }
106
107 /**
108 * Sets the locale for all error messages.
109 *
110 * @param errorLocale Locale for error messages
111 * @return {@code this}
112 * @since 4.0
113 */
114 public CSVReaderHeaderAwareBuilder withErrorLocale(Locale errorLocale) {
115 this.errorLocale = ObjectUtils.defaultIfNull(errorLocale, Locale.getDefault());
116 return this;
117 }
118
119 /**
120 * Adds a {@link LineValidator} to the {@link CSVReaderHeaderAware}.
121 * Multiple {@link LineValidator}s can be added with multiple calls.
122 *
123 * @param lineValidator {@link LineValidator} to inject.
124 * @return {@code this}
125 * @since 5.0
126 */
127 public CSVReaderHeaderAwareBuilder withLineValidator(LineValidator lineValidator) {
128 lineValidatorAggregator.addValidator(lineValidator);
129 return this;
130 }
131
132 /**
133 * Adds a {@link RowValidator} to the {@link CSVReaderHeaderAware}.
134 * Multiple {@link RowValidator}s can be added with multiple calls.
135 *
136 * @param rowValidator {@link RowValidator} to inject
137 * @return {@code this}
138 * @since 5.0
139 */
140 public CSVReaderHeaderAwareBuilder withRowValidator(RowValidator rowValidator) {
141 rowValidatorAggregator.addValidator(rowValidator);
142 return this;
143 }
144
145 /**
146 * Adds a {@link RowProcessor} to the {@link CSVReaderHeaderAware}.
147 * Only a single {@link RowProcessor} can be added so multiple calls will overwrite
148 * the previously set {@link RowProcessor}.
149 *
150 * @param rowProcessor {@link RowProcessor} to inject
151 * @return {@code this}
152 * @since 5.0
153 */
154 public CSVReaderHeaderAwareBuilder withRowProcessor(RowProcessor rowProcessor) {
155 this.rowProcessor = rowProcessor;
156 return this;
157 }
158
159 /**
160 * Creates the {@link CSVReaderHeaderAware}.
161 * @return The {@link CSVReaderHeaderAware} based on the set criteria.
162 * @throws RuntimeException If the header mapping cannot be initialized
163 * from the first line of data
164 */
165 @Override
166 public CSVReaderHeaderAware build() throws RuntimeException {
167 final ICSVParser parser = getOrCreateCsvParser();
168 try {
169 return new CSVReaderHeaderAware(reader, skipLines, parser, keepCR, verifyReader,
170 multilineLimit, errorLocale, lineValidatorAggregator, rowValidatorAggregator, rowProcessor);
171 } catch (IOException e) {
172 throw new RuntimeException(ResourceBundle.getBundle(ICSVParser.DEFAULT_BUNDLE_NAME, errorLocale).getString("csvreaderheaderaware.impossible"), e);
173 }
174 }
175 }