| | 1 | | using ValidateLib.Metadata.Descriptors; |
| | 2 | | using ValidateLib.Metadata.Properties; |
| | 3 | |
|
| | 4 | | namespace ValidateLib.TabularData.Parsing |
| | 5 | | { |
| | 6 | | /// <summary> |
| | 7 | | /// Transforms the dialect descriptor into a flags used for parsing the CSV file. |
| | 8 | | /// Flags are described here: https://www.w3.org/TR/2015/REC-tabular-data-model-20151217/#parsing |
| | 9 | | /// </summary> |
| | 10 | | public class FlagsCreator |
| | 11 | | { |
| | 12 | | public static Flags ExtractFlagsFromDialectDescriptor(DialectDescriptor dialect) |
| | 13 | | { |
| 1 | 14 | | Flags flags = new Flags() |
| 1 | 15 | | { |
| 1 | 16 | | commentPrefix = dialect?.commentPrefix?._value, |
| 1 | 17 | | delimiter = dialect!.delimiter!._value!, |
| 1 | 18 | | encoding = dialect.encoding!._value!, |
| 1 | 19 | | escapeCharacter = dialect!.doubleQuote!._value ? '\"' : '\\', |
| 1 | 20 | | headerRowCount = dialect.headerRowCount!._value!, |
| 1 | 21 | | lineTerminators = dialect.lineTerminators!._value!, |
| 1 | 22 | | quoteCharacter = dialect.quoteChar!._value!, |
| 1 | 23 | | skipBlankRows = dialect.skipBlankRows!._value!, |
| 1 | 24 | | skipColumns = dialect.skipColumns!._value!, |
| 1 | 25 | | skipRows = dialect.skipRows!._value! |
| 1 | 26 | | }; |
| | 27 | |
|
| 1 | 28 | | if (dialect is null) |
| 0 | 29 | | return flags; |
| 1 | 30 | | switch (dialect!.trim!._value) |
| | 31 | | { |
| | 32 | | case "true": |
| 1 | 33 | | flags.trim = TrimOptions.TRUE; |
| 1 | 34 | | break; |
| | 35 | | case "false": |
| 0 | 36 | | flags.trim = TrimOptions.FALSE; |
| 0 | 37 | | break; |
| | 38 | | case "start": |
| 1 | 39 | | flags.trim = TrimOptions.START; |
| 1 | 40 | | break; |
| | 41 | | case "end": |
| 1 | 42 | | flags.trim = TrimOptions.END; |
| | 43 | | break; |
| | 44 | | } |
| | 45 | |
|
| 1 | 46 | | if (dialect.trim.IsDefault && !dialect.skipInitialSpace!.IsDefault) |
| | 47 | | { |
| 1 | 48 | | if (dialect.skipInitialSpace._value) |
| 0 | 49 | | flags.trim = TrimOptions.TRUE; |
| | 50 | | else |
| 1 | 51 | | flags.trim = TrimOptions.FALSE; |
| | 52 | | } |
| | 53 | |
|
| 1 | 54 | | return flags; |
| | 55 | | } |
| | 56 | |
|
| | 57 | | public static void CreateFlags(TableGroupDescriptor tableGroupDescriptor) |
| | 58 | | { |
| 1 | 59 | | foreach (var table in tableGroupDescriptor.tables!._value!) |
| | 60 | | { |
| | 61 | | DialectDescriptor? dialect; |
| 1 | 62 | | if (table.dialect is null || table.dialect._value is null) |
| | 63 | | { |
| 1 | 64 | | dialect = new DialectDescriptor(); |
| | 65 | | } |
| | 66 | | else |
| | 67 | | { |
| 1 | 68 | | dialect = table.dialect._value!; |
| | 69 | | } |
| 1 | 70 | | table._flags = ExtractFlagsFromDialectDescriptor(dialect); |
| 1 | 71 | | table.dialect = new ObjectProperty<DialectDescriptor>(dialect); |
| | 72 | | } |
| 1 | 73 | | } |
| | 74 | | } |
| | 75 | | } |