From 8189083e0bed5f8241454757e2cd6a99b4dfff8b Mon Sep 17 00:00:00 2001 From: Michael Ganss Date: Thu, 28 Nov 2024 20:20:04 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=92=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- XmlSchemaClassGenerator.Console/Program.cs | 2 +- .../NormalizingXmlResolverTests.cs | 6 +-- XmlSchemaClassGenerator/Generator.cs | 39 ++++++++++--------- 4 files changed, 26 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 946204f..b82c713 100644 --- a/README.md +++ b/README.md @@ -199,7 +199,7 @@ Options: property names (default is Pascal; can be: Direct, Pascal, Legacy) --fu, --forceUriScheme=VALUE - force URI scheme when resolving Urls (default is none; can be: none, same, or any defined value for scheme, like https or http) + force URI scheme when resolving URLs (default is none; can be: none, same, or any defined value for scheme, like https or http) For use from code use the [library NuGet package](https://www.nuget.org/packages/XmlSchemaClassGenerator-beta/): diff --git a/XmlSchemaClassGenerator.Console/Program.cs b/XmlSchemaClassGenerator.Console/Program.cs index 8cbb9fa..cbdd061 100644 --- a/XmlSchemaClassGenerator.Console/Program.cs +++ b/XmlSchemaClassGenerator.Console/Program.cs @@ -179,7 +179,7 @@ A file name may be given by appending a pipe sign (|) followed by a file name (l }; } }, - { "fu|forceUriScheme=", "force URI scheme when resolving Urls (default is none; can be: none, same, or any defined value for scheme, like https or http)", v => forceUriScheme = v } + { "fu|forceUriScheme=", "force URI scheme when resolving URLs (default is none; can be: none, same, or any defined value for scheme, like https or http)", v => forceUriScheme = v } }; var globsAndUris = options.Parse(args); diff --git a/XmlSchemaClassGenerator.Tests/NormalizingXmlResolverTests.cs b/XmlSchemaClassGenerator.Tests/NormalizingXmlResolverTests.cs index 2e317fd..3f7340e 100644 --- a/XmlSchemaClassGenerator.Tests/NormalizingXmlResolverTests.cs +++ b/XmlSchemaClassGenerator.Tests/NormalizingXmlResolverTests.cs @@ -16,9 +16,9 @@ public class NormalizingXmlResolverTests [InlineData("https", "https://x.y.z", "http://foo.bar.com/baz.xsd", "http://x.y.z")] [InlineData("file", "file://foo.bar.com/baz.xsd", "http://foo.bar.com", "baz.xsd")] [InlineData("file", "file://x.y.z/a.xsd", "http://foo.bar.com/baz.xsd", "https://x.y.z/a.xsd")] - void TestOverrides(string forceScheme, string expect, string baseUri, string relUri) + public void TestOverrides(string forceScheme, string expect, string baseUri, string relUri) { - var res = new XmlSchemaClassGenerator.NormalizingXmlResolver(forceScheme); - Assert.Equal(new Uri(expect),res.ResolveUri(new Uri(baseUri), relUri)); + var res = new NormalizingXmlResolver(forceScheme); + Assert.Equal(new Uri(expect), res.ResolveUri(new Uri(baseUri), relUri)); } } \ No newline at end of file diff --git a/XmlSchemaClassGenerator/Generator.cs b/XmlSchemaClassGenerator/Generator.cs index aaf940b..55ab73c 100644 --- a/XmlSchemaClassGenerator/Generator.cs +++ b/XmlSchemaClassGenerator/Generator.cs @@ -9,40 +9,41 @@ namespace XmlSchemaClassGenerator { - public class NormalizingXmlResolver : XmlUrlResolver + public class NormalizingXmlResolver(string forceUriScheme) : XmlUrlResolver() { // the Uri scheme to force on the resolved Uris // "none" - do not change Uri scheme // "same" - force the same Uri scheme as base Uri // any other string becomes the new Uri scheme of the baseUri - private string forceUriScheme; - public NormalizingXmlResolver(string forceUriScheme) : base() - { - this.forceUriScheme=forceUriScheme; - } + private readonly string forceUriScheme = forceUriScheme; + public override Uri ResolveUri(Uri baseUri, string relativeUri) { - Uri resolvedUri = base.ResolveUri(baseUri, relativeUri); - var r=NormalizeUri(baseUri, resolvedUri); - return r; + var resolvedUri = base.ResolveUri(baseUri, relativeUri); + var r = NormalizeUri(baseUri, resolvedUri); + return r; } - private Uri NormalizeUri(Uri baseUri, Uri resolvedUri ) + + private Uri NormalizeUri(Uri baseUri, Uri resolvedUri) { - var newScheme=forceUriScheme; + var newScheme = forceUriScheme; + switch (forceUriScheme) { case "none": return resolvedUri; case "same": { - newScheme=baseUri.Scheme; - break; + newScheme = baseUri.Scheme; + break; } } - UriBuilder builder = new UriBuilder(resolvedUri) { Scheme = newScheme, Port=-1}; + + var builder = new UriBuilder(resolvedUri) { Scheme = newScheme, Port = -1 }; + return builder.Uri; } } - + public class Generator { private readonly GeneratorConfiguration _configuration = new(); @@ -52,8 +53,9 @@ public class Generator public string ForceUriScheme { get { return _configuration.ForceUriScheme; } - set { _configuration.ForceUriScheme=value; } + set { _configuration.ForceUriScheme = value; } } + public NamespaceProvider NamespaceProvider { get { return _configuration.NamespaceProvider; } @@ -414,10 +416,11 @@ public void Generate(IEnumerable streams) public void Generate(IEnumerable readers) { - var set = new XmlSchemaSet(); + var set = new XmlSchemaSet(); + ValidationError = false; - set.XmlResolver = new NormalizingXmlResolver(ForceUriScheme); // XmlUrlResolver(); + set.XmlResolver = new NormalizingXmlResolver(ForceUriScheme); set.ValidationEventHandler += (s, e) => { var ex = e.Exception as Exception;