Added a constructor with a String to CSVParser and removed CSVUtils
git-svn-id: https://svn.apache.org/repos/asf/commons/sandbox/csv/trunk@1297135 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
1a35541867
commit
f6c0433b65
|
@ -19,6 +19,7 @@ package org.apache.commons.csv;
|
|||
|
||||
import java.io.IOException;
|
||||
import java.io.Reader;
|
||||
import java.io.StringReader;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
@ -140,6 +141,16 @@ public class CSVParser implements Iterable<String[]> {
|
|||
this.format = format;
|
||||
}
|
||||
|
||||
/**
|
||||
* Customized CSV parser using the given {@link CSVFormat}
|
||||
*
|
||||
* @param input a String containing "csv-formatted" input
|
||||
* @param format the CSVFormat used for CSV parsing
|
||||
*/
|
||||
public CSVParser(String input, CSVFormat format) {
|
||||
this(new StringReader(input), format);
|
||||
}
|
||||
|
||||
// ======================================================
|
||||
// the parser
|
||||
// ======================================================
|
||||
|
|
|
@ -1,85 +0,0 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* 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.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.apache.commons.csv;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.StringReader;
|
||||
|
||||
/**
|
||||
* Utility methods for dealing with CSV files
|
||||
*/
|
||||
public class CSVUtils {
|
||||
|
||||
private static final String[] EMPTY_STRING_ARRAY = new String[0];
|
||||
private static final String[][] EMPTY_DOUBLE_STRING_ARRAY = new String[0][0];
|
||||
|
||||
/**
|
||||
* <p><code>CSVUtils</code> instances should NOT be constructed in
|
||||
* standard programming.
|
||||
*
|
||||
* <p>This constructor is public to permit tools that require a JavaBean
|
||||
* instance to operate.</p>
|
||||
*/
|
||||
public CSVUtils() {
|
||||
}
|
||||
|
||||
// ======================================================
|
||||
// static parsers
|
||||
// ======================================================
|
||||
|
||||
/**
|
||||
* Parses the given String according to the default {@link CSVFormat}.
|
||||
*
|
||||
* @param s CSV String to be parsed.
|
||||
* @return parsed String matrix (which is never null)
|
||||
* @throws IOException in case of error
|
||||
*/
|
||||
public static String[][] parse(String s) throws IOException {
|
||||
if (s == null) {
|
||||
throw new IllegalArgumentException("Null argument not allowed.");
|
||||
}
|
||||
String[][] result = (new CSVParser(new StringReader(s))).getRecords();
|
||||
if (result == null) {
|
||||
// since CSVFormat ignores empty lines an empty array is returned
|
||||
// (i.e. not "result = new String[][] {{""}};")
|
||||
result = EMPTY_DOUBLE_STRING_ARRAY;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses the first line only according to the default {@link CSVFormat}.
|
||||
*
|
||||
* Parsing empty string will be handled as valid records containing zero
|
||||
* elements, so the following property holds: parseLine("").length == 0.
|
||||
*
|
||||
* @param s CSV String to be parsed.
|
||||
* @return parsed String vector (which is never null)
|
||||
* @throws IOException in case of error
|
||||
*/
|
||||
public static String[] parseLine(String s) throws IOException {
|
||||
if (s == null) {
|
||||
throw new IllegalArgumentException("Null argument not allowed.");
|
||||
}
|
||||
// uh,jh: make sure that parseLine("").length == 0
|
||||
if (s.length() == 0) {
|
||||
return EMPTY_STRING_ARRAY;
|
||||
}
|
||||
return (new CSVParser(new StringReader(s))).getLine();
|
||||
}
|
||||
|
||||
}
|
|
@ -244,7 +244,7 @@ public class CSVParserTest extends TestCase {
|
|||
{""},
|
||||
{"\"hello\"", " \"world\"", "abc\ndef", ""}
|
||||
};
|
||||
CSVParser parser = new CSVParser(new StringReader(code), CSVFormat.EXCEL);
|
||||
CSVParser parser = new CSVParser(code, CSVFormat.EXCEL);
|
||||
String[][] tmp = parser.getRecords();
|
||||
assertEquals(res.length, tmp.length);
|
||||
assertTrue(tmp.length > 0);
|
||||
|
@ -262,7 +262,7 @@ public class CSVParserTest extends TestCase {
|
|||
{""},
|
||||
{"world", ""}
|
||||
};
|
||||
CSVParser parser = new CSVParser(new StringReader(code), CSVFormat.EXCEL);
|
||||
CSVParser parser = new CSVParser(code, CSVFormat.EXCEL);
|
||||
String[][] tmp = parser.getRecords();
|
||||
assertEquals(res.length, tmp.length);
|
||||
assertTrue(tmp.length > 0);
|
||||
|
@ -289,7 +289,7 @@ public class CSVParserTest extends TestCase {
|
|||
};
|
||||
|
||||
for (String code : codes) {
|
||||
CSVParser parser = new CSVParser(new StringReader(code), CSVFormat.EXCEL);
|
||||
CSVParser parser = new CSVParser(code, CSVFormat.EXCEL);
|
||||
String[][] tmp = parser.getRecords();
|
||||
assertEquals(res.length, tmp.length);
|
||||
assertTrue(tmp.length > 0);
|
||||
|
@ -314,9 +314,7 @@ public class CSVParserTest extends TestCase {
|
|||
{"hello", ""}, // CSV format ignores empty lines
|
||||
{"world", ""}
|
||||
};
|
||||
String code;
|
||||
for (int codeIndex = 0; codeIndex < codes.length; codeIndex++) {
|
||||
code = codes[codeIndex];
|
||||
for (String code : codes) {
|
||||
CSVParser parser = new CSVParser(new StringReader(code));
|
||||
String[][] tmp = parser.getRecords();
|
||||
assertEquals(res.length, tmp.length);
|
||||
|
@ -339,10 +337,8 @@ public class CSVParserTest extends TestCase {
|
|||
{""}, // Excel format does not ignore empty lines
|
||||
{""}
|
||||
};
|
||||
String code;
|
||||
for (int codeIndex = 0; codeIndex < codes.length; codeIndex++) {
|
||||
code = codes[codeIndex];
|
||||
CSVParser parser = new CSVParser(new StringReader(code), CSVFormat.EXCEL);
|
||||
for (String code : codes) {
|
||||
CSVParser parser = new CSVParser(code, CSVFormat.EXCEL);
|
||||
String[][] tmp = parser.getRecords();
|
||||
assertEquals(res.length, tmp.length);
|
||||
assertTrue(tmp.length > 0);
|
||||
|
@ -362,9 +358,7 @@ public class CSVParserTest extends TestCase {
|
|||
String[][] res = {
|
||||
{"hello", ""} // CSV format ignores empty lines
|
||||
};
|
||||
String code;
|
||||
for (int codeIndex = 0; codeIndex < codes.length; codeIndex++) {
|
||||
code = codes[codeIndex];
|
||||
for (String code : codes) {
|
||||
CSVParser parser = new CSVParser(new StringReader(code));
|
||||
String[][] tmp = parser.getRecords();
|
||||
assertEquals(res.length, tmp.length);
|
||||
|
@ -440,7 +434,7 @@ public class CSVParserTest extends TestCase {
|
|||
|
||||
CSVFormat format = new CSVFormat(',', '\'', CSVFormat.DISABLED, '/', false, false, true, true);
|
||||
|
||||
CSVParser parser = new CSVParser(new StringReader(code), format);
|
||||
CSVParser parser = new CSVParser(code, format);
|
||||
String[][] tmp = parser.getRecords();
|
||||
assertTrue(tmp.length > 0);
|
||||
for (int i = 0; i < res.length; i++) {
|
||||
|
@ -468,7 +462,7 @@ public class CSVParserTest extends TestCase {
|
|||
|
||||
CSVFormat format = new CSVFormat(',', CSVFormat.DISABLED, CSVFormat.DISABLED, '/', false, false, true, true);
|
||||
|
||||
CSVParser parser = new CSVParser(new StringReader(code), format);
|
||||
CSVParser parser = new CSVParser(code, format);
|
||||
String[][] tmp = parser.getRecords();
|
||||
assertTrue(tmp.length > 0);
|
||||
|
||||
|
@ -495,7 +489,7 @@ public class CSVParserTest extends TestCase {
|
|||
CSVFormat format = CSVFormat.DEFAULT;
|
||||
assertEquals(CSVFormat.DISABLED, format.getCommentStart());
|
||||
|
||||
CSVParser parser = new CSVParser(new StringReader(code), format);
|
||||
CSVParser parser = new CSVParser(code, format);
|
||||
String[][] tmp = parser.getRecords();
|
||||
assertTrue(tmp.length > 0);
|
||||
|
||||
|
@ -510,7 +504,7 @@ public class CSVParserTest extends TestCase {
|
|||
};
|
||||
|
||||
format = new CSVFormat(',', '"', '#');
|
||||
parser = new CSVParser(new StringReader(code), format);
|
||||
parser = new CSVParser(code, format);
|
||||
tmp = parser.getRecords();
|
||||
|
||||
if (!CSVPrinterTest.equals(res_comments, tmp)) {
|
||||
|
@ -521,7 +515,7 @@ public class CSVParserTest extends TestCase {
|
|||
|
||||
public void testUnicodeEscape() throws IOException {
|
||||
String code = "abc,\\u0070\\u0075\\u0062\\u006C\\u0069\\u0063";
|
||||
CSVParser parser = new CSVParser(new StringReader(code), CSVFormat.DEFAULT.withUnicodeEscapesInterpreted(true));
|
||||
CSVParser parser = new CSVParser(code, CSVFormat.DEFAULT.withUnicodeEscapesInterpreted(true));
|
||||
String[] data = parser.iterator().next();
|
||||
assertEquals(2, data.length);
|
||||
assertEquals("abc", data[0]);
|
||||
|
|
|
@ -1,150 +0,0 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* 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.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.apache.commons.csv;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
/**
|
||||
* CSVUtilsTest
|
||||
*/
|
||||
public class CSVUtilsTest extends TestCase {
|
||||
|
||||
// ======================================================
|
||||
// static parser tests
|
||||
// ======================================================
|
||||
public void testParse1() throws IOException {
|
||||
String[][] data = CSVUtils.parse("abc\ndef");
|
||||
assertEquals(2, data.length);
|
||||
assertEquals(1, data[0].length);
|
||||
assertEquals(1, data[1].length);
|
||||
assertEquals("abc", data[0][0]);
|
||||
assertEquals("def", data[1][0]);
|
||||
}
|
||||
|
||||
public void testParse2() throws IOException {
|
||||
String[][] data = CSVUtils.parse("abc,def,\"ghi,jkl\"\ndef");
|
||||
assertEquals(2, data.length);
|
||||
assertEquals(3, data[0].length);
|
||||
assertEquals(1, data[1].length);
|
||||
assertEquals("abc", data[0][0]);
|
||||
assertEquals("def", data[0][1]);
|
||||
assertEquals("ghi,jkl", data[0][2]);
|
||||
assertEquals("def", data[1][0]);
|
||||
}
|
||||
|
||||
public void testParse3() throws IOException {
|
||||
String[][] data = CSVUtils.parse("abc,\"def\nghi\"\njkl");
|
||||
assertEquals(2, data.length);
|
||||
assertEquals(2, data[0].length);
|
||||
assertEquals(1, data[1].length);
|
||||
assertEquals("abc", data[0][0]);
|
||||
assertEquals("def\nghi", data[0][1]);
|
||||
assertEquals("jkl", data[1][0]);
|
||||
}
|
||||
|
||||
public void testParse4() throws IOException {
|
||||
String[][] data = CSVUtils.parse("abc,\"def\\\\nghi\"\njkl");
|
||||
assertEquals(2, data.length);
|
||||
assertEquals(2, data[0].length);
|
||||
assertEquals(1, data[1].length);
|
||||
assertEquals("abc", data[0][0]);
|
||||
// an escape char in quotes only escapes a delimiter, not itself
|
||||
assertEquals("def\\\\nghi", data[0][1]);
|
||||
assertEquals("jkl", data[1][0]);
|
||||
}
|
||||
|
||||
public void testParse5() throws IOException {
|
||||
String[][] data = CSVUtils.parse("abc,def\\nghi\njkl");
|
||||
assertEquals(2, data.length);
|
||||
assertEquals(2, data[0].length);
|
||||
assertEquals(1, data[1].length);
|
||||
assertEquals("abc", data[0][0]);
|
||||
assertEquals("def\\nghi", data[0][1]);
|
||||
assertEquals("jkl", data[1][0]);
|
||||
}
|
||||
|
||||
public void testParse6() throws IOException {
|
||||
String[][] data = CSVUtils.parse("");
|
||||
// default format is CSV, which ignores empty lines
|
||||
assertEquals(0, data.length);
|
||||
}
|
||||
|
||||
public void testParse7() throws IOException {
|
||||
boolean io = false;
|
||||
try {
|
||||
CSVUtils.parse(null);
|
||||
} catch (IllegalArgumentException e) {
|
||||
io = true;
|
||||
}
|
||||
assertTrue(io);
|
||||
}
|
||||
|
||||
public void testParseLine1() throws IOException {
|
||||
String[] data = CSVUtils.parseLine("abc,def,ghi");
|
||||
assertEquals(3, data.length);
|
||||
assertEquals("abc", data[0]);
|
||||
assertEquals("def", data[1]);
|
||||
assertEquals("ghi", data[2]);
|
||||
}
|
||||
|
||||
public void testParseLine2() throws IOException {
|
||||
String[] data = CSVUtils.parseLine("abc,def,ghi\n");
|
||||
assertEquals(3, data.length);
|
||||
assertEquals("abc", data[0]);
|
||||
assertEquals("def", data[1]);
|
||||
assertEquals("ghi", data[2]);
|
||||
}
|
||||
|
||||
public void testParseLine3() throws IOException {
|
||||
String[] data = CSVUtils.parseLine("abc,\"def,ghi\"");
|
||||
assertEquals(2, data.length);
|
||||
assertEquals("abc", data[0]);
|
||||
assertEquals("def,ghi", data[1]);
|
||||
}
|
||||
|
||||
public void testParseLine4() throws IOException {
|
||||
String[] data = CSVUtils.parseLine("abc,\"def\nghi\"");
|
||||
assertEquals(2, data.length);
|
||||
assertEquals("abc", data[0]);
|
||||
assertEquals("def\nghi", data[1]);
|
||||
}
|
||||
|
||||
public void testParseLine5() throws IOException {
|
||||
String[] data = CSVUtils.parseLine("");
|
||||
assertEquals(0, data.length);
|
||||
// assertEquals("", data[0]);
|
||||
}
|
||||
|
||||
public void testParseLine6() throws IOException {
|
||||
boolean io = false;
|
||||
try {
|
||||
CSVUtils.parseLine(null);
|
||||
} catch (IllegalArgumentException e) {
|
||||
io = true;
|
||||
}
|
||||
assertTrue(io);
|
||||
}
|
||||
|
||||
public void testParseLine7() throws IOException {
|
||||
String[] res = CSVUtils.parseLine("");
|
||||
assertNotNull(res);
|
||||
assertEquals(0, res.length);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue