/// <summary> /// Consumes the provided data and provides a value indicating whether the data could be processed and a callback to create a success-notification /// </summary> /// <param name="data">the data that was extracted from an ImportSource</param> /// <param name="logCallback">a callback that can be used to log events on the parser-process</param> /// <param name="getNotification">a callback that will provide an acceptance parameter for the current parsing-unit</param> /// <returns>a value indicating whether the data was successfully processed</returns> protected override bool Consume(IDictionary <string, TValue> data, LogParserEventCallback <IDictionary <string, TValue> > logCallback, out Func <DynamicResult, DictionaryAcceptanceCallbackParameter <TValue> > getNotification) { bool ok = false; List <string> acceptedKeys = new List <string>(); switch (columnMode) { case ColumnNameMode.Original: { foreach (string name in data.Keys) { SetValueOfColumn(name, data[name]); acceptedKeys.Add(name); } AddCurrentRecord(); ok = true; break; } case ColumnNameMode.FromFirstLine: { if (!CheckFirstLine(data)) { foreach (KeyValuePair <string, string> item in autoMap) { TValue vl = default(TValue); if (data.ContainsKey(item.Key)) { vl = data[item.Key]; } SetValueOfColumn(item.Value, vl); acceptedKeys.Add(item.Key); } if (data.ContainsKey("$origin")) { SetValueOfColumn("$origin", data["$origin"]); } AddCurrentRecord(); } ok = true; break; } case ColumnNameMode.FromConfig: { foreach (ColumnConfiguration column in config.Columns) { try { SetValueOfColumn(column.TableColumn, data[column.RawDataKey]); acceptedKeys.Add(column.RawDataKey); } catch (Exception ex) { logCallback(data, $@"Unable to process the provided Recordset: {ex.Message}", ParserEventSeverity.Error); } } if (data.ContainsKey("$origin")) { SetValueOfColumn("$origin", data["$origin"]); } AddCurrentRecord(); ok = true; break; } } getNotification = d => new DictionaryAcceptanceCallbackParameter <TValue>(ok, tableName, d, data, acceptedKeys.ToArray()); return(ok); }
/// <summary> /// Consumes the provided text and returns the initial character position of recognition /// </summary> /// <param name="data">the text that was extracted from a TextSource</param> /// <param name="acceptanceCallback">A Callback that is used by the importer to commit the acceptance of a Part of the delivered data</param> /// <param name="logCallback">a callback that can be used to log events on the parser-process</param> /// <returns>a value indicating whether the data was successfully processed</returns> public bool Consume(T data, AcceptanceCallback <TNotification, T> acceptanceCallback, LogParserEventCallback <T> logCallback) { this.logCallback.Value = logCallback; bool success = false; Func <DynamicResult, TNotification> getNotification = null; try { success = Consume(data, logCallback, out getNotification); ConsumedAnyData |= success; } finally { this.logCallback.Value = null; if (success && getNotification != null) { acceptanceCallback(getNotification(newData.Value)); } newData.Value = null; } return(success); }
/// <summary> /// Consumes the provided data and provides a value indicating whether the data could be processed and a callback to create a success-notification /// </summary> /// <param name="data">the data that was extracted from an ImportSource</param> /// <param name="logCallback">a callback that can be used to log events on the parser-process</param> /// <param name="getNotification">a callback that will provide an acceptance parameter for the current parsing-unit</param> /// <returns>a value indicating whether the data was successfully processed</returns> protected abstract bool Consume(T data, LogParserEventCallback <T> logCallback, out Func <DynamicResult, TNotification> getNotification);
/// <summary> /// Consumes the provided text and returns the initial character position of recognition /// </summary> /// <param name="text">the text that was extracted from a TextSource</param> /// <param name="logCallback">a callback that can be used to log events on the parser-process</param> /// <param name="getNotification">a callback that will provide an acceptance parameter for the current parsing-unit</param> /// <returns>a value indicating whether the data was successfully processed</returns> protected override bool Consume(string text, LogParserEventCallback <string> logCallback, out Func <DynamicResult, TextAcceptanceCallbackParameter> getNotification) { getNotification = null; int acceptedLength = -1; int newLineAddition = 0; if (text.EndsWith(Environment.NewLine)) { newLineAddition = Environment.NewLine.Length; text = text.Substring(0, text.Length - newLineAddition); } Dictionary <string, object> variables = new Dictionary <string, object>(); foreach (RegexWrapper cfg in regexes) { Match m = cfg.Regex.Match(text); if (m.Success) { if (cfg.Start) { if (HasOpenValues) { LogEnvironment.LogDebugEvent(null, "Entity may be incomplete! Please check Configuration (Maybe an inappropriate Starter-Regex is configured)", (int)LogSeverity.Warning, null); if (!ignoreWarnedResults) { AddCurrentRecord(); } else { DismissCurrentRecord(text); } } } acceptedLength = m.Length + newLineAddition; foreach (string groupName in cfg.Regex.GetGroupNames()) { ColumnConfiguration col = config.Columns[groupName]; if (col != null) { string val = m.Groups[groupName].Value; object dbValue = val; if (!string.IsNullOrEmpty(col.ConvertExpression)) { variables.Clear(); variables.Add("value", val); dbValue = ExpressionParser.Parse(col.ConvertExpression, variables, a => { DefaultCallbacks.PrepareDefaultCallbacks(a.Scope, a.ReplSession); }); } SetValueOfColumn(col.TableColumn, dbValue); } } bool ok = m.Index == 0 && acceptedLength >= text.Length; if (cfg.End) { if (!HasOpenValues) { LogEnvironment.LogDebugEvent(null, "Preventing empty record from being registered! Please check Configuration.", (int)LogSeverity.Warning, null); //callback(new TextAcceptanceCallbackParameter(false, 0, 0, "", null)); return(false); } if (!ignoreWarnedResults || ok) { AddCurrentRecord(); } else { DismissCurrentRecord(text); } } getNotification = t => new TextAcceptanceCallbackParameter(true, m.Index, acceptedLength, config.Name, t, text); return(true); } } return(false); }