T
- Entity type that we are processing. It should have a public no-arg constructor so it can be created by
this library.public class CsvProcessor<T> extends Object
NOTE: You need to set the entity class either in the constructor or with setEntityClass(Class)
or
withEntityClass(Class)
. Then you need to do any registerConverter(Class, Converter)
or
withConverter(Class, Converter)
calls before the processor gets configured. You can then let the processor
be auto-configured on the first read/write method call or call initialize()
directly (or in spring).
Modifier and Type | Field and Description |
---|---|
static char |
DEFAULT_COLUMN_QUOTE
Default quote character for columns to wrap them if they have special characters.
|
static char |
DEFAULT_COLUMN_SEPARATOR
Default separator character for columns.
|
static String |
DEFAULT_LINE_TERMINATION
Default line termination string to be written at the end of CSV lines.
|
Constructor and Description |
---|
CsvProcessor() |
CsvProcessor(Class<T> entityClass)
Constructs a processor with an entity class whose fields should be marked with
CsvColumn annotations. |
Modifier and Type | Method and Description |
---|---|
String |
buildHeaderLine(boolean appendLineTermination)
Build and return a header string made up of quoted column names.
|
String |
buildLine(T entity,
boolean appendLineTermination)
Convert the entity into a string of column values.
|
CsvProcessor<T> |
initialize()
This initializing the internal configuration information.
|
String[] |
processHeader(String line,
ParseError parseError)
Process a header line and divide it up into a series of quoted columns.
|
T |
processRow(String line,
ParseError parseError)
Read and process a line and return the associated entity.
|
List<T> |
readAll(File file,
Collection<ParseError> parseErrors)
Read in all of the entities in the file passed in.
|
List<T> |
readAll(Reader reader,
Collection<ParseError> parseErrors)
Read in all of the entities in the reader passed in.
|
String[] |
readHeader(BufferedReader bufferedReader,
ParseError parseError)
Read in a line and process it as a CSV header.
|
T |
readRow(BufferedReader bufferedReader,
ParseError parseError)
Read an entity line from the reader.
|
List<T> |
readRows(BufferedReader bufferedReader,
Collection<ParseError> parseErrors)
Read in all of the entities in the reader passed in but without the header.
|
<FT> void |
registerConverter(Class<FT> clazz,
Converter<FT,?> converter)
Register a converter class for all instances of the class argument.
|
void |
setAllowPartialLines(boolean allowPartialLines)
Set to true to allow lines that do not have values for all of the columns.
|
void |
setAlwaysTrimInput(boolean alwaysTrimInput)
Set to true to always call
String.trim() on data input columns to remove any spaces from the start or
end. |
void |
setColumnNameMatcher(ColumnNameMatcher columnNameMatcher)
Set the column name matcher class which will be used to see if the column from the CSV file matches the
definition name.
|
void |
setColumnQuote(char columnQuote)
Quote character that is used to wrap each column.
|
void |
setColumnSeparator(char columnSeparator)
String that separates columns in out CSV input and output.
|
void |
setConstructorCallable(Callable<T> constructorCallable)
Set the a method that will construct the entity we are loading.
|
void |
setEntityClass(Class<T> entityClass)
Class that we are processing.
|
void |
setFirstLineHeader(boolean firstLineHeader)
Set to false if the first line is a header line to be processed.
|
void |
setFlexibleOrder(boolean flexibleOrder)
Set to true if the order of the input columns is flexible and does not have to match the order of the definition
fields in the entity.
|
void |
setHeaderValidation(boolean headerValidation)
Set to false to not validate the header when it is read in.
|
void |
setIgnoreUnknownColumns(boolean ignoreUnknownColumns)
Set to true to ignore columns that are not know to the configuration.
|
void |
setLineTermination(String lineTermination)
Sets the character which is written at the end of the row.
|
void |
setRowValidator(RowValidator<T> rowValidator)
Set the validator which will validate each entity after it has been parsed.
|
boolean |
validateHeader(String line,
ParseError parseError)
Validate the header row against the configured header columns.
|
boolean |
validateHeaderColumns(String[] columns,
ParseError parseError)
Validate header columns returned by
processHeader(String, ParseError) . |
CsvProcessor<T> |
withAllowPartialLines(boolean allowPartialLines)
Set to true to allow lines that do not have values for all of the columns.
|
CsvProcessor<T> |
withAlwaysTrimInput(boolean alwaysTrimInput)
Set to true to always call
String.trim() on data input columns to remove any spaces from the start or
end. |
CsvProcessor<T> |
withColumnNameMatcher(ColumnNameMatcher columnNameMatcher)
Set the column name matcher class which will be used to see if the column from the CSV file matches the
definition name.
|
CsvProcessor<T> |
withColumnQuote(char columnQuote)
Quote character that is used to wrap each column.
|
CsvProcessor<T> |
withColumnSeparator(char columnSeparator)
String that separates columns in out CSV input and output.
|
CsvProcessor<T> |
withConstructorCallable(Callable<T> constructorCallable)
Set the a method that will construct the entity we are loading.
|
<FT> CsvProcessor<T> |
withConverter(Class<FT> clazz,
Converter<FT,?> converter)
Register a converter class for all instances of the class argument.
|
CsvProcessor<T> |
withEntityClass(Class<T> entityClass)
Class that we are processing.
|
CsvProcessor<T> |
withFirstLineHeader(boolean firstLineHeader)
Set to false if the first line is a header line to be processed.
|
CsvProcessor<T> |
withFlexibleOrder(boolean flexibleOrder)
Set to true if the order of the input columns is flexible and does not have to match the order of the definition
fields in the entity.
|
CsvProcessor<T> |
withHeaderValidation(boolean headerValidation)
Set to false to not validate the header when it is read in.
|
CsvProcessor<T> |
withIgnoreUnknownColumns(boolean ignoreUnknownColumns)
Set to true to ignore columns that are not know to the configuration.
|
CsvProcessor<T> |
withLineTermination(String lineTermination)
Sets the character which is written at the end of the row.
|
CsvProcessor<T> |
withRowValidator(RowValidator<T> rowValidator)
Set the validator which will validate each entity after it has been parsed.
|
void |
writeAll(File file,
Collection<T> entities,
boolean writeHeader)
Write a collection of entities to the writer.
|
void |
writeAll(Writer writer,
Collection<T> entities,
boolean writeHeader)
Write a header and then the collection of entities to the writer.
|
void |
writeHeader(BufferedWriter bufferedWriter,
boolean appendLineTermination)
Write the header line to the writer.
|
void |
writeRow(BufferedWriter bufferedWriter,
T entity,
boolean appendLineTermination)
Write an entity row to the writer.
|
public static final char DEFAULT_COLUMN_SEPARATOR
setColumnSeparator(char)
.public static final char DEFAULT_COLUMN_QUOTE
setColumnQuote(char)
.public static final String DEFAULT_LINE_TERMINATION
setLineTermination(String)
.public <FT> void registerConverter(Class<FT> clazz, Converter<FT,?> converter)
CsvColumn.converterClass()
annotation field.public <FT> CsvProcessor<T> withConverter(Class<FT> clazz, Converter<FT,?> converter)
CsvColumn.converterClass()
annotation field. Alternative way to do
registerConverter(Class, Converter)
.public CsvProcessor<T> initialize()
public List<T> readAll(File file, Collection<ParseError> parseErrors) throws IOException, ParseException
file
- Where to read the header and entities from. It will be closed when the method returns.parseErrors
- If not null, any errors will be added to the collection and null will be returned. If validateHeader
is true and the header does not match then no additional lines will be returned. If this is null then
a ParseException will be thrown on parsing problems.ParseException
- Thrown on any parsing problems. If parseErrors is not null then parse errors will be added there and
an exception should not be thrown.IOException
- If there are any IO exceptions thrown when reading.public List<T> readAll(Reader reader, Collection<ParseError> parseErrors) throws IOException, ParseException
reader
- Where to read the header and entities from. It will be closed when the method returns.parseErrors
- If not null, any errors will be added to the collection and null will be returned. If validateHeader
is true and the header does not match then no additional lines will be returned. If this is null then
a ParseException will be thrown on parsing problems.ParseException
- Thrown on any parsing problems. If parseErrors is not null then parse errors will be added there and
an exception should not be thrown.IOException
- If there are any IO exceptions thrown when reading.public String[] readHeader(BufferedReader bufferedReader, ParseError parseError) throws ParseException, IOException
bufferedReader
- Where to read the header from. It needs to be closed by the caller. Consider using
BufferedReaderLineCounter
to populate the line-number for parse errors.parseError
- If not null, this will be set with the first parse error and it will return null. If this is null then
a ParseException will be thrown instead.ParseException
- Thrown on any parsing problems. If parseError is not null then the error will be added there and an
exception should not be thrown.IOException
- If there are any IO exceptions thrown when reading.public List<T> readRows(BufferedReader bufferedReader, Collection<ParseError> parseErrors) throws IOException, ParseException
bufferedReader
- Where to read the entries from. It needs to be closed by the caller. Consider using
BufferedReaderLineCounter
to populate the line-number for parse errors.parseErrors
- If not null, any errors will be added to the collection and null will be returned. If validateHeader
is true and the header does not match then no additional lines will be returned. If this is null then
a ParseException will be thrown on parsing problems.ParseException
- Thrown on any parsing problems. If parseErrors is not null then parse errors will be added there and
an exception should not be thrown.IOException
- If there are any IO exceptions thrown when reading.public T readRow(BufferedReader bufferedReader, ParseError parseError) throws ParseException, IOException
bufferedReader
- Where to read the row from. It needs to be closed by the caller. Consider using
BufferedReaderLineCounter
to populate the line-number for parse errors.parseError
- If not null, this will be set with the first parse error and it will return null. If this is null then
a ParseException will be thrown instead.ParseError.isError()
to see if it was an error or
EOF.ParseException
- Thrown on any parsing problems. If parseError is not null then the error will be added there and an
exception should not be thrown.IOException
- If there are any IO exceptions thrown when reading.public boolean validateHeader(String line, ParseError parseError) throws ParseException
line
- Line to process to get our validate our header.parseError
- If not null, this will be set with the first parse error and it will return null. If this is null then
a ParseException will be thrown instead.ParseException
- Thrown on any parsing problems. If parseError is not null then the error will be added there and an
exception should not be thrown.public boolean validateHeaderColumns(String[] columns, ParseError parseError)
processHeader(String, ParseError)
.columns
- Array of columns to validate.parseError
- If not null, this will be set with the first parse error and it will return null. If this is null then
a ParseException will be thrown instead.ParseException
- Thrown on any parsing problems. If parseError is not null then the error will be added there and an
exception should not be thrown.public String[] processHeader(String line, ParseError parseError) throws ParseException
line
- Line to process looking for header.parseError
- If not null, this will be set with the first parse error and it will return null. If this is null then
a ParseException will be thrown instead.ParseException
- Thrown on any parsing problems. If parseError is not null then the error will be added there and an
exception should not be thrown.public T processRow(String line, ParseError parseError) throws ParseException
line
- to process to build our entity.parseError
- If not null, this will be set with the first parse error and it will return null. If this is null then
a ParseException will be thrown instead.ParseException
- Thrown on any parsing problems. If parseError is not null then the error will be added there and an
exception should not be thrown.public void writeAll(File file, Collection<T> entities, boolean writeHeader) throws IOException
file
- Where to write the header and entities.entities
- Collection of entities to write to the writer.writeHeader
- Set to true to write header at the start of the output file.IOException
- If there are any IO exceptions thrown when writing.public void writeAll(Writer writer, Collection<T> entities, boolean writeHeader) throws IOException
writer
- Where to write the header and entities. It will be closed before this method returns.entities
- Collection of entities to write to the writer.writeHeader
- Set to true to write header at the start of the writer.IOException
- If there are any IO exceptions thrown when writing.public void writeHeader(BufferedWriter bufferedWriter, boolean appendLineTermination) throws IOException
bufferedWriter
- Where to write our header information.appendLineTermination
- Set to true to add the newline to the end of the line.IOException
- If there are any IO exceptions thrown when writing.public void writeRow(BufferedWriter bufferedWriter, T entity, boolean appendLineTermination) throws IOException
bufferedWriter
- Where to write our header information.entity
- The entity we are writing to the buffered writer.appendLineTermination
- Set to true to add the newline to the end of the line.IOException
- If there are any IO exceptions thrown when writing.public String buildHeaderLine(boolean appendLineTermination)
appendLineTermination
- Set to true to add the newline to the end of the line.public String buildLine(T entity, boolean appendLineTermination)
appendLineTermination
- Set to true to add the newline to the end of the line.public CsvProcessor<T> withEntityClass(Class<T> entityClass)
setEntityClass(Class)
.public void setConstructorCallable(Callable<T> constructorCallable)
public CsvProcessor<T> withConstructorCallable(Callable<T> constructorCallable)
setConstructorCallable(Callable)
.public void setColumnSeparator(char columnSeparator)
public CsvProcessor<T> withColumnSeparator(char columnSeparator)
setColumnSeparator(char)
.public void setColumnQuote(char columnQuote)
public CsvProcessor<T> withColumnQuote(char columnQuote)
setColumnQuote(char)
.public void setLineTermination(String lineTermination)
public CsvProcessor<T> withLineTermination(String lineTermination)
setLineTermination(String)
.public void setAllowPartialLines(boolean allowPartialLines)
public CsvProcessor<T> withAllowPartialLines(boolean allowPartialLines)
setAllowPartialLines(boolean)
.public void setAlwaysTrimInput(boolean alwaysTrimInput)
String.trim()
on data input columns to remove any spaces from the start or
end.public CsvProcessor<T> withAlwaysTrimInput(boolean alwaysTrimInput)
String.trim()
on data input columns to remove any spaces from the start or
end. Alternative way to do setAlwaysTrimInput(boolean)
.public void setHeaderValidation(boolean headerValidation)
public CsvProcessor<T> withHeaderValidation(boolean headerValidation)
public void setFirstLineHeader(boolean firstLineHeader)
public CsvProcessor<T> withFirstLineHeader(boolean firstLineHeader)
public void setColumnNameMatcher(ColumnNameMatcher columnNameMatcher)
String.equals(Object)
.public CsvProcessor<T> withColumnNameMatcher(ColumnNameMatcher columnNameMatcher)
String.equals(Object)
.public void setFlexibleOrder(boolean flexibleOrder)
public CsvProcessor<T> withFlexibleOrder(boolean flexibleOrder)
public void setIgnoreUnknownColumns(boolean ignoreUnknownColumns)
public CsvProcessor<T> withIgnoreUnknownColumns(boolean ignoreUnknownColumns)
public void setRowValidator(RowValidator<T> rowValidator)
public CsvProcessor<T> withRowValidator(RowValidator<T> rowValidator)
This documentation content is licensed by Gray Watson under the Creative Commons Attribution-Share Alike 3.0 License.