Path: redb-route/redb.Route.Xml/ElementRegistry.cs Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages. License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route.Xml/ElementRegistry.cs ---- namespace redb.Route.Xml; /// /// The element registry: one name — one contribution, the union of the core set and the /// package extensions of a concrete installation. A duplicate name is a hard build error, never /// a silent override; the parser, the schema and the catalog all read this one list. /// public sealed class ElementRegistry { private readonly Dictionary _byName = new(StringComparer.Ordinal); /// Registers one contribution. A duplicate element name is refused loudly. public void Add(IXmlElementContribution contribution) { ArgumentNullException.ThrowIfNull(contribution); if (!_byName.TryAdd(contribution.Name, contribution)) throw new InvalidOperationException( $"XML element '{contribution.Name}' is already registered by " + $"{_byName[contribution.Name].GetType().FullName}. One name — one contribution; " + "rename the element or drop the duplicate registration."); } /// The contribution for a local element name, or null. public IXmlElementContribution? Find(string localName) => _byName.GetValueOrDefault(localName); /// Every registered element name (for diagnostics and the "did you mean" hint). public IReadOnlyCollection Names => _byName.Keys; /// Every registered contribution — the one list the schema and the catalog read. public IReadOnlyCollection Contributions => _byName.Values; /// The closest registered name by edit distance, or null when nothing is close. internal string? ClosestTo(string name) { string? best = null; var bestDistance = int.MaxValue; foreach (var candidate in _byName.Keys) { var d = Levenshtein(name, candidate); if (d < bestDistance) { bestDistance = d; best = candidate; } } return bestDistance <= Math.Max(2, name.Length / 3) ? best : null; } private static int Levenshtein(string a, string b) { var previous = new int[b.Length + 1]; var current = new int[b.Length + 1]; for (var j = 0; j <= b.Length; j++) previous[j] = j; for (var i = 1; i <= a.Length; i++) { current[0] = i; for (var j = 1; j <= b.Length; j++) { var substitution = previous[j - 1] + (char.ToLowerInvariant(a[i - 1]) == char.ToLowerInvariant(b[j - 1]) ? 0 : 1); current[j] = Math.Min(Math.Min(previous[j] + 1, current[j - 1] + 1), substitution); } (previous, current) = (current, previous); } return previous[b.Length]; } /// Builds the registry: the core contributions plus the supplied extensions. public static ElementRegistry CreateDefault(IReadOnlyList? extensions = null) { var registry = new ElementRegistry(); foreach (var contribution in CoreElements.CoreContributions.All) registry.Add(contribution); if (extensions is not null) foreach (var contribution in extensions) registry.Add(contribution); return registry; } }