001package org.gbif.utils.file.tabular;
002
003import java.io.IOException;
004import java.io.Reader;
005import java.util.List;
006
007import com.fasterxml.jackson.dataformat.csv.CsvSchema;
008import com.google.common.base.Preconditions;
009
010/**
011 * Static utility methods related to {@link TabularDataFileReader} instances.
012 */
013public class TabularFiles {
014
015  /**
016   * Get a new TabularDataFileReader.
017   *
018   * @param reader
019   * @param delimiterChar
020   * @param endOfLineSymbols
021   * @param quoteChar nullable
022   * @param headerLine
023   * @return
024   */
025  public static TabularDataFileReader<List<String>> newTabularFileReader(Reader reader, char delimiterChar,
026                                                           String endOfLineSymbols, Character quoteChar, boolean headerLine) throws IOException {
027    return newTabularFileReader(reader, delimiterChar, endOfLineSymbols, quoteChar, headerLine, null);
028  }
029
030  /**
031   * Get a new TabularDataFileReader.
032   *
033   * @param reader
034   * @param delimiterChar
035   * @param endOfLineSymbols
036   * @param quoteChar Nullable
037   * @param headerLine do we expect the first line before the data to be a header line
038   * @param lineToSkipBeforeHeader Nullable. How many line(s) is required to skip in the file before reading the header or the data.
039   *                               This can be used to skip a comment block but if there is a header line, the comment block shall be before the header.
040   * @return
041   */
042  public static TabularDataFileReader<List<String>> newTabularFileReader(Reader reader, char delimiterChar,
043                                                                         String endOfLineSymbols, Character quoteChar, boolean headerLine,
044                                                                         Integer lineToSkipBeforeHeader) throws IOException {
045
046    Preconditions.checkNotNull(reader, "A Reader must be provided");
047    Preconditions.checkNotNull(endOfLineSymbols, "A endOfLineSymbols must be provided");
048    return new JacksonCsvFileReader(reader, delimiterChar, endOfLineSymbols, quoteChar, headerLine, lineToSkipBeforeHeader);
049  }
050
051  /**
052   * Get a new TabularDataFileReader using default quote char (") and default endOfLineSymbols (\n).
053   * Usage:
054   * <pre>
055   * {@code
056   * try (TabularDataFileReader<List<String>> reader = TabularFiles.newTabularFileReader(
057  Files.newBufferedReader(Paths.get("/tmp/test.csv"), StandardCharsets.UTF_8), ',', true)) {
058  ...
059  }
060   * }
061   * </pre>
062   *
063   *
064   * @param reader
065   * @param delimiterChar
066   * @param headerLine
067   */
068  public static TabularDataFileReader<List<String>> newTabularFileReader(Reader reader, char delimiterChar,
069                                                                         boolean headerLine) throws IOException {
070    return new JacksonCsvFileReader(reader, delimiterChar, new String(CsvSchema.DEFAULT_LINEFEED),
071            CsvSchema.DEFAULT_QUOTE_CHAR, headerLine);
072  }
073
074
075}