com.univocity.parsers.csv
Class CsvWriterSettings

java.lang.Object
  extended by com.univocity.parsers.common.CommonSettings<F>
      extended by com.univocity.parsers.common.CommonWriterSettings<CsvFormat>
          extended by com.univocity.parsers.csv.CsvWriterSettings
All Implemented Interfaces:
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:

Author:
uniVocity Software Pty Ltd - parsers@univocity.com
See Also:
CsvWriter, CsvFormat, CommonWriterSettings

Constructor Summary
CsvWriterSettings()
           
 
Method Summary
protected  void addConfiguration(Map<String,Object> out)
           
 CsvWriterSettings clone()
          Clones this configuration object.
 CsvWriterSettings clone(boolean clearInputSpecificSettings)
          Clones this configuration object to reuse user-provided settings.
protected  CsvFormat createDefaultFormat()
          Returns the default CsvFormat configured to produce CSV outputs compliant to the RFC4180 standard.
 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.
 boolean getQuoteAllFields()
          Indicates that all written values should be enclosed within quotes (as defined in CsvFormat)
 boolean isEscapeUnquotedValues()
          Indicates whether escape sequences should be written in unquoted values.
 boolean isInputEscaped()
          Indicates that the user will provide escaped input, and the writer will not try to introduce escape sequences.
 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.
 boolean isQuotationTrigger(char ch)
          Queries if a given character is a quotation trigger, i.e.
 boolean isQuoteEscapingEnabled()
          Indicates whether the CSV writer should escape values that contain the quote character, by enclosing the entire value in quotes.
 FieldSet<Enum> quoteFields(Enum... columns)
          Selects fields whose values should always be written within quotes
 FieldSet<String> quoteFields(String... columns)
          Selects fields whose values should always be written within quotes
 FieldSet<Integer> quoteIndexes(Integer... columns)
          Selects field positions whose values should always be written within quotes
 void setEscapeUnquotedValues(boolean escapeUnquotedValues)
          Configures the writer to process escape sequences in unquoted values.
 void setInputEscaped(boolean isInputEscaped)
          Configures the writer to prevent it to introduce escape sequences.
 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.
 void setQuotationTriggers(char... quotationTriggers)
          Defines one or more "triggers" for enclosing a value within quotes.
 void setQuoteAllFields(boolean quoteAllFields)
          Indicates indicates whether or not all written values should be enclosed within quotes (as defined in CsvFormat)
 void setQuoteEscapingEnabled(boolean quoteEscapingEnabled)
          Configures the CSV writer to escape values that contain the quote character, by enclosing the entire value in quotes.
 
Methods inherited from class com.univocity.parsers.common.CommonWriterSettings
clearInputSpecificSettings, configureFromAnnotations, getEmptyValue, getExpandIncompleteRows, getRowWriterProcessor, isColumnReorderingEnabled, isHeaderWritingEnabled, setColumnReorderingEnabled, setEmptyValue, setExpandIncompleteRows, setHeaderWritingEnabled, setRowWriterProcessor
 
Methods inherited from class com.univocity.parsers.common.CommonSettings
excludeFields, excludeFields, excludeIndexes, getErrorContentLength, getFormat, getHeaders, getIgnoreLeadingWhitespaces, getIgnoreTrailingWhitespaces, getMaxCharsPerColumn, getMaxColumns, getNullValue, getProcessorErrorHandler, getRowProcessorErrorHandler, getSkipBitsAsWhitespace, getSkipEmptyLines, getWhitespaceRangeStart, isAutoConfigurationEnabled, isProcessorErrorHandlerDefined, selectFields, selectFields, selectIndexes, setAutoConfigurationEnabled, setErrorContentLength, setFormat, setHeaders, setIgnoreLeadingWhitespaces, setIgnoreTrailingWhitespaces, setMaxCharsPerColumn, setMaxColumns, setNullValue, setProcessorErrorHandler, setRowProcessorErrorHandler, setSkipBitsAsWhitespace, setSkipEmptyLines, toString, trimValues
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

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.

addConfiguration

protected void addConfiguration(Map<String,Object> out)
Overrides:
addConfiguration in class CommonWriterSettings<CsvFormat>

clone

public final CsvWriterSettings clone()
Description copied from class: CommonSettings
Clones this configuration object. Use alternative CommonSettings.clone(boolean) method to reset properties that are specific to a given input, such as header names and selection of fields.

Overrides:
clone in class CommonWriterSettings<CsvFormat>
Returns:
a copy of all configurations applied to the current instance.

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.

quoteFields

public final FieldSet<Enum> quoteFields(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<String> quoteFields(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<Integer> quoteIndexes(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.


Copyright © 2018 uniVocity Software Pty Ltd. All rights reserved.