Class FileParserUtils


  • public class FileParserUtils
    extends Object
    Base class for parsing text files based on XML definitions This class makes use of Utah Parser: A Java library for parsing semi-structured text files https://github.com/sonalake/utah-parser/tree/master/src/test/java/com/sonalake/utah

    This is a low level class for most basic functions regarding records

    Author:
    Jose Fernandez
    • Constructor Detail

      • FileParserUtils

        public FileParserUtils()
        Generic constructor
    • Method Detail

      • parseFile

        public List<Map<String,​String>> parseFile​(com.sonalake.utah.config.Config XMLDefinitionFile,
                                                        Reader fileToParse)
                                                 throws IOException,
                                                        JAXBException
        Parses the file passed as a Reader object using the definition in the XMLDefinitionFile as a Config object
        Parameters:
        XMLDefinitionFile - File containing the rules for decoding
        fileToParse - File to extract the values from
        Returns:
        a list of map objects with the result
        Throws:
        IOException - the io exception
        JAXBException - the jaxb exception
      • sumColumn

        public String sumColumn​(List<Map<String,​String>> records,
                                String columnName)
                         throws ParseException
        Sum al lthe values in the given colum. This functions will try to cast all values to longs with dot (".") as decimal separator. The result will be the string representation of the resulting float number
        Parameters:
        records - Initial set of records to perform the operation
        columnName - Column name to sum
        Returns:
        String representation of the sum
        Throws:
        ParseException - the parse exception
      • elementsWhereEqual

        public int elementsWhereEqual​(List<Map<String,​String>> records,
                                      String columnName,
                                      String value)
        Returns the amount of records in which the specified column has the given value
        Parameters:
        records - Initial set of records in which to perform the operation
        columnName - Column name to look for
        value - Value the column should have to be considered
        Returns:
        Amount of records in which column matches the given value
      • elementWhereNotEqual

        public int elementWhereNotEqual​(List<Map<String,​String>> records,
                                        String columnName,
                                        String value)
        Returns tue amount of records where the value of the specified column is different to the given value
        Parameters:
        records - Initial set of records in which to perform the operation
        columnName - Column name to look for
        value - Value the column should not have to be considered
        Returns:
        Amount of records in which column dont matche the given value
      • getValueofColumnAtPosition

        public String getValueofColumnAtPosition​(List<Map<String,​String>> records,
                                                 String columnName,
                                                 int rowNumber)
        Get the value of the given column at the given index (starting at 0)
        Parameters:
        records - Initial set of records in which to perform the operation
        columnName - Column name
        rowNumber - Index (row)
        Returns:
        Value as String
      • getRecordAtPosition

        public Map<String,​String> getRecordAtPosition​(List<Map<String,​String>> records,
                                                            int rowNumber)
        Returns a single record at the specified position
        Parameters:
        records - Initial set of records in which to perform the operation
        rowNumber - Record number
        Returns:
        A single Map object representing a record
      • validateRecordValues

        public boolean validateRecordValues​(Map<String,​String> record,
                                            String key,
                                            String expectedValue)
        Validates the value of a given key in a record
        Parameters:
        record - record
        key - Key to look for in the record
        expectedValue - Expected value for the key
        Returns:
        True if the value is the expected, false otherwise
      • getFirstRecordThatMatches

        public Map<String,​String> getFirstRecordThatMatches​(List<Map<String,​String>> records,
                                                                  String key,
                                                                  String value)
        Returns the first record that matches the given condition
        Parameters:
        records - Initial set of records in which to perform the operation
        key - Key to llok for in the record
        value - Value for the given key
        Returns:
        Fisrt record that matches the expected condition
      • filterRecordThatMatches

        public List<Map<String,​String>> filterRecordThatMatches​(List<Map<String,​String>> initialSet,
                                                                      String key,
                                                                      String value,
                                                                      String condition)
        Returns a list of records that matches the expected condition. The resulted list of records is stored internally and can be used to perform new searches on it
        Parameters:
        initialSet - Records list to perform operation
        key - key to look for in each record
        value - Expected value to match the condition
        condition - Condition to apply (equal, not equal, contains, does not contains, length)
        Returns:
        List of records where the value for the key match the expected condition