| | 1 | | using Newtonsoft.Json.Linq; |
| | 2 | | using ValidateLib.ErrorsAndWarnings.Errors; |
| | 3 | | using ValidateLib.ErrorsAndWarnings.Warnings; |
| | 4 | | using ValidateLib.IRINormalization; |
| | 5 | | using ValidateLib.Metadata.Descriptors; |
| | 6 | | using ValidateLib.Metadata.Descriptors.Interfaces; |
| | 7 | | using ValidateLib.UtilityClasses; |
| | 8 | |
|
| | 9 | | namespace ValidateLib.Metadata.Properties |
| | 10 | | { |
| | 11 | | public class CommonProperty : Property<JToken>, INormalize |
| | 12 | | { |
| 1 | 13 | | public string? IRI { get; set; } |
| | 14 | |
|
| 1 | 15 | | public CommonProperty(string? IRI, JToken value) : base(value) |
| | 16 | | { |
| 1 | 17 | | this.IRI = IRI; |
| 1 | 18 | | } |
| | 19 | |
|
| | 20 | | public static CommonProperty CreateFromProperty(JProperty property) |
| | 21 | | { |
| 0 | 22 | | return new CommonProperty(property.Name, property.Value); |
| | 23 | | } |
| | 24 | |
|
| | 25 | | public List<Warning>? ExpandIRI() |
| | 26 | | { |
| 1 | 27 | | List<Warning> errorsAndWarnings = new List<Warning>(); |
| 1 | 28 | | if (IRI is not null) |
| | 29 | | { |
| 1 | 30 | | if (IriUtilityClass.IsAbsoluteUrl(IRI)) return null; |
| | 31 | | else |
| | 32 | | { |
| 1 | 33 | | string[] IRIParts = IRI.Split(':'); |
| 1 | 34 | | if (IRIParts.Length != 2) |
| | 35 | | { |
| 1 | 36 | | errorsAndWarnings.Add(WarningFactory.GetDoubleDotOrUknownPropertyWarning(IRI, _value!.ToString() |
| 1 | 37 | | return errorsAndWarnings; |
| | 38 | | } |
| | 39 | | else |
| | 40 | | { |
| 1 | 41 | | var prefixes = CommonPropertiesPrefixes.map; |
| 1 | 42 | | if (prefixes.ContainsKey(IRIParts[0])) |
| | 43 | | { |
| 1 | 44 | | string normalizedIri = prefixes[IRIParts[0]] + IRIParts[1]; |
| 1 | 45 | | IRI = normalizedIri; |
| 1 | 46 | | return null; |
| | 47 | | } |
| | 48 | | else |
| | 49 | | { |
| 1 | 50 | | errorsAndWarnings.Add(WarningFactory.GetUnknownPrefixWarning(IRI)); |
| 1 | 51 | | return errorsAndWarnings; |
| | 52 | | } |
| | 53 | |
|
| | 54 | | } |
| | 55 | | } |
| | 56 | | } |
| 0 | 57 | | return errorsAndWarnings; |
| | 58 | |
|
| | 59 | | } |
| | 60 | |
|
| | 61 | | public static List<Warning> Normalize(JToken token, Context context, JProperty? property) |
| | 62 | | { |
| 1 | 63 | | List<Warning> errors = new List<Warning>(); |
| 1 | 64 | | if (token.Type == JTokenType.Array) |
| | 65 | | { |
| 1 | 66 | | JArray modifiedItems = new JArray(); |
| 1 | 67 | | foreach (var item in (JArray)token) |
| | 68 | | { |
| 1 | 69 | | var clonedItem = item.DeepClone(); |
| 1 | 70 | | modifiedItems.Add(clonedItem); |
| 1 | 71 | | errors.AddRange(Normalize(clonedItem, context, property)); |
| | 72 | | } |
| 1 | 73 | | token.Replace(modifiedItems); |
| | 74 | |
|
| | 75 | | } |
| 1 | 76 | | else if (token.Type == JTokenType.String) |
| | 77 | | { |
| 1 | 78 | | token.Replace( |
| 1 | 79 | | new JObject( |
| 1 | 80 | | new JProperty("@value", token.ToString()), |
| 1 | 81 | | new JProperty("@language", context.language._value) |
| 1 | 82 | | ) |
| 1 | 83 | | ); |
| | 84 | | } |
| 1 | 85 | | else if (token.Type == JTokenType.Object) |
| | 86 | | { |
| 1 | 87 | | var jobject = (JObject)token; |
| 1 | 88 | | if (jobject.ContainsKey("@value")) return errors; |
| | 89 | | else |
| | 90 | | { |
| 1 | 91 | | foreach (JProperty objectProperty in token) |
| | 92 | | { |
| 1 | 93 | | if ( |
| 1 | 94 | | objectProperty?.Name == "@id" |
| 1 | 95 | | && (objectProperty.Value.Type != JTokenType.String || objectProperty.Value.ToString().Starts |
| 1 | 96 | | ) ErrorFactory.ThrowCommonPropertyValueErrorError(objectProperty); |
| 1 | 97 | | if (objectProperty?.Name == "@id" && objectProperty.Value.Type == JTokenType.String) |
| | 98 | | { |
| 1 | 99 | | objectProperty.Value = CommonPropertiesPrefixes.ExpandPrefix(objectProperty.Value.ToString() |
| 1 | 100 | | objectProperty.Value = IRINormalizator.TurnUrlIntoAbsoluteUsingBase(objectProperty.Value.ToS |
| | 101 | | } |
| 1 | 102 | | else if (objectProperty?.Name == "@type") continue; |
| 1 | 103 | | else errors.AddRange(Normalize(objectProperty!.Value, context!, objectProperty)); |
| | 104 | |
|
| | 105 | | } |
| | 106 | | } |
| | 107 | | } |
| 1 | 108 | | return errors; |
| | 109 | |
|
| | 110 | | } |
| | 111 | |
|
| | 112 | | public static bool IsCommonProperty(string propertyName) |
| | 113 | | { |
| 1 | 114 | | if (IriUtilityClass.IsAbsoluteUrl(propertyName)) return true; |
| | 115 | | else |
| | 116 | | { |
| 1 | 117 | | string[] IRIParts = propertyName.Split(':'); |
| 1 | 118 | | if (IRIParts.Length != 2) return false; |
| | 119 | | else |
| | 120 | | { |
| 1 | 121 | | var prefixes = CommonPropertiesPrefixes.map; |
| 1 | 122 | | if (prefixes.ContainsKey(IRIParts[0])) return true; |
| 0 | 123 | | else return false; |
| | 124 | | } |
| | 125 | | } |
| | 126 | | } |
| | 127 | |
|
| | 128 | | } |
| | 129 | | } |