| | 1 | | namespace ValidateLib.UtilityClasses |
| | 2 | | { |
| | 3 | | public class IriUtilityClass |
| | 4 | | { |
| | 5 | |
|
| | 6 | | public static bool IsAbsoluteUrl(string iri) |
| | 7 | | { |
| | 8 | |
|
| 1 | 9 | | if (Uri.TryCreate(iri, UriKind.Absolute, out Uri? uri)) |
| | 10 | | { |
| 1 | 11 | | if (uri.Scheme == Uri.UriSchemeFile) return true; |
| 1 | 12 | | return !string.IsNullOrEmpty(uri.Scheme) && !string.IsNullOrEmpty(uri.Host); |
| | 13 | | } |
| 1 | 14 | | return false; |
| | 15 | | } |
| | 16 | |
|
| | 17 | | public static string PathToFileUrl(string filePath) |
| | 18 | | { |
| 0 | 19 | | string url = Uri.EscapeUriString(filePath); // Escape special characters |
| 0 | 20 | | url = url.Replace("\\", "/"); // Use forward slashes |
| 0 | 21 | | url = "file://" + url; |
| 0 | 22 | | return url; |
| | 23 | | } |
| | 24 | |
|
| | 25 | | public static string? GetMetadataLocation(string metadataUrlOrFilePath) |
| | 26 | | { |
| 1 | 27 | | if (Uri.TryCreate(metadataUrlOrFilePath, UriKind.Absolute, out Uri uri) && (uri.Scheme == Uri.UriSchemeHttp |
| | 28 | | { |
| 1 | 29 | | return GetBaseUrlWithPath(metadataUrlOrFilePath); |
| | 30 | | } |
| 0 | 31 | | return null; |
| | 32 | | } |
| | 33 | |
|
| | 34 | | static string GetBaseUrlWithPath(string url) |
| | 35 | | { |
| 1 | 36 | | Uri uri = new Uri(url); |
| 1 | 37 | | string baseUrlWithPath = $"{uri.Scheme}://{uri.Host}{Path.Combine("/", uri.AbsolutePath.TrimStart('/'))}"; |
| 1 | 38 | | return baseUrlWithPath; |
| | 39 | | } |
| | 40 | |
|
| | 41 | | public static bool IsRemoteIri(string iri) |
| | 42 | | { |
| | 43 | | try |
| | 44 | | { |
| 1 | 45 | | Uri uri = new Uri(iri); |
| 1 | 46 | | if (uri.Scheme == Uri.UriSchemeHttp || uri.Scheme == Uri.UriSchemeHttps) |
| 1 | 47 | | return true; |
| | 48 | | else |
| 1 | 49 | | return false; |
| | 50 | | } |
| 0 | 51 | | catch (Exception) |
| | 52 | | { |
| 0 | 53 | | return false; |
| | 54 | | } |
| | 55 | |
|
| 1 | 56 | | } |
| | 57 | | } |
| | 58 | | } |