Skip to content

Commit b7a36d2

Browse files
committed
Merge commit 'bf8f23c3104a137cb42e13bd69b10321cdf92135' into rdptest
2 parents 16572b7 + bf8f23c commit b7a36d2

13 files changed

Lines changed: 230 additions & 134 deletions

File tree

NOTICE.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Apache Commons CSV
2-
Copyright 2005-2013 The Apache Software Foundation
2+
Copyright 2005-2014 The Apache Software Foundation
33

44
This product includes software developed at
55
The Apache Software Foundation (http://www.apache.org/).

pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@ limitations under the License.
2121
<parent>
2222
<groupId>org.apache.commons</groupId>
2323
<artifactId>commons-parent</artifactId>
24-
<version>32</version>
24+
<version>33</version>
2525
</parent>
2626
<groupId>org.apache.commons</groupId>
2727
<artifactId>commons-csv</artifactId>
2828
<version>1.0-SNAPSHOT</version>
29-
<name>Commons CSV</name>
29+
<name>Apache Commons CSV</name>
3030
<url>http://commons.apache.org/proper/commons-csv/</url>
3131
<description>
32-
The Commons CSV library provides a simple interface for reading and writing
32+
The Apache Commons CSV library provides a simple interface for reading and writing
3333
CSV files of various types.
3434
</description>
3535

src/main/java/org/apache/commons/csv/Assertions.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ private Assertions() {
2828
// can not be instantiated
2929
}
3030

31-
public static void notNull(Object parameter, String parameterName) {
31+
public static void notNull(final Object parameter, final String parameterName) {
3232
if (parameter == null) {
3333
throw new IllegalArgumentException("Parameter '" + parameterName + "' must not be null!");
3434
}

src/main/java/org/apache/commons/csv/CSVFormat.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ public static CSVFormat newFormat(final char delimiter) {
293293
final Quote quotePolicy, final Character commentStart,
294294
final Character escape, final boolean ignoreSurroundingSpaces,
295295
final boolean ignoreEmptyLines, final String recordSeparator,
296-
final String nullString, final String[] header, boolean skipHeaderRecord) {
296+
final String nullString, final String[] header, final boolean skipHeaderRecord) {
297297
if (isLineBreak(delimiter)) {
298298
throw new IllegalArgumentException("The delimiter cannot be a line break");
299299
}

src/main/java/org/apache/commons/csv/CSVParser.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ public final class CSVParser implements Iterable<CSVRecord>, Closeable {
145145
* @throws IOException
146146
* If an I/O error occurs
147147
*/
148-
public static CSVParser parse(File file, final CSVFormat format) throws IOException {
148+
public static CSVParser parse(final File file, final CSVFormat format) throws IOException {
149149
Assertions.notNull(file, "file");
150150
Assertions.notNull(format, "format");
151151

@@ -165,7 +165,7 @@ public static CSVParser parse(File file, final CSVFormat format) throws IOExcept
165165
* @throws IOException
166166
* If an I/O error occurs
167167
*/
168-
public static CSVParser parse(String string, final CSVFormat format) throws IOException {
168+
public static CSVParser parse(final String string, final CSVFormat format) throws IOException {
169169
Assertions.notNull(string, "string");
170170
Assertions.notNull(format, "format");
171171

@@ -192,7 +192,7 @@ public static CSVParser parse(String string, final CSVFormat format) throws IOEx
192192
* @throws IOException
193193
* If an I/O error occurs
194194
*/
195-
public static CSVParser parse(URL url, Charset charset, final CSVFormat format) throws IOException {
195+
public static CSVParser parse(final URL url, final Charset charset, final CSVFormat format) throws IOException {
196196
Assertions.notNull(url, "url");
197197
Assertions.notNull(charset, "charset");
198198
Assertions.notNull(format, "format");
@@ -320,6 +320,8 @@ public List<CSVRecord> getRecords() throws IOException {
320320

321321
/**
322322
* Initializes the name to index mapping if the format defines a header.
323+
*
324+
* @return null if the format has no header.
323325
*/
324326
private Map<String, Integer> initializeHeader() throws IOException {
325327
Map<String, Integer> hdrMap = null;
@@ -330,9 +332,9 @@ private Map<String, Integer> initializeHeader() throws IOException {
330332
String[] header = null;
331333
if (formatHeader.length == 0) {
332334
// read the header from the first line of the file
333-
final CSVRecord record = this.nextRecord();
334-
if (record != null) {
335-
header = record.values();
335+
final CSVRecord nextRecord = this.nextRecord();
336+
if (nextRecord != null) {
337+
header = nextRecord.values();
336338
}
337339
} else {
338340
if (this.format.getSkipHeaderRecord()) {

src/main/java/org/apache/commons/csv/CSVPrinter.java

Lines changed: 104 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,10 @@ public CSVPrinter(final Appendable out, final CSVFormat format) {
6868
// printing implementation
6969
// ======================================================
7070

71-
/**
72-
* Outputs the record separator.
73-
*
74-
* @throws IOException
75-
* If an I/O error occurs
76-
*/
77-
public void println() throws IOException {
78-
out.append(format.getRecordSeparator());
79-
newRecord = true;
71+
public void close() throws IOException {
72+
if (out instanceof Closeable) {
73+
((Closeable) out).close();
74+
}
8075
}
8176

8277
/**
@@ -92,77 +87,23 @@ public void flush() throws IOException {
9287
}
9388

9489
/**
95-
* Prints a single line of delimiter separated values. The values will be quoted if needed. Quotes and newLine
96-
* characters will be escaped.
97-
*
98-
* @param values
99-
* values to output.
100-
* @throws IOException
101-
* If an I/O error occurs
102-
*/
103-
public void printRecord(final Object... values) throws IOException {
104-
for (final Object value : values) {
105-
print(value);
106-
}
107-
println();
108-
}
109-
110-
/**
111-
* Prints a single line of delimiter separated values. The values will be quoted if needed. Quotes and newLine
112-
* characters will be escaped.
113-
*
114-
* @param values
115-
* values to output.
116-
* @throws IOException
117-
* If an I/O error occurs
118-
*/
119-
public void printRecord(final Iterable<?> values) throws IOException {
120-
for (final Object value : values) {
121-
print(value);
122-
}
123-
println();
124-
}
125-
126-
/**
127-
* Prints a comment on a new line among the delimiter separated values. Comments will always begin on a new line
128-
* and occupy a least one full line. The character specified to start comments and a space will be inserted at the
129-
* beginning of each new line in the comment.
130-
* <p/>
131-
* If comments are disabled in the current CSV format this method does nothing.
90+
* Prints the string as the next value on the line. The value will be escaped or encapsulated as needed.
13291
*
133-
* @param comment
134-
* the comment to output
92+
* @param value
93+
* value to be output.
13594
* @throws IOException
13695
* If an I/O error occurs
13796
*/
138-
public void printComment(final String comment) throws IOException {
139-
if (!format.isCommentingEnabled()) {
140-
return;
141-
}
142-
if (!newRecord) {
143-
println();
144-
}
145-
out.append(format.getCommentStart().charValue());
146-
out.append(SP);
147-
for (int i = 0; i < comment.length(); i++) {
148-
final char c = comment.charAt(i);
149-
switch (c) {
150-
case CR:
151-
if (i + 1 < comment.length() && comment.charAt(i + 1) == LF) {
152-
i++;
153-
}
154-
//$FALL-THROUGH$ break intentionally excluded.
155-
case LF:
156-
println();
157-
out.append(format.getCommentStart().charValue());
158-
out.append(SP);
159-
break;
160-
default:
161-
out.append(c);
162-
break;
163-
}
97+
public void print(final Object value) throws IOException {
98+
// null values are considered empty
99+
String strValue;
100+
if (value == null) {
101+
final String nullString = format.getNullString();
102+
strValue = nullString == null ? Constants.EMPTY : nullString;
103+
} else {
104+
strValue = value.toString();
164105
}
165-
println();
106+
this.print(value, strValue, 0, strValue.length());
166107
}
167108

168109
private void print(final Object object, final CharSequence value,
@@ -332,34 +273,99 @@ private void printAndQuote(final Object object, final CharSequence value,
332273
}
333274

334275
/**
335-
* Prints the string as the next value on the line. The value will be escaped or encapsulated as needed.
276+
* Prints a comment on a new line among the delimiter separated values. Comments will always begin on a new line
277+
* and occupy a least one full line. The character specified to start comments and a space will be inserted at the
278+
* beginning of each new line in the comment.
279+
* <p/>
280+
* If comments are disabled in the current CSV format this method does nothing.
336281
*
337-
* @param value
338-
* value to be output.
282+
* @param comment
283+
* the comment to output
339284
* @throws IOException
340285
* If an I/O error occurs
341286
*/
342-
public void print(final Object value) throws IOException {
343-
// null values are considered empty
344-
String strValue;
345-
if (value == null) {
346-
final String nullString = format.getNullString();
347-
strValue = nullString == null ? Constants.EMPTY : nullString;
348-
} else {
349-
strValue = value.toString();
287+
public void printComment(final String comment) throws IOException {
288+
if (!format.isCommentingEnabled()) {
289+
return;
350290
}
351-
this.print(value, strValue, 0, strValue.length());
291+
if (!newRecord) {
292+
println();
293+
}
294+
out.append(format.getCommentStart().charValue());
295+
out.append(SP);
296+
for (int i = 0; i < comment.length(); i++) {
297+
final char c = comment.charAt(i);
298+
switch (c) {
299+
case CR:
300+
if (i + 1 < comment.length() && comment.charAt(i + 1) == LF) {
301+
i++;
302+
}
303+
//$FALL-THROUGH$ break intentionally excluded.
304+
case LF:
305+
println();
306+
out.append(format.getCommentStart().charValue());
307+
out.append(SP);
308+
break;
309+
default:
310+
out.append(c);
311+
break;
312+
}
313+
}
314+
println();
352315
}
353316

354317
/**
355-
* Prints all the objects in the given array.
318+
* Outputs the record separator.
319+
*
320+
* @throws IOException
321+
* If an I/O error occurs
322+
*/
323+
public void println() throws IOException {
324+
out.append(format.getRecordSeparator());
325+
newRecord = true;
326+
}
327+
328+
/**
329+
* Prints a single line of delimiter separated values. The values will be quoted if needed. Quotes and newLine
330+
* characters will be escaped.
331+
*
332+
* @param values
333+
* values to output.
334+
* @throws IOException
335+
* If an I/O error occurs
336+
*/
337+
public void printRecord(final Iterable<?> values) throws IOException {
338+
for (final Object value : values) {
339+
print(value);
340+
}
341+
println();
342+
}
343+
344+
/**
345+
* Prints a single line of delimiter separated values. The values will be quoted if needed. Quotes and newLine
346+
* characters will be escaped.
347+
*
348+
* @param values
349+
* values to output.
350+
* @throws IOException
351+
* If an I/O error occurs
352+
*/
353+
public void printRecord(final Object... values) throws IOException {
354+
for (final Object value : values) {
355+
print(value);
356+
}
357+
println();
358+
}
359+
360+
/**
361+
* Prints all the objects in the given collection.
356362
*
357363
* @param values
358364
* the values to print.
359365
* @throws IOException
360366
* If an I/O error occurs
361367
*/
362-
public void printRecords(final Object[] values) throws IOException {
368+
public void printRecords(final Iterable<?> values) throws IOException {
363369
for (final Object value : values) {
364370
if (value instanceof Object[]) {
365371
this.printRecord((Object[]) value);
@@ -372,14 +378,14 @@ public void printRecords(final Object[] values) throws IOException {
372378
}
373379

374380
/**
375-
* Prints all the objects in the given collection.
381+
* Prints all the objects in the given array.
376382
*
377383
* @param values
378384
* the values to print.
379385
* @throws IOException
380386
* If an I/O error occurs
381387
*/
382-
public void printRecords(final Iterable<?> values) throws IOException {
388+
public void printRecords(final Object[] values) throws IOException {
383389
for (final Object value : values) {
384390
if (value instanceof Object[]) {
385391
this.printRecord((Object[]) value);
@@ -410,9 +416,12 @@ public void printRecords(final ResultSet resultSet) throws SQLException, IOExcep
410416
}
411417
}
412418

413-
public void close() throws IOException {
414-
if (out instanceof Closeable) {
415-
((Closeable) out).close();
416-
}
419+
/**
420+
* Gets the target Appendable.
421+
*
422+
* @return the target Appendable.
423+
*/
424+
public Appendable getOut() {
425+
return this.out;
417426
}
418427
}

0 commit comments

Comments
 (0)