Class CsvWriterSettings

  • All Implemented Interfaces:
    java.lang.Cloneable

    public class CsvWriterSettings
    extends CommonWriterSettings<CsvFormat>
    This is the configuration class used by the CSV writer (CsvWriter)

    In addition to the configuration options provided by CommonWriterSettings, the CsvWriterSettings include:

    • emptyValue (defaults to null): Defines a replacement string to signify an empty value (which is not a null value)

      If the writer has an empty String to write to the output, the emptyValue is used instead of an empty string

    • quoteAllFields (defaults to false): By default, only values that contain a field separator are enclosed within quotes.

      If this property is set to true, this indicates that all written values should be enclosed within quotes (as defined in CsvFormat)

    See Also:
    CsvWriter, CsvFormat, CommonWriterSettings
    • Field Detail

      • escapeUnquotedValues

        private boolean escapeUnquotedValues
      • quoteAllFields

        private boolean quoteAllFields
      • isInputEscaped

        private boolean isInputEscaped
      • normalizeLineEndingsWithinQuotes

        private boolean normalizeLineEndingsWithinQuotes
      • quotationTriggers

        private char[] quotationTriggers
      • quoteEscapingEnabled

        private boolean quoteEscapingEnabled
      • quoteNulls

        private boolean quoteNulls
    • Constructor Detail

      • CsvWriterSettings

        public CsvWriterSettings()
    • Method Detail

      • getQuoteAllFields

        public boolean getQuoteAllFields()
        Indicates that all written values should be enclosed within quotes (as defined in CsvFormat)

        (Defaults to false)

        Returns:
        true if all written values should be enclosed within quotes, false otherwise
      • setQuoteAllFields

        public void setQuoteAllFields​(boolean quoteAllFields)
        Indicates indicates whether or not all written values should be enclosed within quotes (as defined in CsvFormat)

        (Defaults to false)

        By default, only values that contain a field separator are enclosed within quotes.

        Parameters:
        quoteAllFields - a flag indicating whether to enclose all fields within quotes
      • isEscapeUnquotedValues

        public boolean isEscapeUnquotedValues()
        Indicates whether escape sequences should be written in unquoted values. Defaults to false.

        By default, this is disabled and if the input is A""B,C, the resulting value will be [A""B] and [C] (i.e. the content is written as-is). However, if the writer is configured to process escape sequences in unquoted values, the values will be written as [A""""B] and [C]

        Returns:
        true if escape sequences should be processed in unquoted values, otherwise false
      • setEscapeUnquotedValues

        public void setEscapeUnquotedValues​(boolean escapeUnquotedValues)
        Configures the writer to process escape sequences in unquoted values. Defaults to false.

        By default, this is disabled and if the input is A""B,C, the result will be written as [A""B] and [C] (i.e. the quotes written as-is). However, if the writer is configured to process escape sequences in unquoted values, the values will written as [A""""B] and [C]

        Parameters:
        escapeUnquotedValues - a flag indicating whether escape sequences should be processed in unquoted values
      • isInputEscaped

        public final boolean isInputEscaped()
        Indicates that the user will provide escaped input, and the writer will not try to introduce escape sequences. The input will be written as-is.

        Warning! ensure your data is properly escaped, otherwise the writer will produce invalid CSV.

        This is disabled by default

        Returns:
        a flag indicating whether the escape sequences should not be introduced by the writer.
      • setInputEscaped

        public final void setInputEscaped​(boolean isInputEscaped)
        Configures the writer to prevent it to introduce escape sequences. The writer will assume the user is providing escaped input, and it will be written as-is.

        Warning! ensure your data is properly escaped, otherwise the writer will produce invalid CSV.

        This is disabled by default

        Parameters:
        isInputEscaped - a flag indicating whether the input that will be written is already properly escaped.
      • isNormalizeLineEndingsWithinQuotes

        public boolean isNormalizeLineEndingsWithinQuotes()
        Flag indicating whether the writer should replace the the normalized line separator character specified in Format.getNormalizedNewline() by the sequence specified in Format.getLineSeparator(), when the value is enclosed within quotes. This is enabled by default and is used to ensure data be read on any platform without introducing unwanted blank lines. For example, consider the quoted value "Line1 \n Line2". If this is written using "\r\n" as the line separator sequence, and the normalized new line is set to '\n' (the default), the output will be: [Line1 \r\n Line2] However, if the value is meant to be kept untouched, and the original line separator should be maintained, set the normalizeLineEndingsWithinQuotes to false. This will make the writer output the value as-is, producing: [Line1 \n Line2]
        Returns:
        true if line separator characters in quoted values should be considered 'normalized' and replaced by the sequence specified in Format.getLineSeparator(), false otherwise
      • setNormalizeLineEndingsWithinQuotes

        public void setNormalizeLineEndingsWithinQuotes​(boolean normalizeLineEndingsWithinQuotes)
        Flag indicating whether the writer should replace the the normalized line separator character specified in Format.getNormalizedNewline() by the sequence specified in Format.getLineSeparator(), when the value is enclosed within quotes. This is enabled by default and is used to ensure data can be used on any platform without producing unrecognized line endings. For example, consider the quoted value "Line1 \n Line2". If this is written using "\r\n" as the line separator sequence, and the normalized new line is set to '\n' (the default), the output will be: [Line1 \r\n Line2] However, if the value is meant to be kept untouched, and the original line separator should be maintained, set the normalizeLineEndingsWithinQuotes to false. This will make the writer output the value as-is, producing: [Line1 \n Line2]
        Parameters:
        normalizeLineEndingsWithinQuotes - flag indicating that line separator characters in quoted values should be considered 'normalized' and occurrences of Format.getNormalizedNewline() should be replaced by the sequence specified in Format.getLineSeparator()
      • createDefaultFormat

        protected CsvFormat createDefaultFormat()
        Returns the default CsvFormat configured to produce CSV outputs compliant to the RFC4180 standard.
        Specified by:
        createDefaultFormat in class CommonSettings<CsvFormat>
        Returns:
        and instance of CsvFormat configured to produce CSV outputs compliant to the RFC4180 standard.
      • getQuotationTriggers

        public char[] getQuotationTriggers()
        Returns the list of characters that when present in a value to be written, will force the output value to be enclosed in quotes.
        Returns:
        the characters that will trigger values to be quoted when present in a value to be written.
      • setQuotationTriggers

        public void setQuotationTriggers​(char... quotationTriggers)
        Defines one or more "triggers" for enclosing a value within quotes. If one of the characters in the quotation trigger list is found in a value to be written, the entire value will be enclosed in quotes.
        Parameters:
        quotationTriggers - a list of characters that when present in a value to be written, will force the output value to be enclosed in quotes.
      • isQuotationTrigger

        public boolean isQuotationTrigger​(char ch)
        Queries if a given character is a quotation trigger, i.e. a character that if present in a value to be written, will make the CSV writer enclose the entire value within quotes.
        Parameters:
        ch - the character to be tested
        Returns:
        true if the given character is a quotation trigger, false otherwise.
      • isQuoteEscapingEnabled

        public boolean isQuoteEscapingEnabled()
        Indicates whether the CSV writer should escape values that contain the quote character, by enclosing the entire value in quotes. For example, consider a value such as [My "precious" value]. When quote escaping is enabled, the output will be: ["My ""precious"" value"] If disabled (the default), the value will be written as-is. Note that the CSV output will not conform to the RFC 4180 standard, but it will still be valid as the value does not contain line separators nor the delimiter character.
        Returns:
        a flag indicating whether values containing quotes should be enclosed in quotes.
      • setQuoteEscapingEnabled

        public void setQuoteEscapingEnabled​(boolean quoteEscapingEnabled)
        Configures the CSV writer to escape values that contain the quote character, by enclosing the entire value in quotes. For example, consider a value such as [My "precious" value]. When quote escaping is enabled, the output will be: ["My ""precious"" value"] If disabled (the default), the value will be written as-is. Note that the CSV output will not conform to the RFC 4180 standard, but it will still be valid as the value does not contain line separators nor the delimiter character.
        Parameters:
        quoteEscapingEnabled - a flag indicating whether values containing quotes should be enclosed in quotes.
      • clone

        public final CsvWriterSettings clone​(boolean clearInputSpecificSettings)
        Description copied from class: CommonSettings
        Clones this configuration object to reuse user-provided settings. Properties that are specific to a given input (such as header names and selection of fields) can be reset to their defaults if the clearInputSpecificSettings flag is set to true
        Overrides:
        clone in class CommonWriterSettings<CsvFormat>
        Parameters:
        clearInputSpecificSettings - flag indicating whether to clear settings that are likely to be associated with a given input.
        Returns:
        a copy of the configurations applied to the current instance.
      • getQuotedFieldSelector

        final FieldSelector getQuotedFieldSelector()
        Returns the current selection of quoted fields (if any)
        Returns:
        the current selection of quoted fields
      • setFieldSet

        private <T> FieldSet<T> setFieldSet​(FieldSet<T> fieldSet,
                                            T... values)
        Replaces the current quoted field selection
        Parameters:
        fieldSet - the new set of selected fields
        values - the values to include to the selection
        Returns:
        the set of selected fields given in as a parameter.
      • quoteFields

        public final FieldSet<java.lang.Enum> quoteFields​(java.lang.Enum... columns)
        Selects fields whose values should always be written within quotes
        Parameters:
        columns - a selection of columns that will always be quoted
        Returns:
        the (modifiable) set of selected fields to be quoted.
      • quoteFields

        public final FieldSet<java.lang.String> quoteFields​(java.lang.String... columns)
        Selects fields whose values should always be written within quotes
        Parameters:
        columns - a selection of columns that will always be quoted
        Returns:
        the (modifiable) set of selected fields to be quoted.
      • quoteIndexes

        public final FieldSet<java.lang.Integer> quoteIndexes​(java.lang.Integer... columns)
        Selects field positions whose values should always be written within quotes
        Parameters:
        columns - a selection of column indexes that will always be quoted
        Returns:
        the (modifiable) set of column positions to be quoted.
      • setQuoteNulls

        public void setQuoteNulls​(boolean quoteNulls)
        Configures whether to quote null values sent to the CsvWriter when the corresponding column is configured to be quoted via quoteFields(String...), or getQuoteAllFields() evaluates to true.
        Parameters:
        quoteNulls - flag indicating whether null values should be quoted.
      • getQuoteNulls

        public boolean getQuoteNulls()
        Flag whether to quote null values sent to the CsvWriter when the corresponding column is configured to be quoted via quoteFields(String...), or getQuoteAllFields() evaluates to true.
        Returns:
        a flag indicating whether null values should be quoted.