Class AbstractCsvReader

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable, ICsvReader
    Direct Known Subclasses:
    CsvBeanReader, CsvListReader, CsvMapReader

    public abstract class AbstractCsvReader
    extends java.lang.Object
    implements ICsvReader
    Defines the standard behaviour of a CSV reader.
    Author:
    Kasper B. Graversen, James Bassett
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()
      Closes the Tokenizer and its associated Reader.
      protected java.util.List<java.lang.Object> executeProcessors​(java.util.List<java.lang.Object> processedColumns, CellProcessor[] processors)
      Executes the supplied cell processors on the last row of CSV that was read and populates the supplied List of processed columns.
      java.lang.String get​(int n)
      Get column N of the current line (column indexes begin at 1).
      protected java.util.List<java.lang.String> getColumns()
      Gets the tokenized columns.
      java.lang.String[] getHeader​(boolean firstLineCheck)
      This method is used to get an optional header of the CSV file and move the file cursor to the first row containing data (the second row from the top).
      int getLineNumber()
      Gets the current position in the file, where the first line of the file is line number 1.
      protected CsvPreference getPreferences()
      Gets the preferences.
      int getRowNumber()
      Gets the current row number (i.e.
      java.lang.String getUntokenizedRow()
      Returns the untokenized CSV row that was just read (which can potentially span multiple lines in the file).
      int length()
      Returns the length (i.e.
      protected boolean readRow()
      Calls the tokenizer to read a CSV row.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • AbstractCsvReader

        public AbstractCsvReader​(java.io.Reader reader,
                                 CsvPreference preferences)
        Constructs a new AbstractCsvReader, using the default Tokenizer.
        Parameters:
        reader - the reader
        preferences - the CSV preferences
        Throws:
        java.lang.NullPointerException - if reader or preferences are null
      • AbstractCsvReader

        public AbstractCsvReader​(ITokenizer tokenizer,
                                 CsvPreference preferences)
        Constructs a new AbstractCsvReader, using a custom Tokenizer (which should have already been set up with the Reader, CsvPreference, and CsvContext). This constructor should only be used if the default Tokenizer doesn't provide the required functionality.
        Parameters:
        tokenizer - the tokenizer
        preferences - the CSV preferences
        Throws:
        java.lang.NullPointerException - if tokenizer or preferences are null
    • Method Detail

      • close

        public void close()
                   throws java.io.IOException
        Closes the Tokenizer and its associated Reader.
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Throws:
        java.io.IOException
      • get

        public java.lang.String get​(int n)
        Get column N of the current line (column indexes begin at 1).
        Specified by:
        get in interface ICsvReader
        Parameters:
        n - the index of the column to get
        Returns:
        the n'th column
      • getHeader

        public java.lang.String[] getHeader​(boolean firstLineCheck)
                                     throws java.io.IOException
        This method is used to get an optional header of the CSV file and move the file cursor to the first row containing data (the second row from the top). The header can subsequently be used as the nameMapping array for read operations.
        Specified by:
        getHeader in interface ICsvReader
        Parameters:
        firstLineCheck - if true, ensures that this method is only called when reading the first line (as that's where the header is meant to be)
        Returns:
        the array of header fields, or null if EOF is encountered
        Throws:
        java.io.IOException - if an I/O exception occurs
      • getLineNumber

        public int getLineNumber()
        Gets the current position in the file, where the first line of the file is line number 1.
        Specified by:
        getLineNumber in interface ICsvReader
        Returns:
        the line number
      • getUntokenizedRow

        public java.lang.String getUntokenizedRow()
        Returns the untokenized CSV row that was just read (which can potentially span multiple lines in the file).
        Specified by:
        getUntokenizedRow in interface ICsvReader
        Returns:
        the untokenized CSV row that was just read
      • getRowNumber

        public int getRowNumber()
        Gets the current row number (i.e. the number of CSV records - including the header - that have been read). This differs from the lineNumber, which is the number of real lines that have been read in the file. The first row is row 1 (which is typically the header row).
        Specified by:
        getRowNumber in interface ICsvReader
        Returns:
        the current row number
      • length

        public int length()
        Returns the length (i.e. number of columns) of the current row.
        Specified by:
        length in interface ICsvReader
        Returns:
        the length of the current row
      • getColumns

        protected java.util.List<java.lang.String> getColumns()
        Gets the tokenized columns.
        Returns:
        the tokenized columns
      • getPreferences

        protected CsvPreference getPreferences()
        Gets the preferences.
        Returns:
        the preferences
      • readRow

        protected boolean readRow()
                           throws java.io.IOException
        Calls the tokenizer to read a CSV row. The columns can then be retrieved using getColumns().
        Returns:
        true if something was read, and false if EOF
        Throws:
        java.io.IOException - when an IOException occurs
        SuperCsvException - on errors in parsing the input
      • executeProcessors

        protected java.util.List<java.lang.Object> executeProcessors​(java.util.List<java.lang.Object> processedColumns,
                                                                     CellProcessor[] processors)
        Executes the supplied cell processors on the last row of CSV that was read and populates the supplied List of processed columns.
        Parameters:
        processedColumns - the List to populate with processed columns
        processors - the cell processors
        Returns:
        the updated List
        Throws:
        java.lang.NullPointerException - if processedColumns or processors is null
        SuperCsvConstraintViolationException - if a CellProcessor constraint failed
        SuperCsvException - if the wrong number of processors are supplied, or CellProcessor execution failed