Class CsvConfig
- java.lang.Object
-
- de.ufinke.cubaja.csv.CsvConfig
-
public class CsvConfig extends Object
Global configuration properties.
XML attributes and subelements Name Description A/E M U file
file name; mandatory if CsvReader
constructor withoutReader
orCsvWriter
constructor withoutWriter
parameter is usedA x charset
character set name A x separator
character which separates columns (default: tab [ x'09'
])A x escapeChar
character which delimits text containing separator characters (default: there is no escape character) A x parser
class name of a RowParser
implementation (default:DefaultRowParser
)A x trim
global trim attribute for column content (default: false
)A x decimalChar
global character for decimal point; may be a point or a comma (default for parsing: both point and comma are decimalChars; default for formatting: depends on Locale
)A x scale
maximum number of fractional digits in formatted decimal numbers (default: 2) A x datePattern
global date format pattern as described in SimpleDateFormat
(default:yyyy-MM-dd
)A x trueValue
value representing boolean true
(default:true
)A x falseValue
value representing boolean false
(default:false
)A x nullValue
replacement for null
(default: empty column isnull
)A x header
flag whether there is a header row (default: false
)A x autoCol
flag whether there is a header row and columns shall be automatically created according to the header row's content (default: false
)A x headerMatch
flag whether there is a header row and column positions depend on the position of their defined header text within the header row (default: false
)A x rowFilter
class name of a RowFilter
implementationA x formatter
class name of a RowFormatter
implementation (default:DefaultRowFormatter
)A x rowSeparator
separator between rows (lines) used by CsvWriter
(default: platform dependent JVM default)A x col
column definition (see ColConfig
)E A/E
: attribute or subelement
M
: mandatory
U
: unique- Author:
- Uwe Finke
-
-
Constructor Summary
Constructors Constructor Description CsvConfig()
Constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addCol(ColConfig column)
Adds a column definition.ColConfig
getColConfig(int position)
Returns a column configuration for a column identified by position.ColConfig
getColConfig(String columnName)
Returns a column configuration for a column identified by name.List<ColConfig>
getColumnList()
Returns a list of columns.int
getColumnPosition(String columnName)
Returns the position of a column identified by name.SimpleDateFormat
getDateFormat()
Returns the global date format.Character
getDecimalChar()
Returns the global decimal point character.Character
getEscapeChar()
Returns the column content delimiter character.String
getFalseValue()
Retrieves the representation of boolean valuefalse
.FileConfig
getFile()
Returns aFileConfig
.RowFormatter
getFormatter()
Returns the formatter forCsvWriter
output.String
getNullValue()
Retrieves the replacement fornull
values.RowParser
getParser()
Returns the parser.RowFilter
getRowFilter()
Returns the row filter.String
getRowSeparator()
Returns the row separator needed forCsvWriter
.Integer
getScale()
Returns the global number of fractional digits for decimal numbers.Character
getSeparator()
Returns the column separator character.Boolean
getTrim()
Returns the global trim property.String
getTrueValue()
Retrieves the representation of boolean valuetrue
.boolean
hasHeaderRow()
Tells whether the CSV input has a header row.boolean
isAutoCol()
Returns theautoCol
property.boolean
isHeaderMatch()
Returns theheaderMatch
property.void
setAutoCol(boolean autoCol)
Sets theautoCol
property.void
setCharset(String charset)
Sets the charset.void
setDatePattern(String datePattern)
Sets the global date pattern.void
setDecimalChar(Character decimalChar)
Sets the global decimal point character.void
setDefaultColConfig(ColConfig defaultColConfig)
Sets a customized default column configuration.void
setEscapeChar(Character escapeChar)
Sets the character which delimits the column content.void
setFalseValue(String falseValue)
Sets the representation of boolean valuefalse
.void
setFile(String fileName)
Sets the file name.void
setFormatter(RowFormatter formatter)
Sets the formatter forCsvWriter
output.void
setHeader(boolean header)
Signals whether the CSV source or target has a header row.void
setHeaderMatch(boolean headerMatch)
Sets theheaderMatch
property.void
setNullValue(String nullValue)
Sets the replacement value fornull
.void
setParser(RowParser parser)
Sets the parser which separates columns.void
setRowFilter(RowFilter rowFilter)
Sets a row filter.void
setRowSeparator(String rowSeparator)
Sets the row separator needed forCsvWriter
.void
setScale(Integer scale)
Sets the global number of fractional digits for decimal numbers.void
setSeparator(Character separator)
Sets the column separator character.void
setTrim(Boolean trim)
Sets the global trim property.void
setTrueValue(String trueValue)
Sets the representation of boolean valuetrue
.
-
-
-
Method Detail
-
setDefaultColConfig
public void setDefaultColConfig(ColConfig defaultColConfig)
Sets a customized default column configuration. A standard default column configuration is created by the constructor.- Parameters:
defaultColConfig
- config with default values
-
getColConfig
public ColConfig getColConfig(String columnName)
Returns a column configuration for a column identified by name. The result isnull
if there is no column with the given name.- Parameters:
columnName
- name of column- Returns:
- column config
-
getColConfig
public ColConfig getColConfig(int position)
Returns a column configuration for a column identified by position. Returns the default column configuration if there is no column with the given index. Position count starts with1
; the position property of the default column is0
.- Parameters:
position
- position of column- Returns:
- column config
-
getColumnPosition
public int getColumnPosition(String columnName) throws CsvException
Returns the position of a column identified by name.- Parameters:
columnName
- name of column- Returns:
- position position of column. Position count starts with
1
- Throws:
CsvException
- if name does not exist
-
setFile
public void setFile(String fileName)
Sets the file name.- Parameters:
fileName
- file path and name
-
setCharset
public void setCharset(String charset)
Sets the charset.- Parameters:
charset
- charset name
-
getFile
public FileConfig getFile() throws ConfigException
Returns aFileConfig
.- Returns:
- file config
- Throws:
ConfigException
- if thefile
attribute is not set
-
setSeparator
public void setSeparator(Character separator)
Sets the column separator character.- Parameters:
separator
- column separator
-
getSeparator
public Character getSeparator()
Returns the column separator character. The default separator is the tab character (\t
).- Returns:
- separator column separator
-
setEscapeChar
public void setEscapeChar(Character escapeChar)
Sets the character which delimits the column content. Typically, this is the quote character.- Parameters:
escapeChar
- column content delimiter
-
getEscapeChar
public Character getEscapeChar()
Returns the column content delimiter character. By default, no such character is defined.- Returns:
- column content delimiter char
-
setParser
public void setParser(RowParser parser)
Sets the parser which separates columns.- Parameters:
parser
- parser instance
-
getParser
public RowParser getParser()
Returns the parser. By default, this is aDefaultRowParser
.- Returns:
- parser instance
-
getDecimalChar
public Character getDecimalChar()
Returns the global decimal point character. By default, both point and comma are decimal point characters.For
CsvWriter
, the default decimal point character depends on the defaultLocale
.- Returns:
- decimal point charcter
-
setDecimalChar
public void setDecimalChar(Character decimalChar)
Sets the global decimal point character. Should be point or comma; other values may lead to unpredictable results.- Parameters:
decimalChar
- point or comma as decimal separator
-
setDatePattern
public void setDatePattern(String datePattern)
Sets the global date pattern.- Parameters:
datePattern
- date pattern. Example:dd.MM.yyyy
-
getDateFormat
public SimpleDateFormat getDateFormat()
Returns the global date format. The default pattern isyyyy-MM-dd
.- Returns:
- date format
-
getTrim
public Boolean getTrim()
Returns the global trim property. When set, column content is trimmed before further processing. Note that on read operations which parse numbers, the content is always trimmed. By default, the trim property isfalse
.- Returns:
- trim propery
-
setTrim
public void setTrim(Boolean trim)
Sets the global trim property.- Parameters:
trim
- flag wether to trim column content
-
hasHeaderRow
public boolean hasHeaderRow()
Tells whether the CSV input has a header row. This istrue
if any of theheader
, theautoCol
or theheaderMatch
properties istrue
.- Returns:
- flag
-
setHeader
public void setHeader(boolean header)
Signals whether the CSV source or target has a header row.When this attribute is set to
true
, aCsvReader
does not treat the first row's content as data, and aCsvWriter
will automatically write a header row.- Parameters:
header
- flag wether there is a header row
-
setAutoCol
public void setAutoCol(boolean autoCol)
Sets theautoCol
property.If set to
true
, aCsvReader
will configure columns automatically. The column names and positions are derived from the header row's content. Within a derived column name, non-Java characters are replaced by underlines. The derived column name is in lower case letters.If a
ColConfig
with the same name has been already defined, it will not be replaced.An automatically added
ColConfig
instance is of the same class as thedefault column
.- Parameters:
autoCol
- flag wether there is a header row with column names
-
isAutoCol
public boolean isAutoCol()
Returns theautoCol
property.- Returns:
- flag
-
setHeaderMatch
public void setHeaderMatch(boolean headerMatch)
Sets theheaderMatch
property. If set totrue
, aCsvReader
will identify the columns' position by header text. If there is any column with aheader
property and that header text is not found within the header row, an exception will be thrown.- Parameters:
headerMatch
- flag wether columns are identified by text in header row
-
isHeaderMatch
public boolean isHeaderMatch()
Returns theheaderMatch
property.- Returns:
- flag
-
getRowFilter
public RowFilter getRowFilter()
Returns the row filter. By default, there is no filter.- Returns:
- row filter
-
setRowFilter
public void setRowFilter(RowFilter rowFilter)
Sets a row filter.- Parameters:
rowFilter
- row filter implementation
-
addCol
public void addCol(ColConfig column)
Adds a column definition.- Parameters:
column
- config of new column
-
getColumnList
public List<ColConfig> getColumnList()
Returns a list of columns. The list is sorted by the column's position.- Returns:
- list
-
getFormatter
public RowFormatter getFormatter()
Returns the formatter forCsvWriter
output.- Returns:
- formatter
-
setFormatter
public void setFormatter(RowFormatter formatter)
Sets the formatter forCsvWriter
output. Default isDefaultRowFormatter
.- Parameters:
formatter
- formatter implementation
-
getRowSeparator
public String getRowSeparator()
Returns the row separator needed forCsvWriter
.- Returns:
- row separator character(s)
-
setRowSeparator
public void setRowSeparator(String rowSeparator)
Sets the row separator needed forCsvWriter
. Default is the platform dependent separator returned bySystem.getProperty("line.separator")
.- Parameters:
rowSeparator
- line separator
-
getTrueValue
public String getTrueValue()
Retrieves the representation of boolean valuetrue
.- Returns:
- true value
-
setTrueValue
public void setTrueValue(String trueValue)
Sets the representation of boolean valuetrue
. Default istrue
.- Parameters:
trueValue
- text representation oftrue
-
getFalseValue
public String getFalseValue()
Retrieves the representation of boolean valuefalse
.- Returns:
- false value
-
setFalseValue
public void setFalseValue(String falseValue)
Sets the representation of boolean valuefalse
.- Parameters:
falseValue
- text representation offalse
-
getNullValue
public String getNullValue()
Retrieves the replacement fornull
values. ACsvReader
replaces the content of an empty column with this value. ACsvWriter
replacesnull
by this value.- Returns:
- null value
-
setNullValue
public void setNullValue(String nullValue)
Sets the replacement value fornull
. By default,null
values remainsnull
(nothing).- Parameters:
nullValue
- alternativenull
representation
-
getScale
public Integer getScale()
Returns the global number of fractional digits for decimal numbers.- Returns:
- scale
-
setScale
public void setScale(Integer scale)
Sets the global number of fractional digits for decimal numbers. Default is2
.- Parameters:
scale
- scale of decimal numbers
-
-