Rename "encapsulator" to "quoteChar" so we have quoteChar and quotePolicy. Encapsulator makes me want to ask "encapsulate what"? fieldEncapsulator would be better but so verbose, quoteChar feels more to the point to me and provides symmetry with quotePolicy.

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/csv/trunk@1398009 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Gary D. Gregory 2012-10-14 04:56:50 +00:00
parent 0030e1a9a2
commit 6e57364216
5 changed files with 41 additions and 41 deletions

View File

@ -40,14 +40,14 @@ public class CSVFormat implements Serializable {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
private final char delimiter; private final char delimiter;
private final Character encapsulator; private final Character quoteChar;
private final Quote quotePolicy;
private final Character commentStart; private final Character commentStart;
private final Character escape; private final Character escape;
private final boolean ignoreSurroundingSpaces; // Should leading/trailing spaces be ignored around values? private final boolean ignoreSurroundingSpaces; // Should leading/trailing spaces be ignored around values?
private final boolean ignoreEmptyLines; private final boolean ignoreEmptyLines;
private final String lineSeparator; // for outputs private final String lineSeparator; // for outputs
private final String[] header; private final String[] header;
private final Quote quotePolicy;
/** /**
* Starting format; used for creating other formats. * Starting format; used for creating other formats.
@ -129,7 +129,7 @@ public class CSVFormat implements Serializable {
* *
* @param delimiter * @param delimiter
* the char used for value separation * the char used for value separation
* @param encapsulator * @param quoteChar
* the char used as value encapsulation marker * the char used as value encapsulation marker
* @param quotePolicy * @param quotePolicy
* the quote policy * the quote policy
@ -150,7 +150,7 @@ public class CSVFormat implements Serializable {
boolean ignoreSurroundingSpaces, final boolean ignoreEmptyLines, final String lineSeparator, boolean ignoreSurroundingSpaces, final boolean ignoreEmptyLines, final String lineSeparator,
final String[] header) { final String[] header) {
this.delimiter = delimiter; this.delimiter = delimiter;
this.encapsulator = encapsulator; this.quoteChar = encapsulator;
this.quotePolicy = quotePolicy; this.quotePolicy = quotePolicy;
this.commentStart = commentStart; this.commentStart = commentStart;
this.escape = escape; this.escape = escape;
@ -178,8 +178,8 @@ public class CSVFormat implements Serializable {
* @throws IllegalStateException * @throws IllegalStateException
*/ */
void validate() throws IllegalStateException { void validate() throws IllegalStateException {
if (encapsulator != null && delimiter == encapsulator) { if (quoteChar != null && delimiter == quoteChar) {
throw new IllegalStateException("The encapsulator character and the delimiter cannot be the same ('" + encapsulator + "')"); throw new IllegalStateException("The quoteChar character and the delimiter cannot be the same ('" + quoteChar + "')");
} }
if (escape != null && delimiter == escape) { if (escape != null && delimiter == escape) {
@ -191,8 +191,8 @@ public class CSVFormat implements Serializable {
"')"); "')");
} }
if (encapsulator != null && encapsulator == commentStart) { if (quoteChar != null && quoteChar == commentStart) {
throw new IllegalStateException("The comment start character and the encapsulator cannot be the same ('" + commentStart + throw new IllegalStateException("The comment start character and the quoteChar cannot be the same ('" + commentStart +
"')"); "')");
} }
@ -236,25 +236,25 @@ public class CSVFormat implements Serializable {
if (isLineBreak(delimiter)) { if (isLineBreak(delimiter)) {
throw new IllegalArgumentException("The delimiter cannot be a line break"); throw new IllegalArgumentException("The delimiter cannot be a line break");
} }
return new CSVFormat(delimiter, encapsulator, quotePolicy, commentStart, escape, return new CSVFormat(delimiter, quoteChar, quotePolicy, commentStart, escape,
ignoreSurroundingSpaces, ignoreEmptyLines, lineSeparator, header); ignoreSurroundingSpaces, ignoreEmptyLines, lineSeparator, header);
} }
/** /**
* Returns the character used to encapsulate values containing special characters. * Returns the character used to encapsulate values containing special characters.
* *
* @return the encapsulator character * @return the quoteChar character
*/ */
public Character getEncapsulator() { public Character getQuoteChar() {
return encapsulator; return quoteChar;
} }
/** /**
* Returns a copy of this format using the specified encapsulator character. * Returns a copy of this format using the specified quoteChar character.
* *
* @param encapsulator * @param quoteChar
* the encapsulator character * the quoteChar character
* @return A copy of this format using the specified encapsulator character * @return A copy of this format using the specified quoteChar character
* @throws IllegalArgumentException * @throws IllegalArgumentException
* thrown if the specified character is a line break * thrown if the specified character is a line break
*/ */
@ -263,29 +263,29 @@ public class CSVFormat implements Serializable {
} }
/** /**
* Returns a copy of this format using the specified encapsulator character. * Returns a copy of this format using the specified quoteChar character.
* *
* @param encapsulator * @param quoteChar
* the encapsulator character * the quoteChar character
* @return A copy of this format using the specified encapsulator character * @return A copy of this format using the specified quoteChar character
* @throws IllegalArgumentException * @throws IllegalArgumentException
* thrown if the specified character is a line break * thrown if the specified character is a line break
*/ */
public CSVFormat withEncapsulator(final Character encapsulator) { public CSVFormat withEncapsulator(final Character encapsulator) {
if (isLineBreak(encapsulator)) { if (isLineBreak(encapsulator)) {
throw new IllegalArgumentException("The encapsulator cannot be a line break"); throw new IllegalArgumentException("The quoteChar cannot be a line break");
} }
return new CSVFormat(delimiter, encapsulator, quotePolicy, commentStart, escape, return new CSVFormat(delimiter, encapsulator, quotePolicy, commentStart, escape,
ignoreSurroundingSpaces, ignoreEmptyLines, lineSeparator, header); ignoreSurroundingSpaces, ignoreEmptyLines, lineSeparator, header);
} }
/** /**
* Returns whether an encapsulator has been defined. * Returns whether an quoteChar has been defined.
* *
* @return {@code true} if an encapsulator is defined * @return {@code true} if an quoteChar is defined
*/ */
public boolean isEncapsulating() { public boolean isEncapsulating() {
return encapsulator != null; return quoteChar != null;
} }
/** /**
@ -327,7 +327,7 @@ public class CSVFormat implements Serializable {
if (isLineBreak(commentStart)) { if (isLineBreak(commentStart)) {
throw new IllegalArgumentException("The comment start character cannot be a line break"); throw new IllegalArgumentException("The comment start character cannot be a line break");
} }
return new CSVFormat(delimiter, encapsulator, quotePolicy, commentStart, escape, return new CSVFormat(delimiter, quoteChar, quotePolicy, commentStart, escape,
ignoreSurroundingSpaces, ignoreEmptyLines, lineSeparator, header); ignoreSurroundingSpaces, ignoreEmptyLines, lineSeparator, header);
} }
@ -377,7 +377,7 @@ public class CSVFormat implements Serializable {
if (isLineBreak(escape)) { if (isLineBreak(escape)) {
throw new IllegalArgumentException("The escape character cannot be a line break"); throw new IllegalArgumentException("The escape character cannot be a line break");
} }
return new CSVFormat(delimiter, encapsulator, quotePolicy, commentStart, escape, return new CSVFormat(delimiter, quoteChar, quotePolicy, commentStart, escape,
ignoreSurroundingSpaces, ignoreEmptyLines, lineSeparator, header); ignoreSurroundingSpaces, ignoreEmptyLines, lineSeparator, header);
} }
@ -409,7 +409,7 @@ public class CSVFormat implements Serializable {
* @return A copy of this format with the specified trimming behavior. * @return A copy of this format with the specified trimming behavior.
*/ */
public CSVFormat withIgnoreSurroundingSpaces(final boolean ignoreSurroundingSpaces) { public CSVFormat withIgnoreSurroundingSpaces(final boolean ignoreSurroundingSpaces) {
return new CSVFormat(delimiter, encapsulator, quotePolicy, commentStart, escape, return new CSVFormat(delimiter, quoteChar, quotePolicy, commentStart, escape,
ignoreSurroundingSpaces, ignoreEmptyLines, lineSeparator, header); ignoreSurroundingSpaces, ignoreEmptyLines, lineSeparator, header);
} }
@ -432,7 +432,7 @@ public class CSVFormat implements Serializable {
* @return A copy of this format with the specified empty line skipping behavior. * @return A copy of this format with the specified empty line skipping behavior.
*/ */
public CSVFormat withIgnoreEmptyLines(final boolean ignoreEmptyLines) { public CSVFormat withIgnoreEmptyLines(final boolean ignoreEmptyLines) {
return new CSVFormat(delimiter, encapsulator, quotePolicy, commentStart, escape, return new CSVFormat(delimiter, quoteChar, quotePolicy, commentStart, escape,
ignoreSurroundingSpaces, ignoreEmptyLines, lineSeparator, header); ignoreSurroundingSpaces, ignoreEmptyLines, lineSeparator, header);
} }
@ -454,7 +454,7 @@ public class CSVFormat implements Serializable {
* @return A copy of this format using the specified output line separator * @return A copy of this format using the specified output line separator
*/ */
public CSVFormat withLineSeparator(final char lineSeparator) { public CSVFormat withLineSeparator(final char lineSeparator) {
return new CSVFormat(delimiter, encapsulator, quotePolicy, commentStart, escape, return new CSVFormat(delimiter, quoteChar, quotePolicy, commentStart, escape,
ignoreSurroundingSpaces, ignoreEmptyLines, String.valueOf(lineSeparator), header); ignoreSurroundingSpaces, ignoreEmptyLines, String.valueOf(lineSeparator), header);
} }
@ -467,7 +467,7 @@ public class CSVFormat implements Serializable {
* @return A copy of this format using the specified output line separator * @return A copy of this format using the specified output line separator
*/ */
public CSVFormat withLineSeparator(final String lineSeparator) { public CSVFormat withLineSeparator(final String lineSeparator) {
return new CSVFormat(delimiter, encapsulator, quotePolicy, commentStart, escape, return new CSVFormat(delimiter, quoteChar, quotePolicy, commentStart, escape,
ignoreSurroundingSpaces, ignoreEmptyLines, lineSeparator, header); ignoreSurroundingSpaces, ignoreEmptyLines, lineSeparator, header);
} }
@ -480,7 +480,7 @@ public class CSVFormat implements Serializable {
* @return A copy of this format using the specified output line separator * @return A copy of this format using the specified output line separator
*/ */
public CSVFormat withQuotePolicy(final Quote quotePolicy) { public CSVFormat withQuotePolicy(final Quote quotePolicy) {
return new CSVFormat(delimiter, encapsulator, quotePolicy, commentStart, escape, return new CSVFormat(delimiter, quoteChar, quotePolicy, commentStart, escape,
ignoreSurroundingSpaces, ignoreEmptyLines, lineSeparator, header); ignoreSurroundingSpaces, ignoreEmptyLines, lineSeparator, header);
} }
@ -508,7 +508,7 @@ public class CSVFormat implements Serializable {
* @return A copy of this format using the specified header * @return A copy of this format using the specified header
*/ */
public CSVFormat withHeader(final String... header) { public CSVFormat withHeader(final String... header) {
return new CSVFormat(delimiter, encapsulator, quotePolicy, commentStart, escape, return new CSVFormat(delimiter, quoteChar, quotePolicy, commentStart, escape,
ignoreSurroundingSpaces, ignoreEmptyLines, lineSeparator, header); ignoreSurroundingSpaces, ignoreEmptyLines, lineSeparator, header);
} }
@ -549,7 +549,7 @@ public class CSVFormat implements Serializable {
} }
if (isEncapsulating()) { if (isEncapsulating()) {
sb.append(' '); sb.append(' ');
sb.append("Encapsulator=<").append(encapsulator).append('>'); sb.append("Encapsulator=<").append(quoteChar).append('>');
} }
if (isCommentingEnabled()) { if (isCommentingEnabled()) {
sb.append(' '); sb.append(' ');

View File

@ -201,7 +201,7 @@ public class CSVPrinter {
printDelimiter(); printDelimiter();
final char delim = format.getDelimiter(); final char delim = format.getDelimiter();
final char encapsulator = format.getEncapsulator(); final char encapsulator = format.getQuoteChar();
if (len <= 0) { if (len <= 0) {
// always quote an empty token that is the first // always quote an empty token that is the first

View File

@ -57,7 +57,7 @@ abstract class Lexer {
this.in = in; this.in = in;
this.delimiter = format.getDelimiter(); this.delimiter = format.getDelimiter();
this.escape = mapNullToDisabled(format.getEscape()); this.escape = mapNullToDisabled(format.getEscape());
this.encapsulator = mapNullToDisabled(format.getEncapsulator()); this.encapsulator = mapNullToDisabled(format.getQuoteChar());
this.commmentStart = mapNullToDisabled(format.getCommentStart()); this.commmentStart = mapNullToDisabled(format.getCommentStart());
this.ignoreSurroundingSpaces = format.getIgnoreSurroundingSpaces(); this.ignoreSurroundingSpaces = format.getIgnoreSurroundingSpaces();
this.ignoreEmptyLines = format.getIgnoreEmptyLines(); this.ignoreEmptyLines = format.getIgnoreEmptyLines();

View File

@ -47,7 +47,7 @@ public class CSVFormatTest {
format.withQuotePolicy(Quote.ALL); format.withQuotePolicy(Quote.ALL);
assertEquals('!', format.getDelimiter()); assertEquals('!', format.getDelimiter());
assertEquals('!', format.getEncapsulator().charValue()); assertEquals('!', format.getQuoteChar().charValue());
assertEquals('!', format.getCommentStart().charValue()); assertEquals('!', format.getCommentStart().charValue());
assertEquals('!', format.getEscape().charValue()); assertEquals('!', format.getEscape().charValue());
assertEquals(CRLF, format.getLineSeparator()); assertEquals(CRLF, format.getLineSeparator());
@ -63,7 +63,7 @@ public class CSVFormatTest {
final CSVFormat format = new CSVFormat('!', '!', null, '!', '!', true, true, CRLF, null); final CSVFormat format = new CSVFormat('!', '!', null, '!', '!', true, true, CRLF, null);
assertEquals('?', format.withDelimiter('?').getDelimiter()); assertEquals('?', format.withDelimiter('?').getDelimiter());
assertEquals('?', format.withEncapsulator('?').getEncapsulator().charValue()); assertEquals('?', format.withEncapsulator('?').getQuoteChar().charValue());
assertEquals('?', format.withCommentStart('?').getCommentStart().charValue()); assertEquals('?', format.withCommentStart('?').getCommentStart().charValue());
assertEquals("?", format.withLineSeparator("?").getLineSeparator()); assertEquals("?", format.withLineSeparator("?").getLineSeparator());
assertEquals('?', format.withEscape('?').getEscape().charValue()); assertEquals('?', format.withEscape('?').getEscape().charValue());
@ -171,7 +171,7 @@ public class CSVFormatTest {
assertNotNull(format); assertNotNull(format);
assertEquals("delimiter", CSVFormat.DEFAULT.getDelimiter(), format.getDelimiter()); assertEquals("delimiter", CSVFormat.DEFAULT.getDelimiter(), format.getDelimiter());
assertEquals("encapsulator", CSVFormat.DEFAULT.getEncapsulator(), format.getEncapsulator()); assertEquals("encapsulator", CSVFormat.DEFAULT.getQuoteChar(), format.getQuoteChar());
assertEquals("comment start", CSVFormat.DEFAULT.getCommentStart(), format.getCommentStart()); assertEquals("comment start", CSVFormat.DEFAULT.getCommentStart(), format.getCommentStart());
assertEquals("line separator", CSVFormat.DEFAULT.getLineSeparator(), format.getLineSeparator()); assertEquals("line separator", CSVFormat.DEFAULT.getLineSeparator(), format.getLineSeparator());
assertEquals("escape", CSVFormat.DEFAULT.getEscape(), format.getEscape()); assertEquals("escape", CSVFormat.DEFAULT.getEscape(), format.getEscape());

View File

@ -108,7 +108,7 @@ class CSVLexer1 extends Lexer {
//noop: tkn.content.append(""); //noop: tkn.content.append("");
tkn.type = EORECORD; tkn.type = EORECORD;
tkn.isReady = true; tkn.isReady = true;
} else if (c == format.getEncapsulator()) { } else if (c == format.getQuoteChar()) {
// consume encapsulated token // consume encapsulated token
encapsulatedTokenLexer(tkn, c); encapsulatedTokenLexer(tkn, c);
} else if (isEndOfFile(c)) { } else if (isEndOfFile(c)) {
@ -201,8 +201,8 @@ class CSVLexer1 extends Lexer {
if (c == format.getEscape()) { if (c == format.getEscape()) {
tkn.content.append((char) readEscape()); tkn.content.append((char) readEscape());
} else if (c == format.getEncapsulator()) { } else if (c == format.getQuoteChar()) {
if (in.lookAhead() == format.getEncapsulator()) { if (in.lookAhead() == format.getQuoteChar()) {
// double or escaped encapsulator -> add single encapsulator to token // double or escaped encapsulator -> add single encapsulator to token
c = in.read(); c = in.read();
tkn.content.append((char) c); tkn.content.append((char) c);