java.lang.Object
de.bcxp.challenge.common.documentParsing.csv.CsvParser
All Implemented Interfaces:
IDocumentParser
Direct Known Subclasses:
CountryCsvParser, WeatherCsvParser

public abstract class CsvParser extends Object implements IDocumentParser
An abstract base class for parsing CSV documents, implementing the IDocumentParser interface.

Provides common CSV reading and parsing functionality, including loading CSV files from the classpath, applying a specified delimiter and locale, and delegating the conversion of CSV records into DocumentEntry objects to subclasses.

Subclasses must implement getEntriesFromRecords(Iterable) to define how CSV records are transformed into document entries specific to their domain.

See Also:
  • Field Details

    • logger

      private static final org.apache.logging.log4j.Logger logger
    • delimiter

      private final char delimiter
    • locale

      private final Locale locale
  • Constructor Details

    • CsvParser

      protected CsvParser(char delimiter, Locale locale)
      Constructs a CsvParser with the specified delimiter character.
      Parameters:
      delimiter - the character used to separate values in the CSV file.
      locale - the locale used to determine the format of numbers that are parsed
  • Method Details

    • parseDocument

      public Document parseDocument(String filepath) throws DocumentCreationException
      Reads a CSV file and converts its records into a Document by delegating the record-to-entry conversion to getEntriesFromRecords(Iterable).

      This method handles parsing errors, file not found errors, and general I/O issues, wrapping them into a DocumentCreationException.

      Specified by:
      parseDocument in interface IDocumentParser
      Parameters:
      filepath - the relative path to the CSV file within the application's classpath
      Returns:
      a Document containing the parsed entries from the CSV file
      Throws:
      DocumentCreationException - if parsing fails due to invalid data formatting, missing file, or I/O errors
    • getEntriesFromRecords

      protected abstract List<DocumentEntry> getEntriesFromRecords(Iterable<org.apache.commons.csv.CSVRecord> records) throws NumberFormatException, ParseException
      Converts CSV records into a list of DocumentEntry objects.

      This method must be implemented by subclasses to define how CSV records are transformed into document entries relevant to the domain.

      Parameters:
      records - the CSV records to be converted
      Returns:
      a list of DocumentEntry objects extracted from the CSV records
      Throws:
      NumberFormatException - if a numeric value in the records cannot be parsed
      ParseException - if a value in the records cannot be parsed according to the locale
    • readFileWithHeader

      private Iterable<org.apache.commons.csv.CSVRecord> readFileWithHeader(String filepath) throws IOException, FileNotFoundException
      Reads a CSV file from the application's classpath and parses its content into an Iterable of CSVRecord objects.

      The CSV file is located using the provided relative classpath filepath, and must exist under the application's resources. The method applies the configured delimiter and treats the first record in the CSV as the header row.

      Behavior:

      • Validates that filepath is not null or empty.
      • Loads the CSV file from the classpath using the current class loader.
      • Parses the CSV using CSVFormat with the configured delimiter.
      • Returns all parsed records as a fully materialized list (safe to iterate after method returns).
      Parameters:
      filepath - the relative path to the CSV file within the classpath
      Returns:
      an Iterable containing all CSV records from the file
      Throws:
      IllegalArgumentException - if filepath is null or empty
      FileNotFoundException - if the CSV file cannot be found on the classpath
      IOException - if an I/O error occurs while reading or parsing the file
    • getDelimiter

      public char getDelimiter()
    • getLocale

      public Locale getLocale()