Path: redb-route/redb.Route.Xml/CoreElements/DataFormatContextContribution.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/CoreElements/DataFormatContextContribution.cs
----
using System.Xml.Linq;
using redb.Route.Abstractions;
using redb.Route.Extensions;
namespace redb.Route.Xml.CoreElements;
///
/// <dataFormat type="…" name="csv" delimiter=";"/> in context.xml: registers a data format on the
/// context, as context.AddDataFormat(new CsvDataFormat(...)) does in code, so <marshal format="…"/> and
/// content-type dispatch find it. The type is the format's serializer (an IMessageSerializer); the other attributes
/// set its options, as on the per-step <marshal type="…"> form. The format answers to its own content type
/// and media types, and to each name in name (comma-separated).
///
internal sealed class DataFormatContextContribution : IXmlContextContribution
{
/// The one instance, in the core registry.
internal static DataFormatContextContribution Instance { get; } = new();
// Not constructible from outside: a host that discovers package contributions by scanning assemblies for
// IXmlElementContribution types with a parameterless constructor (redb.Tsak) must not register a core element twice.
private DataFormatContextContribution() { }
private static readonly string[] OwnAttributes = ["type", "name", "id", "description"];
///
public string Name => "dataFormat";
///
public XmlElementKind Kind => XmlElementKind.ContextLevel;
///
public ElementSpec Spec => CoreContributions.Specs[Name];
///
public IRouteDefinition Apply(XElement element, IRouteDefinition current, XmlParseContext context)
{
context.AddError(element, " registers a format for the whole context — put it inside , not in a route.");
return current;
}
///
public void ApplyContext(XElement element, Core.RouteContext context, XmlParseContext parseContext)
{
if (parseContext.RequiredAttr(element, "type") is not { } typeName)
return;
var options = element.Attributes().Where(a => !OwnAttributes.Contains(a.Name.LocalName)).ToList();
if (CoreContributions.CreateSerializer(element, parseContext, typeName, options) is not { } serializer)
return;
var names = (parseContext.Attr(element, "name") ?? "")
.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
context.AddDataFormat(serializer, names);
}
}