Esempio n. 1
0
        /// <summary>
        /// For every line (after headerRowsToSkip) in the file with the given path, calls the line handling method you pass.
        /// The validationErrors collection will hold all validation errors encountered during the processing of all lines.
        /// When processing extremely large data sets, accumulating validationErrors in one collection may result in high memory
        /// usage. To avoid
        /// this, use the overload without this collection.
        /// Each line handler method will be given a fresh validator to do its work with.
        /// </summary>
        public override void ParseAndProcessAllLines(LineProcessingMethod lineHandler, ICollection <ValidationError> validationErrors)
        {
            var ws1  = workbook.Worksheets.First();
            var rows = ws1.RangeUsed().RowsUsed().ToList();

            rows = rows.Where(r => !r.IsEmpty()).ToList();
            var header       = rows.First();
            var headerFields = header.Cells().ToList().Select(c => c.Value.ToString().ToLower()).ToList();

            foreach (var row in rows.Skip(HeaderRows))
            {
                var parsedLine = new ExcelParsedLine(headerFields, row);
                NonHeaderRows++;
                if (parsedLine.ContainsData)
                {
                    RowsContainingData++;
                    var validator = new Validator();
                    lineHandler(validator, parsedLine);
                    if (validator.ErrorsOccurred)
                    {
                        if (validationErrors != null)
                        {
                            foreach (var error in validator.Errors)
                            {
                                validationErrors.Add(new ValidationError("Line " + parsedLine.LineNumber, error.UnusableValueReturned, error.Message));
                            }
                        }
                    }
                    else
                    {
                        RowsWithoutValidationErrors++;
                    }
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// For every line (after headerRowsToSkip) in the file with the given path, calls the line handling method you pass.
        /// The validationErrors collection will hold all validation errors encountered during the processing of all lines.
        /// When processing extremely large data sets, accumulating validationErrors in one collection may result in high memory
        /// usage. To avoid
        /// this, use the overload without this collection.
        /// Each line handler method will be given a fresh validator to do its work with.
        /// </summary>
        public override void ParseAndProcessAllLines(LineProcessingMethod lineHandler, ICollection <ValidationError> validationErrors)
        {
            fileReader.ExecuteInStreamReader(
                delegate(StreamReader reader) {
                IDictionary <string, int> columnHeadersToIndexes = null;

                // This skips the header row and creates a name to index map out of it.
                if (hasHeaderRow)
                {
                    columnHeadersToIndexes = buildColumnHeadersToIndexesDictionary(reader.ReadLine());
                }

                // GMS NOTE: It may be time to just remove this feature. It seems like only FixedWidth uses it (can be created with a non-zero value) and there are probably better ways to do it for whatever program needed that.
                // This is a bit misleading. HeaderRowsToSkip will be 0 even when we've skipped the header row due to hasHeadRow, above.
                for (var i = 0; i < headerRowsToSkip; i++)
                {
                    reader.ReadLine();
                }

                string line;
                for (var lineNumber = HeaderRows + 1; (line = reader.ReadLine()) != null; lineNumber++)
                {
                    NonHeaderRows++;
                    var parsedLine = Parse(line);
                    if (parsedLine.ContainsData)
                    {
                        RowsContainingData++;
                        parsedLine.LineNumber             = lineNumber;
                        parsedLine.ColumnHeadersToIndexes = columnHeadersToIndexes;
                        var validator = new Validator();
                        lineHandler(validator, parsedLine);
                        if (validator.ErrorsOccurred)
                        {
                            if (validationErrors != null)
                            {
                                foreach (var error in validator.Errors)
                                {
                                    validationErrors.Add(new ValidationError("Line " + lineNumber, error.UnusableValueReturned, error.Message));
                                }
                            }
                        }
                        else
                        {
                            RowsWithoutValidationErrors++;
                        }
                    }
                }
            });
        }
Esempio n. 3
0
        /// <summary>
        /// For every line (after headerRowsToSkip) in the file with the given path, calls the line handling method you pass.
        /// The validationErrors collection will hold all validation errors encountered during the processing of all lines.
        /// When processing extremely large data sets, accumulating validationErrors in one collection may result in high memory usage. To avoid
        /// this, use the overload without this collection.
        /// Each line handler method will be given a fresh validator to do its work with.
        /// </summary>
        public void ParseAndProcessAllLines(LineProcessingMethod lineHandler, ICollection <ValidationError> validationErrors)
        {
            fileReader.ExecuteInStreamReader(
                delegate(StreamReader reader) {
                IDictionary columnHeadersToIndexes = null;
                if (hasHeaderRow)
                {
                    columnHeadersToIndexes = buildColumnHeadersToIndexesDictionary(reader.ReadLine());
                }

                for (var i = 0; i < headerRowsToSkip; i++)
                {
                    reader.ReadLine();
                }

                string line;
                for (var lineNumber = HeaderRows + 1; (line = reader.ReadLine()) != null; lineNumber++)
                {
                    NonHeaderRows++;
                    var parsedLine = parser.Parse(line);
                    if (parsedLine.ContainsData)
                    {
                        RowsContainingData++;
                        parsedLine.LineNumber             = lineNumber;
                        parsedLine.ColumnHeadersToIndexes = columnHeadersToIndexes;
                        var validator = new Validator();
                        lineHandler(validator, parsedLine);
                        if (validator.ErrorsOccurred)
                        {
                            if (validationErrors != null)
                            {
                                foreach (var error in validator.Errors)
                                {
                                    validationErrors.Add(new ValidationError("Line " + lineNumber, error.UnusableValueReturned, error.Message));
                                }
                            }
                        }
                        else
                        {
                            RowsWithoutValidationErrors++;
                        }
                    }
                }
            });
        }
Esempio n. 4
0
 /// <summary>
 /// For every line (after headerRowsToSkip) in the file with the given path, calls the line handling method you pass.
 /// Each line handler method will be given a fresh validator to do its work with.
 /// </summary>
 public virtual void ParseAndProcessAllLines(LineProcessingMethod lineHandler, ICollection <ValidationError> validationErrors)
 {
     throw new NotImplementedException("Each parser must have a specific implementation of parse and process all lines.");
 }
Esempio n. 5
0
 /// <summary>
 /// For every line (after headerRowsToSkip) in the file with the given path, calls the line handling method you pass.
 /// Each line handler method will be given a fresh validator to do its work with.
 /// </summary>
 public void ParseAndProcessAllLines(LineProcessingMethod lineHandler)
 {
     ParseAndProcessAllLines(lineHandler, null);
 }
        /// <summary>
        /// For every line (after headerRowsToSkip) in the file with the given path, calls the line handling method you pass.
        /// The validationErrors collection will hold all validation errors encountered during the processing of all lines.
        /// </summary>
        public void ParseAndProcessAllLines( LineProcessingMethod lineHandler, ICollection<ValidationError> validationErrors )
        {
            fileReader.ExecuteInStreamReader( delegate( StreamReader reader ) {
                IDictionary columnHeadersToIndexes = null;
                if( hasHeaderRow )
                    columnHeadersToIndexes = buildColumnHeadersToIndexesDictionary( reader.ReadLine() );

                for( var i = 0; i < headerRowsToSkip; i++ )
                    reader.ReadLine();

                string line;
                for( var lineNumber = HeaderRows + 1; ( line = reader.ReadLine() ) != null; lineNumber++ ) {
                    NonHeaderRows++;
                    var parsedLine = parser.Parse( line );
                    if( parsedLine.ContainsData ) {
                        RowsContainingData++;
                        parsedLine.LineNumber = lineNumber;
                        parsedLine.ColumnHeadersToIndexes = columnHeadersToIndexes;
                        var validator = new Validator();
                        lineHandler( validator, parsedLine );
                        if( validator.ErrorsOccurred ) {
                            foreach( var error in validator.Errors )
                                validationErrors.Add( new ValidationError( "Line " + lineNumber, error.UnusableValueReturned, error.Message ) );
                        }
                        else
                            RowsWithoutValidationErrors++;
                    }
                }
            } );
        }