-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
FlatFileItemReaderBuilder.java
803 lines (685 loc) · 24.7 KB
/
FlatFileItemReaderBuilder.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
/*
* Copyright 2016-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.batch.item.file.builder;
import java.beans.PropertyEditor;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.batch.item.file.BufferedReaderFactory;
import org.springframework.batch.item.file.DefaultBufferedReaderFactory;
import org.springframework.batch.item.file.FlatFileItemReader;
import org.springframework.batch.item.file.LineCallbackHandler;
import org.springframework.batch.item.file.LineMapper;
import org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper;
import org.springframework.batch.item.file.mapping.DefaultLineMapper;
import org.springframework.batch.item.file.mapping.FieldSetMapper;
import org.springframework.batch.item.file.separator.RecordSeparatorPolicy;
import org.springframework.batch.item.file.separator.SimpleRecordSeparatorPolicy;
import org.springframework.batch.item.file.transform.DefaultFieldSetFactory;
import org.springframework.batch.item.file.transform.DelimitedLineTokenizer;
import org.springframework.batch.item.file.transform.FieldSetFactory;
import org.springframework.batch.item.file.transform.FixedLengthTokenizer;
import org.springframework.batch.item.file.transform.LineTokenizer;
import org.springframework.batch.item.file.transform.Range;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.core.io.Resource;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
/**
* A builder implementation for the {@link FlatFileItemReader}.
*
* @author Michael Minella
* @author Glenn Renfro
* @author Mahmoud Ben Hassine
* @author Drummond Dawson
* @since 4.0
* @see FlatFileItemReader
*/
public class FlatFileItemReaderBuilder<T> {
protected Log logger = LogFactory.getLog(getClass());
private boolean strict = true;
private String encoding = FlatFileItemReader.DEFAULT_CHARSET;
private RecordSeparatorPolicy recordSeparatorPolicy =
new SimpleRecordSeparatorPolicy();
private BufferedReaderFactory bufferedReaderFactory =
new DefaultBufferedReaderFactory();
private Resource resource;
private List<String> comments =
new ArrayList<>(Arrays.asList(FlatFileItemReader.DEFAULT_COMMENT_PREFIXES));
private int linesToSkip = 0;
private LineCallbackHandler skippedLinesCallback;
private LineMapper<T> lineMapper;
private FieldSetMapper<T> fieldSetMapper;
private LineTokenizer lineTokenizer;
private DelimitedBuilder<T> delimitedBuilder;
private FixedLengthBuilder<T> fixedLengthBuilder;
private Class<? extends T> targetType;
private String prototypeBeanName;
private BeanFactory beanFactory;
private Map<Class<?>, PropertyEditor> customEditors = new HashMap<>();
private int distanceLimit = 5;
private boolean beanMapperStrict = true;
private BigInteger tokenizerValidator = new BigInteger("0");
private boolean saveState = true;
private String name;
private int maxItemCount = Integer.MAX_VALUE;
private int currentItemCount;
/**
* Configure if the state of the {@link org.springframework.batch.item.ItemStreamSupport}
* should be persisted within the {@link org.springframework.batch.item.ExecutionContext}
* for restart purposes.
*
* @param saveState defaults to true
* @return The current instance of the builder.
*/
public FlatFileItemReaderBuilder<T> saveState(boolean saveState) {
this.saveState = saveState;
return this;
}
/**
* The name used to calculate the key within the
* {@link org.springframework.batch.item.ExecutionContext}. Required if
* {@link #saveState(boolean)} is set to true.
*
* @param name name of the reader instance
* @return The current instance of the builder.
* @see org.springframework.batch.item.ItemStreamSupport#setName(String)
*/
public FlatFileItemReaderBuilder<T> name(String name) {
this.name = name;
return this;
}
/**
* Configure the max number of items to be read.
*
* @param maxItemCount the max items to be read
* @return The current instance of the builder.
* @see org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader#setMaxItemCount(int)
*/
public FlatFileItemReaderBuilder<T> maxItemCount(int maxItemCount) {
this.maxItemCount = maxItemCount;
return this;
}
/**
* Index for the current item. Used on restarts to indicate where to start from.
*
* @param currentItemCount current index
* @return this instance for method chaining
* @see org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader#setCurrentItemCount(int)
*/
public FlatFileItemReaderBuilder<T> currentItemCount(int currentItemCount) {
this.currentItemCount = currentItemCount;
return this;
}
/**
* Add a string to the list of Strings that indicate commented lines.
* Defaults to {@link FlatFileItemReader#DEFAULT_COMMENT_PREFIXES}.
*
* @param comment the string to define a commented line.
* @return The current instance of the builder.
* @see FlatFileItemReader#setComments(String[])
*/
public FlatFileItemReaderBuilder<T> addComment(String comment) {
this.comments.add(comment);
return this;
}
/**
* Set an array of Strings that indicate lines that are comments (and therefore skipped by
* the reader). This method overrides the default comment prefixes which are
* {@link FlatFileItemReader#DEFAULT_COMMENT_PREFIXES}.
*
* @param comments an array of strings to identify comments.
* @return The current instance of the builder.
* @see FlatFileItemReader#setComments(String[])
*/
public FlatFileItemReaderBuilder<T> comments(String... comments) {
this.comments = Arrays.asList(comments);
return this;
}
/**
* Configure a custom {@link RecordSeparatorPolicy} for the reader.
*
* @param policy custom policy
* @return The current instance of the builder.
* @see FlatFileItemReader#setRecordSeparatorPolicy(RecordSeparatorPolicy)
*/
public FlatFileItemReaderBuilder<T> recordSeparatorPolicy(RecordSeparatorPolicy policy) {
this.recordSeparatorPolicy = policy;
return this;
}
/**
* Configure a custom {@link BufferedReaderFactory} for the reader.
*
* @param factory custom factory
* @return The current instance of the builder.
* @see FlatFileItemReader#setBufferedReaderFactory(BufferedReaderFactory)
*/
public FlatFileItemReaderBuilder<T> bufferedReaderFactory(BufferedReaderFactory factory) {
this.bufferedReaderFactory = factory;
return this;
}
/**
* The {@link Resource} to be used as input.
*
* @param resource the input to the reader.
* @return The current instance of the builder.
* @see FlatFileItemReader#setResource(Resource)
*/
public FlatFileItemReaderBuilder<T> resource(Resource resource) {
this.resource = resource;
return this;
}
/**
* Configure if the reader should be in strict mode (require the input {@link Resource}
* to exist).
*
* @param strict true if the input file is required to exist.
* @return The current instance of the builder.
* @see FlatFileItemReader#setStrict(boolean)
*/
public FlatFileItemReaderBuilder<T> strict(boolean strict) {
this.strict = strict;
return this;
}
/**
* Configure the encoding used by the reader to read the input source.
* Default value is {@link FlatFileItemReader#DEFAULT_CHARSET}.
*
* @param encoding to use to read the input source.
* @return The current instance of the builder.
* @see FlatFileItemReader#setEncoding(String)
*/
public FlatFileItemReaderBuilder<T> encoding(String encoding) {
this.encoding = encoding;
return this;
}
/**
* The number of lines to skip at the beginning of reading the file.
*
* @param linesToSkip number of lines to be skipped.
* @return The current instance of the builder.
* @see FlatFileItemReader#setLinesToSkip(int)
*/
public FlatFileItemReaderBuilder<T> linesToSkip(int linesToSkip) {
this.linesToSkip = linesToSkip;
return this;
}
/**
* A callback to be called for each line that is skipped.
*
* @param callback the callback
* @return The current instance of the builder.
* @see FlatFileItemReader#setSkippedLinesCallback(LineCallbackHandler)
*/
public FlatFileItemReaderBuilder<T> skippedLinesCallback(LineCallbackHandler callback) {
this.skippedLinesCallback = callback;
return this;
}
/**
* A {@link LineMapper} implementation to be used.
*
* @param lineMapper {@link LineMapper}
* @return The current instance of the builder.
* @see FlatFileItemReader#setLineMapper(LineMapper)
*/
public FlatFileItemReaderBuilder<T> lineMapper(LineMapper<T> lineMapper) {
this.lineMapper = lineMapper;
return this;
}
/**
* A {@link FieldSetMapper} implementation to be used.
*
* @param mapper a {@link FieldSetMapper}
* @return The current instance of the builder.
* @see DefaultLineMapper#setFieldSetMapper(FieldSetMapper)
*/
public FlatFileItemReaderBuilder<T> fieldSetMapper(FieldSetMapper<T> mapper) {
this.fieldSetMapper = mapper;
return this;
}
/**
* A {@link LineTokenizer} implementation to be used.
*
* @param tokenizer a {@link LineTokenizer}
* @return The current instance of the builder.
* @see DefaultLineMapper#setLineTokenizer(LineTokenizer)
*/
public FlatFileItemReaderBuilder<T> lineTokenizer(LineTokenizer tokenizer) {
updateTokenizerValidation(tokenizer, 0);
this.lineTokenizer = tokenizer;
return this;
}
/**
* Returns an instance of a {@link DelimitedBuilder} for building a
* {@link DelimitedLineTokenizer}. The {@link DelimitedLineTokenizer} configured by
* this builder will only be used if one is not explicitly configured via
* {@link FlatFileItemReaderBuilder#lineTokenizer}
*
* @return a {@link DelimitedBuilder}
*
*/
public DelimitedBuilder<T> delimited() {
this.delimitedBuilder = new DelimitedBuilder<>(this);
updateTokenizerValidation(this.delimitedBuilder, 1);
return this.delimitedBuilder;
}
/**
* Returns an instance of a {@link FixedLengthBuilder} for building a
* {@link FixedLengthTokenizer}. The {@link FixedLengthTokenizer} configured by this
* builder will only be used if the {@link FlatFileItemReaderBuilder#lineTokenizer}
* has not been configured.
*
* @return a {@link FixedLengthBuilder}
*/
public FixedLengthBuilder<T> fixedLength() {
this.fixedLengthBuilder = new FixedLengthBuilder<>(this);
updateTokenizerValidation(this.fixedLengthBuilder, 2);
return this.fixedLengthBuilder;
}
/**
* The class that will represent the "item" to be returned from the reader. This
* class is used via the {@link BeanWrapperFieldSetMapper}. If more complex logic is
* required, providing your own {@link FieldSetMapper} via
* {@link FlatFileItemReaderBuilder#fieldSetMapper} is required.
*
* @param targetType The class to map to
* @return The current instance of the builder.
* @see BeanWrapperFieldSetMapper#setTargetType(Class)
*/
public FlatFileItemReaderBuilder<T> targetType(Class<? extends T> targetType) {
this.targetType = targetType;
return this;
}
/**
* Configures the id of a prototype scoped bean to be used as the item returned by the
* reader.
*
* @param prototypeBeanName the name of a prototype scoped bean
* @return The current instance of the builder.
* @see BeanWrapperFieldSetMapper#setPrototypeBeanName(String)
*/
public FlatFileItemReaderBuilder<T> prototypeBeanName(String prototypeBeanName) {
this.prototypeBeanName = prototypeBeanName;
return this;
}
/**
* Configures the {@link BeanFactory} used to create the beans that are returned as
* items.
*
* @param beanFactory a {@link BeanFactory}
* @return The current instance of the builder.
* @see BeanWrapperFieldSetMapper#setBeanFactory(BeanFactory)
*/
public FlatFileItemReaderBuilder<T> beanFactory(BeanFactory beanFactory) {
this.beanFactory = beanFactory;
return this;
}
/**
* Register custom type converters for beans being mapped.
*
* @param customEditors a {@link Map} of editors
* @return The current instance of the builder.
* @see BeanWrapperFieldSetMapper#setCustomEditors(Map)
*/
public FlatFileItemReaderBuilder<T> customEditors(Map<Class<?>, PropertyEditor> customEditors) {
if(customEditors != null) {
this.customEditors.putAll(customEditors);
}
return this;
}
/**
* Configures the maximum tolerance between the actual spelling of a field's name and
* the property's name.
*
* @param distanceLimit distance limit to set
* @return The current instance of the builder.
* @see BeanWrapperFieldSetMapper#setDistanceLimit(int)
*/
public FlatFileItemReaderBuilder<T> distanceLimit(int distanceLimit) {
this.distanceLimit = distanceLimit;
return this;
}
/**
* If set to true, mapping will fail if the {@link org.springframework.batch.item.file.transform.FieldSet}
* contains fields that cannot be mapped to the bean.
*
* @param beanMapperStrict defaults to false
* @return The current instance of the builder.
* @see BeanWrapperFieldSetMapper#setStrict(boolean)
*/
public FlatFileItemReaderBuilder<T> beanMapperStrict(boolean beanMapperStrict) {
this.beanMapperStrict = beanMapperStrict;
return this;
}
/**
* Builds the {@link FlatFileItemReader}.
*
* @return a {@link FlatFileItemReader}
*/
public FlatFileItemReader<T> build() {
if(this.saveState) {
Assert.state(StringUtils.hasText(this.name),
"A name is required when saveState is set to true.");
}
if(this.resource == null) {
logger.debug("The resource is null. This is only a valid scenario when " +
"injecting it later as in when using the MultiResourceItemReader");
}
Assert.notNull(this.recordSeparatorPolicy, "A RecordSeparatorPolicy is required.");
Assert.notNull(this.bufferedReaderFactory, "A BufferedReaderFactory is required.");
int validatorValue = this.tokenizerValidator.intValue();
FlatFileItemReader<T> reader = new FlatFileItemReader<>();
if(StringUtils.hasText(this.name)) {
reader.setName(this.name);
}
if(StringUtils.hasText(this.encoding)) {
reader.setEncoding(this.encoding);
}
reader.setResource(this.resource);
if(this.lineMapper != null) {
reader.setLineMapper(this.lineMapper);
}
else {
Assert.state(validatorValue == 0 || validatorValue == 1 || validatorValue == 2 || validatorValue == 4,
"Only one LineTokenizer option may be configured");
DefaultLineMapper<T> lineMapper = new DefaultLineMapper<>();
if(this.lineTokenizer != null) {
lineMapper.setLineTokenizer(this.lineTokenizer);
}
else if(this.fixedLengthBuilder != null) {
lineMapper.setLineTokenizer(this.fixedLengthBuilder.build());
}
else if(this.delimitedBuilder != null) {
lineMapper.setLineTokenizer(this.delimitedBuilder.build());
}
else {
throw new IllegalStateException("No LineTokenizer implementation was provided.");
}
if(this.targetType != null || StringUtils.hasText(this.prototypeBeanName)) {
BeanWrapperFieldSetMapper<T> mapper = new BeanWrapperFieldSetMapper<>();
mapper.setTargetType(this.targetType);
mapper.setPrototypeBeanName(this.prototypeBeanName);
mapper.setStrict(this.beanMapperStrict);
mapper.setBeanFactory(this.beanFactory);
mapper.setDistanceLimit(this.distanceLimit);
mapper.setCustomEditors(this.customEditors);
try {
mapper.afterPropertiesSet();
}
catch (Exception e) {
throw new IllegalStateException("Unable to initialize BeanWrapperFieldSetMapper", e);
}
lineMapper.setFieldSetMapper(mapper);
}
else if(this.fieldSetMapper != null) {
lineMapper.setFieldSetMapper(this.fieldSetMapper);
}
else {
throw new IllegalStateException("No FieldSetMapper implementation was provided.");
}
reader.setLineMapper(lineMapper);
}
reader.setLinesToSkip(this.linesToSkip);
reader.setComments(this.comments.toArray(new String[this.comments.size()]));
reader.setSkippedLinesCallback(this.skippedLinesCallback);
reader.setRecordSeparatorPolicy(this.recordSeparatorPolicy);
reader.setBufferedReaderFactory(this.bufferedReaderFactory);
reader.setMaxItemCount(this.maxItemCount);
reader.setCurrentItemCount(this.currentItemCount);
reader.setSaveState(this.saveState);
reader.setStrict(this.strict);
return reader;
}
private void updateTokenizerValidation(Object tokenizer, int index) {
if(tokenizer != null) {
this.tokenizerValidator = this.tokenizerValidator.flipBit(index);
}
else {
this.tokenizerValidator = this.tokenizerValidator.clearBit(index);
}
}
/**
* A builder for constructing a {@link DelimitedLineTokenizer}
*
* @param <T> the type of the parent {@link FlatFileItemReaderBuilder}
*/
public static class DelimitedBuilder<T> {
private FlatFileItemReaderBuilder<T> parent;
private List<String> names = new ArrayList<>();
private String delimiter;
private Character quoteCharacter;
private List<Integer> includedFields = new ArrayList<>();
private FieldSetFactory fieldSetFactory = new DefaultFieldSetFactory();
private boolean strict = true;
protected DelimitedBuilder(FlatFileItemReaderBuilder<T> parent) {
this.parent = parent;
}
/**
* Define the delimiter for the file.
*
* @param delimiter String used as a delimiter between fields.
* @return The instance of the builder for chaining.
* @see DelimitedLineTokenizer#setDelimiter(String)
*/
public DelimitedBuilder<T> delimiter(String delimiter) {
this.delimiter = delimiter;
return this;
}
/**
* Define the character used to quote fields.
*
* @param quoteCharacter char used to define quoted fields
* @return The instance of the builder for chaining.
* @see DelimitedLineTokenizer#setQuoteCharacter(char)
*/
public DelimitedBuilder<T> quoteCharacter(char quoteCharacter) {
this.quoteCharacter = quoteCharacter;
return this;
}
/**
* A list of indices of the fields within a delimited file to be included
*
* @param fields indices of the fields
* @return The instance of the builder for chaining.
* @see DelimitedLineTokenizer#setIncludedFields(int[])
*/
public DelimitedBuilder<T> includedFields(Integer... fields) {
this.includedFields.addAll(Arrays.asList(fields));
return this;
}
/**
* Add an index to the list of fields to be included from the file
*
* @param field the index to be included
* @return The instance of the builder for chaining.
* @see DelimitedLineTokenizer#setIncludedFields(int[])
*/
public DelimitedBuilder<T> addIncludedField(int field) {
this.includedFields.add(field);
return this;
}
/**
* A factory for creating the resulting
* {@link org.springframework.batch.item.file.transform.FieldSet}. Defaults to
* {@link DefaultFieldSetFactory}.
*
* @param fieldSetFactory Factory for creating {@link org.springframework.batch.item.file.transform.FieldSet}
* @return The instance of the builder for chaining.
* @see DelimitedLineTokenizer#setFieldSetFactory(FieldSetFactory)
*/
public DelimitedBuilder<T> fieldSetFactory(FieldSetFactory fieldSetFactory) {
this.fieldSetFactory = fieldSetFactory;
return this;
}
/**
* Names of each of the fields within the fields that are returned in the order
* they occur within the delimited file. Required.
*
* @param names names of each field
* @return The parent {@link FlatFileItemReaderBuilder}
* @see DelimitedLineTokenizer#setNames(String[])
*/
public FlatFileItemReaderBuilder<T> names(String... names) {
this.names.addAll(Arrays.asList(names));
return this.parent;
}
/**
* Returns a {@link DelimitedLineTokenizer}
*
* @return {@link DelimitedLineTokenizer}
*/
public DelimitedLineTokenizer build() {
Assert.notNull(this.fieldSetFactory, "A FieldSetFactory is required.");
Assert.notEmpty(this.names, "A list of field names is required");
DelimitedLineTokenizer tokenizer = new DelimitedLineTokenizer();
tokenizer.setNames(this.names.toArray(new String[this.names.size()]));
if(StringUtils.hasLength(this.delimiter)) {
tokenizer.setDelimiter(this.delimiter);
}
if(this.quoteCharacter != null) {
tokenizer.setQuoteCharacter(this.quoteCharacter);
}
if(!this.includedFields.isEmpty()) {
Set<Integer> deDupedFields = new HashSet<>(this.includedFields.size());
deDupedFields.addAll(this.includedFields);
deDupedFields.remove(null);
int [] fields = new int[deDupedFields.size()];
Iterator<Integer> iterator = deDupedFields.iterator();
for(int i = 0; i < fields.length; i++) {
fields[i] = iterator.next();
}
tokenizer.setIncludedFields(fields);
}
tokenizer.setFieldSetFactory(this.fieldSetFactory);
tokenizer.setStrict(this.strict);
try {
tokenizer.afterPropertiesSet();
}
catch (Exception e) {
throw new IllegalStateException("Unable to initialize DelimitedLineTokenizer", e);
}
return tokenizer;
}
}
/**
* A builder for constructing a {@link FixedLengthTokenizer}
*
* @param <T> the type of the parent {@link FlatFileItemReaderBuilder}
*/
public static class FixedLengthBuilder<T> {
private FlatFileItemReaderBuilder<T> parent;
private List<Range> ranges = new ArrayList<>();
private List<String> names = new ArrayList<>();
private boolean strict = true;
private FieldSetFactory fieldSetFactory = new DefaultFieldSetFactory();
protected FixedLengthBuilder(FlatFileItemReaderBuilder<T> parent) {
this.parent = parent;
}
/**
* The column ranges for each field
*
* @param ranges column ranges
* @return This instance for chaining
* @see FixedLengthTokenizer#setColumns(Range[])
*/
public FixedLengthBuilder<T> columns(Range... ranges) {
this.ranges.addAll(Arrays.asList(ranges));
return this;
}
/**
* Add a column range to the existing list
*
* @param range a new column range
* @return This instance for chaining
* @see FixedLengthTokenizer#setColumns(Range[])
*/
public FixedLengthBuilder<T> addColumns(Range range) {
this.ranges.add(range);
return this;
}
/**
* Insert a column range to the existing list
*
* @param range a new column range
* @param index index to add it at
* @return This instance for chaining
* @see FixedLengthTokenizer#setColumns(Range[])
*/
public FixedLengthBuilder<T> addColumns(Range range, int index) {
this.ranges.add(index, range);
return this;
}
/**
* The names of the fields to be parsed from the file. Required.
*
* @param names names of fields
* @return The parent builder
* @see FixedLengthTokenizer#setNames(String[])
*/
public FlatFileItemReaderBuilder<T> names(String... names) {
this.names.addAll(Arrays.asList(names));
return this.parent;
}
/**
* Boolean indicating if the number of tokens in a line must match the number of
* fields (ranges) configured. Defaults to true.
*
* @param strict defaults to true
* @return This instance for chaining
* @see FixedLengthTokenizer#setStrict(boolean)
*/
public FixedLengthBuilder<T> strict(boolean strict) {
this.strict = strict;
return this;
}
/**
* A factory for creating the resulting
* {@link org.springframework.batch.item.file.transform.FieldSet}. Defaults to
* {@link DefaultFieldSetFactory}.
* @param fieldSetFactory Factory for creating {@link org.springframework.batch.item.file.transform.FieldSet}
* @return The instance of the builder for chaining.
* @see FixedLengthTokenizer#setFieldSetFactory(FieldSetFactory)
*/
public FixedLengthBuilder<T> fieldSetFactory(FieldSetFactory fieldSetFactory) {
this.fieldSetFactory = fieldSetFactory;
return this;
}
/**
* Returns a {@link FixedLengthTokenizer}
*
* @return a {@link FixedLengthTokenizer}
*/
public FixedLengthTokenizer build() {
Assert.notNull(this.fieldSetFactory, "A FieldSetFactory is required.");
Assert.notEmpty(this.names, "A list of field names is required.");
Assert.notEmpty(this.ranges, "A list of column ranges is required.");
FixedLengthTokenizer tokenizer = new FixedLengthTokenizer();
tokenizer.setNames(this.names.toArray(new String[this.names.size()]));
tokenizer.setColumns(this.ranges.toArray(new Range[this.ranges.size()]));
tokenizer.setFieldSetFactory(this.fieldSetFactory);
tokenizer.setStrict(this.strict);
return tokenizer;
}
}
}