Skip to content

Commit

Permalink
💄
Browse files Browse the repository at this point in the history
  • Loading branch information
mganss committed Nov 28, 2024
1 parent 816306c commit 8189083
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 23 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
</pre>

For use from code use the [library NuGet package](https://www.nuget.org/packages/XmlSchemaClassGenerator-beta/):
Expand Down
2 changes: 1 addition & 1 deletion XmlSchemaClassGenerator.Console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions XmlSchemaClassGenerator.Tests/NormalizingXmlResolverTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
39 changes: 21 additions & 18 deletions XmlSchemaClassGenerator/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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; }
Expand Down Expand Up @@ -414,10 +416,11 @@ public void Generate(IEnumerable<TextReader> streams)

public void Generate(IEnumerable<XmlReader> 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;
Expand Down

0 comments on commit 8189083

Please sign in to comment.