Class CSVFileReader
java.lang.Object
com.broadleafcommerce.dataimport.processor.csv.CSVFileReader
- All Implemented Interfaces:
FileReader<CSVFileReader.CSVFileReaderContext>
- Author:
- Phillip Verheyden (phillipuniverse)
-
Nested Class Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionprotected com.univocity.parsers.csv.CsvParser
protected com.univocity.parsers.csv.CsvParserSettings
boolean
Whether or not this reader is applicable for the current contextlong
countTotalItems
(InputStream file) Counts all of the data records within the file.createParsingContext
(InputStream fileStream, long dataLinesToSkip) Creates a parsing context that is passed back throughout the rest of the parsing flows.long
The current line that the context is pointing to, 1-indexedString[]
Obtains the headers for the given contextboolean
Whether or not there are more data records to read as determined bycontext
.Returns the next row of data.
-
Constructor Details
-
CSVFileReader
public CSVFileReader()
-
-
Method Details
-
canHandle
Description copied from interface:FileReader
Whether or not this reader is applicable for the current context- Specified by:
canHandle
in interfaceFileReader<CSVFileReader.CSVFileReaderContext>
- Parameters:
fileType
- type of the file that should be read- Returns:
- whether or not this can handle the given file type
-
countTotalItems
Description copied from interface:FileReader
Counts all of the data records within the file. This should not include all of the physical lines within the file. For instance this should not include the header line or any lines that are denoted as comments- Specified by:
countTotalItems
in interfaceFileReader<CSVFileReader.CSVFileReaderContext>
- Parameters:
file
- file to count records for- Returns:
- the total number of data records in the file
-
buildParser
protected com.univocity.parsers.csv.CsvParser buildParser() -
buildSettings
protected com.univocity.parsers.csv.CsvParserSettings buildSettings() -
createParsingContext
public CSVFileReader.CSVFileReaderContext createParsingContext(InputStream fileStream, long dataLinesToSkip) throws IOException Description copied from interface:FileReader
Creates a parsing context that is passed back throughout the rest of the parsing flows. This must be invoked before attempting to invoke the parsing logic, but is not required for couting records in the file. Any data necessary to read off items, open additional resources, etc should be initialized here and stored within the returned context- Specified by:
createParsingContext
in interfaceFileReader<CSVFileReader.CSVFileReaderContext>
- Parameters:
fileStream
- the underlying file that should be read fromdataLinesToSkip
- how many lines should be skipped from the read in file. Assuming there is a header line, given a current line number the lines to skip should subtract 2- Returns:
- a context with reader-specific variables passed back through the lifecycle of the processing flow
- Throws:
IOException
-
getHeaders
Description copied from interface:FileReader
Obtains the headers for the given context- Specified by:
getHeaders
in interfaceFileReader<CSVFileReader.CSVFileReaderContext>
- Parameters:
ctx
- the context initialized byFileReader.createParsingContext(InputStream, long)
- Returns:
- the headers from the file
-
currentLineNumber
Description copied from interface:FileReader
The current line that the context is pointing to, 1-indexed- Specified by:
currentLineNumber
in interfaceFileReader<CSVFileReader.CSVFileReaderContext>
- Parameters:
ctx
- the context initialized byFileReader.createParsingContext(InputStream, long)
- Returns:
- the current line number that this reader is pointing to based on the context
-
readNextLine
Description copied from interface:FileReader
Returns the next row of data. Keys correspond to the headers and values are the specific values for those headers- Specified by:
readNextLine
in interfaceFileReader<CSVFileReader.CSVFileReaderContext>
- Parameters:
ctx
- the context initialized byFileReader.createParsingContext(InputStream, long)
- Returns:
- a row of data from the file specified by
context
-
hasNext
Description copied from interface:FileReader
Whether or not there are more data records to read as determined bycontext
. Should be used as a guard againstFileReader.readNextLine(FileReaderContext)
to protect against an attempt at reading too far- Specified by:
hasNext
in interfaceFileReader<CSVFileReader.CSVFileReaderContext>
- Parameters:
ctx
- the context initialized byFileReader.createParsingContext(InputStream, long)
- Returns:
- whether or not there are more data records to read
-