| | 1 | | using Newtonsoft.Json.Linq; |
| | 2 | |
|
| | 3 | | namespace ValidateLib.UtilityClasses |
| | 4 | | { |
| | 5 | | public class ObjectPropertyUtilityClass |
| | 6 | | { |
| | 7 | | public static async Task<JToken> GetDescriptorObjectFromUrlAsync(string url) |
| | 8 | | { |
| 0 | 9 | | using (HttpClient client = new HttpClient()) |
| | 10 | | { |
| 0 | 11 | | string json = await client.GetStringAsync(url); |
| 0 | 12 | | JToken jtoken = JToken.Parse(json); |
| 0 | 13 | | return jtoken; |
| | 14 | | } |
| 0 | 15 | | } |
| | 16 | |
|
| | 17 | | public static void RemoveLocalContext(JObject jobject) |
| | 18 | | { |
| 1 | 19 | | if (jobject.ContainsKey("@context")) |
| | 20 | | { |
| 1 | 21 | | jobject.Property("@context")?.Remove(); |
| | 22 | | } |
| 1 | 23 | | } |
| | 24 | |
|
| | 25 | | public static void AddId(JObject jobject, string id) |
| | 26 | | { |
| 1 | 27 | | if (!jobject.ContainsKey("@id")) |
| | 28 | | { |
| 0 | 29 | | jobject.Add("@id", id); |
| | 30 | | } |
| 1 | 31 | | } |
| | 32 | |
|
| | 33 | | public static JObject? GetDescriptor(string metadataUrlOrPath) |
| | 34 | | { |
| | 35 | | try |
| | 36 | | { |
| 1 | 37 | | if (Uri.TryCreate(metadataUrlOrPath, UriKind.Absolute, out Uri uri) && (uri.Scheme == Uri.UriSchemeHttp |
| | 38 | | { |
| 1 | 39 | | using (HttpClient client = new HttpClient()) |
| | 40 | | { |
| 1 | 41 | | string response = client.GetStringAsync(uri).Result; |
| 1 | 42 | | return JObject.Parse(response); |
| | 43 | | } |
| | 44 | | } |
| 1 | 45 | | else if (Uri.TryCreate(metadataUrlOrPath, UriKind.Absolute, out uri) && uri.Scheme == Uri.UriSchemeFile) |
| | 46 | | { |
| 1 | 47 | | string localPath = uri.LocalPath; |
| 1 | 48 | | if (File.Exists(localPath)) |
| | 49 | | { |
| 1 | 50 | | string content = File.ReadAllText(localPath); |
| 1 | 51 | | return JObject.Parse(content); |
| | 52 | | } |
| | 53 | | else |
| | 54 | | { |
| 0 | 55 | | return null; |
| | 56 | | } |
| | 57 | | } |
| 0 | 58 | | else if (File.Exists(metadataUrlOrPath)) |
| | 59 | | { |
| 0 | 60 | | string content = File.ReadAllText(metadataUrlOrPath); |
| 0 | 61 | | return JObject.Parse(content); |
| | 62 | | } |
| | 63 | | else |
| | 64 | | { |
| 0 | 65 | | return null; |
| | 66 | | } |
| | 67 | | } |
| 1 | 68 | | catch (Exception) |
| | 69 | | { |
| 1 | 70 | | return null; |
| | 71 | | } |
| 1 | 72 | | } |
| | 73 | | } |
| | 74 | | } |