| | | 1 | | using Newtonsoft.Json.Linq; |
| | | 2 | | using ValidateLib.ErrorsAndWarnings.Warnings; |
| | | 3 | | using ValidateLib.Metadata.Descriptors; |
| | | 4 | | using ValidateLib.Metadata.Descriptors.Interfaces; |
| | | 5 | | |
| | | 6 | | namespace ValidateLib.Metadata.Properties |
| | | 7 | | { |
| | | 8 | | public class NaturalLanguageProperty : Property<Dictionary<string, string[]>>, INormalize |
| | | 9 | | { |
| | 0 | 10 | | public static readonly string _default = ""; |
| | 1 | 11 | | public NaturalLanguageProperty(Dictionary<string, string[]> value) : base(value) { } |
| | | 12 | | public void AddNewLanguage(string language, string[] expressions) |
| | | 13 | | { |
| | 0 | 14 | | _value![language] = expressions; |
| | 0 | 15 | | } |
| | | 16 | | |
| | | 17 | | static string[] GetExpressionsFromToken(JToken token) |
| | | 18 | | { |
| | 0 | 19 | | if (token.Type == JTokenType.String) |
| | | 20 | | { |
| | 0 | 21 | | return new string[] { token.ToString() }; |
| | | 22 | | } |
| | 0 | 23 | | else if (token.Type == JTokenType.Array) |
| | | 24 | | { |
| | 0 | 25 | | return ((JArray)token).Select(token => (string)token!)?.ToArray()!; |
| | | 26 | | } |
| | | 27 | | else |
| | | 28 | | { |
| | 0 | 29 | | throw new ArgumentException(); |
| | | 30 | | } |
| | | 31 | | } |
| | | 32 | | |
| | | 33 | | public static List<Warning> Normalize(JToken token, Context context, JProperty? prop = null) |
| | | 34 | | { |
| | 1 | 35 | | List<Warning> errors = new List<Warning>(); |
| | 1 | 36 | | if (token.Type == JTokenType.Object) |
| | | 37 | | { |
| | 1 | 38 | | foreach (JProperty property in token) |
| | | 39 | | { |
| | 1 | 40 | | if (property.Value.Type == JTokenType.String) |
| | | 41 | | { |
| | 1 | 42 | | property.Value = new JArray(property.Value); |
| | | 43 | | } |
| | | 44 | | } |
| | | 45 | | } |
| | 1 | 46 | | else if (token.Type == JTokenType.String) |
| | | 47 | | { |
| | 1 | 48 | | token.Replace(new JObject(new JProperty(context?.language?._value!, new JArray(token.ToString())))); |
| | | 49 | | } |
| | | 50 | | |
| | 1 | 51 | | else if (token.Type == JTokenType.Array) |
| | | 52 | | { |
| | 1 | 53 | | token.Replace(new JObject(new JProperty(context?.language?._value!, token))); |
| | | 54 | | } |
| | 1 | 55 | | else errors.Add(WarningFactory.GetNaturalLanguageWrongValue(token, prop?.Name!)); |
| | 1 | 56 | | return errors; |
| | | 57 | | } |
| | | 58 | | } |
| | | 59 | | } |