private void FormatError(FormatItem errorItem, Exception innerException, int startIndex, FormattingInfo formattingInfo) { OnFormattingFailure?.Invoke(this, new FormattingErrorEventArgs(errorItem.RawText, startIndex, Settings.FormatErrorAction != ErrorAction.ThrowError)); switch (Settings.FormatErrorAction) { case ErrorAction.Ignore: return; case ErrorAction.ThrowError: throw innerException as FormattingException ?? new FormattingException(errorItem, innerException, startIndex); case ErrorAction.OutputErrorInResult: formattingInfo.FormatDetails.FormattingException = innerException as FormattingException ?? new FormattingException(errorItem, innerException, startIndex); formattingInfo.Write(innerException.Message); formattingInfo.FormatDetails.FormattingException = null; break; case ErrorAction.MaintainTokens: formattingInfo.Write(formattingInfo.Placeholder?.RawText ?? "'null'"); break; } }
/// <summary> /// First check whether the named formatter name exist in of the <see cref="FormatterExtensions" />, /// next check whether the named formatter is able to process the format. /// </summary> /// <param name="formattingInfo"></param> /// <returns>True if an FormatterExtension was found, else False.</returns> private bool InvokeFormatterExtensions(FormattingInfo formattingInfo) { if (formattingInfo.Placeholder is null) { return(false); } var formatterName = formattingInfo.Placeholder.FormatterName; // Evaluate the named formatter (or, evaluate all "" formatters) foreach (var formatterExtension in FormatterExtensions) { if (!formatterExtension.Names.Contains(formatterName)) { continue; } var handled = formatterExtension.TryEvaluateFormat(formattingInfo); if (handled) { return(true); } } return(false); }
public override void Format(FormattingInfo formattingInfo) { foreach (var item in formattingInfo.Format.Items) { if (item is LiteralText literalItem) { m_Characters = m_Characters.Concat(item.ToEnumerable()); continue; } // Otherwise, the item must be a placeholder. var placeholder = (Placeholder)item; var childFormattingInfo = formattingInfo.CreateChild(placeholder); var formatterName = childFormattingInfo.Placeholder.FormatterName; // Evaluate the named formatter (or, evaluate all "" formatters) foreach (var formatterExtension in FormatterExtensions) { if (formatterExtension is IFormatterLiteralExtractor literalExtractor && formatterExtension.Names.Contains(formatterName)) { literalExtractor.WriteAllLiterals(childFormattingInfo); } } } }
public static FormattingInfo Get(FormattingInfo parent, FormatDetails formatDetails, Placeholder placeholder, object currentValue) { var fi = s_Pool.Get(); fi.Init(parent, formatDetails, placeholder, currentValue); return(fi); }
private bool InvokeSourceExtensions(FormattingInfo formattingInfo) { foreach (var sourceExtension in SourceExtensions) { // if the current value is of type SmartObjects // then try to find the right source extension for each of the objects in SmartObjects // Note: SmartObjects cannot be nested, so this can be the case only once. var smartObjects = formattingInfo.CurrentValue as SmartObjects; if (smartObjects != null) { var savedCurrentValue = formattingInfo.CurrentValue; foreach (var obj in smartObjects) { formattingInfo.CurrentValue = obj; var handled = sourceExtension.TryEvaluateSelector(formattingInfo); if (handled) { return(true); } } formattingInfo.CurrentValue = savedCurrentValue; } else { // other object - default handling var handled = sourceExtension.TryEvaluateSelector(formattingInfo); if (handled) { return(true); } } } return(false); }
/// <summary> /// Try to get a suitable formatter. /// </summary> /// <param name="formattingInfo"></param> /// <exception cref="FormattingException"></exception> private void EvaluateFormatters(FormattingInfo formattingInfo) { var handled = InvokeFormatterExtensions(formattingInfo); if (!handled) { throw formattingInfo.FormattingException("No suitable Formatter could be found", formattingInfo.Format); } }
private bool InvokeSourceExtensions(FormattingInfo formattingInfo) { foreach (var sourceExtension in SourceExtensions) { var handled = sourceExtension.TryEvaluateSelector(formattingInfo); if (handled) { return(true); } } return(false); }
private void EvaluateSelectors(FormattingInfo formattingInfo) { if (formattingInfo.Placeholder is null) { return; } var firstSelector = true; foreach (var selector in formattingInfo.Placeholder.Selectors) { formattingInfo.Selector = selector; formattingInfo.Result = null; var handled = InvokeSourceExtensions(formattingInfo); if (handled) { formattingInfo.CurrentValue = formattingInfo.Result; } if (firstSelector) { firstSelector = false; // Handle "nested scopes" by traversing the stack: var parentFormattingInfo = formattingInfo; while (!handled && parentFormattingInfo.Parent != null) { parentFormattingInfo = parentFormattingInfo.Parent; parentFormattingInfo.Selector = selector; parentFormattingInfo.Result = null; handled = InvokeSourceExtensions(parentFormattingInfo); if (handled) { formattingInfo.CurrentValue = parentFormattingInfo.Result; } } } if (!handled) { throw formattingInfo.FormattingException($"Could not evaluate the selector \"{selector.RawText}\"", selector); } } }
/// <summary> /// Format the <see cref="FormattingInfo" /> argument. /// </summary> /// <param name="formattingInfo"></param> public void Format(FormattingInfo formattingInfo) { // Before we start, make sure we have at least one source extension and one formatter extension: CheckForExtensions(); if (formattingInfo.Format is null) { return; } foreach (var item in formattingInfo.Format.Items) { if (item is LiteralText literalItem) { formattingInfo.Write(literalItem.ToString()); continue; } // Otherwise, the item must be a placeholder. var placeholder = (Placeholder)item; var childFormattingInfo = formattingInfo.CreateChild(placeholder); try { EvaluateSelectors(childFormattingInfo); } catch (Exception ex) { // An error occurred while evaluation selectors var errorIndex = placeholder.Format?.startIndex ?? placeholder.Selectors.Last().endIndex; FormatError(item, ex, errorIndex, childFormattingInfo); continue; } try { EvaluateFormatters(childFormattingInfo); } catch (Exception ex) { // An error occurred while evaluating formatters var errorIndex = placeholder.Format?.startIndex ?? placeholder.Selectors.Last().endIndex; FormatError(item, ex, errorIndex, childFormattingInfo); } } }
private void Format(FormatDetails formatDetails, Format format, object current) { var formattingInfo = new FormattingInfo(formatDetails, format, current); Format(formattingInfo); }
public static void Release(FormattingInfo toRelease) => s_Pool.Release(toRelease);