Operation IParser.Parse(string[] lines) { Operation operation = new Operation(); CurrentSection section = CurrentSection.AAnfang; lines = Utilities.Trim(lines); foreach (string line in lines) { string keyword; string messageText = line; if (ParserUtility.StartsWithKeyword(line, _keywords, out keyword)) { switch (keyword) { case "Einsatzdepeche": { section = CurrentSection.AAnfang; break; } case "AAO": { section = CurrentSection.BAao; break; } case "Einsatzort": { section = CurrentSection.CEinsatzort; break; } case "Strasse": { section = CurrentSection.DStrasse; break; } case "Ort": { section = CurrentSection.EOrt; break; } case "Objekt": { section = CurrentSection.FObjekt; break; } case "Wer": { section = CurrentSection.GMeldender; break; } case "Was": { section = CurrentSection.HSchlagwort; break; } case "Wo": { section = CurrentSection.JZusatzinfo; break; } case "Einsatzplan": { section = CurrentSection.KEinsatzplan; break; } case "Hinweistext": { section = CurrentSection.LHinweis; break; } case "Einheiten": { section = CurrentSection.MEinheiten; break; } } if (section == CurrentSection.GMeldender || section == CurrentSection.HSchlagwort || section == CurrentSection.JZusatzinfo || section == CurrentSection.MEinheiten) { section = CurrentSection.ZEnde; } messageText = ParserUtility.GetMessageText(line, keyword); } switch (section) { case CurrentSection.AAnfang: { operation.OperationNumber = ParserUtility.GetTextBetween(messageText, null, "am:", StringComparison.InvariantCulture); string textBeteween = ParserUtility.GetTextBetween(messageText, "am:", "um", StringComparison.InvariantCulture); operation.Timestamp = ParserUtility.ReadFaxTimestamp(textBeteween, DateTime.Now); break; } case CurrentSection.BAao: { operation.Keywords.Keyword = messageText; break; } case CurrentSection.CEinsatzort: { operation.Einsatzort.Location = messageText; break; } case CurrentSection.DStrasse: { string street, streetNumber, appendix; ParserUtility.AnalyzeStreetLine(messageText, out street, out streetNumber, out appendix); operation.Einsatzort.Street = street; operation.Einsatzort.StreetNumber = streetNumber; operation.CustomData["Einsatzort Zusatz"] = appendix; break; } case CurrentSection.EOrt: { operation.Einsatzort.City = messageText; break; } case CurrentSection.FObjekt: { operation.Einsatzort.Property = messageText; break; } case CurrentSection.GMeldender: { operation.Messenger = messageText + Environment.NewLine; break; } case CurrentSection.HSchlagwort: { operation.Keywords.EmergencyKeyword = messageText; break; } case CurrentSection.JZusatzinfo: { operation.Picture = messageText + Environment.NewLine; break; } case CurrentSection.KEinsatzplan: { operation.OperationPlan = messageText; break; } case CurrentSection.LHinweis: { operation.Comment = messageText + Environment.NewLine; break; } case CurrentSection.MEinheiten: { Match match = Regex.Match(messageText, "<(.*)>"); if (match.Success) { string value = match.Groups[1].Value; OperationResource operationResource = new OperationResource(); operationResource.FullName = value; operation.Resources.Add(operationResource); } break; } } } operation.Comment = ParserUtility.RemoveTrailingNewline(operation.Comment); operation.Picture = ParserUtility.RemoveTrailingNewline(operation.Picture); operation.Messenger = ParserUtility.RemoveTrailingNewline(operation.Messenger); return(operation); }
Operation IParser.Parse(string[] lines) { Operation operation = new Operation(); OperationResource last = new OperationResource(); lines = Utilities.Trim(lines); CurrentSection section = CurrentSection.AHeader; bool keywordsOnly = true; for (int i = 0; i < lines.Length; i++) { try { string line = lines[i]; if (line.Length == 0) { continue; } operation.Timestamp = ParserUtility.ReadFaxTimestamp(line, operation.Timestamp); if (GetSection(line.Trim(), ref section, ref keywordsOnly)) { continue; } string msg = line; string prefix = ""; string keyword = null; if (keywordsOnly) { if (!ParserUtility.StartsWithKeyword(line, Keywords, out keyword)) { continue; } int x = line.IndexOf(':'); if (x == -1) { prefix = keyword; msg = line.Remove(0, prefix.Length).Trim(); } else { prefix = line.Substring(0, x); msg = line.Substring(x + 1).Trim(); } prefix = prefix.Trim().ToUpperInvariant(); } switch (section) { case CurrentSection.AHeader: { switch (prefix) { case "ABSENDER": operation.CustomData["Absender"] = msg; break; case "TERMIN": operation.CustomData["Termin"] = msg; break; case "EINSATZNUMMER": operation.OperationNumber = msg; break; default: break; } } break; case CurrentSection.BMitteiler: { // This switch would not be necessary in this section (there is only "Name")... switch (prefix) { case "NAME": operation.Messenger = msg; break; default: break; } } break; case CurrentSection.CEinsatzort: { switch (prefix) { case "STRAßE": { string street, streetNumber, appendix; ParserUtility.AnalyzeStreetLine(msg, out street, out streetNumber, out appendix); operation.CustomData["Einsatzort Zusatz"] = appendix; operation.Einsatzort.Street = street; operation.Einsatzort.StreetNumber = streetNumber; } break; case "ORT": { operation.Einsatzort.ZipCode = ParserUtility.ReadZipCodeFromCity(msg); if (string.IsNullOrWhiteSpace(operation.Einsatzort.ZipCode)) { Logger.Instance.LogFormat(LogType.Warning, this, "Could not find a zip code for city '{0}'. Route planning may fail or yield wrong results!", operation.Einsatzort.City); } operation.Einsatzort.City = msg.Remove(0, operation.Einsatzort.ZipCode.Length).Trim(); // The City-text often contains a dash after which the administrative city appears multiple times (like "City A - City A City A"). // However we can (at least with google maps) omit this information without problems! int dashIndex = operation.Einsatzort.City.IndexOf(" - "); if (dashIndex != -1) { // Ignore everything after the dash operation.Einsatzort.City = operation.Einsatzort.City.Substring(0, dashIndex); } } break; case "OBJEKT": operation.Einsatzort.Property = msg; break; case "PLANNUMMER": operation.CustomData["Einsatzort Plannummer"] = msg; break; case "STATION": operation.CustomData["Einsatzort Station"] = msg; break; default: break; } } break; case CurrentSection.DZielort: { switch (prefix) { case "STRAßE": { string street, streetNumber, appendix; ParserUtility.AnalyzeStreetLine(msg, out street, out streetNumber, out appendix); operation.CustomData["Zielort Zusatz"] = appendix; operation.Zielort.Street = street; operation.Zielort.StreetNumber = streetNumber; } break; case "ORT": { string plz = ParserUtility.ReadZipCodeFromCity(msg); operation.Zielort.ZipCode = plz; operation.Zielort.City = msg.Remove(0, plz.Length).Trim(); } break; case "OBJEKT": operation.Zielort.Property = msg; break; case "STATION": operation.CustomData["Zielort Station"] = msg; break; default: break; } } break; case CurrentSection.EEinsatzgrund: { switch (prefix) { case "SCHLAGW.": operation.Keywords.Keyword = msg; break; case "STICHWORT B": operation.Keywords.B = msg; break; case "STICHWORT R": operation.Keywords.R = msg; break; case "STICHWORT S": operation.Keywords.S = msg; break; case "STICHWORT T": operation.Keywords.T = msg; break; case "PRIO.": operation.Priority = msg; break; default: break; } } break; case CurrentSection.FEinsatzmittel: { if (line.StartsWith("EINSATZMITTEL", StringComparison.CurrentCultureIgnoreCase)) { msg = ParserUtility.GetMessageText(line, "EINSATZMITTEL"); last.FullName = msg; } else if (line.StartsWith("ALARMIERT", StringComparison.CurrentCultureIgnoreCase) && !string.IsNullOrEmpty(msg)) { msg = ParserUtility.GetMessageText(line, "Alarmiert"); DateTime dt = ParserUtility.TryGetTimestampFromMessage(msg, operation.Timestamp); last.Timestamp = dt.ToString(); } else if (line.StartsWith("GEFORDERTE AUSSTATTUNG", StringComparison.CurrentCultureIgnoreCase)) { msg = ParserUtility.GetMessageText(line, "Geforderte Ausstattung"); if (!string.IsNullOrWhiteSpace(msg)) { last.RequestedEquipment.Add(msg); } operation.Resources.Add(last); last = new OperationResource(); } } break; case CurrentSection.GBemerkung: { operation.Comment = operation.Comment += msg + "\n"; } break; case CurrentSection.HFooter: // The footer can be ignored completely. break; default: break; } } catch (Exception ex) { Logger.Instance.LogFormat(LogType.Warning, this, "Error while parsing line '{0}'. The error message was: {1}", i, ex.Message); } } operation.Comment = ParserUtility.RemoveTrailingNewline(operation.Comment); return(operation); }