/// <summary> /// Parses a file data stream and outputs a list of transactions. /// </summary> /// <param name="data">File data stream.</param> public async Task <List <Transaction> > ParseFile(Stream data) { // TODO: Exception feedback to UI 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++; // TODO: Make parser selection nicer 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 SebParserTests() { _target = new SebParser(); }