| | 1 | | using Newtonsoft.Json.Linq; |
| | 2 | | using ValidateLib.ErrorsAndWarnings.Warnings; |
| | 3 | | using ValidateLib.Metadata.Properties; |
| | 4 | | using ValidateLib.UtilityClasses; |
| | 5 | |
|
| | 6 | | namespace ValidateLib.Metadata.Parsers |
| | 7 | | { |
| | 8 | | internal class NaturalLanguageParser : IParser<NaturalLanguageProperty> |
| | 9 | | { |
| 0 | 10 | | public NaturalLanguageProperty Descriptor { get => throw new NotImplementedException(); set => throw new NotImpl |
| 1 | 11 | | public List<Warning> Warnings { get; set; } |
| 1 | 12 | | public NaturalLanguageParser(List<Warning> warnings) |
| | 13 | | { |
| 1 | 14 | | Warnings = warnings; |
| 1 | 15 | | } |
| | 16 | |
|
| | 17 | | public NaturalLanguageProperty ParseJToken(JToken jToken, string propertyName) |
| | 18 | | { |
| 1 | 19 | | if (jToken.Type != JTokenType.Object) |
| | 20 | | { |
| 1 | 21 | | Warnings.Add(WarningFactory.GetLinkPropertyNotAbsoluteWarning(jToken, propertyName)); |
| 1 | 22 | | return new NaturalLanguageProperty(new Dictionary<string, string[]>()); |
| | 23 | | } |
| | 24 | | else |
| | 25 | | { |
| 1 | 26 | | var objectValue = (JObject)jToken; |
| 1 | 27 | | var titlesMap = new Dictionary<string, string[]>(); |
| 1 | 28 | | foreach (var jproperty in objectValue.Properties()) |
| | 29 | | { |
| 1 | 30 | | if (jproperty.Value.Type != JTokenType.Array) |
| | 31 | | continue; |
| 1 | 32 | | if (!LanguageUtilityClass.IsCorrectLanguageCode(jproperty.Name)) |
| | 33 | | continue; |
| | 34 | |
|
| 1 | 35 | | titlesMap[jproperty.Name] = GetValidValuesFromArray((JArray)jproperty.Value, propertyName); |
| | 36 | |
|
| | 37 | | } |
| 1 | 38 | | return new NaturalLanguageProperty(titlesMap); |
| | 39 | | } |
| | 40 | | } |
| | 41 | |
|
| | 42 | | string[] GetValidValuesFromArray(JArray array, string propertyName) |
| | 43 | | { |
| 1 | 44 | | List<string> ValidValues = new List<string>(); |
| 1 | 45 | | foreach (var value in array) |
| | 46 | | { |
| 1 | 47 | | if (value.Type == JTokenType.String) ValidValues.Add(value.ToString()); |
| 1 | 48 | | else Warnings.Add(WarningFactory.GetNaturalLanguageWrongValue(value, propertyName)); |
| | 49 | | } |
| 1 | 50 | | return ValidValues.ToArray(); |
| | 51 | | } |
| | 52 | | } |
| | 53 | | } |