public void testDelimiterSameAsCommentStartThrowsException() {
CSVFormat.DEFAULT.withDelimiter('!').withCommentMarker('!');
}
public void testEqualsCommentStart() {
final CSVFormat right = CSVFormat.newFormat('\'')
.withQuote('"')
.withCommentMarker('#')
.withQuoteMode(QuoteMode.ALL);
final CSVFormat left = right
.withCommentMarker('!');
assertNotEquals(right, left);
}
public void testEscapeSameAsCommentStartThrowsExceptionForWrapperType() {
// Cannot assume that callers won't use different Character objects
CSVFormat.DEFAULT.withEscape(new Character('!')).withCommentMarker(new Character('!'));
}
public void testEqualsEscape() {
final CSVFormat right = CSVFormat.newFormat('\'')
.withQuote('"')
.withCommentMarker('#')
.withEscape('+')
.withQuoteMode(QuoteMode.ALL);
final CSVFormat left = right
.withEscape('!');
assertNotEquals(right, left);
}
public void testWithCommentStart() throws Exception {
final CSVFormat formatWithCommentStart = CSVFormat.DEFAULT.withCommentMarker('#');
assertEquals( Character.valueOf('#'), formatWithCommentStart.getCommentMarker());
}
public CSVFormat build() {
CSVFormat format = newFormat(delimiter = initDelimiter());
format = format.withEscape(escape = initEscape());
format = format.withCommentMarker(commentMarker = initCommentMarker());
format = format.withRecordSeparator(lineSeparator = initLineSeparator());
quote = initQuote();
quoting = initQuoting();
if (quoting) {
format = format.withQuoteMode(QuoteMode.MINIMAL);
format = format.withQuote(quote);
}
return format;
}
private static CSVFormat getCSVStrategyFromConfiguration() {
char fieldDelimiter = getCharValueFromConfiguration(
"any23.extraction.csv.field",
DEFAULT_FIELD_DELIMITER
);
char commentDelimiter = getCharValueFromConfiguration(
"any23.extraction.csv.comment",
DEFAULT_COMMENT_DELIMITER
);
return CSVFormat.DEFAULT.withDelimiter(fieldDelimiter).withCommentMarker(commentDelimiter);
}
public CSVEncoder() {
final char escapeCharacter = '\\';
final char quoteCharacter = '"';
final char columnSeparator = ';';
final String lineEnding = System.getProperty("line.separator");
csvFormat = CSVFormat.newFormat(columnSeparator).withCommentMarker('#').withQuote(quoteCharacter).withEscape(escapeCharacter)
.withRecordSeparator(lineEnding);
}
private CSVPrinter printWithHeaderComments(final StringWriter sw, final Date now, final CSVFormat baseFormat)
throws IOException {
CSVFormat format = baseFormat;
// Use withHeaderComments first to test CSV-145
format = format.withHeaderComments("Generated by Apache Commons CSV 1.1", now);
format = format.withCommentMarker('#');
format = format.withHeader("Col1", "Col2");
final CSVPrinter csvPrinter = format.print(sw);
csvPrinter.printRecord("A", "B");
csvPrinter.printRecord("C", "D");
csvPrinter.close();
return csvPrinter;
}
CSVFormat getActiveFormat() {
CSVFormat answer = format;
if (commentMarkerDisabled) {
answer = answer.withCommentMarker(null); // null disables the comment marker
} else if (commentMarker != null) {
answer = answer.withCommentMarker(commentMarker);
}
if (delimiter != null) {
answer = answer.withDelimiter(delimiter);
}
if (escapeDisabled) {
answer = answer.withEscape(null); // null disables the escape
} else if (escape != null) {
answer = answer.withEscape(escape);
}
if (headerDisabled) {
answer = answer.withHeader((String[]) null); // null disables the header
} else if (header != null) {
answer = answer.withHeader(header);
}
if (allowMissingColumnNames != null) {
answer = answer.withAllowMissingColumnNames(allowMissingColumnNames);
}
if (ignoreEmptyLines != null) {
answer = answer.withIgnoreEmptyLines(ignoreEmptyLines);
}
if (ignoreSurroundingSpaces != null) {
answer = answer.withIgnoreSurroundingSpaces(ignoreSurroundingSpaces);
}
if (nullStringDisabled) {
answer = answer.withNullString(null); // null disables the null string replacement
} else if (nullString != null) {
answer = answer.withNullString(nullString);
}
if (quoteDisabled) {
answer = answer.withQuote(null); // null disables quotes
} else if (quote != null) {
answer = answer.withQuote(quote);
}
if (quoteMode != null) {
answer = answer.withQuoteMode(quoteMode);
}
if (recordSeparatorDisabled) {
answer = answer.withRecordSeparator(null); // null disables the record separator
} else if (recordSeparator != null) {
answer = answer.withRecordSeparator(recordSeparator);
}
if (skipHeaderRecord != null) {
answer = answer.withSkipHeaderRecord(skipHeaderRecord);
}
return answer;
}