View Javadoc
1   package com.opencsv;
2   
3   import com.opencsv.enums.CSVReaderNullFieldIndicator;
4   import com.opencsv.processor.RowProcessor;
5   import com.opencsv.validators.LineValidatorAggregator;
6   import com.opencsv.validators.RowValidatorAggregator;
7   import org.apache.commons.lang3.ObjectUtils;
8   
9   import java.io.Reader;
10  import java.util.Locale;
11  
12  /**
13   * Base class for the builders of various incarnations of CSVReaders.
14   * @param <T> The type pf the CSVReader class to return
15   *
16   * @author Andrew Rucker Jones
17   * @since 5.5.2
18   */
19  abstract public class CSVReaderBaseBuilder<T> {
20      protected final Reader reader;
21      protected final LineValidatorAggregator lineValidatorAggregator = new LineValidatorAggregator();
22      protected final RowValidatorAggregator rowValidatorAggregator = new RowValidatorAggregator();
23      private final CSVParserBuilder parserBuilder = new CSVParserBuilder();
24      protected int skipLines = CSVReader.DEFAULT_SKIP_LINES;
25      protected ICSVParser icsvParser = null;
26      protected boolean keepCR;
27      protected boolean verifyReader = CSVReader.DEFAULT_VERIFY_READER;
28      protected CSVReaderNullFieldIndicator nullFieldIndicator = CSVReaderNullFieldIndicator.NEITHER;
29      protected int multilineLimit = CSVReader.DEFAULT_MULTILINE_LIMIT;
30      protected Locale errorLocale = Locale.getDefault();
31      protected RowProcessor rowProcessor = null;
32  
33      /**
34       * Base Constructor
35       *
36       * @param reader The reader to an underlying CSV source.
37       */
38      protected CSVReaderBaseBuilder(final Reader reader) {
39          this.reader = reader;
40      }
41  
42      /**
43       * Used by unit tests.
44       *
45       * @return The reader.
46       */
47      protected Reader getReader() {
48          return reader;
49      }
50  
51      /**
52       * Used by unit tests.
53       *
54       * @return The set number of lines to skip
55       */
56      protected int getSkipLines() {
57          return skipLines;
58      }
59  
60      /**
61       * Used by unit tests.
62       *
63       * @return The CSVParser used by the builder.
64       */
65      protected ICSVParser getCsvParser() {
66          return icsvParser;
67      }
68  
69      /**
70       * Used by unit tests.
71       *
72       * @return The upper limit on lines in multiline records.
73       */
74      protected int getMultilineLimit() {
75          return multilineLimit;
76      }
77  
78      /**
79       * Returns if the reader built will keep or discard carriage returns.
80       *
81       * @return {@code true} if the reader built will keep carriage returns,
82       * {@code false} otherwise
83       */
84      protected boolean keepCarriageReturn() {
85          return this.keepCR;
86      }
87  
88      /**
89       * Creates a new {@link ICSVParser} if the class doesn't already hold one.
90       *
91       * @return The injected {@link ICSVParser} or a default parser.
92       */
93      protected ICSVParser getOrCreateCsvParser() {
94          return ObjectUtils.defaultIfNull(icsvParser,
95                  parserBuilder
96                          .withFieldAsNull(nullFieldIndicator)
97                          .withErrorLocale(errorLocale)
98                          .build());
99      }
100 
101     /**
102      * @return The flag indicating whether the reader should be verified before each read.
103      */
104     public boolean isVerifyReader() {
105         return verifyReader;
106     }
107 
108     /**
109      * @return The locale for error messages
110      */
111     public Locale getErrorLocale() {
112         return errorLocale;
113     }
114 
115     /**
116      * @return The {@link LineValidatorAggregator} for custom defined {@link com.opencsv.validators.LineValidator}s.
117      */
118     public LineValidatorAggregator getLineValidatorAggregator() {
119         return lineValidatorAggregator;
120     }
121 
122     /**
123      * @return The {@link RowValidatorAggregator} for the custom defined {@link com.opencsv.validators.RowValidator}s.
124      */
125     public RowValidatorAggregator getRowValidatorAggregator() {
126         return rowValidatorAggregator;
127     }
128 
129     /**
130      * Must create the CSVReader type requested.
131      * @return A new instance of {@link CSVReader} or derived class
132      */
133     public abstract T build();
134 }