Remove factory methods for creating CSVParsers for classpath resources

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/csv/trunk@1513994 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Benedikt Ritter 2013-08-14 18:07:13 +00:00
parent 817561f4d7
commit 33cf289907
3 changed files with 9 additions and 77 deletions

View File

@ -107,59 +107,6 @@ public final class CSVParser implements Iterable<CSVRecord>, Closeable {
return new CSVParser(new FileReader(file), format); return new CSVParser(new FileReader(file), format);
} }
/**
* Creates a parser for the given resource.
*
* <p>
* If you do not read all records from the given source, you should call {@link #close()} on the parser.
* </p>
*
* @param resource
* a resource path
* @param charset
* the charset for the resource
* @param classLoader
* the class loader to load the resource.
* @param format
* the CSVFormat used for CSV parsing
* @return a new parser
* @throws IOException
* If an I/O error occurs
*/
public static CSVParser parse(String resource, Charset charset, ClassLoader classLoader,
final CSVFormat format) throws IOException {
final URL url = classLoader.getResource(resource);
if (url == null) {
throw new IllegalArgumentException("Resource cannot be found: " + resource);
}
return parse(url, charset, format);
}
/**
* Creates a parser for the given resource.
*
* <p>
* If you do not read all records from the given source, you should call {@link #close()} on the parser.
* </p>
*
* @param resource
* a resource path
* @param charset
* the charset for the resource
* @param format
* the CSVFormat used for CSV parsing
* @return a new parser
* @throws IOException
* If an I/O error occurs
*/
public static CSVParser parse(String resource, Charset charset, final CSVFormat format) throws IOException {
final URL url = ClassLoader.getSystemResource(resource);
if (url == null) {
throw new IllegalArgumentException("System resource cannot be found: " + resource);
}
return parse(url, charset, format);
}
/** /**
* Creates a parser for the given {@link String}. * Creates a parser for the given {@link String}.
* *
@ -213,25 +160,6 @@ public final class CSVParser implements Iterable<CSVRecord>, Closeable {
private final Token reusableToken = new Token(); private final Token reusableToken = new Token();
/**
* CSV parser using the default format {@link CSVFormat#DEFAULT}.
*
* <p>
* If you do not read all records from the given {@code reader}, you should call {@link #close()} on the parser,
* unless you close the {@code reader}.
* </p>
*
* @param input
* a Reader containing "csv-formatted" input
* @throws IllegalArgumentException
* thrown if the parameters of the format are inconsistent
* @throws IOException
* If an I/O error occurs
*/
public CSVParser(final Reader input) throws IOException {
this(input, CSVFormat.DEFAULT);
}
/** /**
* Customized CSV parser using the given {@link CSVFormat} * Customized CSV parser using the given {@link CSVFormat}
* *

View File

@ -28,6 +28,7 @@ import java.io.FileNotFoundException;
import java.io.FileReader; import java.io.FileReader;
import java.io.FilenameFilter; import java.io.FilenameFilter;
import java.io.IOException; import java.io.IOException;
import java.net.URL;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
@ -126,7 +127,7 @@ public class CSVFileParserTest {
} }
@Test @Test
public void testCSVResource() throws Exception { public void testCSVUrl() throws Exception {
String line = readTestData(); String line = readTestData();
assertNotNull("file must contain config line", line); assertNotNull("file must contain config line", line);
final String[] split = line.split(" "); final String[] split = line.split(" ");
@ -153,8 +154,8 @@ public class CSVFileParserTest {
assertEquals(testName + " Expected format ", line, format.toString()); assertEquals(testName + " Expected format ", line, format.toString());
// Now parse the file and compare against the expected results // Now parse the file and compare against the expected results
final CSVParser parser = CSVParser.parse("CSVFileParser/" + split[0], Charset.forName("UTF-8"), URL resource = ClassLoader.getSystemResource("CSVFileParser/" + split[0]);
this.getClass().getClassLoader(), format); final CSVParser parser = CSVParser.parse(resource, Charset.forName("UTF-8"), format);
for (final CSVRecord record : parser) { for (final CSVRecord record : parser) {
String parsed = record.toString(); String parsed = record.toString();
if (checkComments) { if (checkComments) {

View File

@ -17,6 +17,7 @@
package org.apache.commons.csv; package org.apache.commons.csv;
import java.io.IOException; import java.io.IOException;
import java.net.URL;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.util.List; import java.util.List;
@ -42,7 +43,8 @@ public class FercGovTest {
@Test @Test
public void testContractFile() throws IOException { public void testContractFile() throws IOException {
final CSVParser parser = CSVParser.parse("ferc.gov/contract.txt", US_ASCII, URL contractData = ClassLoader.getSystemClassLoader().getResource("ferc.gov/contract.txt");
final CSVParser parser = CSVParser.parse(contractData, US_ASCII,
CSVFormat.DEFAULT.withHeader()); CSVFormat.DEFAULT.withHeader());
try { try {
final List<CSVRecord> records = parser.getRecords(); final List<CSVRecord> records = parser.getRecords();
@ -65,7 +67,8 @@ public class FercGovTest {
@Test @Test
public void testTransactionFile() throws IOException { public void testTransactionFile() throws IOException {
final CSVParser parser = CSVParser.parse("ferc.gov/transaction.txt", US_ASCII, URL transactionData = ClassLoader.getSystemClassLoader().getResource("ferc.gov/transaction.txt");
final CSVParser parser = CSVParser.parse(transactionData, US_ASCII,
CSVFormat.DEFAULT.withHeader()); CSVFormat.DEFAULT.withHeader());
try { try {
final List<CSVRecord> records = parser.getRecords(); final List<CSVRecord> records = parser.getRecords();