More constants clean ups.

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/csv/trunk@1397556 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Gary D. Gregory 2012-10-12 13:15:30 +00:00
parent b5c334222c
commit 53fd86dfbc
4 changed files with 29 additions and 14 deletions

View File

@ -18,7 +18,7 @@
package org.apache.commons.csv;
import static org.apache.commons.csv.Constants.COMMA;
import static org.apache.commons.csv.Constants.CR;
import static org.apache.commons.csv.Constants.CRLF;
import static org.apache.commons.csv.Constants.DOUBLE_QUOTE;
import static org.apache.commons.csv.Constants.ESCAPE;
import static org.apache.commons.csv.Constants.LF;
@ -36,13 +36,8 @@ import java.io.StringWriter;
*/
public class CSVFormat implements Serializable {
private static final String LF_STR = "" + LF;
private static final long serialVersionUID = 1L;
/** According to RFC 4180, line breaks are delimited by CRLF */
public static final String CRLF = "" + CR + LF;
private final char delimiter;
private final char encapsulator;
private final char commentStart;
@ -136,7 +131,7 @@ public class CSVFormat implements Serializable {
PRISTINE
.withDelimiter(TAB)
.withEscape(ESCAPE)
.withLineSeparator(LF_STR);
.withLineSeparator(LF);
/**
* Creates a customized CSV format.
@ -405,6 +400,19 @@ public class CSVFormat implements Serializable {
return lineSeparator;
}
/**
* Returns a copy of this format using the specified output line separator.
*
* @param lineSeparator
* the line separator to be used for output.
*
* @return A copy of this format using the specified output line separator
*/
public CSVFormat withLineSeparator(final char lineSeparator) {
return new CSVFormat(delimiter, encapsulator, commentStart, escape, ignoreSurroundingSpaces,
ignoreEmptyLines, String.valueOf(lineSeparator), header);
}
/**
* Returns a copy of this format using the specified output line separator.
*

View File

@ -39,6 +39,10 @@ class Constants {
/** Undefined state for the lookahead char */
static final int UNDEFINED = -2;
/** According to RFC 4180, line breaks are delimited by CRLF */
public static final String CRLF = EMPTY + CR + LF;
}

View File

@ -17,6 +17,7 @@
package org.apache.commons.csv;
import static org.apache.commons.csv.Constants.CRLF;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
@ -34,7 +35,7 @@ public class CSVFormatTest {
@Test
public void testImmutalibity() {
final CSVFormat format = new CSVFormat('!', '!', '!', '!', true, true, CSVFormat.CRLF, null);
final CSVFormat format = new CSVFormat('!', '!', '!', '!', true, true, CRLF, null);
format.withDelimiter('?');
format.withEncapsulator('?');
@ -48,7 +49,7 @@ public class CSVFormatTest {
assertEquals('!', format.getEncapsulator());
assertEquals('!', format.getCommentStart());
assertEquals('!', format.getEscape());
assertEquals(CSVFormat.CRLF, format.getLineSeparator());
assertEquals(CRLF, format.getLineSeparator());
assertTrue(format.getIgnoreSurroundingSpaces());
assertTrue(format.getIgnoreEmptyLines());
@ -56,7 +57,7 @@ public class CSVFormatTest {
@Test
public void testMutators() {
final CSVFormat format = new CSVFormat('!', '!', '!', '!', true, true, CSVFormat.CRLF, null);
final CSVFormat format = new CSVFormat('!', '!', '!', '!', true, true, CRLF, null);
assertEquals('?', format.withDelimiter('?').getDelimiter());
assertEquals('?', format.withEncapsulator('?').getEncapsulator());

View File

@ -17,6 +17,8 @@
package org.apache.commons.csv;
import static org.apache.commons.csv.Constants.CRLF;
import static org.apache.commons.csv.Constants.LF;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
@ -307,7 +309,7 @@ public class CSVParserTest {
final CSVFormat format = CSVFormat.PRISTINE.withDelimiter(',').withEncapsulator('\'').withEscape('/')
.withIgnoreEmptyLines(true).withLineSeparator(CSVFormat.CRLF);
.withIgnoreEmptyLines(true).withLineSeparator(CRLF);
final CSVParser parser = new CSVParser(code, format);
final List<CSVRecord> records = parser.getRecords();
@ -337,7 +339,7 @@ public class CSVParserTest {
final CSVFormat format = CSVFormat.PRISTINE.withDelimiter(',').withEscape('/')
.withIgnoreEmptyLines(true).withLineSeparator(CSVFormat.CRLF);
.withIgnoreEmptyLines(true).withLineSeparator(CRLF);
final CSVParser parser = new CSVParser(code, format);
final List<CSVRecord> records = parser.getRecords();
@ -584,7 +586,7 @@ public class CSVParserTest {
@Test
public void testGetLineNumberWithLF() throws Exception {
final CSVParser parser = new CSVParser("a\nb\nc", CSVFormat.DEFAULT.withLineSeparator("\n"));
final CSVParser parser = new CSVParser("a\nb\nc", CSVFormat.DEFAULT.withLineSeparator(LF));
assertEquals(0, parser.getLineNumber());
assertNotNull(parser.getRecord());
@ -598,7 +600,7 @@ public class CSVParserTest {
@Test
public void testGetLineNumberWithCRLF() throws Exception {
final CSVParser parser = new CSVParser("a\r\nb\r\nc", CSVFormat.DEFAULT.withLineSeparator(CSVFormat.CRLF));
final CSVParser parser = new CSVParser("a\r\nb\r\nc", CSVFormat.DEFAULT.withLineSeparator(CRLF));
assertEquals(0, parser.getLineNumber());
assertNotNull(parser.getRecord());