Class 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 without Reader or CsvWriter constructor without Writer parameter is used A 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 is null) 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 implementation A 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 Detail

      • CsvConfig

        public CsvConfig()
        Constructor.
    • 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 is null 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 with 1; the position property of the default column is 0.
        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
      • 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 a DefaultRowParser.
        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 default Locale.

        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 is yyyy-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 is false.
        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 is true if any of the header, the autoCol or the headerMatch properties is true.
        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, a CsvReader does not treat the first row's content as data, and a CsvWriter will automatically write a header row.

        Parameters:
        header - flag wether there is a header row
      • setAutoCol

        public void setAutoCol​(boolean autoCol)
        Sets the autoCol property.

        If set to true, a CsvReader 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 the default column.

        Parameters:
        autoCol - flag wether there is a header row with column names
      • isAutoCol

        public boolean isAutoCol()
        Returns the autoCol property.
        Returns:
        flag
      • setHeaderMatch

        public void setHeaderMatch​(boolean headerMatch)
        Sets the headerMatch property. If set to true, a CsvReader will identify the columns' position by header text. If there is any column with a header 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 the headerMatch 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 for CsvWriter output.
        Returns:
        formatter
      • setFormatter

        public void setFormatter​(RowFormatter formatter)
        Sets the formatter for CsvWriter output. Default is DefaultRowFormatter.
        Parameters:
        formatter - formatter implementation
      • getRowSeparator

        public String getRowSeparator()
        Returns the row separator needed for CsvWriter.
        Returns:
        row separator character(s)
      • setRowSeparator

        public void setRowSeparator​(String rowSeparator)
        Sets the row separator needed for CsvWriter. Default is the platform dependent separator returned by System.getProperty("line.separator").
        Parameters:
        rowSeparator - line separator
      • getTrueValue

        public String getTrueValue()
        Retrieves the representation of boolean value true.
        Returns:
        true value
      • setTrueValue

        public void setTrueValue​(String trueValue)
        Sets the representation of boolean value true. Default is true.
        Parameters:
        trueValue - text representation of true
      • getFalseValue

        public String getFalseValue()
        Retrieves the representation of boolean value false.
        Returns:
        false value
      • setFalseValue

        public void setFalseValue​(String falseValue)
        Sets the representation of boolean value false.
        Parameters:
        falseValue - text representation of false
      • getNullValue

        public String getNullValue()
        Retrieves the replacement for null values. A CsvReader replaces the content of an empty column with this value. A CsvWriter replaces null by this value.
        Returns:
        null value
      • setNullValue

        public void setNullValue​(String nullValue)
        Sets the replacement value for null. By default, null values remains null (nothing).
        Parameters:
        nullValue - alternative null 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 is 2.
        Parameters:
        scale - scale of decimal numbers