From bdd895e0f105669184be92141b5cef68e7c09375 Mon Sep 17 00:00:00 2001 From: Michael Ganss Date: Tue, 21 Jun 2022 17:21:19 +0200 Subject: [PATCH] Add namespace mapping file option --- XmlSchemaClassGenerator.Console/Program.cs | 40 +++++++++++++++++++--- 1 file changed, 36 insertions(+), 4 deletions(-) diff --git a/XmlSchemaClassGenerator.Console/Program.cs b/XmlSchemaClassGenerator.Console/Program.cs index 300c5880..d66cb0b1 100644 --- a/XmlSchemaClassGenerator.Console/Program.cs +++ b/XmlSchemaClassGenerator.Console/Program.cs @@ -57,6 +57,7 @@ static void Main(string[] args) var nullableReferenceAttributes = false; var generateCommandLineArgs = true; var useArrayItemAttribute = true; + var namespaceFiles = new List(); var options = new OptionSet { { "h|help", "show this message and exit", v => showHelp = v != null }, @@ -65,6 +66,9 @@ static void Main(string[] args) One option must be given for each namespace to be mapped. A file name may be given by appending a pipe sign (|) followed by a file name (like schema.xsd) to the XML namespace. If no mapping is found for an XML namespace, a name is generated automatically (may fail).", v => namespaces.Add(v) }, + { "nf|namespaceFile=", @"file containing mapppings from XML namespaces to C# namespaces +The format is one mapping per line, whitespace separated, XML namespace, C# namespace, and optionally file name. +Lines starting with # are ignored.", v => namespaceFiles.Add(v) }, { "o|output=", "the {FOLDER} to write the resulting .cs files to", v => outputFolder = v }, { "i|integer=", @"map xs:integer and derived types to {TYPE} instead of automatic approximation {TYPE} can be i[nt], l[ong], or d[ecimal]", v => { @@ -159,7 +163,9 @@ A file name may be given by appending a pipe sign (|) followed by a file name (l } uris.AddRange(expandedGlob); - } + } + + ParseNamespaceFiles(namespaces, namespaceFiles); var namespaceMap = namespaces.Select(n => CodeUtilities.ParseNamespace(n, namespacePrefix)).ToNamespaceProvider(key => { @@ -187,7 +193,7 @@ A file name may be given by appending a pipe sign (|) followed by a file name (l EntityFramework = entityFramework, GenerateInterfaces = interfaces, NamingScheme = pascal ? NamingScheme.PascalCase : NamingScheme.Direct, - AssemblyVisible=assembly, + AssemblyVisible = assembly, CollectionType = collectionType, CollectionImplementationType = collectionImplementationType, CodeTypeReferenceOptions = codeTypeReferenceOptions, @@ -227,8 +233,34 @@ A file name may be given by appending a pipe sign (|) followed by a file name (l if (verbose) { generator.Log = s => System.Console.Out.WriteLine(s); } generator.Generate(uris); - } - + } + + private static void ParseNamespaceFiles(List namespaces, List namespaceFiles) + { + foreach (var namespaceFile in namespaceFiles) + { + foreach (var (line, number) in File.ReadAllLines(namespaceFile) + .Select((l, i) => (Line: l.Trim(), Number: i + 1)) + .Where(l => !string.IsNullOrWhiteSpace(l.Line) && !l.Line.StartsWith("#"))) + { + var parts = line.Split(); + + if (parts.Length < 2 || parts.Length > 3) + { + System.Console.WriteLine($"{namespaceFile}:{number}: Must contain XML namespace, C# namespace, and optionally filename, separated by whitespace"); + Environment.Exit(1); + } + + var ns = $"{parts[0]}={parts[1]}"; + + if (parts.Length == 3) + ns += $"={parts[2]}"; + + namespaces.Add(ns); + } + } + } + static void ShowHelp(OptionSet p) { System.Console.WriteLine("Usage: xscgen [OPTIONS]+ xsdFile...");