| | 1 | | using System.Text.RegularExpressions; |
| | 2 | | using ValidateLib.ErrorsAndWarnings.Errors; |
| | 3 | | using ValidateLib.Metadata.Descriptors; |
| | 4 | |
|
| | 5 | | namespace ValidateLib.TabularData.Datatypes |
| | 6 | | { |
| | 7 | |
|
| | 8 | | public class Base64BinaryDT : BaseDT, ILength |
| | 9 | | { |
| 1 | 10 | | public byte[]? Value { get; set; } |
| 1 | 11 | | public Base64BinaryDT() { } |
| 1 | 12 | | public Base64BinaryDT(string stringValue) : base(stringValue) |
| | 13 | | { |
| 1 | 14 | | CheckLexicalSpace(stringValue); |
| | 15 | | try |
| | 16 | | { |
| 1 | 17 | | Value = Convert.FromBase64String(stringValue); |
| 1 | 18 | | } |
| 1 | 19 | | catch (Exception) |
| | 20 | | { |
| 1 | 21 | | ErrorFactory.ThrowDatatypeValidationError(stringValue); |
| 0 | 22 | | } |
| | 23 | |
|
| 1 | 24 | | } |
| | 25 | |
|
| 0 | 26 | | public Base64BinaryDT(string stringValue, FormatDescriptor format) : base(stringValue, format) |
| | 27 | | { |
| 0 | 28 | | CheckLexicalSpace(stringValue); |
| 0 | 29 | | MatchRegex(format.pattern!, stringValue); |
| | 30 | | try |
| | 31 | | { |
| 0 | 32 | | Value = Convert.FromBase64String(stringValue); |
| 0 | 33 | | } |
| 0 | 34 | | catch (Exception) |
| | 35 | | { |
| 0 | 36 | | ErrorFactory.ThrowDatatypeValidationError(stringValue); |
| 0 | 37 | | } |
| | 38 | |
|
| 0 | 39 | | } |
| | 40 | |
|
| | 41 | | void CheckLexicalSpace(string stringValue) |
| | 42 | | { |
| 1 | 43 | | string pattern = @"((([A-Za-z0-9+/] ?){4})*(([A-Za-z0-9+/] ?){3}[A-Za-z0-9+/]|([A-Za-z0-9+/] ?){2}[AEIMQUYcg |
| 1 | 44 | | if (!Regex.IsMatch(stringValue, pattern)) |
| 0 | 45 | | ErrorFactory.ThrowDatatypeValidationError(stringValue); |
| | 46 | |
|
| 1 | 47 | | } |
| | 48 | |
|
| | 49 | | public int Length() |
| | 50 | | { |
| 1 | 51 | | if (Value == null) return 0; |
| 1 | 52 | | return Value!.Count(); |
| | 53 | | } |
| | 54 | |
|
| | 55 | | public override bool IsPatternValid(FormatDescriptor format) |
| | 56 | | { |
| | 57 | | try |
| | 58 | | { |
| 0 | 59 | | Regex regex = new Regex(format.pattern!); |
| 0 | 60 | | return true; |
| | 61 | | } |
| 0 | 62 | | catch (Exception) |
| | 63 | | { |
| 0 | 64 | | return false; |
| | 65 | | } |
| 0 | 66 | | } |
| | 67 | | } |
| | 68 | | } |