| | 1 | | using Spdx3.Exceptions; |
| | 2 | |
|
| | 3 | | namespace Spdx3.Utility; |
| | 4 | |
|
| | 5 | | public static class Naming |
| | 6 | | { |
| 1 | 7 | | private static readonly Dictionary<string, string> PrefixesForNamespaces = new() |
| 1 | 8 | | { |
| 1 | 9 | | { "Model.Core", "" }, |
| 1 | 10 | | { "Model.Security", "security_" }, |
| 1 | 11 | | { "Model.Software", "software_" }, |
| 1 | 12 | | { "Model.Licensing", "licensing_" }, |
| 1 | 13 | | { "Model.SimpleLicensing", "simplelicensing_" }, |
| 1 | 14 | | { "Model.ExpandedLicensing", "expandedlicensing_" }, |
| 1 | 15 | | { "Model.Dataset", "dataset_" }, |
| 1 | 16 | | { "Model.AI", "ai_" }, |
| 1 | 17 | | { "Model.Build", "build_" }, |
| 1 | 18 | | { "Model.Lite", "lite_" }, |
| 1 | 19 | | { "Model.Extension", "extension_" } |
| 1 | 20 | | }; |
| | 21 | |
|
| | 22 | | public static string ClassNameForSpdxType(Dictionary<string, object> hashTable) |
| | 23 | | { |
| 59 | 24 | | var spdxType = (string)hashTable[hashTable.ContainsKey("type") ? "type" : "@type"]; |
| | 25 | |
|
| 59 | 26 | | if (spdxType == "NoAssertionElement" || spdxType == "NoneElement" || spdxType == "SpdxOrganization") |
| | 27 | | { |
| 3 | 28 | | return $"Spdx3.Model.Core.Individuals.{spdxType}"; |
| | 29 | | } |
| | 30 | |
|
| 56 | 31 | | if (spdxType == "NoneLicense" || spdxType == "NoAssertionLicense") |
| | 32 | | { |
| 2 | 33 | | return $"Spdx3.Model.ExpandedLicensing.Individuals.{spdxType}"; |
| | 34 | | } |
| | 35 | |
|
| 54 | 36 | | if (!spdxType.Contains('_')) |
| | 37 | | { |
| 39 | 38 | | return $"Spdx3.Model.Core.Classes.{spdxType}"; |
| | 39 | | } |
| | 40 | |
|
| 45 | 41 | | foreach (var prefix in PrefixesForNamespaces.Where(prefix => |
| 93 | 42 | | !string.IsNullOrWhiteSpace(prefix.Value) && spdxType.StartsWith(prefix.Value))) |
| | 43 | | { |
| 15 | 44 | | return $"Spdx3.{prefix.Key}.Classes.{spdxType[prefix.Value.Length..]}"; |
| | 45 | | } |
| | 46 | |
|
| 0 | 47 | | throw new Spdx3SerializationException($"Unable to determine class type for node of Type={spdxType}"); |
| 15 | 48 | | } |
| | 49 | |
|
| | 50 | | public static string SpdxTypeForClass(Type classType) |
| | 51 | | { |
| 1138 | 52 | | if (classType.Namespace == null) |
| | 53 | | { |
| 1 | 54 | | throw new Spdx3Exception($"Unable to determine SPDX3 node type value for {classType.FullName}"); |
| | 55 | | } |
| | 56 | |
|
| 3410 | 57 | | foreach (var prefix in PrefixesForNamespaces.Keys.Where(prefix => |
| 3383 | 58 | | classType.Namespace.StartsWith($"Spdx3.{prefix}") || |
| 3383 | 59 | | classType.Namespace.StartsWith($"Spdx3.Tests.{prefix}"))) |
| | 60 | | { |
| 1136 | 61 | | return $"{PrefixesForNamespaces[prefix]}{classType.Name}"; |
| | 62 | | } |
| | 63 | |
|
| 1 | 64 | | throw new Spdx3Exception($"Unable to determine SPDX3 node type value for {classType.FullName}"); |
| 1136 | 65 | | } |
| | 66 | | } |