Path: redb-route/redb.Route.Xml/RouteContextXmlExtensions.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/RouteContextXmlExtensions.cs
----
using System.Xml.Linq;
using redb.Route.Core;
namespace redb.Route.Xml;
///
/// Loading sugar on — the TestKit-style surface
/// (new RouteContext().AddXmlRoutesFromContent(...)) and file loading. Both go through one
/// .
///
public static class RouteContextXmlExtensions
{
/// Parses the XML text and registers its routes. Errors carry positions and are aggregated.
public static RouteContext AddXmlRoutesFromContent(this RouteContext context, string xml,
string? sourceName = null, XmlRouteLoaderOptions? options = null)
{
ArgumentNullException.ThrowIfNull(context);
ArgumentException.ThrowIfNullOrWhiteSpace(xml);
var document = SafeXml.Parse(xml, LoadOptions.SetLineInfo);
new XmlRouteLoader(context, options).Load(document, sourceName);
return context;
}
/// Loads every named .route.xml file, each parsed with its own positions.
public static RouteContext AddXmlRoutes(this RouteContext context, params string[] filePaths)
{
ArgumentNullException.ThrowIfNull(context);
if (filePaths.Length == 0)
throw new ArgumentException("At least one route file path is required.", nameof(filePaths));
var loader = new XmlRouteLoader(context);
foreach (var path in filePaths)
LoadFile(loader, context, path);
return context;
}
///
/// Loads route files by a glob pattern ("routes/*.route.xml"); a plain path without
/// wildcards loads that one file. A pattern matching nothing is an error, not a silent empty
/// context — same fail-fast rule the rest of the loader follows.
///
public static RouteContext AddXmlRoutes(this RouteContext context, string globPattern,
XmlRouteLoaderOptions? options = null)
{
ArgumentNullException.ThrowIfNull(context);
ArgumentException.ThrowIfNullOrWhiteSpace(globPattern);
var loader = new XmlRouteLoader(context, options);
if (!globPattern.Contains('*') && !globPattern.Contains('?'))
{
LoadFile(loader, context, globPattern);
return context;
}
var directory = Path.GetDirectoryName(globPattern) is { Length: > 0 } dir ? dir : ".";
var mask = Path.GetFileName(globPattern);
var probed = new List();
string? found = null;
foreach (var root in ProbeRoots(directory))
{
probed.Add(root);
if (Directory.Exists(root) && Directory.EnumerateFiles(root, mask).Any())
{
found = root;
break;
}
}
if (found is null)
{
throw new XmlRouteException(globPattern,
[$"no route files match '{mask}'. Searched: {string.Join("; ", probed.Distinct())}."]);
}
// Ordinal order keeps the load deterministic across file systems.
foreach (var path in Directory.GetFiles(found, mask).OrderBy(p => p, StringComparer.Ordinal))
{
var document = SafeXml.LoadFile(path, LoadOptions.SetLineInfo);
loader.Load(document, path);
}
return context;
}
/// Parses a context.xml text: components, context beans, the onInit pipeline.
public static RouteContext AddXmlContextFromContent(this RouteContext context, string xml,
string? sourceName = null, XmlRouteLoaderOptions? options = null)
{
ArgumentNullException.ThrowIfNull(context);
ArgumentException.ThrowIfNullOrWhiteSpace(xml);
var document = SafeXml.Parse(xml, LoadOptions.SetLineInfo);
new XmlContextLoader(context, options).Load(document, sourceName);
return context;
}
/// Loads a context.xml file through the route resource resolver.
public static RouteContext AddXmlContext(this RouteContext context, string filePath,
XmlRouteLoaderOptions? options = null)
{
ArgumentNullException.ThrowIfNull(context);
ArgumentException.ThrowIfNullOrWhiteSpace(filePath);
var resolved = ResourceResolution.Resolve(context, filePath, "Context file");
var document = SafeXml.LoadFile(resolved, LoadOptions.SetLineInfo);
new XmlContextLoader(context, options).Load(document, filePath);
return context;
}
private static void LoadFile(XmlRouteLoader loader, RouteContext context, string path)
{
// Through the route resource resolver: a relative path must not depend on the process
// working directory (the AppContext base wins over cwd).
var resolved = ResourceResolution.Resolve(context, path, "Route file");
var document = SafeXml.LoadFile(resolved, LoadOptions.SetLineInfo);
loader.Load(document, path);
}
private static IEnumerable ProbeRoots(string directory)
{
if (Path.IsPathRooted(directory))
{
yield return directory;
yield break;
}
yield return Path.Combine(AppContext.BaseDirectory, directory);
yield return Path.GetFullPath(directory);
}
}