Path: redb-route/redb.Route.Xml/StructuredEndpoint.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/StructuredEndpoint.cs ---- using System.Text; using System.Xml.Linq; using redb.Route.Abstractions; namespace redb.Route.Xml; /// /// The structured endpoint form: an address-carrying step (<from>, /// <to>, <toD>, <wireTap>, <enrich>, /// <pollEnrich>) takes either uri= or exactly one child endpoint element, /// normalized HERE into the same URI string a hand-written address would be — the engine /// (endpoint cache, statistics, mock masks, secret redaction) sees one canon and zero new code. /// One generic procedure for every scheme: no switch per connector, ever (owner rule /// 2026-09-02 — "one general approach, copy-paste ruled out by construction"). /// internal static class StructuredEndpoint { /// /// The endpoint URI of an address-carrying step, whichever form it uses. Returns null after /// recording errors; is the element to blame in messages (the /// endpoint child for the structured form, the step itself otherwise). /// internal static string? Resolve(XElement step, XmlParseContext ctx, out XElement position) { position = step; var uri = ctx.Attr(step, "uri"); var children = step.Elements().ToList(); if (uri is not null) { if (children.Count > 0) { ctx.AddError(step, $"<{step.Name.LocalName}> takes either uri= or a structured endpoint child, not both."); return null; } return uri; } if (children.Count == 0) { ctx.AddError(step, $"<{step.Name.LocalName}> requires the uri attribute or one endpoint child " + "(the structured form, e.g. …)."); return null; } if (children.Count > 1) { ctx.AddError(step, $"<{step.Name.LocalName}> takes exactly one endpoint child; {children.Count} found."); return null; } position = children[0]; return Build(children[0], ctx); } /// Builds scheme://path?options from one endpoint element. See the class remarks. private static string? Build(XElement endpoint, XmlParseContext ctx) { var scheme = endpoint.Name.LocalName; // The path part: the universal path= attribute, the component's own one-line synonym // (kafka → topic; read from the component, never from a list here), or the // element's text content (CDATA — the SQL-style connectors whose path IS a text). var component = (ctx.RouteContext as redb.Route.Core.RouteContext) ?.GetComponent(scheme) as redb.Route.Core.ComponentBase; var synonym = component?.StructuredPathSynonym is { Length: > 0 } s ? s : null; var pathAttr = ctx.Attr(endpoint, "path"); var synonymAttr = synonym is not null ? ctx.Attr(endpoint, synonym) : null; var text = Text(endpoint); if (component?.PathIsText == true && (pathAttr is not null || synonymAttr is not null)) { ctx.AddError(endpoint, $"the path of <{scheme}> is a text — put it in the element content (CDATA), not an attribute."); return null; } var pathSources = new[] { pathAttr, synonymAttr, text }.Count(v => v is not null); if (pathSources > 1) { ctx.AddError(endpoint, $"<{scheme}> carries its path more than once " + $"(path=, {(synonym is null ? "" : $"{synonym}=, ")}text content — exactly one)."); return null; } var path = pathAttr ?? synonymAttr ?? text ?? string.Empty; if (path.Contains('?')) { ctx.AddError(endpoint, $"the path of <{scheme}> contains '?', which starts the option part of a URI — " + "this address is not expressible as an endpoint URI."); return null; } var options = Options(endpoint, synonym, ctx.AddError); if (options is null) return null; return Compose(scheme, path, options); } /// The attributes of a step <bean> that are not options of the bean: endpoint. private static readonly string[] BeanStepAttributes = ["ref", "beanType", "id", "description"]; /// /// The bean: URI of a step <bean ref="#svc" method="Handle"/> (amended /// 2026-11-05): the structured endpoint child of <to> without the wrapper. ref= or /// beanType= carries what path= carries there; every other attribute is an option, exactly as on /// <to><bean/></to>. Null after recording errors. /// internal static string? ResolveBeanStep(XElement step, XmlParseContext ctx) => BeanStepUri(step, ctx.AddError); /// Context-free form of for the code generator. internal static string NormalizeBeanStepForPrint(XElement step) => BeanStepUri(step, (at, message) => throw new InvalidOperationException($"<{at.Name.LocalName}>: {message}"))!; /// The bean reference a step <bean> names: #name or Type, Assembly; null when malformed. internal static string? BeanStepReference(XElement step) { var reference = step.Attribute("ref")?.Value; var type = step.Attribute("beanType")?.Value; if (reference is { Length: > 0 } && type is null) return reference.StartsWith('#') ? reference : "#" + reference; if (type is { Length: > 0 } && reference is null) return type; return null; } private static string? BeanStepUri(XElement step, Action error) { if (step.Attribute("name") is not null || step.Attribute("type") is not null) { error(step, "a with name= or type= is a declaration: it belongs directly under or . " + "A step calls a bean: or ."); return null; } if (step.Attribute("path") is not null) { error(step, "a step names its bean with ref= or beanType=, not path= (path= belongs to )."); return null; } if (step.HasElements) { error(step, "a step takes attributes only; write the bean's options as attributes."); return null; } var reference = BeanStepReference(step); if (reference is null) { error(step, "a step takes exactly one of ref=\"#name\" and beanType=\"Type, Assembly\"."); return null; } if (reference.Contains('?')) { error(step, "the bean reference contains '?', which starts the option part of a URI; write the method as method=."); return null; } var options = Options(step, pathSynonym: null, error, BeanStepAttributes); return options is null ? null : Compose("bean", reference, options); } private static string Compose(string scheme, string path, IEnumerable<(string Key, string Value, XElement At)> options) { var builder = new StringBuilder(scheme).Append("://").Append(path); var separator = '?'; foreach (var (key, value, _) in options) { builder.Append(separator).Append(key).Append('=').Append(EscapeValue(value)); separator = '&'; } return builder.ToString(); } /// /// The query options one endpoint element writes, in document order, each with the element /// that wrote it — what puts after '?', and what the package gate holds /// against the connector's options. Null after reporting a malformed child. /// /// Every unqualified attribute is an option verbatim — the same names, the same /// converter, the same unknown-name rule the URI form has (typed validation happens where it /// always did: in BindFromUri at endpoint creation). path= and the component's /// path synonym are the path, not options. /// An attribute in a foreign namespace is metadata for other tools (tolerated, /// never read) — it is not an option, or a strict connector would refuse an editor's mark. /// Two generic child forms, both an orthography of URI options: a family /// entry <param name="login" value="…"/> → param.login=…; a long text /// option <onSuccess><![CDATA[…]]></onSuccess> → onSuccess=…. /// /// internal static List<(string Key, string Value, XElement At)>? Options( XElement endpoint, string? pathSynonym, Action error, IReadOnlyCollection? notOptions = null) { var options = new List<(string Key, string Value, XElement At)>(); foreach (var attribute in endpoint.Attributes()) { if (attribute.IsNamespaceDeclaration || attribute.Name.NamespaceName.Length > 0 || attribute.Name.LocalName == "path" || (pathSynonym is not null && attribute.Name.LocalName == pathSynonym) || (notOptions is not null && notOptions.Contains(attribute.Name.LocalName))) continue; options.Add((attribute.Name.LocalName, attribute.Value, endpoint)); } foreach (var child in endpoint.Elements()) { var name = child.Attribute("name")?.Value; var value = child.Attribute("value")?.Value; var content = Text(child); if (name is not null && value is not null && content is null && !child.Elements().Any()) { options.Add(($"{child.Name.LocalName}.{name}", value, child)); continue; } if (name is null && content is not null && !child.HasAttributes && !child.Elements().Any()) { options.Add((child.Name.LocalName, content, child)); continue; } error(child, $"<{child.Name.LocalName}> inside <{endpoint.Name.LocalName}> must be a family entry " + $"(<{child.Name.LocalName} name=… value=…/>) or a text option " + $"(<{child.Name.LocalName}>content)."); return null; } return options; } /// /// Option values travel raw, like hand-written URIs and the fluent builders — except the two /// characters the engine's query parse cannot survive: '&' splits pairs and '%' is /// unescaped by EndpointUriParser. Encoding just those keeps ordinary values /// byte-identical to the URI form and round-trips the rest exactly. /// private static string EscapeValue(string value) => value.Replace("%", "%25").Replace("&", "%26"); /// /// Context-free normalization for the code generator: no component lookup, so path synonyms /// are unavailable (the tool passes a catalog for those); the universal path=/content and /// the two child forms normalize identically to . /// internal static string NormalizeForPrint(XElement endpoint) { var scheme = endpoint.Name.LocalName; var path = endpoint.Attribute("path")?.Value ?? Text(endpoint) ?? string.Empty; var options = Options(endpoint, pathSynonym: null, (at, message) => throw new InvalidOperationException($"<{at.Name.LocalName}>: {message}"))!; return Compose(scheme, path, options); } /// The element's own text (CDATA included), null when blank. private static string? Text(XElement element) { var text = string.Concat(element.Nodes().OfType().Select(t => t.Value)); return string.IsNullOrWhiteSpace(text) ? null : text.Trim(); } }