Skip to content

Commit 95dafbc

Browse files
committed
Improve automatic generation of namespace names (breaking change)
1 parent 7638395 commit 95dafbc

10 files changed

Lines changed: 368 additions & 25 deletions

File tree

XmlSchemaClassGenerator.Console/Program.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -213,10 +213,8 @@ A file name may be given by appending a pipe sign (|) followed by a file name (l
213213

214214
var namespaceMap = namespaces.Select(n => CodeUtilities.ParseNamespace(n, namespacePrefix)).ToNamespaceProvider(key =>
215215
{
216-
var xn = key.XmlSchemaNamespace;
217-
var name = string.Join(".", xn.Split('/').Where(p => p != "schema" && GeneratorConfiguration.IdentifierRegex.IsMatch(p))
218-
.Select(n => n.ToTitleCase(NamingScheme.PascalCase)));
219-
if (!string.IsNullOrEmpty(namespacePrefix)) { name = namespacePrefix + (string.IsNullOrEmpty(name) ? "" : ("." + name)); }
216+
var xmlns = key.XmlSchemaNamespace;
217+
var name = CodeUtilities.GenerateNamespace(xmlns, namespacePrefix);
220218
return name;
221219
});
222220

XmlSchemaClassGenerator.Console/XmlSchemaClassGenerator.Console.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
<IncludeSymbols>true</IncludeSymbols>
2626
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
2727
<LangVersion>latest</LangVersion>
28+
<NoWarn>$(NoWarn);IDE0130;SYSLIB1045;IDE0057</NoWarn>
2829
</PropertyGroup>
2930
<ItemGroup>
3031
<ProjectReference Include="..\XmlSchemaClassGenerator\XmlSchemaClassGenerator.csproj" />

XmlSchemaClassGenerator.Tests/NamespaceProviderTests.cs

Lines changed: 304 additions & 0 deletions
Large diffs are not rendered by default.

XmlSchemaClassGenerator.Tests/XmlSchemaClassGenerator.Tests.csproj

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@
2828
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
2929
</PackageReference>
3030
<PackageReference Include="Glob.cs" Version="5.1.1643" />
31-
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.12.0" />
32-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
33-
<PackageReference Include="System.CodeDom" Version="9.0.1" />
34-
<PackageReference Include="System.Text.Encoding.CodePages" Version="9.0.1" />
35-
<PackageReference Include="System.ValueTuple" Version="4.5.0" />
36-
<PackageReference Include="xunit.runner.visualstudio" Version="3.0.1">
31+
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.14.0" />
32+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.14.1" />
33+
<PackageReference Include="System.CodeDom" Version="9.0.7" />
34+
<PackageReference Include="System.Text.Encoding.CodePages" Version="9.0.7" />
35+
<PackageReference Include="System.ValueTuple" Version="4.6.1" />
36+
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.1">
3737
<PrivateAssets>all</PrivateAssets>
3838
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
3939
</PackageReference>

XmlSchemaClassGenerator.Tests/XsdElsterDatenabholung5.cs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,7 @@ public void CanGenerateClasses()
5454
}
5555
}
5656
};
57-
var xsdFiles = new[]
58-
{
59-
"headerbasis000002.xsd",
60-
"ndh000010_extern.xsd",
61-
"th000008_extern.xsd",
62-
"datenabholung_5.xsd",
63-
"elster0810_datenabholung_5.xsd",
64-
}.Select(x => Path.Combine(InputPath, x)).ToList();
57+
var xsdFiles = sourceArray.Select(x => Path.Combine(InputPath, x)).ToList();
6558
var encodings = System.Text.Encoding.GetEncodings();
6659
System.Text.Encoding.GetEncoding("ISO-8859-15");
6760
gen.Generate(xsdFiles);
@@ -83,6 +76,15 @@ private static string InputPath
8376
get { return Path.Combine(Directory.GetCurrentDirectory(), "xsd", "elster-xml-datenabholung5"); }
8477
}
8578

79+
private static readonly string[] sourceArray =
80+
[
81+
"headerbasis000002.xsd",
82+
"ndh000010_extern.xsd",
83+
"th000008_extern.xsd",
84+
"datenabholung_5.xsd",
85+
"elster0810_datenabholung_5.xsd",
86+
];
87+
8688
private static string GetOutputPath(string testCaseId)
8789
{
8890
var result = Path.Combine(Directory.GetCurrentDirectory(), "output", "elster-xml-datenabholung5", testCaseId);

XmlSchemaClassGenerator/CodeUtilities.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,44 @@ public static bool IsXmlLangOrSpace(XmlQualifiedName name)
466466
XmlSchemaAttributeGroup attrGroup => attrGroup.QualifiedName,
467467
_ => null
468468
};
469+
470+
public static string GenerateNamespace(string xmlns, string namespacePrefix = null)
471+
{
472+
if (string.IsNullOrEmpty(xmlns)) return !string.IsNullOrEmpty(namespacePrefix) ? namespacePrefix : string.Empty;
473+
474+
var schemeIndex = xmlns.IndexOf("://");
475+
List<string> segments;
476+
477+
if (xmlns.StartsWith("urn:"))
478+
{
479+
segments = [.. xmlns.Substring(4).Split(':', '-', '.', '_')];
480+
}
481+
else
482+
{
483+
if (schemeIndex > 0) xmlns = xmlns.Substring(schemeIndex + 3);
484+
485+
segments = [.. xmlns.Split('/')];
486+
487+
if (segments.Count > 0)
488+
{
489+
var splitSegments = segments[0].Split('.').Reverse().Where(s => s != "www").ToList();
490+
if (segments.Count > 1)
491+
splitSegments.AddRange(segments.Skip(1));
492+
segments = splitSegments.SelectMany(s => s.Split('-', '.', '_')).ToList();
493+
}
494+
}
495+
496+
var cleanedSegments = segments
497+
.Where(s => !string.IsNullOrEmpty(s))
498+
.Select(s => s.ToTitleCase(NamingScheme.PascalCase))
499+
.Select(s => s.Length > 0 && !char.IsLetter(s[0]) && s[0] != '_' ? "_" + s : s)
500+
.ToList();
501+
var name = string.Join(".", cleanedSegments);
502+
503+
if (!string.IsNullOrEmpty(namespacePrefix)) { name = namespacePrefix + (string.IsNullOrEmpty(name) ? "" : ("." + name)); }
504+
505+
return name;
506+
}
469507
}
470508

471509
public readonly record struct TypeInfo(string Namespace, string Name);

XmlSchemaClassGenerator/XmlSchemaClassGenerator.csproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,14 @@
4545
</ItemGroup>
4646

4747
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
48-
<PackageReference Include="System.CodeDom" Version="9.0.1" />
48+
<PackageReference Include="System.CodeDom" Version="9.0.7" />
4949
</ItemGroup>
5050

5151
<ItemGroup>
5252
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All" />
53-
<PackageReference Include="System.Collections.Immutable" Version="9.0.1" />
54-
<PackageReference Include="System.Text.Encoding.CodePages" Version="9.0.1" />
55-
<PackageReference Include="System.ValueTuple" Version="4.5.0" />
53+
<PackageReference Include="System.Collections.Immutable" Version="9.0.7" />
54+
<PackageReference Include="System.Text.Encoding.CodePages" Version="9.0.7" />
55+
<PackageReference Include="System.ValueTuple" Version="4.6.1" />
5656
</ItemGroup>
5757

5858
<ItemGroup>

appveyor.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
version: 2.1.{build}
1+
version: 3.0.{build}
22
skip_tags: true
33
image: Visual Studio 2022
44
environment:

xscgen-proj/xscgen-proj.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
<IncludeSymbols>true</IncludeSymbols>
2727
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
2828
<LangVersion>latest</LangVersion>
29-
<NoWarn>$(NoWarn);IDE0130;SYSLIB1045;CA1866</NoWarn>
29+
<NoWarn>$(NoWarn);IDE0130;SYSLIB1045;CA1866;IDE0057</NoWarn>
3030
</PropertyGroup>
3131
<ItemGroup>
3232
<ProjectReference Include="..\XmlSchemaClassGenerator\XmlSchemaClassGenerator.csproj" />

xscgen/xscgen.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
2929
<LangVersion>latest</LangVersion>
3030
<RollForward>Major</RollForward>
31-
<NoWarn>$(NoWarn);IDE0130;SYSLIB1045;CA1866</NoWarn>
31+
<NoWarn>$(NoWarn);IDE0130;SYSLIB1045;CA1866;IDE0057</NoWarn>
3232
</PropertyGroup>
3333
<ItemGroup>
3434
<ProjectReference Include="..\XmlSchemaClassGenerator\XmlSchemaClassGenerator.csproj" />

0 commit comments

Comments
 (0)