1 /*
2 Copyright 2005 Bytecode Pty Ltd.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16 package com.opencsv;
17
18
19 import com.opencsv.enums.CSVReaderNullFieldIndicator;
20 import org.apache.commons.lang3.ObjectUtils;
21
22 import java.util.Locale;
23
24 /**
25 * Builder for creating a CSVParser.
26 * <p>Example code for using this class:<br><br>
27 * <code>
28 * final CSVParser parser =<br>
29 * new CSVParserBuilder()<br>
30 * .withSeparator('\t')<br>
31 * .withIgnoreQuotations(true)<br>
32 * .build();<br>
33 * </code></p>
34 *
35 * @see CSVParser
36 */
37 public class CSVParserBuilder {
38
39 private char separator = ICSVParser.DEFAULT_SEPARATOR;
40 private char quoteChar = ICSVParser.DEFAULT_QUOTE_CHARACTER;
41 private char escapeChar = ICSVParser.DEFAULT_ESCAPE_CHARACTER;
42 private boolean strictQuotes = ICSVParser.DEFAULT_STRICT_QUOTES;
43 private boolean ignoreLeadingWhiteSpace = ICSVParser.DEFAULT_IGNORE_LEADING_WHITESPACE;
44 private boolean ignoreQuotations = ICSVParser.DEFAULT_IGNORE_QUOTATIONS;
45 private CSVReaderNullFieldIndicator nullFieldIndicator = CSVReaderNullFieldIndicator.NEITHER;
46 private Locale errorLocale = Locale.getDefault();
47
48
49 /**
50 * Default constructor.
51 */
52 public CSVParserBuilder() {
53 }
54
55 /**
56 * Sets the delimiter to use for separating entries.
57 *
58 * @param separator The delimiter to use for separating entries
59 * @return The CSVParserBuilder
60 */
61 public CSVParserBuilder withSeparator(
62 final char separator) {
63 this.separator = separator;
64 return this;
65 }
66
67
68 /**
69 * Sets the character to use for quoted elements.
70 *
71 * @param quoteChar The character to use for quoted element.
72 * @return The CSVParserBuilder
73 */
74 public CSVParserBuilder withQuoteChar(
75 final char quoteChar) {
76 this.quoteChar = quoteChar;
77 return this;
78 }
79
80
81 /**
82 * Sets the character to use for escaping a separator or quote.
83 *
84 * @param escapeChar The character to use for escaping a separator or quote.
85 * @return The CSVParserBuilder
86 */
87 public CSVParserBuilder withEscapeChar(
88 final char escapeChar) {
89 this.escapeChar = escapeChar;
90 return this;
91 }
92
93
94 /**
95 * Sets the strict quotes setting - if true, characters
96 * outside the quotes are ignored.
97 *
98 * @param strictQuotes If true, characters outside the quotes are ignored
99 * @return The CSVParserBuilder
100 */
101 public CSVParserBuilder withStrictQuotes(
102 final boolean strictQuotes) {
103 this.strictQuotes = strictQuotes;
104 return this;
105 }
106
107 /**
108 * Sets the ignore leading whitespace setting - if true, white space
109 * in front of a quote in a field is ignored.
110 *
111 * @param ignoreLeadingWhiteSpace If true, white space in front of a quote in a field is ignored
112 * @return The CSVParserBuilder
113 */
114 public CSVParserBuilder withIgnoreLeadingWhiteSpace(
115 final boolean ignoreLeadingWhiteSpace) {
116 this.ignoreLeadingWhiteSpace = ignoreLeadingWhiteSpace;
117 return this;
118 }
119
120 /**
121 * Sets the ignore quotations mode - if true, quotations are ignored.
122 *
123 * @param ignoreQuotations If true, quotations are ignored
124 * @return The CSVParserBuilder
125 */
126 public CSVParserBuilder withIgnoreQuotations(
127 final boolean ignoreQuotations) {
128 this.ignoreQuotations = ignoreQuotations;
129 return this;
130 }
131
132 /**
133 * Constructs CSVParser.
134 *
135 * @return A new CSVParser with defined settings.
136 */
137 public CSVParser build() {
138
139 return new CSVParser(
140 separator,
141 quoteChar,
142 escapeChar,
143 strictQuotes,
144 ignoreLeadingWhiteSpace,
145 ignoreQuotations,
146 nullFieldIndicator,
147 errorLocale);
148 }
149
150 /**
151 * @return The defined separator.
152 */
153 public char getSeparator() {
154 return separator;
155 }
156
157 /**
158 * @return The defined quotation character.
159 */
160 public char getQuoteChar() {
161 return quoteChar;
162 }
163
164 /**
165 * @return The defined escape character.
166 */
167 public char getEscapeChar() {
168 return escapeChar;
169 }
170
171 /**
172 * @return The defined strict quotation setting.
173 */
174 public boolean isStrictQuotes() {
175 return strictQuotes;
176 }
177
178 /**
179 * @return The defined ignoreLeadingWhiteSpace setting.
180 */
181 public boolean isIgnoreLeadingWhiteSpace() {
182 return ignoreLeadingWhiteSpace;
183 }
184
185 /**
186 * @return The defined ignoreQuotation setting.
187 */
188 public boolean isIgnoreQuotations() {
189 return ignoreQuotations;
190 }
191
192 /**
193 * Sets the NullFieldIndicator.
194 *
195 * @param fieldIndicator CSVReaderNullFieldIndicator set to what should be considered a null field.
196 * @return The CSVParserBuilder
197 */
198 public CSVParserBuilder withFieldAsNull(final CSVReaderNullFieldIndicator fieldIndicator) {
199 this.nullFieldIndicator = fieldIndicator;
200 return this;
201 }
202
203 /**
204 * Sets the locale for all error messages.
205 *
206 * @param errorLocale Locale for error messages
207 * @return {@code this}
208 * @since 4.0
209 */
210 public CSVParserBuilder withErrorLocale(Locale errorLocale) {
211 this.errorLocale = ObjectUtils.defaultIfNull(errorLocale, Locale.getDefault());
212 return this;
213 }
214
215 /**
216 * @return The null field indicator.
217 * @deprecated As of 5.11.3, replaced by {@link #getNullFieldIndicator()}.
218 */
219 @Deprecated
220 public CSVReaderNullFieldIndicator nullFieldIndicator() {
221 return nullFieldIndicator;
222 }
223
224 /**
225 * @return The null field indicator.
226 * @since 5.11.3
227 */
228 public CSVReaderNullFieldIndicator getNullFieldIndicator() {
229 return nullFieldIndicator;
230 }
231 }
232