Path: redb-route/redb.Route/Xslt/XsltProcessor.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/XsltProcessor.cs
----
using System.Collections.Concurrent;
using redb.Route.Abstractions;
namespace redb.Route.Xslt;
///
/// Pipeline processor that transforms the exchange body through an — the
/// transformation counterpart of . The result
/// (string / bytes / DOM, per ) replaces In.Body. The
/// Content-Type header is left unchanged (matching Apache Camel).
///
/// By default all message headers and exchange properties are passed as stylesheet parameters
/// (xsl:param), like Camel. When allowTemplateFromHeader is enabled a per-message
/// stylesheet from /
/// overrides the compiled default; those dynamic stylesheets are compiled once and cached.
///
///
public sealed class XsltProcessor : IProcessor
{
private readonly IXsltEngine _defaultEngine;
private readonly XsltOutput _output;
private readonly bool _failOnNullBody;
private readonly bool _passHeadersAsParameters;
private readonly bool _allowTemplateFromHeader;
private readonly Func? _fileEngineFactory;
private readonly Func? _contentEngineFactory;
private readonly ConcurrentDictionary _dynamicEngines = new(StringComparer.Ordinal);
/// Creates an XSLT processor around a compiled engine.
/// The default (compile-once) transformation engine.
/// The result form (default ).
/// Throw when the input body is null (Camel default: true).
/// Pass headers/properties as stylesheet parameters (default: true).
/// Allow a per-message stylesheet from headers (default: false).
/// Compiles an engine from a stylesheet path (for the dynamic path).
/// Compiles an engine from inline stylesheet content (for the dynamic path).
public XsltProcessor(
IXsltEngine engine,
XsltOutput output = XsltOutput.String,
bool failOnNullBody = true,
bool passHeadersAsParameters = true,
bool allowTemplateFromHeader = false,
Func? fileEngineFactory = null,
Func? contentEngineFactory = null)
{
_defaultEngine = engine ?? throw new ArgumentNullException(nameof(engine));
_output = output;
_failOnNullBody = failOnNullBody;
_passHeadersAsParameters = passHeadersAsParameters;
_allowTemplateFromHeader = allowTemplateFromHeader;
_fileEngineFactory = fileEngineFactory;
_contentEngineFactory = contentEngineFactory;
}
///
public Task Process(IExchange exchange, CancellationToken ct = default)
{
var body = exchange.In.Body;
if (body is null)
{
if (_failOnNullBody)
throw new InvalidOperationException("XSLT: the input body is null (failOnNullBody=true).");
return Task.CompletedTask;
}
var engine = ResolveEngine(exchange);
var parameters = _passHeadersAsParameters ? XsltParameters.FromExchange(exchange) : null;
exchange.In.Body = engine.Transform(body, _output, parameters);
return Task.CompletedTask;
}
private IXsltEngine ResolveEngine(IExchange exchange)
{
if (!_allowTemplateFromHeader)
return _defaultEngine;
if (_fileEngineFactory is not null &&
exchange.In.Headers.TryGetValue(XsltHeaders.ResourceUri, out var uriObj) &&
uriObj is string uri && !string.IsNullOrWhiteSpace(uri))
{
return _dynamicEngines.GetOrAdd("uri:" + uri, _ => _fileEngineFactory(uri));
}
if (_contentEngineFactory is not null &&
exchange.In.Headers.TryGetValue(XsltHeaders.Stylesheet, out var xmlObj) &&
xmlObj is string xml && !string.IsNullOrWhiteSpace(xml))
{
return _dynamicEngines.GetOrAdd("xml:" + xml, _ => _contentEngineFactory(xml));
}
return _defaultEngine;
}
}