Path: redb-route/redb.Route/Xslt/XsltComponent.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/Xslt/XsltComponent.cs
----
using redb.Route.Abstractions;
using redb.Route.Core;
namespace redb.Route.Xslt;
// ── Component ────────────────────────────────────────────────────────────────
///
/// Component for XSLT transformation (Apache Camel xslt: parity). Producer-only — you send
/// to it. Mirrors .
///
/// URI format: xslt:path/to/stylesheet.xsl. Parameters:
///
/// - output — result form: string (default) / bytes / dom.
/// - failOnNullBody — throw when the input body is null (default true).
/// - allowTemplateFromHeader — allow a per-message stylesheet from headers (default false).
///
///
///
/// Registered out of the box under the xslt scheme, so .To("xslt:...") works without any
/// setup. All headers and exchange properties are passed to the stylesheet as parameters (Camel parity).
///
///
[EndpointRole(EndpointRole.Producer, Hint = "Use .To(\"xslt:...\") or the .Xslt(...) DSL.")]
public sealed class XsltComponent : ComponentBase
{
///
public override string Scheme => "xslt";
///
public override IEndpoint CreateEndpoint(EndpointUri uri)
{
ArgumentNullException.ThrowIfNull(uri);
var options = new XsltEndpointOptions();
options.BindFromUri(uri.RawParameters);
// The stylesheet path resolves through the context's resource resolver
// (absolute → ResourceRoot → AppContext.BaseDirectory → working directory), so a stylesheet
// inside an unpacked package is found wherever the worker process happens to run from.
options.StylesheetPath = string.IsNullOrWhiteSpace(uri.Path)
? uri.Path
: ResourceResolution.Resolve(Context, uri.Path, "XSLT stylesheet");
options.Validate();
// Resolved here, where the component still has its context: the endpoint compiles the
// stylesheet in its constructor and would otherwise have no way to ask.
return new XsltEndpoint(uri, this, options, Context.GetXsltEngineFactory());
}
}
// ── Options ──────────────────────────────────────────────────────────────────
/// Options for the XSLT endpoint.
public sealed class XsltEndpointOptions : EndpointOptions
{
/// Path to the stylesheet file (from the URI path).
public string StylesheetPath { get; set; } = string.Empty;
/// Result form: string (default) / bytes / dom.
public XsltOutput Output { get; set; } = XsltOutput.String;
/// Throw when the input body is null. Default: true.
public bool FailOnNullBody { get; set; } = true;
/// Allow a per-message stylesheet from the CamelXsltResourceUri/CamelXsltStylesheet
/// headers to override the compiled default. Default: false.
public bool AllowTemplateFromHeader { get; set; }
///
public override void Validate()
{
if (string.IsNullOrWhiteSpace(StylesheetPath))
throw new ArgumentException("Stylesheet path is required for the xslt component.");
if (!File.Exists(StylesheetPath))
throw new FileNotFoundException($"XSLT stylesheet not found: {StylesheetPath}");
}
}
// ── Endpoint ─────────────────────────────────────────────────────────────────
/// Endpoint that transforms the body through a compiled XSLT stylesheet.
public sealed class XsltEndpoint : EndpointBase
{
private readonly XsltProcessor _processor;
internal XsltEndpoint(
EndpointUri uri,
XsltComponent component,
XsltEndpointOptions options,
IXsltEngineFactory engines)
: base(uri, component, options)
{
ArgumentNullException.ThrowIfNull(engines);
// Reuse XsltProcessor so the component gets parameters + dynamic-from-header behaviour for free.
// The stylesheet is compiled once when the endpoint is created (Camel contentCache=true).
_processor = new XsltProcessor(
engines.FromFile(options.StylesheetPath),
options.Output, options.FailOnNullBody,
passHeadersAsParameters: true,
allowTemplateFromHeader: options.AllowTemplateFromHeader,
fileEngineFactory: engines.FromFile,
contentEngineFactory: engines.FromContent);
}
internal XsltProcessor Processor => _processor;
///
public override IProducer CreateProducer() => new XsltProducer(this);
///
public override IConsumer CreateConsumer(IProcessor processor)
=> throw new NotSupportedException(
"XSLT component does not support consuming (from). Use .To(\"xslt:...\") or the .Xslt(...) DSL.");
}
// ── Producer ─────────────────────────────────────────────────────────────────
/// Producer that runs the XSLT transform when the route sends to this endpoint.
public sealed class XsltProducer : IProducer
{
private readonly XsltEndpoint _endpoint;
internal XsltProducer(XsltEndpoint endpoint)
=> _endpoint = endpoint ?? throw new ArgumentNullException(nameof(endpoint));
///
public Task Process(IExchange exchange, CancellationToken ct = default)
=> _endpoint.Processor.Process(exchange, ct);
///
public Task Start(CancellationToken ct = default) => Task.CompletedTask;
///
public Task Stop(CancellationToken ct = default) => Task.CompletedTask;
}