Ed.BudgetVisualizer.Logic/ParserLogic.cs
/// <summary>
/// Parses a file data stream and outputs a list of transactions.
/// </summary>
/// <param name="data">File data stream.</param>
Parses a file data stream and outputs a list of transactions.
Parses a file data stream and outputs a list of transactions .
public async Task<List<Transaction>> ParseFile(Stream data)
        {
            IParser parser = null;
            var transactions = new List<Transaction>();
            int lineNumber = 0;
            string line;
            using (StreamReader reader = new StreamReader(data))
            {
                while ((line = await reader.ReadLineAsync()) != null)
                {
                    lineNumber++;
                    if (lineNumber == 1)
                    {
                        if (Regex.IsMatch(line, SebParser.FirstLineRegex))
                        {
                            parser = new SebParser();
                        }
                        else if (Regex.IsMatch(line, SwedbankParser.FirstLineRegex))
                        {
                            parser = new SwedbankParser();
                        }
                        else
                        {
                            throw new NotSupportedException("CSV format not recognized and not supported.");
                        }
                    }
                    if (string.IsNullOrEmpty(line))
                    {
                        continue;
                    }
                    if (parser.HasTitleRow && lineNumber == 1)
                    {
                        continue;
                    }
                    if (parser.HasHeaderRow && lineNumber == 1)
                    {
                        continue;
                    }
                    if (parser.HasTitleRow && parser.HasHeaderRow && lineNumber == 2)
                    {
                        continue;
                    }
                    try
                    {
                        string[] fields = SplitAndClean(line, parser.Separator);
                        var transaction = parser.MapTransaction(fields);
                        if (transaction != null)
                        {
                            transactions.Add(transaction);
                        }
                    }
                    catch (ApplicationException ex)
                    {
                        Debug.WriteLine($"Could not parse line '{line}': {ex.Message}");
                    }
                }
            }
            return transactions;
        }
public async Task < List < Transaction > > ParseFile ( Stream data ) { IParser parser = null ; var transactions = new List < Transaction > ( ) ; int lineNumber = 0 ; string line ; using ( StreamReader reader = new StreamReader ( data ) ) { while ( ( line = await reader . ReadLineAsync ( ) ) != null ) { lineNumber ++ ; if ( lineNumber == 1 ) { if ( Regex . IsMatch ( line , SebParser . FirstLineRegex ) ) { parser = new SebParser ( ) ; } else if ( Regex . IsMatch ( line , SwedbankParser . FirstLineRegex ) ) { parser = new SwedbankParser ( ) ; } else { throw new NotSupportedException ( " " ) ; } } if ( string . IsNullOrEmpty ( line ) ) { continue ; } if ( parser . HasTitleRow && lineNumber == 1 ) { continue ; } if ( parser . HasHeaderRow && lineNumber == 1 ) { continue ; } if ( parser . HasTitleRow && parser . HasHeaderRow && lineNumber == 2 ) { continue ; } try { string [ ] fields = SplitAndClean ( line , parser . Separator ) ; var transaction = parser . MapTransaction ( fields ) ; if ( transaction != null ) { transactions . Add ( transaction ) ; } } catch ( ApplicationException ex ) { Debug . WriteLine ( $" Could not parse line ' { line } ':  { ex . Message } " ) ; } } } return transactions ; }
Parses a file data stream and outputs a list of transactions.
Parses a file data stream and outputs a list of transactions .
// TODO: Exception feedback to UI // TODO: Make parser selection nicer
data
Stream
File data stream.
File data stream .