Path: redb-route/redb.Route/Expressions/FileLanguage.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/Expressions/FileLanguage.cs
----
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace redb.Route.Expressions;
/// What a key of the file language means: one fact about a file.
public enum FileFact
{
/// The file name with its extension.
Name,
/// The file name without its extension.
NameOnly,
/// The extension, without the leading dot.
Extension,
/// The directory that holds the file.
Parent,
/// The full path of the file.
AbsolutePath,
/// The path of the file relative to the directory the consumer polls.
RelativePath,
/// The size in bytes.
Length,
/// The last write time.
LastModified,
}
///
/// The facts about one file that the file language reads. Each source of files supplies its own: the expression
/// engine reads the redbFile.* headers of the message, a file consumer the file it is handling, the S3 consumer
/// the object key. A fact the source does not know is null.
///
public interface IFileFacts
{
/// The file name with its extension.
string? Name { get; }
/// The file name without its extension.
string? NameOnly { get; }
/// The extension, without the leading dot (xml).
string? Extension { get; }
/// The directory that holds the file.
string? Parent { get; }
/// The full path of the file.
string? AbsolutePath { get; }
/// The path relative to the polled directory.
string? RelativePath { get; }
/// The size in bytes.
long? Length { get; }
/// The last write time.
DateTimeOffset? LastModified { get; }
}
///
/// Apache Camel's file language: ${file:name}, ${file:name.noext} and the other keys. One table of keys
/// and one resolver serve the expression engine and the components that substitute file keys in their own options
/// (moveTo, doneFileName and the like), so a key means the same thing in both.
///
/// The expression engine matches keys with , as Camel does; component options
/// match with , where ${FILE:NAME} has always worked.
///
///
public static class FileLanguage
{
/// The keys, in the order an error lists them, and the fact each one reads.
public static IReadOnlyList> Keys { get; } =
[
new("name", FileFact.Name),
new("onlyname", FileFact.Name),
new("name.noext", FileFact.NameOnly),
new("onlyname.noext", FileFact.NameOnly),
new("ext", FileFact.Extension),
new("name.ext", FileFact.Extension),
new("parent", FileFact.Parent),
new("path", FileFact.AbsolutePath),
new("absolute.path", FileFact.AbsolutePath),
new("relative.path", FileFact.RelativePath),
new("length", FileFact.Length),
new("size", FileFact.Length),
new("modified", FileFact.LastModified),
];
private static readonly Regex OrdinalPlaceholder = new(@"\$\{file:(?[^}]*)\}", RegexOptions.Compiled);
private static readonly Regex IgnoreCasePlaceholder = new(@"\$\{(?i:file):(?[^}]*)\}", RegexOptions.Compiled);
/// The fact a key reads; false when the key is not one of .
/// The key as written after file:; surrounding spaces are ignored.
/// or .
/// The fact, when the key is known.
public static bool TryGetFact(string key, StringComparison comparison, out FileFact fact)
{
ArgumentNullException.ThrowIfNull(key);
CheckComparison(comparison);
var trimmed = key.Trim();
foreach (var (name, meaning) in Keys)
if (string.Equals(name, trimmed, comparison))
{
fact = meaning;
return true;
}
fact = default;
return false;
}
/// The value of one fact: a string, a length or a .
public static object? Value(FileFact fact, IFileFacts facts)
{
ArgumentNullException.ThrowIfNull(facts);
return fact switch
{
FileFact.Name => facts.Name,
FileFact.NameOnly => facts.NameOnly,
FileFact.Extension => facts.Extension,
FileFact.Parent => facts.Parent,
FileFact.AbsolutePath => facts.AbsolutePath,
FileFact.RelativePath => facts.RelativePath,
FileFact.Length => facts.Length,
FileFact.LastModified => facts.LastModified,
_ => throw new ArgumentOutOfRangeException(nameof(fact), fact, null),
};
}
/// The value a key reads from .
/// The key is not one of ; the message lists them.
public static object? Resolve(string key, IFileFacts facts, StringComparison comparison)
=> TryGetFact(key, comparison, out var fact)
? Value(fact, facts)
: throw new ArgumentException(UnknownKeyMessage(key), nameof(key));
///
/// Replaces every ${file:key} in with the value it reads, written as a template
/// writes it (numbers and dates culture-invariant); a fact the source does not know writes nothing.
///
/// A key is not one of .
public static string Substitute(string pattern, IFileFacts facts, StringComparison comparison)
{
ArgumentNullException.ThrowIfNull(pattern);
ArgumentNullException.ThrowIfNull(facts);
if (!pattern.Contains("${", StringComparison.Ordinal))
return pattern;
return Placeholder(comparison).Replace(pattern,
m => ExpressionResolver.TemplateText(Resolve(m.Groups["key"].Value, facts, comparison)));
}
///
/// The first key in that is not one of , or null when every
/// ${file:key} in it is known. A component calls it when its endpoint is created.
///
public static string? FindUnknownKey(string pattern, StringComparison comparison)
{
ArgumentNullException.ThrowIfNull(pattern);
if (!pattern.Contains("${", StringComparison.Ordinal))
return null;
return Placeholder(comparison).Matches(pattern)
.Select(m => m.Groups["key"].Value)
.FirstOrDefault(key => !TryGetFact(key, comparison, out _));
}
/// An extension as the file language gives it: without the leading dot (.xml is xml).
public static string? WithoutDot(string? extension)
=> extension is not null && extension.StartsWith('.') ? extension[1..] : extension;
/// The words of the error for a key that is not one of .
public static string UnknownKeyMessage(string key)
=> $"'{key.Trim()}' is not a file key; the keys are {string.Join(", ", Keys.Select(k => k.Key))}.";
private static Regex Placeholder(StringComparison comparison)
{
CheckComparison(comparison);
return comparison == StringComparison.Ordinal ? OrdinalPlaceholder : IgnoreCasePlaceholder;
}
private static void CheckComparison(StringComparison comparison)
{
if (comparison is not (StringComparison.Ordinal or StringComparison.OrdinalIgnoreCase))
throw new ArgumentOutOfRangeException(nameof(comparison), comparison,
"The file language compares keys with Ordinal (the expression engine) or OrdinalIgnoreCase (component options).");
}
}