< Summary

Information
Class: ValidateLib.TabularData.Validation.TabularDataCreator
Assembly: validatelib.dll
File(s): C:\skola_karlovka\RP\code\csv-validator\CSV_Validator\ValidateLib\TabularData\Validation\TabularDataCreator.cs
Line coverage
98%
Covered lines: 50
Uncovered lines: 1
Coverable lines: 51
Total lines: 131
Line coverage: 98%
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\TabularData\Validation\TabularDataCreator.cs

#LineLine coverage
 1using Newtonsoft.Json.Linq;
 2using ValidateLib.Metadata.Embedded;
 3using ValidateLib.TabularData.AnnotatedTabularDataModel;
 4using ValidateLib.TabularData.Parsing;
 5
 6namespace ValidateLib.TabularData.Validation
 7{
 8    public class TabularDataCreator : EmbeddedMetadataExtractor
 9    {
 110        Table table { get; set; }
 11        string? firstRecordRowContent = null;
 112        int rowNumber = 1;
 113        int sourceColumnNumber = 1;
 114        public int rowsProcessed { get; set; }
 115        public int cellsProcessed { get; set; }
 116        public TabularDataCreator(Flags flags, Table table) : base(flags)
 17        {
 118            _rowParser = new RowParser(flags);
 119            _rowReader = new RowReader(flags);
 120            this.table = table;
 21
 122        }
 23        public void HandleHeaderAndSkipRows(CustomStreamReader csr)
 24        {
 125            _csr = csr;
 26
 127            string content = "{\r\n  \"@context\": \"http://www.w3.org/ns/csvw\",\r\n  \"rdfs:comment\": [],\r\n  \"tabl
 128            JObject embeddedMetadata = JObject.Parse(content);
 29
 130            _sourceRowNumber = 1;    // point 4
 131            HandleEncoding();   // point 5
 132            HandleSkipRows(embeddedMetadata);
 133            HandleHeader(embeddedMetadata);
 34
 135            firstRecordRowContent = _rowReader!.ReadRow(csr);
 136            if (firstRecordRowContent is not null)
 137                HandleZeroHeaderRowCount(embeddedMetadata, firstRecordRowContent);
 38
 139        }
 40
 41        public Row? CreateRow(CustomStreamReader csr, RowReader rowReader)
 42        {
 143            Row? row = null;
 44
 45            // 10.1
 146            sourceColumnNumber = 1;
 47
 48            // 10.2
 49            // not a first round we needed to read to process the header
 50            string? rowContent;
 151            if (firstRecordRowContent is null)
 152                rowContent = rowReader.ReadRow(csr!);
 53            else
 54            {
 155                rowContent = firstRecordRowContent;
 156                firstRecordRowContent = null;
 57            }
 58
 59            // there is nothing left to read
 160            if (rowContent is null)
 161                return null;
 62
 63            // 10.3 - we do not do anything, just skip 10.4
 164            if (!IsComment(rowContent))
 65            {
 66
 67
 168                row = new Row(table, rowNumber, _sourceRowNumber);
 69
 70                // 10.4
 171                List<string> cellValues = _rowParser!.ParseRow(rowContent, row);
 72
 173                cellsProcessed += cellValues.Count;
 74
 75                // if 10.4.1 skip the row and do nothing
 176                if (!ShouldSkipTheRow(cellValues))
 77                {
 78                    // 10.4.2
 79                    // row = new Row(table, rowNumber, sourceRowNumber);
 80                    // 10.4.3 - skipped
 81
 82                    // 10.4.4
 183                    HandleSkipColumns(cellValues);
 84
 185                    Column? column = null;
 86                    // 10.4.5.1
 87                    // watch out that at the algorithm the i starts at 1
 188                    for (int i = 0; i < cellValues.Count; i++)
 89                    {
 90
 191                        if (table.columns.Count >= i + 1)
 92                        {
 93                            // 10.4.5.1
 194                            column = table.columns[i];
 95                        }
 96
 97                        // 10.4.5.2
 198                        Cell cell = new Cell(table, column, cellValues[i], row);
 99
 100                        // 10.4.5.4
 1101                        row.cells.Add(cell);
 102
 103                        // 10.4.5.5
 1104                        sourceColumnNumber++;
 105
 106                    }
 107                }
 108            }
 109
 110            // 10.5
 1111            _sourceRowNumber++;
 112
 1113            if (row is not null)
 1114                rowsProcessed++;
 1115            return row;
 116        }
 117
 118        protected void HandleSkipColumns(List<string> cellValues)
 119        {
 1120            RemoveSkipColumns(cellValues);
 1121            sourceColumnNumber += flags.skipColumns;
 1122        }
 123        static protected bool AreAllStringsEmpty(List<string> stringList)
 0124            => stringList.All(string.IsNullOrEmpty);
 125
 126        protected bool ShouldSkipTheRow(List<string> cellValues)
 127        {
 1128            return flags.skipBlankRows && AreAllStringsEmpty(cellValues);
 129        }
 130    }
 131}