| | 1 | | using Newtonsoft.Json.Linq; |
| | 2 | | using ValidateLib.ErrorsAndWarnings.Errors; |
| | 3 | | using ValidateLib.ErrorsAndWarnings.Warnings; |
| | 4 | | using ValidateLib.IRINormalization; |
| | 5 | | using ValidateLib.Metadata.Descriptors.Interfaces; |
| | 6 | | using ValidateLib.Metadata.Properties.AtomicProperties; |
| | 7 | | using ValidateLib.UtilityClasses; |
| | 8 | |
|
| | 9 | | namespace ValidateLib.Metadata.Descriptors |
| | 10 | | { |
| | 11 | | public class Context : INormalize |
| | 12 | | { |
| | 13 | | public string? contextURL; |
| 1 | 14 | | public AtomicPropertyString? _base { get; set; } |
| 1 | 15 | | public AtomicPropertyString language { get; set; } = new AtomicPropertyString("und"); |
| | 16 | | public static Context CreateContextFromArray(JArray array) |
| | 17 | | { |
| 1 | 18 | | Context context = new Context(); |
| 1 | 19 | | context.contextURL = array[0].ToString(); |
| 1 | 20 | | foreach (JProperty property in array[1]) |
| | 21 | | { |
| 1 | 22 | | if (property.Name == "@base") context._base = new AtomicPropertyString(property.Value.ToString()); |
| 1 | 23 | | else if (property.Name == "@language") context.language = new AtomicPropertyString(property.Value.ToStri |
| 1 | 24 | | else ErrorFactory.ThrowInvalidPropertyOnContextDescriptorError(property.ToString()); |
| | 25 | |
|
| | 26 | | } |
| 1 | 27 | | return context; |
| | 28 | | } |
| | 29 | |
|
| | 30 | | public static List<Warning> Normalize(JToken token, Context context, JProperty? property = null) |
| | 31 | | { |
| 0 | 32 | | throw new NotImplementedException(); |
| | 33 | | } |
| | 34 | |
|
| | 35 | | public static Context GetContextFromJToken(JToken jToken, List<Warning> warnings, string objectFileLocation) |
| | 36 | | { |
| 1 | 37 | | Context context = new Context(); |
| 1 | 38 | | if (jToken["@context"] != null) |
| | 39 | | { |
| 1 | 40 | | JToken contextValue = jToken["@context"]!; |
| 1 | 41 | | if (contextValue!.Type == JTokenType.String) |
| | 42 | | { |
| 1 | 43 | | context.contextURL = contextValue.ToString(); |
| | 44 | | } |
| 1 | 45 | | else if (contextValue.Type == JTokenType.Array) context = CreateContextFromArray((JArray)contextValue); |
| 0 | 46 | | else ErrorFactory.ThrowInvalidContextValueError(contextValue.ToString(), contextValue.Type.ToString()); |
| 1 | 47 | | if (context._base == null) |
| | 48 | | { |
| 1 | 49 | | string fileName = Path.GetFileName(objectFileLocation)!; |
| 1 | 50 | | context._base = new AtomicPropertyString(objectFileLocation!.Replace(fileName, "")); |
| | 51 | | } |
| | 52 | | else |
| | 53 | | { |
| 1 | 54 | | context._base._value = IRINormalizator.TurnUrlIntoAbsoluteWithReferencedUrl(objectFileLocation, cont |
| | 55 | | } |
| 1 | 56 | | if (context?.language?._value is not null && !LanguageUtilityClass.IsCorrectLanguageCode(context.languag |
| | 57 | | { |
| 1 | 58 | | warnings.Add(WarningFactory.GetInvalidContextLanuageWarning(context.language._value)); |
| 1 | 59 | | context.language = new AtomicPropertyString("und"); |
| | 60 | | } |
| 1 | 61 | | return context!; |
| | 62 | | } |
| | 63 | | else |
| | 64 | | { |
| 0 | 65 | | ErrorFactory.ThrowMissingContextError(jToken); |
| 0 | 66 | | return null!; |
| | 67 | | } |
| | 68 | | } |
| | 69 | | } |
| | 70 | | } |