@Test public void parseFileTable5CSV() throws Exception { CsvParserResult result = CsvParser.AUTOMATIC.parse(new InputStreamReader(getClass().getResource("table5.csv").openStream())); assertThat(result.getColumnNames().size(), equalTo(3)); assertThat(result.getColumnNames().get(0), equalTo("A")); assertThat(result.getColumnNames().get(1), equalTo("B")); assertThat(result.getColumnNames().get(2), equalTo("C")); assertThat((Object) result.getColumnTypes().get(0), equalTo((Object) String.class)); assertThat((Object) result.getColumnTypes().get(1), equalTo((Object) double.class)); assertThat((Object) result.getColumnTypes().get(2), equalTo((Object) double.class)); assertThat(result.getColumnValues().get(0), equalTo((Object) Arrays.asList("A", "B", "C", "D", "E"))); assertThat(result.getColumnValues().get(1), equalTo((Object) new ArrayDouble(0.234, 1.456, 234567891234.0, 0.000000123, 123))); assertThat(result.getColumnValues().get(2), equalTo((Object) new ArrayDouble(1,2,3,4,5))); }
public CsvParserResult parse(Reader reader) { State state = new State(); List<String> lines = csvLines(reader); separatorLoop: for(int nSeparator = 0; nSeparator < getSeparators().length(); nSeparator++) { state.currentSeparator = getSeparators().substring(nSeparator, nSeparator+1); String regex = "\\G(?:^|" + state.currentSeparator + ")" + "(?:" + "\"" + "((?:[^\"]++|\"\")*+)" + "\"" + "|" + "([^\"" + state.currentSeparator + "]*)" + ")"; state.mLineTokens = Pattern.compile(regex).matcher(""); state.columnNames = parseTitles(state, lines.get(0)); state.nColumns = state.columnNames.size(); if (state.nColumns == 1) { continue; } state.columnMismatch = false; state.columnNumberParsable = new ArrayList<>(state.nColumns); state.columnTimestampParsable = new ArrayList<>(state.nColumns); state.columnTokens = new ArrayList<>(); for (int i = 0; i < state.nColumns; i++) { state.columnNumberParsable.add(true); state.columnTimestampParsable.add(false); state.columnTokens.add(new ArrayList<String>()); } for (int i = 1; i < lines.size(); i++) { parseLine(state, lines.get(i)); if (state.columnMismatch) { if (i > state.bestNLines) { state.bestSeparator = state.currentSeparator; state.bestNLines = i; } continue separatorLoop; } } break; } if (state.columnMismatch) { return new CsvParserResult(null, null, null, 0, false, "Parsing failed: number of columns not constant. Using separator '" + state.bestSeparator + "', line " + (state.bestNLines + 1)); } if (header == Header.NONE || (header == Header.AUTO && isFirstLineData(state, state.columnNames))) { for (int i = 0; i < state.nColumns; i++) { state.columnTokens.set(i, joinList(state.columnNames.get(i), state.columnTokens.get(i))); state.columnNames.set(i, alphabeticName(i)); } } List<Object> columnValues = new ArrayList<>(state.nColumns); List<Class<?>> columnTypes = new ArrayList<>(state.nColumns); for (int i = 0; i < state.nColumns; i++) { if (state.columnNumberParsable.get(i)) { columnValues.add(convertToListDouble(state.columnTokens.get(i))); columnTypes.add(double.class); } else { columnValues.add(state.columnTokens.get(i)); columnTypes.add(String.class); } } CsvParserResult result = new CsvParserResult(state.columnNames, columnValues, columnTypes, state.columnTokens.get(0).size(), true, null); return result; }
CsvParser { public CsvParserResult parse(Reader reader) { State state = new State(); List<String> lines = csvLines(reader); separatorLoop: for(int nSeparator = 0; nSeparator < getSeparators().length(); nSeparator++) { state.currentSeparator = getSeparators().substring(nSeparator, nSeparator+1); String regex = "\\G(?:^|" + state.currentSeparator + ")" + "(?:" + "\"" + "((?:[^\"]++|\"\")*+)" + "\"" + "|" + "([^\"" + state.currentSeparator + "]*)" + ")"; state.mLineTokens = Pattern.compile(regex).matcher(""); state.columnNames = parseTitles(state, lines.get(0)); state.nColumns = state.columnNames.size(); if (state.nColumns == 1) { continue; } state.columnMismatch = false; state.columnNumberParsable = new ArrayList<>(state.nColumns); state.columnTimestampParsable = new ArrayList<>(state.nColumns); state.columnTokens = new ArrayList<>(); for (int i = 0; i < state.nColumns; i++) { state.columnNumberParsable.add(true); state.columnTimestampParsable.add(false); state.columnTokens.add(new ArrayList<String>()); } for (int i = 1; i < lines.size(); i++) { parseLine(state, lines.get(i)); if (state.columnMismatch) { if (i > state.bestNLines) { state.bestSeparator = state.currentSeparator; state.bestNLines = i; } continue separatorLoop; } } break; } if (state.columnMismatch) { return new CsvParserResult(null, null, null, 0, false, "Parsing failed: number of columns not constant. Using separator '" + state.bestSeparator + "', line " + (state.bestNLines + 1)); } if (header == Header.NONE || (header == Header.AUTO && isFirstLineData(state, state.columnNames))) { for (int i = 0; i < state.nColumns; i++) { state.columnTokens.set(i, joinList(state.columnNames.get(i), state.columnTokens.get(i))); state.columnNames.set(i, alphabeticName(i)); } } List<Object> columnValues = new ArrayList<>(state.nColumns); List<Class<?>> columnTypes = new ArrayList<>(state.nColumns); for (int i = 0; i < state.nColumns; i++) { if (state.columnNumberParsable.get(i)) { columnValues.add(convertToListDouble(state.columnTokens.get(i))); columnTypes.add(double.class); } else { columnValues.add(state.columnTokens.get(i)); columnTypes.add(String.class); } } CsvParserResult result = new CsvParserResult(state.columnNames, columnValues, columnTypes, state.columnTokens.get(0).size(), true, null); return result; } }
CsvParser { public CsvParserResult parse(Reader reader) { State state = new State(); List<String> lines = csvLines(reader); separatorLoop: for(int nSeparator = 0; nSeparator < getSeparators().length(); nSeparator++) { state.currentSeparator = getSeparators().substring(nSeparator, nSeparator+1); String regex = "\\G(?:^|" + state.currentSeparator + ")" + "(?:" + "\"" + "((?:[^\"]++|\"\")*+)" + "\"" + "|" + "([^\"" + state.currentSeparator + "]*)" + ")"; state.mLineTokens = Pattern.compile(regex).matcher(""); state.columnNames = parseTitles(state, lines.get(0)); state.nColumns = state.columnNames.size(); if (state.nColumns == 1) { continue; } state.columnMismatch = false; state.columnNumberParsable = new ArrayList<>(state.nColumns); state.columnTimestampParsable = new ArrayList<>(state.nColumns); state.columnTokens = new ArrayList<>(); for (int i = 0; i < state.nColumns; i++) { state.columnNumberParsable.add(true); state.columnTimestampParsable.add(false); state.columnTokens.add(new ArrayList<String>()); } for (int i = 1; i < lines.size(); i++) { parseLine(state, lines.get(i)); if (state.columnMismatch) { if (i > state.bestNLines) { state.bestSeparator = state.currentSeparator; state.bestNLines = i; } continue separatorLoop; } } break; } if (state.columnMismatch) { return new CsvParserResult(null, null, null, 0, false, "Parsing failed: number of columns not constant. Using separator '" + state.bestSeparator + "', line " + (state.bestNLines + 1)); } if (header == Header.NONE || (header == Header.AUTO && isFirstLineData(state, state.columnNames))) { for (int i = 0; i < state.nColumns; i++) { state.columnTokens.set(i, joinList(state.columnNames.get(i), state.columnTokens.get(i))); state.columnNames.set(i, alphabeticName(i)); } } List<Object> columnValues = new ArrayList<>(state.nColumns); List<Class<?>> columnTypes = new ArrayList<>(state.nColumns); for (int i = 0; i < state.nColumns; i++) { if (state.columnNumberParsable.get(i)) { columnValues.add(convertToListDouble(state.columnTokens.get(i))); columnTypes.add(double.class); } else { columnValues.add(state.columnTokens.get(i)); columnTypes.add(String.class); } } CsvParserResult result = new CsvParserResult(state.columnNames, columnValues, columnTypes, state.columnTokens.get(0).size(), true, null); return result; } private  CsvParser(String separators, Header header); }
CsvParser { public CsvParserResult parse(Reader reader) { State state = new State(); List<String> lines = csvLines(reader); separatorLoop: for(int nSeparator = 0; nSeparator < getSeparators().length(); nSeparator++) { state.currentSeparator = getSeparators().substring(nSeparator, nSeparator+1); String regex = "\\G(?:^|" + state.currentSeparator + ")" + "(?:" + "\"" + "((?:[^\"]++|\"\")*+)" + "\"" + "|" + "([^\"" + state.currentSeparator + "]*)" + ")"; state.mLineTokens = Pattern.compile(regex).matcher(""); state.columnNames = parseTitles(state, lines.get(0)); state.nColumns = state.columnNames.size(); if (state.nColumns == 1) { continue; } state.columnMismatch = false; state.columnNumberParsable = new ArrayList<>(state.nColumns); state.columnTimestampParsable = new ArrayList<>(state.nColumns); state.columnTokens = new ArrayList<>(); for (int i = 0; i < state.nColumns; i++) { state.columnNumberParsable.add(true); state.columnTimestampParsable.add(false); state.columnTokens.add(new ArrayList<String>()); } for (int i = 1; i < lines.size(); i++) { parseLine(state, lines.get(i)); if (state.columnMismatch) { if (i > state.bestNLines) { state.bestSeparator = state.currentSeparator; state.bestNLines = i; } continue separatorLoop; } } break; } if (state.columnMismatch) { return new CsvParserResult(null, null, null, 0, false, "Parsing failed: number of columns not constant. Using separator '" + state.bestSeparator + "', line " + (state.bestNLines + 1)); } if (header == Header.NONE || (header == Header.AUTO && isFirstLineData(state, state.columnNames))) { for (int i = 0; i < state.nColumns; i++) { state.columnTokens.set(i, joinList(state.columnNames.get(i), state.columnTokens.get(i))); state.columnNames.set(i, alphabeticName(i)); } } List<Object> columnValues = new ArrayList<>(state.nColumns); List<Class<?>> columnTypes = new ArrayList<>(state.nColumns); for (int i = 0; i < state.nColumns; i++) { if (state.columnNumberParsable.get(i)) { columnValues.add(convertToListDouble(state.columnTokens.get(i))); columnTypes.add(double.class); } else { columnValues.add(state.columnTokens.get(i)); columnTypes.add(String.class); } } CsvParserResult result = new CsvParserResult(state.columnNames, columnValues, columnTypes, state.columnTokens.get(0).size(), true, null); return result; } private  CsvParser(String separators, Header header); String getSeparators(); CsvParser withSeparators(String separators); Header getHeader(); CsvParser withHeader(Header header); CsvParserResult parse(Reader reader); static List<Object> parseCSVLine(String line, String separatorChar); }
CsvParser { public CsvParserResult parse(Reader reader) { State state = new State(); List<String> lines = csvLines(reader); separatorLoop: for(int nSeparator = 0; nSeparator < getSeparators().length(); nSeparator++) { state.currentSeparator = getSeparators().substring(nSeparator, nSeparator+1); String regex = "\\G(?:^|" + state.currentSeparator + ")" + "(?:" + "\"" + "((?:[^\"]++|\"\")*+)" + "\"" + "|" + "([^\"" + state.currentSeparator + "]*)" + ")"; state.mLineTokens = Pattern.compile(regex).matcher(""); state.columnNames = parseTitles(state, lines.get(0)); state.nColumns = state.columnNames.size(); if (state.nColumns == 1) { continue; } state.columnMismatch = false; state.columnNumberParsable = new ArrayList<>(state.nColumns); state.columnTimestampParsable = new ArrayList<>(state.nColumns); state.columnTokens = new ArrayList<>(); for (int i = 0; i < state.nColumns; i++) { state.columnNumberParsable.add(true); state.columnTimestampParsable.add(false); state.columnTokens.add(new ArrayList<String>()); } for (int i = 1; i < lines.size(); i++) { parseLine(state, lines.get(i)); if (state.columnMismatch) { if (i > state.bestNLines) { state.bestSeparator = state.currentSeparator; state.bestNLines = i; } continue separatorLoop; } } break; } if (state.columnMismatch) { return new CsvParserResult(null, null, null, 0, false, "Parsing failed: number of columns not constant. Using separator '" + state.bestSeparator + "', line " + (state.bestNLines + 1)); } if (header == Header.NONE || (header == Header.AUTO && isFirstLineData(state, state.columnNames))) { for (int i = 0; i < state.nColumns; i++) { state.columnTokens.set(i, joinList(state.columnNames.get(i), state.columnTokens.get(i))); state.columnNames.set(i, alphabeticName(i)); } } List<Object> columnValues = new ArrayList<>(state.nColumns); List<Class<?>> columnTypes = new ArrayList<>(state.nColumns); for (int i = 0; i < state.nColumns; i++) { if (state.columnNumberParsable.get(i)) { columnValues.add(convertToListDouble(state.columnTokens.get(i))); columnTypes.add(double.class); } else { columnValues.add(state.columnTokens.get(i)); columnTypes.add(String.class); } } CsvParserResult result = new CsvParserResult(state.columnNames, columnValues, columnTypes, state.columnTokens.get(0).size(), true, null); return result; } private  CsvParser(String separators, Header header); String getSeparators(); CsvParser withSeparators(String separators); Header getHeader(); CsvParser withHeader(Header header); CsvParserResult parse(Reader reader); static List<Object> parseCSVLine(String line, String separatorChar); static final CsvParser AUTOMATIC; }