org.jooq.CSVFormat Maven / Gradle / Ivy
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Other licenses:
* -----------------------------------------------------------------------------
* Commercial licenses for this work are available. These replace the above
* Apache-2.0 and offer limited warranties, support, maintenance, and commercial
* database integrations.
*
* For more information, please visit: https://www.jooq.org/legal/licensing
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
package org.jooq;
import static org.jooq.CSVFormat.Quote.SPECIAL_CHARACTERS;
import org.jetbrains.annotations.NotNull;
/**
* A CSV formatting type, which can be used to configure CSV imports / exports.
*
* The type is immutable, meaning calls to setters like {@link #delimiter(char)}
* do not modify the original reference, but return a new one instead.
*
* @author Lukas Eder
*/
public final class CSVFormat {
public static final CSVFormat DEFAULT = new CSVFormat();
final String delimiter;
final String nullString;
final String emptyString;
final String newline;
final String quoteString;
final Quote quote;
final boolean header;
public CSVFormat() {
this(
",",
"\"\"",
"\"\"",
"\n",
"\"",
SPECIAL_CHARACTERS,
true
);
}
private CSVFormat(
String delimiter,
String nullString,
String emptyString,
String newline,
String quoteString,
Quote quote,
boolean header
) {
this.delimiter = delimiter;
this.nullString = nullString;
this.emptyString = emptyString;
this.newline = newline;
this.quoteString = quoteString;
this.quote = quote;
this.header = header;
}
/**
* The delimiter to be used between CSV cells, defaulting to
* ","
.
*
*
*
* Using ","
* a,b,c
*
*
* Using ";"
* a;b;c
*
*
*/
@NotNull
public CSVFormat delimiter(String newDelimiter) {
return new CSVFormat(
newDelimiter,
nullString,
emptyString,
newline,
quoteString,
quote,
header
);
}
/**
* The delimiter to be used between CSV cells, defaulting to
* ","
.
*
*
*
* Using ","
* a,b,c
*
*
* Using ";"
* a;b;c
*
*
*/
@NotNull
public CSVFormat delimiter(char newDelimiter) {
return delimiter("" + newDelimiter);
}
/**
* The delimiter to be used between CSV cells, defaulting to
* ","
.
*
*
*
* Using ","
* a,b,c
*
*
* Using ";"
* a;b;c
*
*
*/
@NotNull
public String delimiter() {
return delimiter;
}
/**
* The string to be used for null
values, defaulting to the
* empty string.
*
*
*
* Using ""
* a,,c
*
*
* Using "\"\""
* a,"",c
*
*
* Using "{null}"
* a,{null},c
*
*
*/
@NotNull
public CSVFormat nullString(String newNullString) {
return new CSVFormat(
delimiter,
newNullString,
emptyString,
newline,
quoteString,
quote,
header
);
}
/**
* The string to be used for null
values, defaulting to the
* empty string.
*
*
*
* Using ""
* a,,c
*
*
* Using "\"\""
* a,"",c
*
*
* Using "{null}"
* a,{null},c
*
*
*/
@NotNull
public String nullString() {
return nullString;
}
/**
* The string to be used for ""
values, defaulting to the
* empty string.
*
*
*
* Using ""
* a,,c
*
*
* Using "\"\""
* a,"",c
*
*
*/
@NotNull
public CSVFormat emptyString(String newEmptyString) {
return new CSVFormat(
delimiter,
nullString,
newEmptyString,
newline,
quoteString,
quote,
header
);
}
/**
* The string to be used for ""
values, defaulting to the
* empty string.
*
*
*
* Using ""
* a,,c
*
*
* Using "\"\""
* a,"",c
*
*
*/
@NotNull
public String emptyString() {
return emptyString;
}
/**
* The string to be used to separate rows, defaulting to \n
.
*/
@NotNull
public CSVFormat newline(String newNewline) {
return new CSVFormat(
delimiter,
nullString,
emptyString,
newNewline,
quoteString,
quote,
header
);
}
/**
* The string to be used to separate rows, defaulting to \n
.
*/
@NotNull
public String newline() {
return newline;
}
/**
* The string used to quote values according to the rules specified in
* {@link #quote()}.
*/
@NotNull
public CSVFormat quoteString(String newQuoteString) {
return new CSVFormat(
delimiter,
nullString,
emptyString,
newline,
newQuoteString,
quote,
header
);
}
/**
* The string used to quote values according to the rules specified in
* {@link #quote()}.
*/
@NotNull
public String quoteString() {
return quoteString;
}
/**
* When to quote CSV content.
*/
@NotNull
public CSVFormat quote(Quote newQuote) {
return new CSVFormat(
delimiter,
nullString,
emptyString,
newline,
quoteString,
newQuote,
header
);
}
/**
* When to quote CSV content.
*/
@NotNull
public Quote quote() {
return quote;
}
/**
* Whether to emit a header row with column names, defaulting to
* true
.
*/
@NotNull
public CSVFormat header(boolean newHeader) {
return new CSVFormat(
delimiter,
nullString,
emptyString,
newline,
quoteString,
quote,
newHeader
);
}
/**
* Whether to emit a header row with column names, defaulting to
* true
.
*/
public boolean header() {
return header;
}
/**
* When to apply the quote
*/
public enum Quote {
/**
* Each content element is quoted.
*/
ALWAYS,
/**
* Only content elements are quoted containing special characters.
*
* Special characters consist of:
*
* ,
: The comma
* ;
: The semi colon
* "
: The double quote
* '
: The apostrophe
* \
: The backslash
* \t
: The tab character
* \n
: The line feed character
* \r
: The carriage return character
*
*/
SPECIAL_CHARACTERS,
/**
* Content is never quoted.
*/
NEVER
}
}