Remove trailing spaces.

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/csv/trunk@1383577 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Gary D. Gregory 2012-09-11 19:40:37 +00:00
parent 0638a37368
commit bf06bed9b8
21 changed files with 199 additions and 199 deletions

View File

@ -33,7 +33,7 @@ public class CSVFormat implements Serializable {
/** According to RFC 4180, line breaks are delimited by CRLF */
public static final String CRLF = "\r\n";
private final char delimiter;
private final char encapsulator;
private final char commentStart;
@ -60,14 +60,14 @@ public class CSVFormat implements Serializable {
*/
static final CSVFormat PRISTINE = new CSVFormat(DISABLED, DISABLED, DISABLED, DISABLED, false, false, null, null);
/**
* Standard comma separated format, as for {@link #RFC4180} but allowing blank lines.
/**
* Standard comma separated format, as for {@link #RFC4180} but allowing blank lines.
* <ul>
* <li>withDelimiter(',')</li>
* <li>withEncapsulator('"')</li>
* <li>withEmptyLinesIgnored(true)</li>
* <li>withLineSeparator(CRLF)</li>
* </ul>
* </ul>
*/
public static final CSVFormat DEFAULT =
PRISTINE.
@ -83,7 +83,7 @@ public class CSVFormat implements Serializable {
* <li>withEncapsulator('"')</li>
* <li>withLineSeparator(CRLF)</li>
* <li></li>
* </ul>
* </ul>
*/
public static final CSVFormat RFC4180 =
PRISTINE.
@ -99,7 +99,7 @@ public class CSVFormat implements Serializable {
* <p/>
* For example for parsing or generating a CSV file on a French system
* the following format will be used:
*
*
* <pre>CSVFormat fmt = CSVFormat.EXCEL.withDelimiter(';');</pre>
*/
public static final CSVFormat EXCEL =
@ -122,7 +122,7 @@ public class CSVFormat implements Serializable {
* <tt>LOAD DATA INFILE</tt> operations. This is a tab-delimited
* format with a LF character as the line separator. Values are not quoted
* and special characters are escaped with '\'.
*
*
* @see <a href="http://dev.mysql.com/doc/refman/5.1/en/load-data.html">http://dev.mysql.com/doc/refman/5.1/en/load-data.html</a>
*/
public static final CSVFormat MYSQL =
@ -168,9 +168,9 @@ public class CSVFormat implements Serializable {
/**
* Returns true if the given character is a line break character.
*
*
* @param c the character to check
*
*
* @return true if <code>c</code> is a line break character
*/
private static boolean isLineBreak(char c) {
@ -184,19 +184,19 @@ public class CSVFormat implements Serializable {
if (delimiter == encapsulator) {
throw new IllegalArgumentException("The encapsulator character and the delimiter cannot be the same (\"" + encapsulator + "\")");
}
if (delimiter == escape) {
throw new IllegalArgumentException("The escape character and the delimiter cannot be the same (\"" + escape + "\")");
}
if (delimiter == commentStart) {
throw new IllegalArgumentException("The comment start character and the delimiter cannot be the same (\"" + commentStart + "\")");
}
if (encapsulator != DISABLED && encapsulator == commentStart) {
throw new IllegalArgumentException("The comment start character and the encapsulator cannot be the same (\"" + commentStart + "\")");
}
if (escape != DISABLED && escape == commentStart) {
throw new IllegalArgumentException("The comment start and the escape character cannot be the same (\"" + commentStart + "\")");
}
@ -204,7 +204,7 @@ public class CSVFormat implements Serializable {
/**
* Returns the character delimiting the values (typically ';', ',' or '\t').
*
*
* @return the delimiter character
*/
public char getDelimiter() {
@ -213,7 +213,7 @@ public class CSVFormat implements Serializable {
/**
* Returns a copy of this format using the specified delimiter character.
*
*
* @param delimiter the delimiter character
* @return A copy of this format using the specified delimiter character
* @throws IllegalArgumentException thrown if the specified character is a line break
@ -228,7 +228,7 @@ public class CSVFormat implements Serializable {
/**
* Returns the character used to encapsulate values containing special characters.
*
*
* @return the encapsulator character
*/
public char getEncapsulator() {
@ -237,7 +237,7 @@ public class CSVFormat implements Serializable {
/**
* Returns a copy of this format using the specified encapsulator character.
*
*
* @param encapsulator the encapsulator character
* @return A copy of this format using the specified encapsulator character
* @throws IllegalArgumentException thrown if the specified character is a line break
@ -246,13 +246,13 @@ public class CSVFormat implements Serializable {
if (isLineBreak(encapsulator)) {
throw new IllegalArgumentException("The encapsulator cannot be a line break");
}
return new CSVFormat(delimiter, encapsulator, commentStart, escape, surroundingSpacesIgnored, emptyLinesIgnored, lineSeparator, header);
}
/**
* Returns whether an encapsulator has been defined.
*
*
* @return {@code true} if an encapsulator is defined
*/
public boolean isEncapsulating() {
@ -261,7 +261,7 @@ public class CSVFormat implements Serializable {
/**
* Returns the character marking the start of a line comment.
*
*
* @return the comment start marker.
*/
public char getCommentStart() {
@ -270,10 +270,10 @@ public class CSVFormat implements Serializable {
/**
* Returns a copy of this format using the specified character as the comment start marker.
*
*
* Note that the comment introducer character is only recognised
* at the start of a line.
*
*
* @param commentStart the comment start marker
* @return A copy of this format using the specified character as the comment start marker
* @throws IllegalArgumentException thrown if the specified character is a line break
@ -282,16 +282,16 @@ public class CSVFormat implements Serializable {
if (isLineBreak(commentStart)) {
throw new IllegalArgumentException("The comment start character cannot be a line break");
}
return new CSVFormat(delimiter, encapsulator, commentStart, escape, surroundingSpacesIgnored, emptyLinesIgnored, lineSeparator, header);
}
/**
* Specifies whether comments are supported by this format.
*
*
* Note that the comment introducer character is only recognised
* at the start of a line.
*
*
* @return <tt>true</tt> is comments are supported, <tt>false</tt> otherwise
*/
public boolean isCommentingEnabled() {
@ -300,7 +300,7 @@ public class CSVFormat implements Serializable {
/**
* Returns the escape character.
*
*
* @return the escape character
*/
public char getEscape() {
@ -309,7 +309,7 @@ public class CSVFormat implements Serializable {
/**
* Returns a copy of this format using the specified escape character.
*
*
* @param escape the escape character
* @return A copy of this format using the specified escape character
* @throws IllegalArgumentException thrown if the specified character is a line break
@ -318,13 +318,13 @@ public class CSVFormat implements Serializable {
if (isLineBreak(escape)) {
throw new IllegalArgumentException("The escape character cannot be a line break");
}
return new CSVFormat(delimiter, encapsulator, commentStart, escape, surroundingSpacesIgnored, emptyLinesIgnored, lineSeparator, header);
}
/**
* Returns whether escape are being processed.
*
*
* @return {@code true} if escapes are processed
*/
public boolean isEscaping() {
@ -333,7 +333,7 @@ public class CSVFormat implements Serializable {
/**
* Specifies whether spaces around values are ignored when parsing input.
*
*
* @return <tt>true</tt> if spaces around values are ignored, <tt>false</tt> if they are treated as part of the value.
*/
public boolean isSurroundingSpacesIgnored() {
@ -353,7 +353,7 @@ public class CSVFormat implements Serializable {
/**
* Specifies whether empty lines between records are ignored when parsing input.
*
*
* @return <tt>true</tt> if empty lines between records are ignored, <tt>false</tt> if they are turned into empty records.
*/
public boolean isEmptyLinesIgnored() {
@ -373,7 +373,7 @@ public class CSVFormat implements Serializable {
/**
* Returns the line separator delimiting output records.
*
*
* @return the line separator
*/
public String getLineSeparator() {
@ -382,9 +382,9 @@ public class CSVFormat implements Serializable {
/**
* 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(String lineSeparator) {
@ -415,7 +415,7 @@ public class CSVFormat implements Serializable {
/**
* Parses the specified content.
*
*
* @param in the input stream
*/
public Iterable<CSVRecord> parse(Reader in) throws IOException {
@ -424,7 +424,7 @@ public class CSVFormat implements Serializable {
/**
* Format the specified values.
*
*
* @param values the values to format
*/
public String format(String... values) {
@ -435,7 +435,7 @@ public class CSVFormat implements Serializable {
} catch (IOException e) {
// should not happen
throw new IllegalStateException(e);
}
}
}
@Override
@ -448,19 +448,19 @@ public class CSVFormat implements Serializable {
}
if (isEncapsulating()) {
sb.append(' ');
sb.append("Encapsulator=<").append(encapsulator).append('>');
sb.append("Encapsulator=<").append(encapsulator).append('>');
}
if (isCommentingEnabled()) {
sb.append(' ');
sb.append("CommentStart=<").append(commentStart).append('>');
}
if (isEmptyLinesIgnored()) {
sb.append(" EmptyLines:ignored");
sb.append(" EmptyLines:ignored");
}
if (isSurroundingSpacesIgnored()) {
sb.append(" SurroundingSpaces:ignored");
sb.append(" SurroundingSpaces:ignored");
}
return sb.toString();
}
}

View File

@ -27,7 +27,7 @@ class CSVLexer extends Lexer {
public CSVLexer(CSVFormat format, ExtendedBufferedReader in) {
super(format, in);
}
/**
* Returns the next token.
* <p/>
@ -92,7 +92,7 @@ class CSVLexer extends Lexer {
eol = isEndOfLine(c);
}
}
// ok, start of token reached: encapsulated, or token
if (isDelimiter(c)) {
// empty token return TOKEN("")
@ -183,7 +183,7 @@ class CSVLexer extends Lexer {
*
* @param tkn the current token
* @return a valid token object
* @throws IOException on invalid state:
* @throws IOException on invalid state:
* EOF before closing encapsulator or invalid character before delimiter or EOL
*/
private Token encapsulatedTokenLexer(Token tkn) throws IOException {
@ -192,7 +192,7 @@ class CSVLexer extends Lexer {
int c;
while (true) {
c = in.read();
if (isEscape(c)) {
tkn.content.append((char) readEscape());
} else if (isEncapsulator(c)) {

View File

@ -66,7 +66,7 @@ public class CSVParser implements Iterable<CSVRecord> {
private final Map<String, Integer> headerMapping;
// the following objects are shared to reduce garbage
/** A record buffer for getRecord(). Grows as necessary and is reused. */
private final List<String> record = new ArrayList<String>();
private final Token reusableToken = new Token();
@ -90,9 +90,9 @@ public class CSVParser implements Iterable<CSVRecord> {
*/
public CSVParser(Reader input, CSVFormat format) throws IOException {
format.validate();
this.lexer = new CSVLexer(format, new ExtendedBufferedReader(input));
this.headerMapping = initializeHeader(format);
}
@ -166,9 +166,9 @@ public class CSVParser implements Iterable<CSVRecord> {
break;
}
} while (reusableToken.type == TOKEN);
if (!record.isEmpty()) {
result = new CSVRecord(record.toArray(new String[record.size()]), headerMapping,
result = new CSVRecord(record.toArray(new String[record.size()]), headerMapping,
sb == null ? null : sb.toString());
}
return result;
@ -210,12 +210,12 @@ public class CSVParser implements Iterable<CSVRecord> {
public Iterator<CSVRecord> iterator() {
return new Iterator<CSVRecord>() {
private CSVRecord current;
public boolean hasNext() {
if (current == null) {
current = getNextRecord();
}
return current != null;
}
@ -230,10 +230,10 @@ public class CSVParser implements Iterable<CSVRecord> {
throw new NoSuchElementException("No more CSV records available");
}
}
return next;
}
private CSVRecord getNextRecord() {
try {
return getRecord();

View File

@ -62,7 +62,7 @@ public class CSVPrinter {
/**
* Flush the underlying stream.
*
*
* @throws IOException
*/
public void flush() throws IOException {
@ -127,7 +127,7 @@ public class CSVPrinter {
}
private void print(CharSequence value, int offset, int len) throws IOException {
private void print(CharSequence value, int offset, int len) throws IOException {
if (format.isEncapsulating()) {
printAndEncapsulate(value, offset, len);
} else if (format.isEscaping()) {
@ -284,7 +284,7 @@ public class CSVPrinter {
// null values are considered empty
value = "";
}
if (!checkForEscape) {
// write directly from string
printSep();

View File

@ -26,11 +26,11 @@ import java.util.Map;
* A CSV record
*/
public class CSVRecord implements Serializable, Iterable<String> {
private static final long serialVersionUID = 1L;
private static final String[] EMPTY_STRING_ARRAY = new String[0];
/** The values of the record */
private final String[] values;

View File

@ -71,7 +71,7 @@ class ExtendedBufferedReader extends BufferedReader {
* character has been read then this will return {@link #UNDEFINED}. If the
* end of the stream was reached on the last read then this will return
* {@link #END_OF_STREAM}.
*
*
* @return the last character that was read
*/
int readAgain() {
@ -83,16 +83,16 @@ class ExtendedBufferedReader extends BufferedReader {
if (length == 0) {
return 0;
}
int len = super.read(buf, offset, length);
if (len > 0) {
for (int i = offset; i < offset + len; i++) {
char ch = buf[i];
if (ch == LF) {
if (CR != (i > 0 ? buf[i-1]: lastChar)) {
lineCounter++;
lineCounter++;
}
} else if (ch == CR) {
lineCounter++;
@ -104,7 +104,7 @@ class ExtendedBufferedReader extends BufferedReader {
} else if (len == -1) {
lastChar = END_OF_STREAM;
}
return len;
}
@ -115,9 +115,9 @@ class ExtendedBufferedReader extends BufferedReader {
* <p>
* Increments {@link #lineCounter}
* <p>
* Sets {@link #lastChar} to {@link #END_OF_STREAM} at EOF,
* otherwise to LF
*
* Sets {@link #lastChar} to {@link #END_OF_STREAM} at EOF,
* otherwise to LF
*
* @return the line that was read, or null if reached EOF.
*/
@Override
@ -137,9 +137,9 @@ class ExtendedBufferedReader extends BufferedReader {
/**
* Returns the next character in the current reader without consuming it. So
* the next call to {@link #read()} will still return this value.
*
*
* @return the next character
*
*
* @throws IOException if there is an error in reading
*/
int lookAhead() throws IOException {

View File

@ -27,17 +27,17 @@ abstract class Lexer {
private final boolean isEncapsulating;
private final boolean isEscaping;
private final boolean isCommentEnabled;
private final char delimiter;
private final char escape;
private final char encapsulator;
private final char commmentStart;
final boolean surroundingSpacesIgnored;
final boolean emptyLinesIgnored;
final CSVFormat format;
/** The input stream */
final ExtendedBufferedReader in;
@ -116,7 +116,7 @@ abstract class Lexer {
/**
* Checks if the current character represents the start of a line:
* a CR, LF or is at the start of the file.
*
*
* @param c
* @return true if the character is at the start of a line.
*/
@ -131,7 +131,7 @@ abstract class Lexer {
}
abstract Token nextToken(Token reusableToken) throws IOException;
boolean isDelimiter(int c) {
return c == delimiter;
}

View File

@ -28,30 +28,30 @@ class Token {
/** length of the initial token (content-)buffer */
private static final int INITIAL_TOKEN_LENGTH = 50;
enum Type {
/** Token has no valid content, i.e. is in its initialized state. */
INVALID,
/** Token with content, at beginning or in the middle of a line. */
TOKEN,
/** Token (which can have content) when end of file is reached. */
EOF,
/** Token with content when end of a line is reached. */
EORECORD,
/** Token is a comment line */
COMMENT
}
/** Token type */
Token.Type type = INVALID;
/** The content buffer. */
StringBuilder content = new StringBuilder(INITIAL_TOKEN_LENGTH);
/** Token ready flag: indicates a valid token with content (ready for the parser). */
boolean isReady;

View File

@ -17,11 +17,11 @@
/**
* Apache Commons CSV Format Support.
*
*
* <p>CSV (or its dialects) are widely used as interfaces to legacy systems or
* manual data-imports. Basically CSV stands for "Comma Separated Values" but
* this simple abbreviation leads to more confusion than definitions.</p>
*
*
* <p>Common to all file dialects is its basic structure: The CSV data-format
* is record oriented, whereas each record starts on a new textual line. A
* record is build of a list of values. Keep in mind that not all records
@ -30,41 +30,41 @@
* csv := records*
* record := values*
* </pre>
*
*
* <p>The following list contains the csv aspects the Commons CSV parser supports:</p>
* <dl>
* <dt>Separators (for lines)</dt>
* <dd>The record separators are hardcoded and cannot be changed. The must be '\r', '\n' or '\r\n'.</dd>
*
*
* <dt>Delimiter (for values)</dt>
* <dd>The delimiter for values is freely configurable (default ',').</dd>
*
*
* <dt>Comments</dt>
* <dd>Some CSV-dialects support a simple comment syntax. A comment is a record
* which must start with a designated character (the commentStarter). A record
* of this kind is treated as comment and gets removed from the input (default none)</dd>
*
*
* <dt>Encapsulator</dt>
* <dd>Two encapsulator characters (default '"') are used to enclose -&gt; complex values.</dd>
*
*
* <dt>Simple values</dt>
* <dd>A simple value consist of all characters (except the delimiter) until
* <dd>A simple value consist of all characters (except the delimiter) until
* (but not including) the next delimiter or a record-terminator. Optionally
* all surrounding whitespaces of a simple value can be ignored (default: true).</dd>
*
*
* <dt>Complex values</dt>
* <dd>Complex values are encapsulated within a pair of the defined encapsulator characters.
* The encapsulator itself must be escaped or doubled when used inside complex values.
* Complex values preserve all kind of formatting (including newlines -&gt; multiline-values)</dd>
*
*
* <dt>Empty line skipping</dt>
* <dd>Optionally empty lines in CSV files can be skipped.
* <dd>Optionally empty lines in CSV files can be skipped.
* Otherwise, empty lines will return a record with a single empty value.</dd>
* </dl>
*
* <p>In addition to individually defined dialects, two predefined dialects (strict-csv, and excel-csv)
*
* <p>In addition to individually defined dialects, two predefined dialects (strict-csv, and excel-csv)
* can be set directly.</p> <!-- TODO fix -->
*
*
* <p>Example usage:</p>
* <blockquote><pre>
* Reader in = new StringReader("a,b,c");

View File

@ -5,9 +5,9 @@
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@ -45,7 +45,7 @@ import org.junit.runners.Parameterized.Parameters;
public class CSVFileParserTest {
private static final File BASE = new File("src/test/resources/CSVFileParser");
private final BufferedReader testData;
private final String testName;
@ -67,7 +67,7 @@ public class CSVFileParserTest {
public static Collection<Object[]> generateData()
{
List<Object[]> list = new ArrayList<Object[]>();
final FilenameFilter filenameFilter = new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.startsWith("test") && name.endsWith(".txt");
@ -79,7 +79,7 @@ public class CSVFileParserTest {
}
return list;
}
@Test
public void testCSVFile() throws Exception {
String line = readTestData();
@ -107,7 +107,7 @@ public class CSVFileParserTest {
}
line = readTestData(); // get string version of format
assertEquals(testName+" Expected format ", line, fmt.toString());
// Now parse the file and compare against the expected results
for(CSVRecord rec : fmt.parse(csvFile)) {
String parsed = rec.toString();

View File

@ -5,9 +5,9 @@
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@ -31,7 +31,7 @@ public class CSVFormatTest {
@Test
public void testImmutalibity() {
CSVFormat format = new CSVFormat('!', '!', '!', '!', true, true, CSVFormat.CRLF, null);
format.withDelimiter('?');
format.withEncapsulator('?');
format.withCommentStart('?');
@ -39,13 +39,13 @@ public class CSVFormatTest {
format.withEscape('?');
format.withSurroundingSpacesIgnored(false);
format.withEmptyLinesIgnored(false);
assertEquals('!', format.getDelimiter());
assertEquals('!', format.getEncapsulator());
assertEquals('!', format.getCommentStart());
assertEquals('!', format.getEscape());
assertEquals(CSVFormat.CRLF, format.getLineSeparator());
assertTrue(format.isSurroundingSpacesIgnored());
assertTrue(format.isEmptyLinesIgnored());
}
@ -53,13 +53,13 @@ public class CSVFormatTest {
@Test
public void testMutators() {
CSVFormat format = new CSVFormat('!', '!', '!', '!', true, true, CSVFormat.CRLF, null);
assertEquals('?', format.withDelimiter('?').getDelimiter());
assertEquals('?', format.withEncapsulator('?').getEncapsulator());
assertEquals('?', format.withCommentStart('?').getCommentStart());
assertEquals("?", format.withLineSeparator("?").getLineSeparator());
assertEquals('?', format.withEscape('?').getEscape());
assertFalse(format.withSurroundingSpacesIgnored(false).isSurroundingSpacesIgnored());
assertFalse(format.withEmptyLinesIgnored(false).isEmptyLinesIgnored());
}
@ -67,77 +67,77 @@ public class CSVFormatTest {
@Test
public void testFormat() {
CSVFormat format = CSVFormat.DEFAULT;
assertEquals("", format.format());
assertEquals("a,b,c", format.format("a", "b", "c"));
assertEquals("\"x,y\",z", format.format("x,y", "z"));
}
@Test
public void testValidation() {
CSVFormat format = CSVFormat.DEFAULT;
try {
format.withDelimiter('\n');
fail();
} catch (IllegalArgumentException e) {
// expected
}
try {
format.withEscape('\r');
fail();
} catch (IllegalArgumentException e) {
// expected
}
try {
format.withEncapsulator('\n');
fail();
} catch (IllegalArgumentException e) {
// expected
}
try {
format.withCommentStart('\r');
fail();
} catch (IllegalArgumentException e) {
// expected
}
try {
format.withDelimiter('!').withEscape('!').validate();
fail();
} catch (IllegalArgumentException e) {
// expected
}
try {
format.withDelimiter('!').withCommentStart('!').validate();
fail();
} catch (IllegalArgumentException e) {
// expected
}
try {
format.withEncapsulator('!').withCommentStart('!').validate();
fail();
} catch (IllegalArgumentException e) {
// expected
}
format.withEncapsulator(CSVFormat.DISABLED).withCommentStart(CSVFormat.DISABLED).validate();
try {
format.withEscape('!').withCommentStart('!').validate();
fail();
} catch (IllegalArgumentException e) {
// expected
}
format.withEscape(CSVFormat.DISABLED).withCommentStart(CSVFormat.DISABLED).validate();
try {
format.withEncapsulator('!').withDelimiter('!').validate();
fail();
@ -150,15 +150,15 @@ public class CSVFormatTest {
@Test
public void testSerialization() throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(out);
oos.writeObject(CSVFormat.DEFAULT);
oos.flush();
oos.close();
ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(out.toByteArray()));
CSVFormat format = (CSVFormat) in.readObject();
assertNotNull(format);
assertEquals("delimiter", CSVFormat.DEFAULT.getDelimiter(), format.getDelimiter());
assertEquals("encapsulator", CSVFormat.DEFAULT.getEncapsulator(), format.getEncapsulator());
@ -168,4 +168,4 @@ public class CSVFormatTest {
assertEquals("trim", CSVFormat.DEFAULT.isSurroundingSpacesIgnored(), format.isSurroundingSpacesIgnored());
assertEquals("empty lines", CSVFormat.DEFAULT.isEmptyLinesIgnored(), format.isEmptyLinesIgnored());
}
}
}

View File

@ -5,9 +5,9 @@
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@ -24,12 +24,12 @@ import static org.apache.commons.csv.Token.Type.*;
class CSVLexer1 extends Lexer {
private final StringBuilder wsBuf = new StringBuilder();
// ctor needs to be public so can be called dynamically by PerformanceTest class
public CSVLexer1(CSVFormat format, ExtendedBufferedReader in) {
super(format, in);
}
/**
* Returns the next token.
* <p/>
@ -89,7 +89,7 @@ class CSVLexer1 extends Lexer {
eol = isEndOfLine(c);
}
}
// ok, start of token reached: comment, encapsulated, or token
if (c == format.getCommentStart()) {
// ignore everything till end of line and continue (incr linecount)
@ -194,7 +194,7 @@ class CSVLexer1 extends Lexer {
// assert c == delimiter;
while (true) {
c = in.read();
if (c == format.getEscape()) {
tkn.content.append((char) readEscape());
} else if (c == format.getEncapsulator()) {

View File

@ -5,9 +5,9 @@
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@ -27,7 +27,7 @@ class CSVLexer1306663 extends Lexer {
public CSVLexer1306663(CSVFormat format, ExtendedBufferedReader in) {
super(format, in);
}
/**
* Returns the next token.
* <p/>
@ -92,7 +92,7 @@ class CSVLexer1306663 extends Lexer {
eol = isEndOfLine(c);
}
}
// ok, start of token reached: encapsulated, or token
if (isDelimiter(c)) {
// empty token return TOKEN("")
@ -184,7 +184,7 @@ class CSVLexer1306663 extends Lexer {
int c;
while (true) {
c = in.read();
if (isEscape(c)) {
tkn.content.append((char) readEscape());
} else if (isEncapsulator(c)) {

View File

@ -5,9 +5,9 @@
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@ -27,7 +27,7 @@ class CSVLexer1306667 extends Lexer {
public CSVLexer1306667(CSVFormat format, ExtendedBufferedReader in) {
super(format, in);
}
/**
* Returns the next token.
* <p/>
@ -92,7 +92,7 @@ class CSVLexer1306667 extends Lexer {
eol = isEndOfLine(c);
}
}
// ok, start of token reached: encapsulated, or token
if (isDelimiter(c)) {
// empty token return TOKEN("")
@ -184,7 +184,7 @@ class CSVLexer1306667 extends Lexer {
int c;
while (true) {
c = in.read();
if (isEscape(c)) {
tkn.content.append((char) readEscape());
} else if (isEncapsulator(c)) {

View File

@ -5,9 +5,9 @@
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@ -25,18 +25,18 @@ import static org.apache.commons.csv.Token.Type.*;
* Experimental Lexer using enums to keep track of state and character type.
* Unfortunately it is twice as slow.
* For reference purpose only.
*
*
*/
class CSVLexer3 extends Lexer {
private final char escape;
// ctor needs to be public so can be called dynamically by PerformanceTest class
public CSVLexer3(CSVFormat format, ExtendedBufferedReader in) {
super(format, in);
this.escape = format.getEscape();
}
/**
* Classify the character types
*/
@ -130,7 +130,7 @@ class CSVLexer3 extends Lexer {
case WHITESPACE:
if (!surroundingSpacesIgnored){
tkn.content.append((char) intch);
state = State.PLAIN;
state = State.PLAIN;
}
break;
}
@ -165,7 +165,7 @@ class CSVLexer3 extends Lexer {
break;
case EOFCHAR:
throw new IOException("(line " + getLineNumber() + ") unexpected EOF in quoted string");
default:
default:
tkn.content.append((char) intch);
break;
}
@ -209,7 +209,7 @@ class CSVLexer3 extends Lexer {
break;
case COMMENT_START: // TODO should comment be escaped?
case ENCAP: // TODO is this correct?
case OTHER: // TODO may need to escape further
case OTHER: // TODO may need to escape further
case WHITESPACE:
tkn.content.append(escape);
tkn.content.append((char) intch);

View File

@ -5,9 +5,9 @@
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@ -26,7 +26,7 @@ import static org.apache.commons.csv.Token.Type.*;
import static org.junit.Assert.*;
public class CSVLexerTest {
private Lexer getLexer(String input, CSVFormat format) {
return new CSVLexer(format, new ExtendedBufferedReader(new StringReader(input)));
}
@ -35,7 +35,7 @@ public class CSVLexerTest {
assertEquals("Token type", expectedType, token.type);
assertEquals("Token content", expectedContent, token.content.toString());
}
// Single line (without comment)
@Test
public void testNextToken1() throws IOException {
@ -56,7 +56,7 @@ public class CSVLexerTest {
// multiline including comments (and empty lines)
@Test
public void testNextToken2() throws IOException {
final String code =
final String code =
"1,2,3,\n"+ // 1
"\n"+
"\n"+
@ -74,7 +74,7 @@ public class CSVLexerTest {
"# Final comment\n"; // 7
CSVFormat format = CSVFormat.DEFAULT.withCommentStart('#');
assertTrue("Should ignore empty lines", format.isEmptyLinesIgnored());
Lexer parser = getLexer(code, format);
@ -100,7 +100,7 @@ public class CSVLexerTest {
// multiline including comments (and empty lines)
@Test
public void testNextToken2EmptyLines() throws IOException {
final String code =
final String code =
"1,2,3,\n"+ // 1
"\n"+ // 1b
"\n"+ // 1c
@ -117,7 +117,7 @@ public class CSVLexerTest {
"# Final comment\n"; // 7
CSVFormat format = CSVFormat.DEFAULT.withCommentStart('#').withEmptyLinesIgnored(false);
assertFalse("Should not ignore empty lines", format.isEmptyLinesIgnored());
Lexer parser = getLexer(code, format);

View File

@ -5,9 +5,9 @@
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@ -60,7 +60,7 @@ public class CSVParserTest {
for (String[] re : RESULT) {
assertArrayEquals(re, parser.getRecord().values());
}
assertNull(parser.getRecord());
}
@ -132,7 +132,7 @@ public class CSVParserTest {
{""}, // Excel format does not ignore empty lines
{"world", ""}
};
for (String code : codes) {
CSVParser parser = new CSVParser(code, CSVFormat.EXCEL);
List<CSVRecord> records = parser.getRecords();
@ -369,7 +369,7 @@ public class CSVParserTest {
format = CSVFormat.DEFAULT.withCommentStart('#');
parser = new CSVParser(code, format);
records = parser.getRecords();
Utils.compare("Failed to parse with comments", res_comments, records);
}
@ -410,13 +410,13 @@ public class CSVParserTest {
@Test
public void testForEach() throws Exception {
List<CSVRecord> records = new ArrayList<CSVRecord>();
Reader in = new StringReader("a,b,c\n1,2,3\nx,y,z");
for (CSVRecord record : CSVFormat.DEFAULT.parse(in)) {
records.add(record);
}
assertEquals(3, records.size());
assertArrayEquals(new String[]{"a", "b", "c"}, records.get(0).values());
assertArrayEquals(new String[]{"1", "2", "3"}, records.get(1).values());
@ -426,9 +426,9 @@ public class CSVParserTest {
@Test
public void testIterator() throws Exception {
Reader in = new StringReader("a,b,c\n1,2,3\nx,y,z");
Iterator<CSVRecord> iterator = CSVFormat.DEFAULT.parse(in).iterator();
assertTrue(iterator.hasNext());
try {
iterator.remove();
@ -442,7 +442,7 @@ public class CSVParserTest {
assertTrue(iterator.hasNext());
assertArrayEquals(new String[]{"x", "y", "z"}, iterator.next().values());
assertFalse(iterator.hasNext());
try {
iterator.next();
fail("NoSuchElementException expected");
@ -450,13 +450,13 @@ public class CSVParserTest {
// expected
}
}
@Test
public void testHeader() throws Exception {
Reader in = new StringReader("a,b,c\n1,2,3\nx,y,z");
Iterator<CSVRecord> records = CSVFormat.DEFAULT.withHeader().parse(in).iterator();
for (int i = 0; i < 2; i++) {
assertTrue(records.hasNext());
CSVRecord record = records.next();
@ -464,7 +464,7 @@ public class CSVParserTest {
assertEquals(record.get(1), record.get("b"));
assertEquals(record.get(2), record.get("c"));
}
assertFalse(records.hasNext());
}
@ -473,7 +473,7 @@ public class CSVParserTest {
Reader in = new StringReader("# comment\na,b,c\n1,2,3\nx,y,z");
Iterator<CSVRecord> records = CSVFormat.DEFAULT.withCommentStart('#').withHeader().parse(in).iterator();
for (int i = 0; i < 2; i++) {
assertTrue(records.hasNext());
CSVRecord record = records.next();
@ -481,7 +481,7 @@ public class CSVParserTest {
assertEquals(record.get(1), record.get("b"));
assertEquals(record.get(2), record.get("c"));
}
assertFalse(records.hasNext());
}
@ -505,7 +505,7 @@ public class CSVParserTest {
@Test
public void testGetLineNumberWithLF() throws Exception {
CSVParser parser = new CSVParser("a\nb\nc", CSVFormat.DEFAULT.withLineSeparator("\n"));
assertEquals(0, parser.getLineNumber());
assertNotNull(parser.getRecord());
assertEquals(1, parser.getLineNumber());
@ -519,7 +519,7 @@ public class CSVParserTest {
@Test
public void testGetLineNumberWithCRLF() throws Exception {
CSVParser parser = new CSVParser("a\r\nb\r\nc", CSVFormat.DEFAULT.withLineSeparator(CSVFormat.CRLF));
assertEquals(0, parser.getLineNumber());
assertNotNull(parser.getRecord());
assertEquals(1, parser.getLineNumber());
@ -533,7 +533,7 @@ public class CSVParserTest {
@Test
public void testGetLineNumberWithCR() throws Exception {
CSVParser parser = new CSVParser("a\rb\rc", CSVFormat.DEFAULT.withLineSeparator("\r"));
assertEquals(0, parser.getLineNumber());
assertNotNull(parser.getRecord());
assertEquals(1, parser.getLineNumber());

View File

@ -5,9 +5,9 @@
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@ -115,7 +115,7 @@ public class CSVPrinterTest {
StringWriter sw = new StringWriter();
CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
printer.printComment("This is a comment");
assertEquals("", sw.toString());
}
@ -124,7 +124,7 @@ public class CSVPrinterTest {
StringWriter sw = new StringWriter();
CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withCommentStart('#'));
printer.printComment("This is a comment");
assertEquals("# This is a comment" + lineSeparator, sw.toString());
}
@ -133,7 +133,7 @@ public class CSVPrinterTest {
StringWriter sw = new StringWriter();
CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withCommentStart('#'));
printer.printComment("This is a comment\non multiple lines");
assertEquals("# This is a comment" + lineSeparator + "# on multiple lines" + lineSeparator, sw.toString());
}
@ -153,7 +153,7 @@ public class CSVPrinterTest {
public void doOneRandom(CSVFormat format) throws Exception {
Random r = new Random();
int nLines = r.nextInt(4) + 1;
int nCol = r.nextInt(3) + 1;
// nLines=1;nCol=2;
@ -199,7 +199,7 @@ public class CSVPrinterTest {
public String randStr() {
Random r = new Random();
int sz = r.nextInt(20);
// sz = r.nextInt(3);
char[] buf = new char[sz];

View File

@ -5,9 +5,9 @@
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@ -90,7 +90,7 @@ public class ExtendedBufferedReaderTest {
public void testReadLookahead2() throws Exception {
char[] ref = new char[5];
char[] res = new char[5];
ExtendedBufferedReader br = getBufferedReader("abcdefg");
ref[0] = 'a';
ref[1] = 'b';
@ -151,7 +151,7 @@ public class ExtendedBufferedReaderTest {
/*
* Test to illustrate https://issues.apache.org/jira/browse/CSV-75
*
*
*/
@Test
public void testReadChar() throws Exception {
@ -160,7 +160,7 @@ public class ExtendedBufferedReaderTest {
// EOL eol EOL EOL eol eol EOL+CR EOL
final int EOLeolct=9;
ExtendedBufferedReader br;
br = getBufferedReader(test);
assertEquals(0, br.getLineNumber());
while(br.readLine()!=null) {}

View File

@ -13,7 +13,7 @@
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*
*/
package org.apache.commons.csv;
@ -29,10 +29,10 @@ public class Utils {
private Utils() {
}
/**
* Checks if the two 2d arrays have identical contents.
*
*
* @param message the message to be displayed
* @param expected the 2d array of expected results
* @param actual the 2d array of actual results
@ -43,10 +43,10 @@ public class Utils {
Assert.assertArrayEquals(message+" (entry "+i+")",expected[i], actual[i]);
}
}
/**
* Checks if the 2d array has the same contents as the list of records.
*
*
* @param message the message to be displayed
* @param expected the 2d array of expected results
* @param actual the List of {@link CSVRecord} entries, each containing an array of values

View File

@ -5,9 +5,9 @@
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@ -28,7 +28,7 @@ import org.junit.Test;
/**
* Tests performance.
*
*
* Only enable for your own development.
*/
public class PerformanceTest {