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 filefile name; mandatory if CsvReaderconstructor withoutReaderorCsvWriterconstructor withoutWriterparameter is usedA x charsetcharacter set name A x separatorcharacter which separates columns (default: tab [ x'09'])A x escapeCharcharacter which delimits text containing separator characters (default: there is no escape character) A x parserclass name of a RowParserimplementation (default:DefaultRowParser)A x trimglobal trim attribute for column content (default: false)A x decimalCharglobal 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 scalemaximum number of fractional digits in formatted decimal numbers (default: 2) A x datePatternglobal date format pattern as described in SimpleDateFormat(default:yyyy-MM-dd)A x trueValuevalue representing boolean true(default:true)A x falseValuevalue representing boolean false(default:false)A x nullValuereplacement for null(default: empty column isnull)A x headerflag whether there is a header row (default: false)A x autoColflag whether there is a header row and columns shall be automatically created according to the header row's content (default: false)A x headerMatchflag 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 rowFilterclass name of a RowFilterimplementationA x formatterclass name of a RowFormatterimplementation (default:DefaultRowFormatter)A x rowSeparatorseparator between rows (lines) used by CsvWriter(default: platform dependent JVM default)A x colcolumn 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 voidaddCol(ColConfig column)Adds a column definition.ColConfiggetColConfig(int position)Returns a column configuration for a column identified by position.ColConfiggetColConfig(String columnName)Returns a column configuration for a column identified by name.List<ColConfig>getColumnList()Returns a list of columns.intgetColumnPosition(String columnName)Returns the position of a column identified by name.SimpleDateFormatgetDateFormat()Returns the global date format.CharactergetDecimalChar()Returns the global decimal point character.CharactergetEscapeChar()Returns the column content delimiter character.StringgetFalseValue()Retrieves the representation of boolean valuefalse.FileConfiggetFile()Returns aFileConfig.RowFormattergetFormatter()Returns the formatter forCsvWriteroutput.StringgetNullValue()Retrieves the replacement fornullvalues.RowParsergetParser()Returns the parser.RowFiltergetRowFilter()Returns the row filter.StringgetRowSeparator()Returns the row separator needed forCsvWriter.IntegergetScale()Returns the global number of fractional digits for decimal numbers.CharactergetSeparator()Returns the column separator character.BooleangetTrim()Returns the global trim property.StringgetTrueValue()Retrieves the representation of boolean valuetrue.booleanhasHeaderRow()Tells whether the CSV input has a header row.booleanisAutoCol()Returns theautoColproperty.booleanisHeaderMatch()Returns theheaderMatchproperty.voidsetAutoCol(boolean autoCol)Sets theautoColproperty.voidsetCharset(String charset)Sets the charset.voidsetDatePattern(String datePattern)Sets the global date pattern.voidsetDecimalChar(Character decimalChar)Sets the global decimal point character.voidsetDefaultColConfig(ColConfig defaultColConfig)Sets a customized default column configuration.voidsetEscapeChar(Character escapeChar)Sets the character which delimits the column content.voidsetFalseValue(String falseValue)Sets the representation of boolean valuefalse.voidsetFile(String fileName)Sets the file name.voidsetFormatter(RowFormatter formatter)Sets the formatter forCsvWriteroutput.voidsetHeader(boolean header)Signals whether the CSV source or target has a header row.voidsetHeaderMatch(boolean headerMatch)Sets theheaderMatchproperty.voidsetNullValue(String nullValue)Sets the replacement value fornull.voidsetParser(RowParser parser)Sets the parser which separates columns.voidsetRowFilter(RowFilter rowFilter)Sets a row filter.voidsetRowSeparator(String rowSeparator)Sets the row separator needed forCsvWriter.voidsetScale(Integer scale)Sets the global number of fractional digits for decimal numbers.voidsetSeparator(Character separator)Sets the column separator character.voidsetTrim(Boolean trim)Sets the global trim property.voidsetTrueValue(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 isnullif 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 thefileattribute 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 istrueif any of theheader, theautoColor theheaderMatchproperties 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, aCsvReaderdoes not treat the first row's content as data, and aCsvWriterwill automatically write a header row.- Parameters:
header- flag wether there is a header row
-
setAutoCol
public void setAutoCol(boolean autoCol)
Sets theautoColproperty.If set to
true, aCsvReaderwill 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
ColConfigwith the same name has been already defined, it will not be replaced.An automatically added
ColConfiginstance 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 theautoColproperty.- Returns:
- flag
-
setHeaderMatch
public void setHeaderMatch(boolean headerMatch)
Sets theheaderMatchproperty. If set totrue, aCsvReaderwill identify the columns' position by header text. If there is any column with aheaderproperty 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 theheaderMatchproperty.- 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 forCsvWriteroutput.- Returns:
- formatter
-
setFormatter
public void setFormatter(RowFormatter formatter)
Sets the formatter forCsvWriteroutput. 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 fornullvalues. ACsvReaderreplaces the content of an empty column with this value. ACsvWriterreplacesnullby this value.- Returns:
- null value
-
setNullValue
public void setNullValue(String nullValue)
Sets the replacement value fornull. By default,nullvalues remainsnull(nothing).- Parameters:
nullValue- alternativenullrepresentation
-
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
-
-