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:
parent
b5c334222c
commit
53fd86dfbc
|
@ -18,7 +18,7 @@
|
||||||
package org.apache.commons.csv;
|
package org.apache.commons.csv;
|
||||||
|
|
||||||
import static org.apache.commons.csv.Constants.COMMA;
|
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.DOUBLE_QUOTE;
|
||||||
import static org.apache.commons.csv.Constants.ESCAPE;
|
import static org.apache.commons.csv.Constants.ESCAPE;
|
||||||
import static org.apache.commons.csv.Constants.LF;
|
import static org.apache.commons.csv.Constants.LF;
|
||||||
|
@ -36,13 +36,8 @@ import java.io.StringWriter;
|
||||||
*/
|
*/
|
||||||
public class CSVFormat implements Serializable {
|
public class CSVFormat implements Serializable {
|
||||||
|
|
||||||
private static final String LF_STR = "" + LF;
|
|
||||||
|
|
||||||
private static final long serialVersionUID = 1L;
|
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 delimiter;
|
||||||
private final char encapsulator;
|
private final char encapsulator;
|
||||||
private final char commentStart;
|
private final char commentStart;
|
||||||
|
@ -136,7 +131,7 @@ public class CSVFormat implements Serializable {
|
||||||
PRISTINE
|
PRISTINE
|
||||||
.withDelimiter(TAB)
|
.withDelimiter(TAB)
|
||||||
.withEscape(ESCAPE)
|
.withEscape(ESCAPE)
|
||||||
.withLineSeparator(LF_STR);
|
.withLineSeparator(LF);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a customized CSV format.
|
* Creates a customized CSV format.
|
||||||
|
@ -405,6 +400,19 @@ public class CSVFormat implements Serializable {
|
||||||
return lineSeparator;
|
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.
|
* Returns a copy of this format using the specified output line separator.
|
||||||
*
|
*
|
||||||
|
|
|
@ -40,5 +40,9 @@ class Constants {
|
||||||
/** Undefined state for the lookahead char */
|
/** Undefined state for the lookahead char */
|
||||||
static final int UNDEFINED = -2;
|
static final int UNDEFINED = -2;
|
||||||
|
|
||||||
|
/** According to RFC 4180, line breaks are delimited by CRLF */
|
||||||
|
public static final String CRLF = EMPTY + CR + LF;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,6 +17,7 @@
|
||||||
|
|
||||||
package org.apache.commons.csv;
|
package org.apache.commons.csv;
|
||||||
|
|
||||||
|
import static org.apache.commons.csv.Constants.CRLF;
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertFalse;
|
import static org.junit.Assert.assertFalse;
|
||||||
import static org.junit.Assert.assertNotNull;
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
@ -34,7 +35,7 @@ public class CSVFormatTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testImmutalibity() {
|
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.withDelimiter('?');
|
||||||
format.withEncapsulator('?');
|
format.withEncapsulator('?');
|
||||||
|
@ -48,7 +49,7 @@ public class CSVFormatTest {
|
||||||
assertEquals('!', format.getEncapsulator());
|
assertEquals('!', format.getEncapsulator());
|
||||||
assertEquals('!', format.getCommentStart());
|
assertEquals('!', format.getCommentStart());
|
||||||
assertEquals('!', format.getEscape());
|
assertEquals('!', format.getEscape());
|
||||||
assertEquals(CSVFormat.CRLF, format.getLineSeparator());
|
assertEquals(CRLF, format.getLineSeparator());
|
||||||
|
|
||||||
assertTrue(format.getIgnoreSurroundingSpaces());
|
assertTrue(format.getIgnoreSurroundingSpaces());
|
||||||
assertTrue(format.getIgnoreEmptyLines());
|
assertTrue(format.getIgnoreEmptyLines());
|
||||||
|
@ -56,7 +57,7 @@ public class CSVFormatTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testMutators() {
|
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.withDelimiter('?').getDelimiter());
|
||||||
assertEquals('?', format.withEncapsulator('?').getEncapsulator());
|
assertEquals('?', format.withEncapsulator('?').getEncapsulator());
|
||||||
|
|
|
@ -17,6 +17,8 @@
|
||||||
|
|
||||||
package org.apache.commons.csv;
|
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.assertArrayEquals;
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertFalse;
|
import static org.junit.Assert.assertFalse;
|
||||||
|
@ -307,7 +309,7 @@ public class CSVParserTest {
|
||||||
|
|
||||||
|
|
||||||
final CSVFormat format = CSVFormat.PRISTINE.withDelimiter(',').withEncapsulator('\'').withEscape('/')
|
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 CSVParser parser = new CSVParser(code, format);
|
||||||
final List<CSVRecord> records = parser.getRecords();
|
final List<CSVRecord> records = parser.getRecords();
|
||||||
|
@ -337,7 +339,7 @@ public class CSVParserTest {
|
||||||
|
|
||||||
|
|
||||||
final CSVFormat format = CSVFormat.PRISTINE.withDelimiter(',').withEscape('/')
|
final CSVFormat format = CSVFormat.PRISTINE.withDelimiter(',').withEscape('/')
|
||||||
.withIgnoreEmptyLines(true).withLineSeparator(CSVFormat.CRLF);
|
.withIgnoreEmptyLines(true).withLineSeparator(CRLF);
|
||||||
|
|
||||||
final CSVParser parser = new CSVParser(code, format);
|
final CSVParser parser = new CSVParser(code, format);
|
||||||
final List<CSVRecord> records = parser.getRecords();
|
final List<CSVRecord> records = parser.getRecords();
|
||||||
|
@ -584,7 +586,7 @@ public class CSVParserTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetLineNumberWithLF() throws Exception {
|
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());
|
assertEquals(0, parser.getLineNumber());
|
||||||
assertNotNull(parser.getRecord());
|
assertNotNull(parser.getRecord());
|
||||||
|
@ -598,7 +600,7 @@ public class CSVParserTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetLineNumberWithCRLF() throws Exception {
|
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());
|
assertEquals(0, parser.getLineNumber());
|
||||||
assertNotNull(parser.getRecord());
|
assertNotNull(parser.getRecord());
|
||||||
|
|
Loading…
Reference in New Issue