Class CsvParser
- All Implemented Interfaces:
IDocumentParser
- Direct Known Subclasses:
CountryCsvParser
,WeatherCsvParser
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 Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionchar
protected abstract List<DocumentEntry>
getEntriesFromRecords
(Iterable<org.apache.commons.csv.CSVRecord> records) Converts CSV records into a list ofDocumentEntry
objects.parseDocument
(String filepath) Reads a CSV file and converts its records into aDocument
by delegating the record-to-entry conversion to getEntriesFromRecords(Iterable).private Iterable<org.apache.commons.csv.CSVRecord>
readFileWithHeader
(String filepath) Reads a CSV file from the application's classpath and parses its content into anIterable
ofCSVRecord
objects.
-
Field Details
-
logger
private static final org.apache.logging.log4j.Logger logger -
delimiter
private final char delimiter -
locale
-
-
Constructor Details
-
CsvParser
Constructs aCsvParser
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
Reads a CSV file and converts its records into aDocument
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 interfaceIDocumentParser
- 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 ofDocumentEntry
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 parsedParseException
- 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 anIterable
ofCSVRecord
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 notnull
or empty. - Loads the CSV file from the classpath using the current class loader.
- Parses the CSV using
CSVFormat
with the configureddelimiter
. - 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
- iffilepath
is null or emptyFileNotFoundException
- if the CSV file cannot be found on the classpathIOException
- if an I/O error occurs while reading or parsing the file
- Validates that
-
getDelimiter
public char getDelimiter() -
getLocale
-