Fix Checkstyle: Format for 120 line length.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/csv/trunk@1383598 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
8608520aa1
commit
6c1b0fa1c4
|
@ -120,8 +120,8 @@ public class CSVFormat implements Serializable {
|
|||
* 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>
|
||||
* @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 =
|
||||
PRISTINE
|
||||
|
|
|
@ -27,24 +27,29 @@ import java.util.List;
|
|||
import java.util.Map;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
|
||||
import static org.apache.commons.csv.Token.Type.*;
|
||||
|
||||
/**
|
||||
* Parses CSV files according to the specified configuration.
|
||||
*
|
||||
* Because CSV appears in many different dialects, the parser supports many
|
||||
* configuration settings by allowing the specification of a {@link CSVFormat}.
|
||||
* Because CSV appears in many different dialects, the parser supports many configuration settings by allowing the
|
||||
* specification of a {@link CSVFormat}.
|
||||
*
|
||||
* <p>
|
||||
* Parsing of a csv-string having tabs as separators, '"' as an optional value encapsulator, and comments starting with
|
||||
* '#':
|
||||
* </p>
|
||||
*
|
||||
* <p>Parsing of a csv-string having tabs as separators,
|
||||
* '"' as an optional value encapsulator, and comments starting with '#':</p>
|
||||
* <pre>
|
||||
* CSVFormat format = new CSVFormat('\t', '"', '#');
|
||||
* Reader in = new StringReader("a\tb\nc\td");
|
||||
* List<CSVRecord> records = new CSVParser(in, format).getRecords();
|
||||
* CSVFormat format = new CSVFormat('\t', '"', '#');
|
||||
* Reader in = new StringReader("a\tb\nc\td");
|
||||
* List<CSVRecord> records = new CSVParser(in, format).getRecords();
|
||||
* </pre>
|
||||
*
|
||||
* <p>Parsing of a csv-string in Excel CSV format, using a for-each loop:</p>
|
||||
* <p>
|
||||
* Parsing of a csv-string in Excel CSV format, using a for-each loop:
|
||||
* </p>
|
||||
*
|
||||
* <pre>
|
||||
* Reader in = new StringReader("a;b\nc;d");
|
||||
* CSVParser parser = new CSVParser(in, CSVFormat.EXCEL);
|
||||
|
@ -54,11 +59,12 @@ import static org.apache.commons.csv.Token.Type.*;
|
|||
* </pre>
|
||||
*
|
||||
* <p>
|
||||
* Internal parser state is completely covered by the format
|
||||
* and the reader-state.</p>
|
||||
* Internal parser state is completely covered by the format and the reader-state.
|
||||
* </p>
|
||||
*
|
||||
* <p>see <a href="package-summary.html">package documentation</a>
|
||||
* for more details</p>
|
||||
* <p>
|
||||
* see <a href="package-summary.html">package documentation</a> for more details
|
||||
* </p>
|
||||
*/
|
||||
public class CSVParser implements Iterable<CSVRecord> {
|
||||
|
||||
|
@ -74,8 +80,10 @@ public class CSVParser implements Iterable<CSVRecord> {
|
|||
/**
|
||||
* CSV parser using the default {@link CSVFormat}.
|
||||
*
|
||||
* @param input a Reader containing "csv-formatted" input
|
||||
* @throws IllegalArgumentException thrown if the parameters of the format are inconsistent
|
||||
* @param input
|
||||
* a Reader containing "csv-formatted" input
|
||||
* @throws IllegalArgumentException
|
||||
* thrown if the parameters of the format are inconsistent
|
||||
*/
|
||||
public CSVParser(Reader input) throws IOException {
|
||||
this(input, CSVFormat.DEFAULT);
|
||||
|
@ -84,9 +92,12 @@ public class CSVParser implements Iterable<CSVRecord> {
|
|||
/**
|
||||
* Customized CSV parser using the given {@link CSVFormat}
|
||||
*
|
||||
* @param input a Reader containing "csv-formatted" input
|
||||
* @param format the CSVFormat used for CSV parsing
|
||||
* @throws IllegalArgumentException thrown if the parameters of the format are inconsistent
|
||||
* @param input
|
||||
* a Reader containing "csv-formatted" input
|
||||
* @param format
|
||||
* the CSVFormat used for CSV parsing
|
||||
* @throws IllegalArgumentException
|
||||
* thrown if the parameters of the format are inconsistent
|
||||
*/
|
||||
public CSVParser(Reader input, CSVFormat format) throws IOException {
|
||||
format.validate();
|
||||
|
@ -99,23 +110,26 @@ public class CSVParser implements Iterable<CSVRecord> {
|
|||
/**
|
||||
* Customized CSV parser using the given {@link CSVFormat}
|
||||
*
|
||||
* @param input a String containing "csv-formatted" input
|
||||
* @param format the CSVFormat used for CSV parsing
|
||||
* @throws IllegalArgumentException thrown if the parameters of the format are inconsistent
|
||||
* @param input
|
||||
* a String containing "csv-formatted" input
|
||||
* @param format
|
||||
* the CSVFormat used for CSV parsing
|
||||
* @throws IllegalArgumentException
|
||||
* thrown if the parameters of the format are inconsistent
|
||||
*/
|
||||
public CSVParser(String input, CSVFormat format) throws IOException{
|
||||
public CSVParser(String input, CSVFormat format) throws IOException {
|
||||
this(new StringReader(input), format);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Parses the CSV input according to the given format and returns the content
|
||||
* as an array of {@link CSVRecord} entries.
|
||||
* Parses the CSV input according to the given format and returns the content as an array of {@link CSVRecord}
|
||||
* entries.
|
||||
* <p/>
|
||||
* The returned content starts at the current parse-position in the stream.
|
||||
*
|
||||
* @return list of {@link CSVRecord} entries, may be empty
|
||||
* @throws IOException on parse error or input read-failure
|
||||
* @throws IOException
|
||||
* on parse error or input read-failure
|
||||
*/
|
||||
public List<CSVRecord> getRecords() throws IOException {
|
||||
List<CSVRecord> records = new ArrayList<CSVRecord>();
|
||||
|
@ -130,7 +144,8 @@ public class CSVParser implements Iterable<CSVRecord> {
|
|||
* Parses the next record from the current point in the stream.
|
||||
*
|
||||
* @return the record as an array of values, or <tt>null</tt> if the end of the stream has been reached
|
||||
* @throws IOException on parse error or input read-failure
|
||||
* @throws IOException
|
||||
* on parse error or input read-failure
|
||||
*/
|
||||
CSVRecord getRecord() throws IOException {
|
||||
CSVRecord result = new CSVRecord(null, headerMapping, null);
|
||||
|
@ -168,8 +183,8 @@ public class CSVParser implements Iterable<CSVRecord> {
|
|||
} while (reusableToken.type == TOKEN);
|
||||
|
||||
if (!record.isEmpty()) {
|
||||
result = new CSVRecord(record.toArray(new String[record.size()]), headerMapping,
|
||||
sb == null ? null : sb.toString());
|
||||
result = new CSVRecord(record.toArray(new String[record.size()]), headerMapping, sb == null ? null
|
||||
: sb.toString());
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
@ -204,8 +219,8 @@ public class CSVParser implements Iterable<CSVRecord> {
|
|||
}
|
||||
|
||||
/**
|
||||
* Returns an iterator on the records. IOExceptions occuring
|
||||
* during the iteration are wrapped in a RuntimeException.
|
||||
* Returns an iterator on the records. IOExceptions occuring during the iteration are wrapped in a
|
||||
* RuntimeException.
|
||||
*/
|
||||
public Iterator<CSVRecord> iterator() {
|
||||
return new Iterator<CSVRecord>() {
|
||||
|
@ -251,8 +266,7 @@ public class CSVParser implements Iterable<CSVRecord> {
|
|||
/**
|
||||
* Returns the current line number in the input stream.
|
||||
* <p/>
|
||||
* ATTENTION: in case your csv has multiline-values the returned
|
||||
* number does not correspond to the record-number
|
||||
* ATTENTION: in case your csv has multiline-values the returned number does not correspond to the record-number
|
||||
*
|
||||
* @return current line number
|
||||
*/
|
||||
|
|
|
@ -35,12 +35,15 @@ public class CSVPrinter {
|
|||
/**
|
||||
* Create a printer that will print values to the given stream following the CSVFormat.
|
||||
* <p/>
|
||||
* Currently, only a pure encapsulation format or a pure escaping format
|
||||
* is supported. Hybrid formats (encapsulation and escaping with a different character) are not supported.
|
||||
* Currently, only a pure encapsulation format or a pure escaping format is supported. Hybrid formats
|
||||
* (encapsulation and escaping with a different character) are not supported.
|
||||
*
|
||||
* @param out stream to which to print.
|
||||
* @param format the CSV format. If null the default format is used ({@link CSVFormat#DEFAULT})
|
||||
* @throws IllegalArgumentException thrown if the parameters of the format are inconsistent
|
||||
* @param out
|
||||
* stream to which to print.
|
||||
* @param format
|
||||
* the CSV format. If null the default format is used ({@link CSVFormat#DEFAULT})
|
||||
* @throws IllegalArgumentException
|
||||
* thrown if the parameters of the format are inconsistent
|
||||
*/
|
||||
public CSVPrinter(Appendable out, CSVFormat format) {
|
||||
this.out = out;
|
||||
|
@ -72,11 +75,11 @@ public class CSVPrinter {
|
|||
}
|
||||
|
||||
/**
|
||||
* Print a single line of comma separated values.
|
||||
* The values will be quoted if needed. Quotes and
|
||||
* newLine characters will be escaped.
|
||||
* Print a single line of comma separated values. The values will be quoted if needed. Quotes and newLine
|
||||
* characters will be escaped.
|
||||
*
|
||||
* @param values values to be outputted.
|
||||
* @param values
|
||||
* values to be outputted.
|
||||
*/
|
||||
public void println(String... values) throws IOException {
|
||||
for (String value : values) {
|
||||
|
@ -85,16 +88,15 @@ public class CSVPrinter {
|
|||
println();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Put a comment on a new line among the comma separated values. Comments
|
||||
* will always begin on a new line and occupy a least one full line. The
|
||||
* character specified to start comments and a space will be inserted at
|
||||
* the beginning of each new line in the comment.
|
||||
* Put a comment on a new line among the comma separated values. Comments will always begin on a new line and
|
||||
* occupy a least one full line. The character specified to start comments and a space will be inserted at the
|
||||
* beginning of each new line in the comment.
|
||||
* <p/>
|
||||
* If comments are disabled in the current CSV format this method does nothing.
|
||||
*
|
||||
* @param comment the comment to output
|
||||
* @param comment
|
||||
* the comment to output
|
||||
*/
|
||||
public void printComment(String comment) throws IOException {
|
||||
if (!format.isCommentingEnabled()) {
|
||||
|
@ -126,7 +128,6 @@ public class CSVPrinter {
|
|||
println();
|
||||
}
|
||||
|
||||
|
||||
private void print(CharSequence value, int offset, int len) throws IOException {
|
||||
if (format.isEncapsulating()) {
|
||||
printAndEncapsulate(value, offset, len);
|
||||
|
@ -208,11 +209,7 @@ public class CSVPrinter {
|
|||
char c = value.charAt(pos);
|
||||
|
||||
// Hmmm, where did this rule come from?
|
||||
if (first
|
||||
&& (c < '0'
|
||||
|| (c > '9' && c < 'A')
|
||||
|| (c > 'Z' && c < 'a')
|
||||
|| (c > 'z'))) {
|
||||
if (first && (c < '0' || (c > '9' && c < 'A') || (c > 'Z' && c < 'a') || (c > 'z'))) {
|
||||
quote = true;
|
||||
// } else if (c == ' ' || c == '\f' || c == '\t') {
|
||||
} else if (c <= '#') {
|
||||
|
@ -274,10 +271,11 @@ public class CSVPrinter {
|
|||
}
|
||||
|
||||
/**
|
||||
* Print the string as the next value on the line. The value
|
||||
* will be escaped or encapsulated as needed if checkForEscape==true
|
||||
* Print the string as the next value on the line. The value will be escaped or encapsulated as needed if
|
||||
* checkForEscape==true
|
||||
*
|
||||
* @param value value to be outputted.
|
||||
* @param value
|
||||
* value to be outputted.
|
||||
*/
|
||||
public void print(String value, boolean checkForEscape) throws IOException {
|
||||
if (value == null) {
|
||||
|
@ -295,10 +293,10 @@ public class CSVPrinter {
|
|||
}
|
||||
|
||||
/**
|
||||
* Print the string as the next value on the line. The value
|
||||
* will be escaped or encapsulated as needed.
|
||||
* Print the string as the next value on the line. The value will be escaped or encapsulated as needed.
|
||||
*
|
||||
* @param value value to be outputted.
|
||||
* @param value
|
||||
* value to be outputted.
|
||||
*/
|
||||
public void print(String value) throws IOException {
|
||||
print(value, true);
|
||||
|
|
|
@ -49,7 +49,8 @@ public class CSVRecord implements Serializable, Iterable<String> {
|
|||
/**
|
||||
* Returns a value by index.
|
||||
*
|
||||
* @param i the index of the column retrieved
|
||||
* @param i
|
||||
* the index of the column retrieved
|
||||
*/
|
||||
public String get(int i) {
|
||||
return values[i];
|
||||
|
@ -58,9 +59,11 @@ public class CSVRecord implements Serializable, Iterable<String> {
|
|||
/**
|
||||
* Returns a value by name.
|
||||
*
|
||||
* @param name the name of the column to be retrieved
|
||||
* @param name
|
||||
* the name of the column to be retrieved
|
||||
* @return the column value, or {@code null} if the column name is not found
|
||||
* @throws IllegalStateException if no header mapping was provided
|
||||
* @throws IllegalStateException
|
||||
* if no header mapping was provided
|
||||
*/
|
||||
public String get(String name) {
|
||||
if (mapping == null) {
|
||||
|
@ -83,6 +86,7 @@ public class CSVRecord implements Serializable, Iterable<String> {
|
|||
public String getComment() {
|
||||
return comment;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the number of values in this record.
|
||||
*/
|
||||
|
|
|
@ -22,11 +22,9 @@ import java.io.IOException;
|
|||
import java.io.Reader;
|
||||
|
||||
/**
|
||||
* A special reader decorator which supports more
|
||||
* sophisticated access to the underlying reader object.
|
||||
* A special reader decorator which supports more sophisticated access to the underlying reader object.
|
||||
* <p>
|
||||
* In particular the reader supports a look-ahead option,
|
||||
* which allows you to see the next char returned by
|
||||
* In particular the reader supports a look-ahead option, which allows you to see the next char returned by
|
||||
* {@link #read()}.
|
||||
*/
|
||||
class ExtendedBufferedReader extends BufferedReader {
|
||||
|
@ -65,12 +63,10 @@ class ExtendedBufferedReader extends BufferedReader {
|
|||
}
|
||||
|
||||
/**
|
||||
* Returns the last character that was read as an integer (0 to 65535). This
|
||||
* will be the last character returned by any of the read methods. This will
|
||||
* not include a character read using the {@link #peek()} method. If no
|
||||
* 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}.
|
||||
* Returns the last character that was read as an integer (0 to 65535). This will be the last character returned by
|
||||
* any of the read methods. This will not include a character read using the {@link #peek()} method. If no
|
||||
* 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
|
||||
*/
|
||||
|
@ -91,7 +87,7 @@ class ExtendedBufferedReader extends BufferedReader {
|
|||
for (int i = offset; i < offset + len; i++) {
|
||||
char ch = buf[i];
|
||||
if (ch == LF) {
|
||||
if (CR != (i > 0 ? buf[i-1]: lastChar)) {
|
||||
if (CR != (i > 0 ? buf[i - 1] : lastChar)) {
|
||||
lineCounter++;
|
||||
}
|
||||
} else if (ch == CR) {
|
||||
|
@ -109,14 +105,12 @@ class ExtendedBufferedReader extends BufferedReader {
|
|||
}
|
||||
|
||||
/**
|
||||
* Calls {@link BufferedReader#readLine()} which drops the line terminator(s).
|
||||
* This method should only be called when processing a comment, otherwise
|
||||
* information can be lost.
|
||||
* Calls {@link BufferedReader#readLine()} which drops the line terminator(s). This method should only be called
|
||||
* when processing a comment, otherwise information can be lost.
|
||||
* <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.
|
||||
*/
|
||||
|
@ -135,12 +129,13 @@ 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.
|
||||
* 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
|
||||
* @throws IOException
|
||||
* if there is an error in reading
|
||||
*/
|
||||
int lookAhead() throws IOException {
|
||||
super.mark(1);
|
||||
|
|
|
@ -99,8 +99,7 @@ abstract class Lexer {
|
|||
}
|
||||
|
||||
/**
|
||||
* Greedy - accepts \n, \r and \r\n
|
||||
* This checker consumes silently the second control-character...
|
||||
* Greedy - accepts \n, \r and \r\n This checker consumes silently the second control-character...
|
||||
*
|
||||
* @return true if the given character is a line-terminator
|
||||
*/
|
||||
|
@ -114,8 +113,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.
|
||||
* 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.
|
||||
|
@ -123,6 +121,7 @@ abstract class Lexer {
|
|||
boolean isStartOfLine(int c) {
|
||||
return c == '\n' || c == '\r' || c == ExtendedBufferedReader.UNDEFINED;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return true if the given character indicates end of file
|
||||
*/
|
||||
|
|
|
@ -65,6 +65,6 @@ class Token {
|
|||
// Provide toString method for IDE debugging
|
||||
@Override
|
||||
public String toString() {
|
||||
return type.name()+" ["+content.toString()+"]";
|
||||
return type.name() + " [" + content.toString() + "]";
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue