Skip to content

Commit

Permalink
FIX FasterXML#45 Option to skip ending line break
Browse files Browse the repository at this point in the history
  • Loading branch information
mathieu-lavigne committed Oct 23, 2017
1 parent ec789fa commit 0e7e808
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,9 @@ public static class Builder
protected int _escapeChar = DEFAULT_ESCAPE_CHAR;

protected char[] _lineSeparator = DEFAULT_LINEFEED;

/** CSV ends with a line separator ? Default is <code>true</code> */
protected boolean _endingLineSeparator = true;

/**
* @since 2.5
Expand All @@ -440,6 +443,7 @@ public Builder(CsvSchema src)
_quoteChar = src._quoteChar;
_escapeChar = src._escapeChar;
_lineSeparator = src._lineSeparator;
_endingLineSeparator = true;
_nullValue = src._nullValue;
_anyPropertyName = src._anyPropertyName;
}
Expand Down Expand Up @@ -786,6 +790,11 @@ public Builder setLineSeparator(char lf) {
_lineSeparator = new char[] { lf };
return this;
}

public Builder setEndingLineSeparator(boolean elf) {
_endingLineSeparator = elf;
return this;
}

public Builder setNullValue(String nvl) {
return setNullValue((nvl == null) ? null : nvl.toCharArray());
Expand All @@ -801,7 +810,7 @@ public CsvSchema build()
Column[] cols = _columns.toArray(new Column[_columns.size()]);
return new CsvSchema(cols, _encodingFeatures,
_columnSeparator, _quoteChar, _escapeChar,
_lineSeparator, _arrayElementSeparator,
_lineSeparator, _endingLineSeparator, _arrayElementSeparator,
_nullValue, _anyPropertyName);
}

Expand Down Expand Up @@ -842,6 +851,9 @@ protected void _checkIndex(int index) {
protected final int _escapeChar;

protected final char[] _lineSeparator;

/** CSV ends with a line separator ? Default is <code>true</code> */
protected final boolean _endingLineSeparator;

/**
* @since 2.5
Expand Down Expand Up @@ -871,7 +883,7 @@ public CsvSchema(Column[] columns, int features,
char columnSeparator, int quoteChar, int escapeChar,
char[] lineSeparator, int arrayElementSeparator,
char[] nullValue) {
this(columns, features, columnSeparator, quoteChar, escapeChar, lineSeparator,
this(columns, features, columnSeparator, quoteChar, escapeChar, lineSeparator, true,
arrayElementSeparator == -1 ? "" : Character.toString((char) arrayElementSeparator), nullValue,
DEFAULT_ANY_PROPERTY_NAME);
}
Expand All @@ -881,7 +893,7 @@ public CsvSchema(Column[] columns, int features,
*/
public CsvSchema(Column[] columns, int features,
char columnSeparator, int quoteChar, int escapeChar,
char[] lineSeparator, String arrayElementSeparator,
char[] lineSeparator, boolean endingLineSeparator, String arrayElementSeparator,
char[] nullValue, String anyPropertyName)
{
if (columns == null) {
Expand All @@ -896,6 +908,7 @@ public CsvSchema(Column[] columns, int features,
_quoteChar = quoteChar;
_escapeChar = escapeChar;
_lineSeparator = lineSeparator;
_endingLineSeparator = endingLineSeparator;
_nullValue = nullValue;
_anyPropertyName = anyPropertyName;

Expand All @@ -916,7 +929,7 @@ public CsvSchema(Column[] columns, int features,
*/
protected CsvSchema(Column[] columns, int features,
char columnSeparator, int quoteChar, int escapeChar,
char[] lineSeparator, String arrayElementSeparator,
char[] lineSeparator, boolean endingLineSeparator, String arrayElementSeparator,
char[] nullValue,
Map<String,Column> columnsByName, String anyPropertyName)
{
Expand All @@ -926,6 +939,7 @@ protected CsvSchema(Column[] columns, int features,
_quoteChar = quoteChar;
_escapeChar = escapeChar;
_lineSeparator = lineSeparator;
_endingLineSeparator = endingLineSeparator;
_arrayElementSeparator = arrayElementSeparator;
_nullValue = nullValue;
_columnsByName = columnsByName;
Expand All @@ -944,6 +958,7 @@ protected CsvSchema(CsvSchema base, Column[] columns)
_quoteChar = base._quoteChar;
_escapeChar = base._escapeChar;
_lineSeparator = base._lineSeparator;
_endingLineSeparator = base._endingLineSeparator;
_arrayElementSeparator = base._arrayElementSeparator;
_nullValue = base._nullValue;
_anyPropertyName = base._anyPropertyName;
Expand Down Expand Up @@ -971,6 +986,7 @@ protected CsvSchema(CsvSchema base, int features) {
_quoteChar = base._quoteChar;
_escapeChar = base._escapeChar;
_lineSeparator = base._lineSeparator;
_endingLineSeparator = base._endingLineSeparator;
_arrayElementSeparator = base._arrayElementSeparator;
_nullValue = base._nullValue;
_anyPropertyName = base._anyPropertyName;
Expand Down Expand Up @@ -1122,35 +1138,35 @@ protected CsvSchema _withFeature(int feature, boolean state) {
public CsvSchema withColumnSeparator(char sep) {
return (_columnSeparator == sep) ? this :
new CsvSchema(_columns, _features,
sep, _quoteChar, _escapeChar, _lineSeparator, _arrayElementSeparator,
sep, _quoteChar, _escapeChar, _lineSeparator, _endingLineSeparator, _arrayElementSeparator,
_nullValue, _columnsByName, _anyPropertyName);
}

public CsvSchema withQuoteChar(char c) {
return (_quoteChar == c) ? this :
new CsvSchema(_columns, _features,
_columnSeparator, c, _escapeChar, _lineSeparator,_arrayElementSeparator,
_columnSeparator, c, _escapeChar, _lineSeparator, _endingLineSeparator,_arrayElementSeparator,
_nullValue, _columnsByName, _anyPropertyName);
}

public CsvSchema withoutQuoteChar() {
return (_quoteChar == -1) ? this :
new CsvSchema(_columns, _features,
_columnSeparator, -1, _escapeChar, _lineSeparator, _arrayElementSeparator,
_columnSeparator, -1, _escapeChar, _lineSeparator, _endingLineSeparator, _arrayElementSeparator,
_nullValue, _columnsByName, _anyPropertyName);
}

public CsvSchema withEscapeChar(char c) {
return (_escapeChar == c) ? this
: new CsvSchema(_columns, _features,
_columnSeparator, _quoteChar, c, _lineSeparator, _arrayElementSeparator,
_columnSeparator, _quoteChar, c, _lineSeparator, _endingLineSeparator, _arrayElementSeparator,
_nullValue, _columnsByName, _anyPropertyName);
}

public CsvSchema withoutEscapeChar() {
return (_escapeChar == -1) ? this
: new CsvSchema(_columns, _features,
_columnSeparator, _quoteChar, -1, _lineSeparator, _arrayElementSeparator,
_columnSeparator, _quoteChar, -1, _lineSeparator, _endingLineSeparator, _arrayElementSeparator,
_nullValue, _columnsByName, _anyPropertyName);
}

Expand All @@ -1169,7 +1185,7 @@ public CsvSchema withArrayElementSeparator(char c) {
public CsvSchema withArrayElementSeparator(String separator) {
String sep = separator == null ? "" : separator;
return (_arrayElementSeparator.equals(sep)) ? this : new CsvSchema(_columns, _features,
_columnSeparator, _quoteChar, _escapeChar, _lineSeparator, separator,
_columnSeparator, _quoteChar, _escapeChar, _lineSeparator, _endingLineSeparator, separator,
_nullValue, _columnsByName, _anyPropertyName);
}

Expand All @@ -1180,30 +1196,50 @@ public CsvSchema withArrayElementSeparator(String separator) {
public CsvSchema withoutArrayElementSeparator() {
return (_arrayElementSeparator.isEmpty()) ? this
: new CsvSchema(_columns, _features,
_columnSeparator, _quoteChar, _escapeChar, _lineSeparator, "",
_columnSeparator, _quoteChar, _escapeChar, _lineSeparator, _endingLineSeparator, "",
_nullValue, _columnsByName, _anyPropertyName);
}

public CsvSchema withLineSeparator(String sep) {
return new CsvSchema(_columns, _features,
_columnSeparator, _quoteChar, _escapeChar, sep.toCharArray(),
_columnSeparator, _quoteChar, _escapeChar, sep.toCharArray(), _endingLineSeparator,
_arrayElementSeparator, _nullValue, _columnsByName, _anyPropertyName);
}

/**
* @return
*/
public CsvSchema withEndingLineSeparator() {

return new CsvSchema(_columns, _features,
_columnSeparator, _quoteChar, _escapeChar, _lineSeparator, true,
_arrayElementSeparator, _nullValue, _columnsByName, _anyPropertyName);
}

/**
* @return
*/
public CsvSchema withoutEndingLineSeparator() {

return new CsvSchema(_columns, _features,
_columnSeparator, _quoteChar, _escapeChar, _lineSeparator, false,
_arrayElementSeparator, _nullValue, _columnsByName, _anyPropertyName);
}

/**
* @since 2.5
*/
public CsvSchema withNullValue(String nvl) {
return new CsvSchema(_columns, _features,
_columnSeparator, _quoteChar, _escapeChar, _lineSeparator,
_columnSeparator, _quoteChar, _escapeChar, _lineSeparator, _endingLineSeparator,
_arrayElementSeparator,
(nvl == null) ? null : nvl.toCharArray(),
_columnsByName, _anyPropertyName);
}

public CsvSchema withoutColumns() {
return new CsvSchema(NO_COLUMNS, _features,
_columnSeparator, _quoteChar, _escapeChar, _lineSeparator, _arrayElementSeparator,
_columnSeparator, _quoteChar, _escapeChar, _lineSeparator, _endingLineSeparator, _arrayElementSeparator,
_nullValue, _columnsByName, _anyPropertyName);
}

Expand Down Expand Up @@ -1239,7 +1275,7 @@ public CsvSchema withColumnsFrom(CsvSchema toAppend) {
*/
public CsvSchema withAnyPropertyName(String name) {
return new CsvSchema(_columns, _features,
_columnSeparator, _quoteChar, _escapeChar, _lineSeparator, _arrayElementSeparator,
_columnSeparator, _quoteChar, _escapeChar, _lineSeparator, _endingLineSeparator, _arrayElementSeparator,
_nullValue, _columnsByName, name);
}

Expand Down Expand Up @@ -1328,6 +1364,7 @@ public String getSchemaType() {
public int getEscapeChar() { return _escapeChar; }

public char[] getLineSeparator() { return _lineSeparator; }
public boolean getEndingLineSeparator() { return _endingLineSeparator; }

/**
* @return Null value defined, as char array, if one is defined to be recognized; Java null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ public class CsvEncoder
final protected int _cfgEscapeCharacter;

final protected char[] _cfgLineSeparator;

final protected boolean _cfgEndingLineSeparator;

/**
* @since 2.5
Expand Down Expand Up @@ -180,6 +182,7 @@ public CsvEncoder(IOContext ctxt, int csvFeatures, Writer out, CsvSchema schema)
_cfgEscapeCharacter = schema.getEscapeChar();
_cfgLineSeparator = schema.getLineSeparator();
_cfgLineSeparatorLength = (_cfgLineSeparator == null) ? 0 : _cfgLineSeparator.length;
_cfgEndingLineSeparator = schema.getEndingLineSeparator();
_cfgNullValue = schema.getNullValueOrEmpty();

_columnCount = schema.size();
Expand Down Expand Up @@ -209,6 +212,7 @@ public CsvEncoder(CsvEncoder base, CsvSchema newSchema)
_cfgEscapeCharacter = newSchema.getEscapeChar();
_cfgLineSeparator = newSchema.getLineSeparator();
_cfgLineSeparatorLength = _cfgLineSeparator.length;
_cfgEndingLineSeparator = newSchema.getEndingLineSeparator();
_cfgNullValue = newSchema.getNullValueOrEmpty();
_cfgMinSafeChar = _calcSafeChar();
_columnCount = newSchema.size();
Expand Down Expand Up @@ -440,6 +444,14 @@ public void endRow() throws IOException
_outputTail += _cfgLineSeparatorLength;
}

/**
*
*/
public void removeEndingLineSeparator() {

_outputTail -= _cfgLineSeparatorLength;
}

/*
/**********************************************************
/* Writer API, writes via buffered values
Expand Down Expand Up @@ -835,6 +847,9 @@ public void flush(boolean flushStream) throws IOException

public void close(boolean autoClose) throws IOException
{
if (!_cfgEndingLineSeparator) {
removeEndingLineSeparator();
}
_flushBuffer();
if (autoClose) {
_out.close();
Expand Down

0 comments on commit 0e7e808

Please sign in to comment.