public ParseFileResult ParseFile(Stream fileStream, IFieldDataResultsAppender fieldDataResultsAppender, ILog logger) { try { var gaugingSummary = ParseGaugingSummaryFromFile(fileStream); var gaugingSummaryProcessor = new GaugingSummaryProcessor(fieldDataResultsAppender, logger); gaugingSummaryProcessor.ProcessGaugingSummary(gaugingSummary); return(ParseFileResult.SuccessfullyParsedAndDataValid()); } catch (Exception ex) when(ex is PocketGaugerZipFileMissingRequiredContentException || ex is PocketGaugerZipFileException) { return(ParseFileResult.CannotParse()); } catch (PocketGaugerDataFormatException e) { return(ParseFileResult.SuccessfullyParsedButDataInvalid(e)); } catch (Exception e) { return(ParseFileResult.CannotParse(e)); } }
public ParseFileResult ParseFile(Stream fileStream, IFieldDataResultsAppender appender, ILog logger) { var xmlRoot = SectionBySectionSerializer.DeserializeNoThrow(fileStream, logger); if (xmlRoot == null) { return(ParseFileResult.CannotParse()); } var stationNo = xmlRoot.Summary?.WinRiver_II_Section_by_Section_Summary?.Station_No; if (string.IsNullOrWhiteSpace(stationNo)) { logger.Error("File can be parsed but there is no Station_No specified."); return(ParseFileResult.SuccessfullyParsedButDataInvalid("Missing Station_No.")); } try { var trimmedLocationIdentifier = stationNo.Trim(); var location = appender.GetLocationByIdentifier(trimmedLocationIdentifier); return(ParseXmlRootNoThrow(location, xmlRoot, appender, logger)); } catch (Exception exception) { logger.Error($"Cannot find location with identifier {stationNo}."); return(ParseFileResult.CannotParse(exception)); } }
public ParseFileResult Parse(Stream stream, LocationInfo locationInfo = null) { var jsonText = ReadTextFromStream(stream); if (jsonText == null) { return(ParseFileResult.CannotParse()); } try { LocationInfo = locationInfo; AppendedResults = ParseJson(jsonText); if (AppendedResults == null) { return(ParseFileResult.CannotParse()); } return(MapToFramework()); } catch (Exception exception) { return(ParseFileResult.SuccessfullyParsedButDataInvalid(exception)); } }
public ParseFileResult ParseFile(Stream fileStream) { CrossSectionSurvey parsedFileContents; try { parsedFileContents = ProcessFileStream(fileStream); } catch (CrossSectionCsvFormatException) { return(ParseFileResult.CannotParse()); } catch (Exception e) { return(ParseFileResult.CannotParse(e)); } try { return(ProcessParsedFileContents(parsedFileContents)); } catch (Exception e) { return(ParseFileResult.SuccessfullyParsedButDataInvalid(e)); } }
private ParseFileResult ParseEntry(ZipArchiveEntry entry, IFieldDataPlugin plugin, LocationInfo locationInfo) { using (var entryStream = entry.Open()) using (var reader = new BinaryReader(entryStream)) using (var memoryStream = new MemoryStream(reader.ReadBytes((int)entry.Length))) { var proxyLog = ProxyLog.Create(Log, plugin, entry); var result = locationInfo == null ? plugin.ParseFile(memoryStream, ResultsAppender, proxyLog) : plugin.ParseFile(memoryStream, locationInfo, ResultsAppender, proxyLog); switch (result.Status) { case ParseFileStatus.CannotParse: result = ParseFileResult.CannotParse($"{proxyLog.Prefix}: {result.ErrorMessage}"); break; case ParseFileStatus.SuccessfullyParsedButDataInvalid: result = ParseFileResult.SuccessfullyParsedButDataInvalid($"{proxyLog.Prefix}: {result.ErrorMessage}"); break; } return(result); } }
public ParseFileResult ParseFile(Stream fileStream, IFieldDataResultsAppender fieldDataResultsAppender, ILog logger) { try { var parser = new Parser(fieldDataResultsAppender, logger); var eHsn = parser.LoadFromStream(fileStream); if (eHsn == null) { return(ParseFileResult.CannotParse()); } try { parser.Parse(eHsn); return(ParseFileResult.SuccessfullyParsedAndDataValid()); } catch (Exception exception) { logger.Error($"File can be parsed but an error occurred: {exception.Message}\n{exception.StackTrace}"); return(ParseFileResult.SuccessfullyParsedButDataInvalid(exception)); } } catch (Exception exception) { logger.Error($"Something went wrong: {exception.Message}\n{exception.StackTrace}"); return(ParseFileResult.CannotParse(exception)); } }
public ParseFileResult ParseFile(Stream fileStream, IFieldDataResultsAppender appender, ILog logger) { var channel = QRevSerializer.DeserializeNoThrow(fileStream, logger); if (channel == null) { return(ParseFileResult.CannotParse()); } var locationIdentifier = channel.SiteInformation?.SiteID?.Value; if (string.IsNullOrWhiteSpace(locationIdentifier)) { logger.Error("File can be parsed but there is no SiteID specified."); return(ParseFileResult.SuccessfullyParsedButDataInvalid("Missing <Channel/SiteInformation/SiteID>")); } try { var trimmedLocationIdentifier = locationIdentifier.Trim(); var location = appender.GetLocationByIdentifier(trimmedLocationIdentifier); return(ParseXmlRootNoThrow(location, channel, appender, logger)); } catch (Exception exception) { logger.Error($"Cannot find location with identifier '{locationIdentifier}'."); return(ParseFileResult.CannotParse(exception)); } }
private static ParseFileResult GetFieldDataResults(FieldDataResultsGenerator resultsGenerator, FieldVisitRecord record) { try { resultsGenerator.GenerateFieldDataResults(record); return(ParseFileResult.SuccessfullyParsedAndDataValid()); } catch (Exception ex) { return(ParseFileResult.SuccessfullyParsedButDataInvalid(ex)); } }
public ParseFileResult Parse(Stream stream, LocationInfo locationInfo = null) { try { var zipArchive = GetZipArchiveOrNull(stream); if (zipArchive == null) { return(ParseFileResult.CannotParse()); } using (zipArchive) { if (zipArchive.Entries.Any(e => e.Name != e.FullName)) { return(ParseFileResult.CannotParse()); } if (!zipArchive.Entries.Any()) { return(ParseFileResult.CannotParse()); } var plugins = LoadPlugins(); if (!plugins.Any()) { return(ParseFileResult.CannotParse()); } var result = ParseArchive(zipArchive, locationInfo, plugins); if (result.Status != ParseFileStatus.SuccessfullyParsedAndDataValid) { return(result); } // Now append all the visits ResultsAppender.AppendAllResults(); return(ParseFileResult.SuccessfullyParsedAndDataValid()); } } catch (Exception exception) { return(ParseFileResult.SuccessfullyParsedButDataInvalid(exception)); } }
private ParseFileResult MapToFramework() { if (!AppendedResults.AppendedVisits.Any()) { return(ParseFileResult.SuccessfullyParsedButDataInvalid("No visits contained in the JSON document.")); } Log.Info($"Importing {AppendedResults.AppendedVisits.Count} visits parsed by '{AppendedResults.PluginAssemblyQualifiedTypeName}' using '{AppendedResults.FrameworkAssemblyQualifiedName}'"); foreach (var visit in AppendedResults.AppendedVisits) { AppendVisit(visit); } return(ParseFileResult.SuccessfullyParsedAndDataValid()); }
private ParseFileResult ParseXmlRootNoThrow(LocationInfo location, Channel channel, IFieldDataResultsAppender appender, ILog logger) { try { var parser = new Parser(location, appender, logger); parser.Parse(channel); return(ParseFileResult.SuccessfullyParsedAndDataValid()); } catch (Exception exception) { logger.Error($"Something went wrong: {exception.Message}\n{exception.StackTrace}"); return(ParseFileResult.SuccessfullyParsedButDataInvalid(exception)); } }
private ParseFileResult AppendResults(LocationInfo locationInfo) { try { UnitSystem = CreateUnitSystem(); var visit = CreateVisit(locationInfo); var dischargeActivity = CreateDischargeActivity(visit); AddGageHeightMeasurement(dischargeActivity); var manualGauging = CreateManualGauging(dischargeActivity); var startStation = DataFile.Stations.First(); var endStation = DataFile.Stations.Last(); foreach (var station in DataFile.Stations) { manualGauging.Verticals.Add(CreateVertical(manualGauging.Verticals.Count, station, startStation, endStation)); } AdjustUnknownTotalDischargePortion(manualGauging); _resultsAppender.AddDischargeActivity(visit, dischargeActivity); manualGauging.MeterCalibration = manualGauging .Verticals .Select(v => v.VelocityObservation.MeterCalibration) .FirstOrDefault(mc => mc != null); AddTemperatureReadings(visit); return(ParseFileResult.SuccessfullyParsedAndDataValid()); } catch (Exception exception) { // Something has gone sideways rather hard. The framework won't log the exception's stack trace // so we explicitly do that here, to help track down any bugs. LogException("Parsing error", exception); return(ParseFileResult.SuccessfullyParsedButDataInvalid(exception)); } }
public ParseFileResult Parse(Stream stream, LocationInfo locationInfo) { DataFile = GetDataFile(stream); if (DataFile == null) { return(ParseFileResult.CannotParse()); } if (locationInfo == null) { if (string.IsNullOrEmpty(DataFile.Properties.SiteNumber)) { return(ParseFileResult.SuccessfullyParsedButDataInvalid($"No {nameof(DataFile.Properties.SiteNumber)} property is set, so no AQUARIUS location can be inferred. Try uploading the file directly to a location.")); } locationInfo = _resultsAppender.GetLocationByIdentifier(DataFile.Properties.SiteNumber); } return(AppendResults(locationInfo)); }
public ParseFileResult Parse(Stream stream, LocationInfo locationInfo = null) { var csvText = ReadTextFromStream(stream); if (csvText == null) { return(ParseFileResult.CannotParse()); } try { LocationInfo = locationInfo; Surveys = LoadSurveys(); using (ResultsAppender) { foreach (var survey in Surveys) { Survey = survey; var result = ParseSurvey(csvText); if (result.Status == ParseFileStatus.CannotParse) { continue; } return(result); } return(ParseFileResult.CannotParse()); } } catch (Exception exception) { return(ParseFileResult.SuccessfullyParsedButDataInvalid(exception)); } }
public ParseFileResult ParseFile(Stream fileStream, IFieldDataResultsAppender fieldDataResultsAppender, ILog logger) { _log = logger; try { using (var delayedAppender = new DelayedAppender(fieldDataResultsAppender)) { _fieldDataResultsAppender = delayedAppender; var parsedRecords = _parser.ParseInputData(fileStream); if (parsedRecords == null) { return(ParseFileResult.CannotParse(NoRecordsInInputFile)); } if (_parser.Errors.Any()) { if (_parser.ValidRecords > 0) { return(ParseFileResult.SuccessfullyParsedButDataInvalid( $"{InputFileContainsInvalidRecords}: {_parser.Errors.Length} errors:\n{string.Join("\n", _parser.Errors.Take(3))}")); } return(ParseFileResult.CannotParse()); } _log.Info($"Parsed {_parser.ValidRecords} rows from input file."); SaveRecords(parsedRecords); return(ParseFileResult.SuccessfullyParsedAndDataValid()); } } catch (Exception e) { _log.Error($"Failed to parse file; {e.Message}\n{e.StackTrace}"); return(ParseFileResult.CannotParse(e)); } }
public ParseFileResult ParseFile(Stream fileStream, LocationInfo targetLocation, IFieldDataResultsAppender appender, ILog logger) { try { Config = new ConfigLoader(appender) .Load(); var summary = GetSummary(fileStream, logger); if (summary == null) { return(ParseFileResult.CannotParse()); } if (targetLocation == null) { var locationIdentifier = ExtractLocationIdentifier(summary.StationName); if (string.IsNullOrEmpty(locationIdentifier)) { return(ParseFileResult.SuccessfullyParsedButDataInvalid("Missing station name")); } targetLocation = appender.GetLocationByIdentifier(locationIdentifier); } var parser = new Parser(Config, targetLocation, appender, logger); parser.Parse(summary); return(ParseFileResult.SuccessfullyParsedAndDataValid()); } catch (Exception e) { return(ParseFileResult.SuccessfullyParsedButDataInvalid(e.Message)); } }