Class CsvToBeanBuilder<T>
- java.lang.Object
-
- com.opencsv.bean.CsvToBeanBuilder<T>
-
- Type Parameters:
T
- Type of the bean to be populated
public class CsvToBeanBuilder<T> extends java.lang.Object
This class makes it possible to bypass all the intermediate steps and classes in setting up to read from a CSV source to a list of beans.This is the place to start if you're reading a CSV source into beans, especially if you're binding the input's columns to the bean's variables using the annotations
CsvBindByName
,CsvCustomBindByName
,CsvBindByPosition
, orCsvCustomBindByPosition
.If you want nothing but defaults for the entire import, your code can look as simple as this, where
myreader
is any validReader
:
List<MyBean> result = new CsvToBeanBuilder(myreader).withType(MyBean.class).build().parse();
This builder is intelligent enough to guess the mapping strategy according to the following strategy:
- If a mapping strategy is explicitly set, it is always used.
- If
CsvBindByPosition
orCsvCustomBindByPosition
is present,ColumnPositionMappingStrategy
is used. - Otherwise,
HeaderColumnNameMappingStrategy
is used. This includes the case whenCsvBindByName
orCsvCustomBindByName
are being used. The annotations will automatically be recognized.
- Since:
- 3.9
-
-
Field Summary
Fields Modifier and Type Field Description private CSVReader
csvReader
Allow the user to pass in a prebuilt/customCSVReader
.private java.util.Locale
errorLocale
private java.lang.Character
escapeChar
private CsvExceptionHandler
exceptionHandler
private CsvToBeanFilter
filter
private org.apache.commons.collections4.ListValuedMap<java.lang.Class<?>,java.lang.reflect.Field>
ignoredFields
private boolean
ignoreEmptyLines
private java.lang.Boolean
ignoreLeadingWhiteSpace
private java.lang.Boolean
ignoreQuotations
private boolean
keepCR
private MappingStrategy<? extends T>
mappingStrategy
private java.lang.Integer
multilineLimit
private CSVReaderNullFieldIndicator
nullFieldIndicator
private boolean
orderedResults
private java.lang.String
profile
private java.lang.Character
quoteChar
private java.io.Reader
reader
A CSVReader will be built out of thisReader
.private java.lang.Character
separator
private java.lang.Integer
skipLines
private java.lang.Boolean
strictQuotes
private java.lang.Boolean
throwsExceptions
private java.lang.Class<? extends T>
type
private java.util.List<BeanVerifier<T>>
verifiers
private java.lang.Boolean
verifyReader
-
Constructor Summary
Constructors Constructor Description CsvToBeanBuilder(CSVReader csvReader)
Constructor with the one parameter that is most definitely mandatory, and always will be.CsvToBeanBuilder(java.io.Reader reader)
Constructor with the one parameter that is most definitely mandatory, and always will be.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description CsvToBean<T>
build()
Builds theCsvToBean
out of the provided information.private CSVParser
buildParser()
Builds aCSVParser
from the information provided to this builder.private CSVReader
buildReader(CSVParser parser)
Builds aCSVReader
from the information provided to this builder.CsvToBeanBuilder<T>
withErrorLocale(java.util.Locale errorLocale)
Sets the locale for all error messages.CsvToBeanBuilder<T>
withEscapeChar(char escapeChar)
CsvToBeanBuilder<T>
withExceptionHandler(CsvExceptionHandler exceptionHandler)
Sets the handler for recoverable exceptions raised during processing of records.CsvToBeanBuilder<T>
withFieldAsNull(CSVReaderNullFieldIndicator indicator)
CsvToBeanBuilder<T>
withFilter(CsvToBeanFilter filter)
CsvToBeanBuilder<T>
withIgnoreEmptyLine(boolean ignore)
CsvToBeanBuilder<T>
withIgnoreField(java.lang.Class<?> type, java.lang.reflect.Field field)
Adds aField
to the list of fields opencsv should ignore completely.CsvToBeanBuilder<T>
withIgnoreLeadingWhiteSpace(boolean ignoreLeadingWhiteSpace)
CsvToBeanBuilder<T>
withIgnoreQuotations(boolean ignoreQuotations)
CsvToBeanBuilder<T>
withKeepCarriageReturn(boolean keepCR)
CsvToBeanBuilder<T>
withMappingStrategy(MappingStrategy<? extends T> mappingStrategy)
CsvToBeanBuilder<T>
withMultilineLimit(int multilineLimit)
Sets the maximum number of lines allowed in a multiline record.CsvToBeanBuilder<T>
withOrderedResults(boolean orderedResults)
Sets whether the resulting beans must be ordered as in the input.CsvToBeanBuilder<T>
withProfile(java.lang.String profile)
Selects a profile for deciding which configurations to use for the bean fields.CsvToBeanBuilder<T>
withQuoteChar(char quoteChar)
CsvToBeanBuilder<T>
withSeparator(char separator)
CsvToBeanBuilder<T>
withSkipLines(int skipLines)
CsvToBeanBuilder<T>
withStrictQuotes(boolean strictQuotes)
CsvToBeanBuilder<T>
withThrowExceptions(boolean throwExceptions)
Sets how the CsvToBean will act when an exception occurs.CsvToBeanBuilder<T>
withType(java.lang.Class<? extends T> type)
Sets the type of the bean to be populated.CsvToBeanBuilder<T>
withVerifier(BeanVerifier<T> verifier)
Adds aBeanVerifier
to the list of verifiers to run on all beans created.CsvToBeanBuilder<T>
withVerifyReader(boolean verifyReader)
-
-
-
Field Detail
-
mappingStrategy
private MappingStrategy<? extends T> mappingStrategy
- See Also:
CsvToBean.mappingStrategy
-
reader
private final java.io.Reader reader
A CSVReader will be built out of thisReader
.- See Also:
CsvToBean.csvReader
-
filter
private CsvToBeanFilter filter
- See Also:
CsvToBean.filter
-
exceptionHandler
private CsvExceptionHandler exceptionHandler
- See Also:
CsvToBean#throwExceptions
-
nullFieldIndicator
private CSVReaderNullFieldIndicator nullFieldIndicator
- See Also:
AbstractCSVParser.nullFieldIndicator
-
keepCR
private boolean keepCR
- See Also:
CSVReader.keepCR
-
skipLines
private java.lang.Integer skipLines
- See Also:
CSVReader.skipLines
-
verifyReader
private java.lang.Boolean verifyReader
- See Also:
CSVReader.verifyReader
-
separator
private java.lang.Character separator
- See Also:
AbstractCSVParser.separator
-
quoteChar
private java.lang.Character quoteChar
- See Also:
AbstractCSVParser.quotechar
-
escapeChar
private java.lang.Character escapeChar
- See Also:
CSVParser.escape
-
strictQuotes
private java.lang.Boolean strictQuotes
- See Also:
CSVParser.strictQuotes
-
ignoreLeadingWhiteSpace
private java.lang.Boolean ignoreLeadingWhiteSpace
- See Also:
CSVParser.ignoreLeadingWhiteSpace
-
ignoreQuotations
private java.lang.Boolean ignoreQuotations
- See Also:
CSVParser.ignoreQuotations
-
throwsExceptions
private java.lang.Boolean throwsExceptions
- See Also:
CsvToBean.setThrowExceptions(boolean)
-
type
private java.lang.Class<? extends T> type
- See Also:
AbstractMappingStrategy.type
-
multilineLimit
private java.lang.Integer multilineLimit
- See Also:
CSVReader.multilineLimit
-
orderedResults
private boolean orderedResults
- See Also:
CsvToBean.orderedResults
-
ignoreEmptyLines
private boolean ignoreEmptyLines
- See Also:
CsvToBean.ignoreEmptyLines
-
errorLocale
private java.util.Locale errorLocale
- See Also:
CsvToBean.errorLocale
-
verifiers
private final java.util.List<BeanVerifier<T>> verifiers
- See Also:
CsvToBean.verifiers
-
ignoredFields
private final org.apache.commons.collections4.ListValuedMap<java.lang.Class<?>,java.lang.reflect.Field> ignoredFields
- See Also:
AbstractMappingStrategy.ignoredFields
-
profile
private java.lang.String profile
- See Also:
AbstractMappingStrategy.profile
-
-
Constructor Detail
-
CsvToBeanBuilder
public CsvToBeanBuilder(java.io.Reader reader)
Constructor with the one parameter that is most definitely mandatory, and always will be.- Parameters:
reader
- The reader that is the source of data for the CSV import
-
CsvToBeanBuilder
public CsvToBeanBuilder(CSVReader csvReader)
Constructor with the one parameter that is most definitely mandatory, and always will be.- Parameters:
csvReader
- The CSVReader that is the source of data for the CSV import
-
-
Method Detail
-
build
public CsvToBean<T> build() throws java.lang.IllegalStateException
Builds theCsvToBean
out of the provided information.- Returns:
- A valid
CsvToBean
- Throws:
java.lang.IllegalStateException
- If a necessary parameter was not specified. Currently this means that both the mapping strategy and the bean type are not set, so it is impossible to determine a mapping strategy.
-
buildParser
private CSVParser buildParser()
Builds aCSVParser
from the information provided to this builder. This is an intermediate step in building theCsvToBean
.- Returns:
- An appropriate
CSVParser
-
withMappingStrategy
public CsvToBeanBuilder<T> withMappingStrategy(MappingStrategy<? extends T> mappingStrategy)
- Parameters:
mappingStrategy
- Please see the "See Also" section- Returns:
this
- See Also:
CsvToBean.setMappingStrategy(com.opencsv.bean.MappingStrategy)
-
withFilter
public CsvToBeanBuilder<T> withFilter(CsvToBeanFilter filter)
- Parameters:
filter
- Please see the "See Also" section- Returns:
this
- See Also:
CsvToBean.setFilter(com.opencsv.bean.CsvToBeanFilter)
-
withThrowExceptions
public CsvToBeanBuilder<T> withThrowExceptions(boolean throwExceptions)
Sets how the CsvToBean will act when an exception occurs. If both withThrowsExcpetion andwithExceptionHandler(CsvExceptionHandler)
are used then the withExceptionHandler takes precedence and is used.- Parameters:
throwExceptions
- Please see the "See Also" section- Returns:
this
- See Also:
CsvToBean.setThrowExceptions(boolean)
,withExceptionHandler(CsvExceptionHandler)
-
withExceptionHandler
public CsvToBeanBuilder<T> withExceptionHandler(CsvExceptionHandler exceptionHandler)
Sets the handler for recoverable exceptions raised during processing of records. If bothwithThrowExceptions(boolean)
and withExceptionHandler are used then the withExceptionHandler takes precedence and is used.If neither this method nor
withThrowExceptions(boolean)
is called, the default exception handler isExceptionHandlerThrow
.Please note that if both this method and
withThrowExceptions(boolean)
are called, the last call wins.- Parameters:
exceptionHandler
- The exception handler to be used. Ifnull
, this method does nothing.- Returns:
this
- Since:
- 5.2
-
withFieldAsNull
public CsvToBeanBuilder<T> withFieldAsNull(CSVReaderNullFieldIndicator indicator)
- Parameters:
indicator
- Which field content will be returned as null: EMPTY_SEPARATORS, EMPTY_QUOTES, BOTH, NEITHER (default)- Returns:
this
-
withKeepCarriageReturn
public CsvToBeanBuilder<T> withKeepCarriageReturn(boolean keepCR)
- Parameters:
keepCR
- True to keep carriage returns in data read, false otherwise- Returns:
this
-
withVerifyReader
public CsvToBeanBuilder<T> withVerifyReader(boolean verifyReader)
- Parameters:
verifyReader
- Please see the "See Also" section- Returns:
this
- See Also:
CSVReaderBuilder.withVerifyReader(boolean)
-
withSkipLines
public CsvToBeanBuilder<T> withSkipLines(int skipLines)
- Parameters:
skipLines
- Please see the "See Also" section- Returns:
this
- See Also:
CSVReaderBuilder.withSkipLines(int)
-
withSeparator
public CsvToBeanBuilder<T> withSeparator(char separator)
- Parameters:
separator
- Please see the "See Also" section- Returns:
this
- See Also:
CSVParser(char, char, char, boolean, boolean, boolean, CSVReaderNullFieldIndicator, Locale)
-
withQuoteChar
public CsvToBeanBuilder<T> withQuoteChar(char quoteChar)
- Parameters:
quoteChar
- Please see the "See Also" section- Returns:
this
- See Also:
CSVParser(char, char, char, boolean, boolean, boolean, CSVReaderNullFieldIndicator, Locale)
-
withEscapeChar
public CsvToBeanBuilder<T> withEscapeChar(char escapeChar)
- Parameters:
escapeChar
- Please see the "See Also" section- Returns:
this
- See Also:
CSVParser(char, char, char, boolean, boolean, boolean, CSVReaderNullFieldIndicator, Locale)
-
withStrictQuotes
public CsvToBeanBuilder<T> withStrictQuotes(boolean strictQuotes)
- Parameters:
strictQuotes
- Please see the "See Also" section- Returns:
this
- See Also:
CSVParser(char, char, char, boolean, boolean, boolean, CSVReaderNullFieldIndicator, Locale)
-
withIgnoreLeadingWhiteSpace
public CsvToBeanBuilder<T> withIgnoreLeadingWhiteSpace(boolean ignoreLeadingWhiteSpace)
- Parameters:
ignoreLeadingWhiteSpace
- Please see the "See Also" section- Returns:
this
- See Also:
CSVParser(char, char, char, boolean, boolean, boolean, CSVReaderNullFieldIndicator, Locale)
-
withIgnoreQuotations
public CsvToBeanBuilder<T> withIgnoreQuotations(boolean ignoreQuotations)
- Parameters:
ignoreQuotations
- Please see the "See Also" section- Returns:
this
- See Also:
CSVParser(char, char, char, boolean, boolean, boolean, CSVReaderNullFieldIndicator, Locale)
-
withType
public CsvToBeanBuilder<T> withType(java.lang.Class<? extends T> type)
Sets the type of the bean to be populated. Ignored ifwithMappingStrategy(com.opencsv.bean.MappingStrategy)
is called.- Parameters:
type
- Class of the destination bean- Returns:
this
- See Also:
AbstractMappingStrategy.setType(java.lang.Class)
,AbstractMappingStrategy.setType(java.lang.Class)
-
withMultilineLimit
public CsvToBeanBuilder<T> withMultilineLimit(int multilineLimit)
Sets the maximum number of lines allowed in a multiline record. More than this number in one record results in an IOException.- Parameters:
multilineLimit
- No more than this number of lines is allowed in a single input record. The default isCSVReader.DEFAULT_MULTILINE_LIMIT
.- Returns:
this
-
withOrderedResults
public CsvToBeanBuilder<T> withOrderedResults(boolean orderedResults)
Sets whether the resulting beans must be ordered as in the input.- Parameters:
orderedResults
- Whether to order the results or not- Returns:
this
- Since:
- 4.0
- See Also:
CsvToBean.setOrderedResults(boolean)
-
withErrorLocale
public CsvToBeanBuilder<T> withErrorLocale(java.util.Locale errorLocale)
Sets the locale for all error messages.- Parameters:
errorLocale
- Locale for error messages- Returns:
this
- Since:
- 4.0
- See Also:
CsvToBean.setErrorLocale(java.util.Locale)
-
withVerifier
public CsvToBeanBuilder<T> withVerifier(BeanVerifier<T> verifier)
Adds aBeanVerifier
to the list of verifiers to run on all beans created. This method may be called as many times as desired. All added verifiers will be run on every bean. No guarantee is made as to the order in which the verifiers are run.- Parameters:
verifier
- A new verifier that is to process all beans after creation.null
is permissible but has no effect.- Returns:
this
- Since:
- 4.4
-
withIgnoreField
public CsvToBeanBuilder<T> withIgnoreField(java.lang.Class<?> type, java.lang.reflect.Field field) throws java.lang.IllegalArgumentException
Adds aField
to the list of fields opencsv should ignore completely.May be called as many times as necessary.
- Parameters:
type
- The class opencsv will encounter the field in during processing. In the case of inheritance, this may not be the declaring class.field
- The field opencsv is to ignore- Returns:
this
- Throws:
java.lang.IllegalArgumentException
- If one of the parameters isnull
orfield
cannot be found intype
.- Since:
- 5.0
- See Also:
MappingStrategy.ignoreFields(MultiValuedMap)
-
withIgnoreEmptyLine
public CsvToBeanBuilder<T> withIgnoreEmptyLine(boolean ignore)
- Parameters:
ignore
- Please see the "See Also" section- Returns:
this
- See Also:
CsvToBean.ignoreEmptyLines
-
withProfile
public CsvToBeanBuilder<T> withProfile(java.lang.String profile)
Selects a profile for deciding which configurations to use for the bean fields.- Parameters:
profile
- The name of the profile to be used- Returns:
this
- Since:
- 5.4
-
-