< Summary

Information
Class: ValidateLib.Metadata.Properties.CommonProperty
Assembly: validatelib.dll
File(s): C:\skola_karlovka\RP\code\csv-validator\CSV_Validator\ValidateLib\Metadata\Properties\CommonProperty.cs
Line coverage
94%
Covered lines: 52
Uncovered lines: 3
Coverable lines: 55
Total lines: 129
Line coverage: 94.5%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

File(s)

C:\skola_karlovka\RP\code\csv-validator\CSV_Validator\ValidateLib\Metadata\Properties\CommonProperty.cs

#LineLine coverage
 1using Newtonsoft.Json.Linq;
 2using ValidateLib.ErrorsAndWarnings.Errors;
 3using ValidateLib.ErrorsAndWarnings.Warnings;
 4using ValidateLib.IRINormalization;
 5using ValidateLib.Metadata.Descriptors;
 6using ValidateLib.Metadata.Descriptors.Interfaces;
 7using ValidateLib.UtilityClasses;
 8
 9namespace ValidateLib.Metadata.Properties
 10{
 11    public class CommonProperty : Property<JToken>, INormalize
 12    {
 113        public string? IRI { get; set; }
 14
 115        public CommonProperty(string? IRI, JToken value) : base(value)
 16        {
 117            this.IRI = IRI;
 118        }
 19
 20        public static CommonProperty CreateFromProperty(JProperty property)
 21        {
 022            return new CommonProperty(property.Name, property.Value);
 23        }
 24
 25        public List<Warning>? ExpandIRI()
 26        {
 127            List<Warning> errorsAndWarnings = new List<Warning>();
 128            if (IRI is not null)
 29            {
 130                if (IriUtilityClass.IsAbsoluteUrl(IRI)) return null;
 31                else
 32                {
 133                    string[] IRIParts = IRI.Split(':');
 134                    if (IRIParts.Length != 2)
 35                    {
 136                        errorsAndWarnings.Add(WarningFactory.GetDoubleDotOrUknownPropertyWarning(IRI, _value!.ToString()
 137                        return errorsAndWarnings;
 38                    }
 39                    else
 40                    {
 141                        var prefixes = CommonPropertiesPrefixes.map;
 142                        if (prefixes.ContainsKey(IRIParts[0]))
 43                        {
 144                            string normalizedIri = prefixes[IRIParts[0]] + IRIParts[1];
 145                            IRI = normalizedIri;
 146                            return null;
 47                        }
 48                        else
 49                        {
 150                            errorsAndWarnings.Add(WarningFactory.GetUnknownPrefixWarning(IRI));
 151                            return errorsAndWarnings;
 52                        }
 53
 54                    }
 55                }
 56            }
 057            return errorsAndWarnings;
 58
 59        }
 60
 61        public static List<Warning> Normalize(JToken token, Context context, JProperty? property)
 62        {
 163            List<Warning> errors = new List<Warning>();
 164            if (token.Type == JTokenType.Array)
 65            {
 166                JArray modifiedItems = new JArray();
 167                foreach (var item in (JArray)token)
 68                {
 169                    var clonedItem = item.DeepClone();
 170                    modifiedItems.Add(clonedItem);
 171                    errors.AddRange(Normalize(clonedItem, context, property));
 72                }
 173                token.Replace(modifiedItems);
 74
 75            }
 176            else if (token.Type == JTokenType.String)
 77            {
 178                token.Replace(
 179                    new JObject(
 180                        new JProperty("@value", token.ToString()),
 181                        new JProperty("@language", context.language._value)
 182                        )
 183                    );
 84            }
 185            else if (token.Type == JTokenType.Object)
 86            {
 187                var jobject = (JObject)token;
 188                if (jobject.ContainsKey("@value")) return errors;
 89                else
 90                {
 191                    foreach (JProperty objectProperty in token)
 92                    {
 193                        if (
 194                            objectProperty?.Name == "@id"
 195                            && (objectProperty.Value.Type != JTokenType.String || objectProperty.Value.ToString().Starts
 196                            ) ErrorFactory.ThrowCommonPropertyValueErrorError(objectProperty);
 197                        if (objectProperty?.Name == "@id" && objectProperty.Value.Type == JTokenType.String)
 98                        {
 199                            objectProperty.Value = CommonPropertiesPrefixes.ExpandPrefix(objectProperty.Value.ToString()
 1100                            objectProperty.Value = IRINormalizator.TurnUrlIntoAbsoluteUsingBase(objectProperty.Value.ToS
 101                        }
 1102                        else if (objectProperty?.Name == "@type") continue;
 1103                        else errors.AddRange(Normalize(objectProperty!.Value, context!, objectProperty));
 104
 105                    }
 106                }
 107            }
 1108            return errors;
 109
 110        }
 111
 112        public static bool IsCommonProperty(string propertyName)
 113        {
 1114            if (IriUtilityClass.IsAbsoluteUrl(propertyName)) return true;
 115            else
 116            {
 1117                string[] IRIParts = propertyName.Split(':');
 1118                if (IRIParts.Length != 2) return false;
 119                else
 120                {
 1121                    var prefixes = CommonPropertiesPrefixes.map;
 1122                    if (prefixes.ContainsKey(IRIParts[0])) return true;
 0123                    else return false;
 124                }
 125            }
 126        }
 127
 128    }
 129}