Skip to content

Commit

Permalink
Add namespace mapping file option
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Ganss committed Jun 21, 2022
1 parent 26d8ce6 commit bdd895e
Showing 1 changed file with 36 additions and 4 deletions.
40 changes: 36 additions & 4 deletions XmlSchemaClassGenerator.Console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ static void Main(string[] args)
var nullableReferenceAttributes = false;
var generateCommandLineArgs = true;
var useArrayItemAttribute = true;
var namespaceFiles = new List<string>();

var options = new OptionSet {
{ "h|help", "show this message and exit", v => showHelp = v != null },
Expand All @@ -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 => {
Expand Down Expand Up @@ -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 =>
{
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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<string> namespaces, List<string> 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...");
Expand Down

0 comments on commit bdd895e

Please sign in to comment.