Path: redb-route/redb.Route.Xml/Packaging/RoutePackage.XmlJson.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/Packaging/RoutePackage.XmlJson.cs
----
using System.Xml;
using System.Xml.Linq;
namespace redb.Route.Xml.Packaging;
///
/// The package checks of the xmljson data format that need no code of the format, so they run without --bin:
/// the format is recognised by its type name on <dataFormat type> and the per-step
/// <marshal type> / <unmarshal type>; its arrays= paths are well formed; its schema is a
/// file the package ships; a format with neither schema= nor arrays= is warned. What needs the schema
/// compiled (an arrays= path through the schema's structure, wildcards, mixed content, the plural pairs) is the
/// format's own , which runs with --bin.
///
public static partial class RoutePackage
{
private const string XmlJsonType = "redb.Route.DataFormats.XmlJson.XmlJsonDataFormat";
private static void CheckXmlJsonFormats(string projectDir, List<(string File, XDocument Document)> documents,
List findings)
{
foreach (var (file, document) in documents)
foreach (var element in document.Descendants())
{
if (element.Name.LocalName is not ("dataFormat" or "marshal" or "unmarshal"))
continue;
var type = element.Attribute("type")?.Value;
if (type is null || !string.Equals(type.Split(',')[0].Trim(), XmlJsonType, StringComparison.Ordinal))
continue;
CheckXmlJson(projectDir, file, element, findings);
}
}
private static void CheckXmlJson(string projectDir, string file, XElement element, List findings)
{
var schema = element.Attribute("schema");
var arrays = element.Attribute("arrays");
var paths = (arrays?.Value ?? "").Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
foreach (var path in paths)
if (XmlJsonPathProblem(path) is { } problem)
findings.Add(new PackageFinding(true, Position(file, arrays!), $"xmljson arrays path '{path}' {problem}."));
if (schema is null)
{
if (paths.Length == 0)
findings.Add(new PackageFinding(false, Position(file, element),
"xmljson without schema= or arrays=: a single element becomes an object and two become an array, so one " +
"message type gives two JSON shapes. Name the schema, or the repeated elements with arrays=."));
return;
}
var value = schema.Value;
if (value.Contains("{{", StringComparison.Ordinal) || value.Contains("${", StringComparison.Ordinal))
return; // known only once configuration resolves it
if (PackagedResource(projectDir, value, out var schemaProblem) is null)
findings.Add(new PackageFinding(true, Position(file, schema), $"xmljson schema '{value}' {schemaProblem}."));
}
/// What is wrong with an arrays path, worded after the format's own error; null when it is well formed.
private static string? XmlJsonPathProblem(string path)
{
var anywhere = path.StartsWith("//", StringComparison.Ordinal);
if (!anywhere && !path.StartsWith('/'))
return "starts with '/' (from the root) or '//' (at any depth)";
var steps = path[(anywhere ? 2 : 1)..].Split('/');
return steps.Any(s => s.Length == 0 || s.Any(char.IsWhiteSpace))
? "has an empty or blank step; write it as /Order/Items/Item or //Item"
: null;
}
private static string Position(string file, XObject node)
{
var line = (IXmlLineInfo)node;
return $"{file}({line.LineNumber},{line.LinePosition})";
}
}