CSVParser { public String[] parseLine(String nextLine) throws IOException { return parseLine(nextLine, false); } CSVParser();  CSVParser(char separator);  CSVParser(char separator, char quotechar);  CSVParser(char separator, char quotechar, char escape);  CSVParser(char separator, char quotechar, char escape, boolean strictQuotes);  CSVParser(char separator, char quotechar, char escape, boolean strictQuotes, boolean ignoreLeadingWhiteSpace);  CSVParser(char separator, char quotechar, char escape, boolean strictQuotes, boolean ignoreLeadingWhiteSpace,
                     boolean ignoreQuotations); boolean isPending(); String[] parseLineMulti(String nextLine); String[] parseLine(String nextLine); static final char DEFAULT_SEPARATOR; static final int INITIAL_READ_SIZE; static final char DEFAULT_QUOTE_CHARACTER; static final char DEFAULT_ESCAPE_CHARACTER; static final boolean DEFAULT_STRICT_QUOTES; static final boolean DEFAULT_IGNORE_LEADING_WHITESPACE; static final boolean DEFAULT_IGNORE_QUOTATIONS; }
@Test public void testParseLine() throws Exception { String nextItem[] = csvParser.parseLine("This, is, a, test."); assertEquals(4, nextItem.length); assertEquals("This", nextItem[0]); assertEquals(" is", nextItem[1]); assertEquals(" a", nextItem[2]); assertEquals(" test.", nextItem[3]); }
@Test public void testParsedLineWithInternalQuota() throws IOException { String[] nextLine = csvParser.parseLine("a,123\"4\"567,c"); assertEquals(3, nextLine.length); assertEquals("123\"4\"567", nextLine[1]); }
@Test public void parseQuotedStringWithCommas() throws IOException { String[] nextLine = csvParser.parseLine("a,\"b,b,b\",c"); assertEquals("a", nextLine[0]); assertEquals("b,b,b", nextLine[1]); assertEquals("c", nextLine[2]); assertEquals(3, nextLine.length); }
@Test public void parseQuotedStringWithDefinedSeperator() throws IOException { csvParser = new CSVParser(':'); String[] nextLine = csvParser.parseLine("a:\"b:b:b\":c"); assertEquals("a", nextLine[0]); assertEquals("b:b:b", nextLine[1]); assertEquals("c", nextLine[2]); assertEquals(3, nextLine.length); }
@Test public void parseQuotedStringWithDefinedSeperatorAndQuote() throws IOException { csvParser = new CSVParser(':', '\''); String[] nextLine = csvParser.parseLine("a:'b:b:b':c"); assertEquals("a", nextLine[0]); assertEquals("b:b:b", nextLine[1]); assertEquals("c", nextLine[2]); assertEquals(3, nextLine.length); }
@Test public void parseEmptyElements() throws IOException { String[] nextLine = csvParser.parseLine(",,"); assertEquals(3, nextLine.length); assertEquals("", nextLine[0]); assertEquals("", nextLine[1]); assertEquals("", nextLine[2]); }
@Test public void parseMultiLinedQuoted() throws IOException { String[] nextLine = csvParser.parseLine("a,\"PO Box 123,\nKippax,ACT. 2615.\nAustralia\",d.\n"); assertEquals(3, nextLine.length); assertEquals("a", nextLine[0]); assertEquals("PO Box 123,\nKippax,ACT. 2615.\nAustralia", nextLine[1]); assertEquals("d.\n", nextLine[2]); }
@Test public void testADoubleQuoteAsDataElement() throws IOException { String[] nextLine = csvParser.parseLine("a,\"\"\"\",c"); assertEquals(3, nextLine.length); assertEquals("a", nextLine[0]); assertEquals(1, nextLine[1].length()); assertEquals("\"", nextLine[1]); assertEquals("c", nextLine[2]); }
@Test public void testEscapedDoubleQuoteAsDataElement() throws IOException { String[] nextLine = csvParser.parseLine("\"test\",\"this,test,is,good\",\"\\\"test\\\"\",\"\\\"quote\\\"\""); assertEquals(4, nextLine.length); assertEquals("test", nextLine[0]); assertEquals("this,test,is,good", nextLine[1]); assertEquals("\"test\"", nextLine[2]); assertEquals("\"quote\"", nextLine[3]); }
@Test public void parseMultipleQuotes() throws IOException { String[] nextLine = csvParser.parseLine("\"\"\"\"\"\",\"test\"\n"); assertEquals("\"\"", nextLine[0]); assertEquals("test\"\n", nextLine[1]); assertEquals(2, nextLine.length); }
@Test public void parseTrickyString() throws IOException { String[] nextLine = csvParser.parseLine("\"a\nb\",b,\"\nd\",e\n"); assertEquals(4, nextLine.length); assertEquals("a\nb", nextLine[0]); assertEquals("b", nextLine[1]); assertEquals("\nd", nextLine[2]); assertEquals("e\n", nextLine[3]); }
@Test public void testStrictQuoteSimple() throws IOException { csvParser = new CSVParser(',', '\"', '\\', true); String testString = "\"a\",\"b\",\"c\""; String[] nextLine = csvParser.parseLine(testString); assertEquals(3, nextLine.length); assertEquals("a", nextLine[0]); assertEquals("b", nextLine[1]); assertEquals("c", nextLine[2]); }
@Test public void testStrictQuoteWithSpacesAndTabs() throws IOException { csvParser = new CSVParser(',', '\"', '\\', true); String testString = " \t \"a\",\"b\" \t , \"c\" "; String[] nextLine = csvParser.parseLine(testString); assertEquals(3, nextLine.length); assertEquals("a", nextLine[0]); assertEquals("b", nextLine[1]); assertEquals("c", nextLine[2]); }
@Test public void testStrictQuoteWithGarbage() throws IOException { csvParser = new CSVParser(',', '\"', '\\', true); String testString = "abc',!@#\",\\\"\" xyz,"; String[] nextLine = csvParser.parseLine(testString); assertEquals(3, nextLine.length); assertEquals("", nextLine[0]); assertEquals(",\"", nextLine[1]); assertEquals("", nextLine[2]); }
@Test public void testCanIgnoreQuotations() throws IOException { csvParser = new CSVParser(CSVParser.DEFAULT_SEPARATOR, CSVParser.DEFAULT_QUOTE_CHARACTER, CSVParser.DEFAULT_ESCAPE_CHARACTER, CSVParser.DEFAULT_STRICT_QUOTES, CSVParser.DEFAULT_IGNORE_LEADING_WHITESPACE, true); String testString = "Bob,test\",Beaumont,TX"; String[] nextLine = csvParser.parseLine(testString); assertEquals(4, nextLine.length); assertEquals("Bob", nextLine[0]); assertEquals("test", nextLine[1]); assertEquals("Beaumont", nextLine[2]); assertEquals("TX", nextLine[3]); }
@Test public void testIssue3314579() throws IOException { csvParser = new CSVParser(';', CSVParser.DEFAULT_QUOTE_CHARACTER, CSVParser.DEFAULT_ESCAPE_CHARACTER, CSVParser.DEFAULT_STRICT_QUOTES, CSVParser.DEFAULT_IGNORE_LEADING_WHITESPACE, true); String testString = "RPO;2012;P; ; ; ;SDX;ACCESSORY WHEEL, 16\", ALUMINUM, DESIGN 1"; String[] nextLine = csvParser.parseLine(testString); assertEquals(8, nextLine.length); assertEquals("RPO", nextLine[0]); assertEquals("2012", nextLine[1]); assertEquals("P", nextLine[2]); assertEquals(" ", nextLine[3]); assertEquals(" ", nextLine[4]); assertEquals(" ", nextLine[5]); assertEquals("SDX", nextLine[6]); assertEquals("ACCESSORY WHEEL, 16\", ALUMINUM, DESIGN 1", nextLine[7]); }
@Test public void testIssue2263439() throws IOException { csvParser = new CSVParser(',', '\''); String[] nextLine = csvParser.parseLine("865,0,'AmeriKKKa\\'s_Most_Wanted','',294,0,0,0.734338696798625,'20081002052147',242429208,18448"); assertEquals(11, nextLine.length); assertEquals("865", nextLine[0]); assertEquals("0", nextLine[1]); assertEquals("AmeriKKKa's_Most_Wanted", nextLine[2]); assertEquals("", nextLine[3]); assertEquals("18448", nextLine[10]); }
@Test public void testIssue2859181() throws IOException { csvParser = new CSVParser(';'); String[] nextLine = csvParser.parseLine("field1;\\=field2;\"\"\"field3\"\"\""); assertEquals(3, nextLine.length); assertEquals("field1", nextLine[0]); assertEquals("=field2", nextLine[1]); assertEquals("\"field3\"", nextLine[2]); }
@Test public void testIssue2726363() throws IOException { String[] nextLine = csvParser.parseLine("\"804503689\",\"London\",\"\"London\"shop\",\"address\",\"116.453182\",\"39.918884\""); assertEquals(6, nextLine.length); assertEquals("804503689", nextLine[0]); assertEquals("London", nextLine[1]); assertEquals("\"London\"shop", nextLine[2]); assertEquals("address", nextLine[3]); assertEquals("116.453182", nextLine[4]); assertEquals("39.918884", nextLine[5]); }
@Test(expected = IOException.class) public void anIOExceptionThrownifStringEndsInsideAQuotedString() throws IOException { String[] nextLine = csvParser.parseLine("This,is a \"bad line to parse."); }
@Test public void spacesAtEndOfQuotedStringDoNotCountIfStrictQuotesIsTrue() throws IOException { CSVParser parser = new CSVParser(CSVParser.DEFAULT_SEPARATOR, CSVParser.DEFAULT_QUOTE_CHARACTER, CSVParser.DEFAULT_ESCAPE_CHARACTER, true); String[] nextLine = parser.parseLine("\"Line with\", \"spaces at end\" "); assertEquals(2, nextLine.length); assertEquals("Line with", nextLine[0]); assertEquals("spaces at end", nextLine[1]); }
@Test public void returnNullWhenNullPassedIn() throws IOException { String[] nextLine = csvParser.parseLine(null); assertNull(nextLine); }
@Test public void whitespaceBeforeEscape() throws IOException { String[] nextItem = csvParser.parseLine("\"this\", \"is\",\"a test\""); assertEquals("this", nextItem[0]); assertEquals("is", nextItem[1]); assertEquals("a test", nextItem[2]); }
@Test public void testIssue2958242WithoutQuotes() throws IOException { CSVParser testParser = new CSVParser('\t'); String[] nextItem = testParser.parseLine("zo\"\"har\"\"at\t10-04-1980\t29\tC:\\\\foo.txt"); assertEquals(4, nextItem.length); assertEquals("zo\"har\"at", nextItem[0]); assertEquals("10-04-1980", nextItem[1]); assertEquals("29", nextItem[2]); assertEquals("C:\\foo.txt", nextItem[3]); }