1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.orangesignal.csv;
18
19 import java.io.Serializable;
20 import java.util.regex.Pattern;
21
22
23
24
25
26
27
28 public class CsvConfig implements Serializable, Cloneable {
29
30 private static final long serialVersionUID = -7531286991159010295L;
31
32
33
34
35 public static final char DEFAULT_SEPARATOR = ',';
36
37
38
39
40 public static final char DEFAULT_QUOTE = '"';
41
42
43
44
45 public static final char DEFAULT_ESCAPE = '\\';
46
47
48
49
50 public static final int DEFAULT_SKIP_LINES = 0;
51
52
53
54
55
56
57 public static final QuotePolicy DEFAULT_QUOTE_POLICY = QuotePolicy.ALL;
58
59
60
61
62
63
64 private char separator;
65
66
67
68
69 private char quote;
70
71
72
73
74 private char escape;
75
76
77
78
79 private boolean quoteDisabled;
80
81
82
83
84 private boolean escapeDisabled;
85
86
87
88
89 private String breakString;
90
91
92
93
94 private String nullString;
95
96
97
98
99 private boolean ignoreCaseNullString;
100
101
102
103
104 private boolean ignoreLeadingWhitespaces;
105
106
107
108
109 private boolean ignoreTrailingWhitespaces;
110
111
112
113
114 private boolean ignoreEmptyLines;
115
116
117
118
119 private Pattern[] ignoreLinePatterns;
120
121
122
123
124 private int skipLines = DEFAULT_SKIP_LINES;
125
126
127
128
129 private QuotePolicy quotePolicy = DEFAULT_QUOTE_POLICY;
130
131
132
133
134 private boolean utf8bomPolicy;
135
136
137
138
139
140
141 private boolean variableColumns = true;
142
143
144
145
146 private String lineSeparator = System.getProperty("line.separator");
147
148
149
150
151
152
153
154 public CsvConfig() {
155 this(DEFAULT_SEPARATOR, DEFAULT_QUOTE, DEFAULT_ESCAPE, true, true);
156 }
157
158
159
160
161
162
163 public CsvConfig(final char separator) {
164 this(separator, DEFAULT_QUOTE, DEFAULT_ESCAPE, true, true);
165 }
166
167
168
169
170
171
172
173
174
175 public CsvConfig(final char separator, final char quote, final char escape) {
176 this(separator, quote, escape, false, false);
177 }
178
179
180
181
182
183
184
185
186
187
188
189 public CsvConfig(final char separator, final char quote, final char escape, final boolean quoteDisabled, final boolean escapeDisabled) {
190 this.separator = separator;
191 this.quote = quote;
192 this.escape = escape;
193 this.quoteDisabled = quoteDisabled;
194 this.escapeDisabled = escapeDisabled;
195 }
196
197
198
199
200
201
202
203
204 public void validate() {
205
206 if (separator == '\r' || separator == '\n') {
207 throw new IllegalArgumentException("Invalid separator character");
208 }
209
210 if (!quoteDisabled && (quote == separator || quote == '\r' || quote == '\n')) {
211 throw new IllegalArgumentException("Invalid quote character");
212 }
213
214 if (!escapeDisabled && (escape == separator || escape == '\r' || escape == '\n')) {
215 throw new IllegalArgumentException("Invalid escape character");
216 }
217 }
218
219
220
221
222
223
224
225
226
227 public char getSeparator() { return separator; }
228
229
230
231
232
233
234 public void setSeparator(final char separator) { this.separator = separator; }
235
236
237
238
239
240
241 public char getQuote() { return quote; }
242
243
244
245
246
247
248 public void setQuote(final char quote) { this.quote = quote; }
249
250
251
252
253
254
255 public char getEscape() { return escape; }
256
257
258
259
260
261
262 public void setEscape(final char escape) { this.escape = escape; }
263
264
265
266
267
268
269 public boolean isQuoteDisabled() { return quoteDisabled; }
270
271
272
273
274
275
276 public void setQuoteDisabled(final boolean disabled) { this.quoteDisabled = disabled; }
277
278
279
280
281
282
283 public boolean isEscapeDisabled() { return escapeDisabled; }
284
285
286
287
288
289
290 public void setEscapeDisabled(final boolean disabled) { this.escapeDisabled = disabled; }
291
292
293
294
295
296
297 public String getBreakString() { return breakString; }
298
299
300
301
302
303
304 public void setBreakString(final String breakString) { this.breakString = breakString; }
305
306
307
308
309
310
311 public String getNullString() { return nullString; }
312
313
314
315
316
317
318 public void setNullString(final String nullString) {
319 setNullString(nullString, false);
320 }
321
322
323
324
325
326
327
328 public void setNullString(final String nullString, final boolean ignoreCase) {
329 this.nullString = nullString;
330 this.ignoreCaseNullString = ignoreCase;
331 }
332
333
334
335
336
337
338 public boolean isIgnoreCaseNullString() { return ignoreCaseNullString; }
339
340
341
342
343
344
345
346 public void setIgnoreCaseNullString(final boolean ignoreCaseNullString) {
347 this.ignoreCaseNullString = ignoreCaseNullString;
348 }
349
350
351
352
353
354
355 public boolean isIgnoreLeadingWhitespaces() { return ignoreLeadingWhitespaces; }
356
357
358
359
360
361
362
363 public void setIgnoreLeadingWhitespaces(final boolean ignore) { this.ignoreLeadingWhitespaces = ignore; }
364
365
366
367
368
369
370 public boolean isIgnoreTrailingWhitespaces() { return ignoreTrailingWhitespaces; }
371
372
373
374
375
376
377
378 public void setIgnoreTrailingWhitespaces(final boolean ignore) { this.ignoreTrailingWhitespaces = ignore; }
379
380
381
382
383
384
385 public boolean isIgnoreEmptyLines() { return ignoreEmptyLines; }
386
387
388
389
390
391
392 public void setIgnoreEmptyLines(final boolean ignore) { this.ignoreEmptyLines = ignore; }
393
394
395
396
397
398
399 public Pattern[] getIgnoreLinePatterns() { return ignoreLinePatterns; }
400
401
402
403
404
405
406 public void setIgnoreLinePatterns(final Pattern...ignoreLinePatterns) { this.ignoreLinePatterns = ignoreLinePatterns; }
407
408
409
410
411
412
413 public int getSkipLines() { return skipLines; }
414
415
416
417
418
419
420 public void setSkipLines(final int skipLines) { this.skipLines = skipLines; }
421
422
423
424
425
426
427 public String getLineSeparator() { return lineSeparator; }
428
429
430
431
432
433
434 public void setLineSeparator(final String lineSeparator) { this.lineSeparator = lineSeparator; }
435
436
437
438
439
440
441
442 public QuotePolicy getQuotePolicy() { return quotePolicy; }
443
444
445
446
447
448
449
450
451 public void setQuotePolicy(final QuotePolicy quotePolicy) {
452 if (quotePolicy == null) {
453 throw new IllegalArgumentException("QuotePolicy must not be null");
454 }
455 this.quotePolicy = quotePolicy;
456 }
457
458
459
460
461
462
463
464 public boolean isUtf8bomPolicy() { return utf8bomPolicy; }
465
466
467
468
469
470
471
472 public void setUtf8bomPolicy(final boolean utf8bomPolicy) { this.utf8bomPolicy = utf8bomPolicy; }
473
474
475
476
477
478
479
480 public boolean isVariableColumns() { return variableColumns; }
481
482
483
484
485
486
487
488 public void setVariableColumns(final boolean variableColumns) { this.variableColumns = variableColumns; }
489
490
491
492
493
494 @Override public CsvConfig clone() { return (CsvConfig) SerializationUtils.clone(this); }
495
496 }