Java Code Examples for org.supercsv.io.CsvListReader#read()

The following examples show how to use org.supercsv.io.CsvListReader#read() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TimeSeries.java    From powsybl-core with Mozilla Public License 2.0 6 votes vote down vote up
static void readCsvValues(CsvListReader reader, CsvParsingContext context,
                          Map<Integer, List<TimeSeries>> timeSeriesPerVersion) throws IOException {
    List<String> tokens;
    int currentVersion = Integer.MIN_VALUE;
    while ((tokens = reader.read()) != null) {

        if (tokens.size() != context.names.size() + 2 && tokens.size() != context.names.size() + 1) {
            throw new TimeSeriesException("Columns of line " + context.times.size() + " are inconsistent with header");
        }

        int version = Integer.parseInt(tokens.get(1));
        if (currentVersion == Integer.MIN_VALUE) {
            currentVersion = version;
        } else if (version != currentVersion) {
            timeSeriesPerVersion.put(currentVersion, context.createTimeSeries());
            context.reInit();
            currentVersion = version;
        }

        context.parseLine(tokens);
    }
    timeSeriesPerVersion.put(currentVersion, context.createTimeSeries());
}
 
Example 2
Source File: ReadingFeaturesTest.java    From super-csv with Apache License 2.0 6 votes vote down vote up
@Test
public void testSkipCommentLines() throws IOException {
	String csv = "<!--Sarah,Connor-->\r\nJohn,Connor\r\n<!--Kyle,Reese-->";
	
	CsvPreference customPreference = new Builder(STANDARD_PREFERENCE).skipComments(new CommentMatches("<!--.*-->"))
		.build();
	CsvListReader listReader = new CsvListReader(new StringReader(csv), customPreference);
	List<String> line = listReader.read();
	List<String> emptyLine = listReader.read();
	
	Assert.assertNotNull(line);
	Assert.assertEquals(2, line.size());
	Assert.assertEquals("John", line.get(0));
	Assert.assertEquals("Connor", line.get(1));
	Assert.assertNull(emptyLine);
}
 
Example 3
Source File: StringAnonymizer.java    From powsybl-core with Mozilla Public License 2.0 5 votes vote down vote up
public void readCsv(BufferedReader reader, char separator) {
    CsvListReader csvReader = new CsvListReader(reader, createPreference(separator));
    List<String> nextLine;
    try {
        while ((nextLine = csvReader.read()) != null) {
            if (nextLine.size() != 2) {
                throw new PowsyblException("Invalid line '" + nextLine + "'");
            }
            mapping.put(nextLine.get(0), nextLine.get(1));
        }
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
}
 
Example 4
Source File: ReadingFeaturesTest.java    From super-csv with Apache License 2.0 5 votes vote down vote up
@Test
public void testCustomSeparator() throws IOException {
	String csv = "John+Connor";
	CellProcessor[] processors = { new NotNull(), new NotNull() };
	
	char customSeparator = '+';
	CsvPreference customPreference = new Builder('"', customSeparator, "").build();
	CsvListReader listReader = new CsvListReader(new StringReader(csv), customPreference);
	List<Object> result = listReader.read(processors);
	
	Assert.assertNotNull(result);
	Assert.assertEquals(2, result.size());
	Assert.assertEquals("John", result.get(0));
	Assert.assertEquals("Connor", result.get(1));
}
 
Example 5
Source File: ReadingFeaturesTest.java    From super-csv with Apache License 2.0 5 votes vote down vote up
@Test
public void testCustomQuote() throws IOException {
	String csv = "|John  Connor|";
	CellProcessor[] processors = { new NotNull() };
	
	char customQuote = '|';
	CsvPreference customPreference = new Builder(customQuote, ',', "").build();
	CsvListReader listReader = new CsvListReader(new StringReader(csv), customPreference);
	List<Object> result = listReader.read(processors);
	
	Assert.assertNotNull(result);
	Assert.assertEquals(1, result.size());
	Assert.assertEquals("John  Connor", result.get(0));
}
 
Example 6
Source File: ReadingFeaturesTest.java    From super-csv with Apache License 2.0 5 votes vote down vote up
@Test
public void testCustomEOL() throws IOException {
	String csv = "John,Connor\r>\n";
	CellProcessor[] processors = { new NotNull(), new NotNull() };
	
	String customEndOfLine = "\r>\n";
	CsvPreference customPreference = new Builder('"', ',', customEndOfLine).build();
	CsvListReader listReader = new CsvListReader(new StringReader(csv), customPreference);
	List<Object> result = listReader.read(processors);
	
	Assert.assertNotNull(result);
	Assert.assertEquals(2, result.size());
	Assert.assertEquals("John", result.get(0));
	Assert.assertEquals("Connor", result.get(1));
}
 
Example 7
Source File: ReadingFeaturesTest.java    From super-csv with Apache License 2.0 5 votes vote down vote up
@Test
public void testNewLineInDelimitedField() throws IOException {
	String csv = "\"Jo\nhn\",\"Con\nnor\"\n";
	CellProcessor[] processors = { new NotNull(), new NotNull() };
	
	CsvPreference customPreference = new Builder('"', ',', "\n").build();
	CsvListReader listReader = new CsvListReader(new StringReader(csv), customPreference);
	List<Object> result = listReader.read(processors);
	
	Assert.assertNotNull(result);
	Assert.assertEquals(2, result.size());
	Assert.assertEquals("Jo\nhn", result.get(0));
	Assert.assertEquals("Con\nnor", result.get(1));
}
 
Example 8
Source File: ReadingFeaturesTest.java    From super-csv with Apache License 2.0 5 votes vote down vote up
@Test
public void testEscapedQuoteInQuotedField() throws IOException {
	String csv = "\"Joh\"\"n\",\"Con\"\"nor\"";
	CellProcessor[] processors = { new NotNull(), new NotNull() };
	
	CsvPreference customPreference = new Builder('"', ',', "").build();
	CsvListReader listReader = new CsvListReader(new StringReader(csv), customPreference);
	List<Object> result = listReader.read(processors);
	
	Assert.assertNotNull(result);
	Assert.assertEquals(2, result.size());
	Assert.assertEquals("Joh\"n", result.get(0));
	Assert.assertEquals("Con\"nor", result.get(1));
}
 
Example 9
Source File: ReadingFeaturesTest.java    From super-csv with Apache License 2.0 5 votes vote down vote up
@Test
public void testDealWithLeadingTrailingWhitespace() throws IOException {
	String csv = "     John    ,     Connor   ";
	CellProcessor[] processors = { new Trim(), new Trim() };
	
	char customQuote = '"';
	CsvPreference customPreference = new Builder(customQuote, ',', "").build();
	CsvListReader listReader = new CsvListReader(new StringReader(csv), customPreference);
	List<Object> result = listReader.read(processors);
	
	Assert.assertNotNull(result);
	Assert.assertEquals(2, result.size());
	Assert.assertEquals("John", result.get(0));
	Assert.assertEquals("Connor", result.get(1));
}
 
Example 10
Source File: ReadingFeaturesTest.java    From super-csv with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetByColumnIndex() throws IOException {
	String csv = "John,Connor";
	
	CsvListReader listReader = new CsvListReader(new StringReader(csv), STANDARD_PREFERENCE);
	List<String> line = listReader.read();
	
	Assert.assertNotNull(line);
	Assert.assertEquals(2, line.size());
	Assert.assertEquals("John", line.get(0));
	Assert.assertEquals("Connor", line.get(1));
}
 
Example 11
Source File: ReadingFeaturesTest.java    From super-csv with Apache License 2.0 5 votes vote down vote up
@Test
public void testReadSingleLineStreaming() throws IOException {
	String csv = "Sarah,Connor\r\nJohn,Connor\r\nKyle,Reese";
	
	CsvListReader listReader = new CsvListReader(new StringReader(csv), STANDARD_PREFERENCE);
	// skip first line
	listReader.read();
	// read second line
	List<String> line = listReader.read();
	
	Assert.assertNotNull(line);
	Assert.assertEquals(2, line.size());
	Assert.assertEquals("John", line.get(0));
	Assert.assertEquals("Connor", line.get(1));
}
 
Example 12
Source File: ReadingFeaturesTest.java    From super-csv with Apache License 2.0 5 votes vote down vote up
@Test
public void testIgnoreEmptyLines() throws IOException {
	String csv = "\r\n\n\nJohn,Connor\r\n\r\n";
	
	CsvListReader listReader = new CsvListReader(new StringReader(csv), STANDARD_PREFERENCE);
	List<String> line = listReader.read();
	List<String> emptyLine = listReader.read();
	
	Assert.assertNotNull(line);
	Assert.assertEquals(2, line.size());
	Assert.assertEquals("John", line.get(0));
	Assert.assertEquals("Connor", line.get(1));
	Assert.assertNull(emptyLine);
}
 
Example 13
Source File: CsvUtil.java    From openscoring with GNU Affero General Public License v3.0 5 votes vote down vote up
static
private boolean checkFormat(BufferedReader reader, CsvPreference format) throws IOException {
	CsvListReader parser = new CsvListReader(reader, format){

		@Override
		public void close(){
		}
	};

	int columns = 0;

	// Check the header line and the first ten lines
	for(int line = 0; line < (1 + 10); line++){
		List<String> row = parser.read();

		if(row == null){
			break;
		}

		int rowColumns = row.size();
		if((rowColumns > 1) && (columns == 0 || columns == rowColumns)){
			columns = rowColumns;
		} else

		{
			return false;
		}
	}

	parser.close();

	return (columns > 1);
}
 
Example 14
Source File: TabularFileAnalyserTest.java    From waltz with Apache License 2.0 4 votes vote down vote up
@Test
public void foo() throws IOException {

    char[] delimeters = new char[]{',', '|', '\t', ';', '!'};
    char[] quoteChars = new char[]{'"', '\''};


    List<ParseAnalysis> analysisResults = ListUtilities.newArrayList();


    for (char quoteChar : quoteChars) {
        for (char delimeter : delimeters) {

            InputStreamReader simpleReader = getReader();

            CsvPreference prefs = new CsvPreference.Builder(quoteChar, delimeter, "\n")
                    .ignoreEmptyLines(false)
                    .build();

            CsvListReader csvReader = new CsvListReader(simpleReader, prefs);

            List<String> cells = csvReader.read();

            ImmutableParseAnalysis.Builder parseAnalysisBuilder = ImmutableParseAnalysis.builder()
                    .quoteChar(quoteChar)
                    .delimiterChar(delimeter);


            while (cells != null) {
                parseAnalysisBuilder.addFieldCounts(cells.size());
                cells = csvReader.read();
            }

            ParseAnalysis parseAnalysis = parseAnalysisBuilder.build();
            analysisResults.add(parseAnalysis);

        }
    }

    analysisResults
            .forEach(r -> {
                System.out.println(r.quoteChar()
                        + " "
                        + r.delimiterChar()
                        + " => [ "
                        + r.fieldCounts().size()
                        + " ] "
                        + r.fieldCounts());
            });
}
 
Example 15
Source File: Config.java    From stratio-cassandra with Apache License 2.0 4 votes vote down vote up
public static List<String> parseHintedHandoffEnabledDCs(final String dcNames) throws IOException
{
    final CsvListReader csvListReader = new CsvListReader(new StringReader(dcNames), STANDARD_SURROUNDING_SPACES_NEED_QUOTES);
    return csvListReader.read();
}