private static object[] AssertExtra(FixedLengthReader reader, String expected)
        {
            Assert.IsTrue(reader.Read(), "Could not read the next record.");
            object[]          values = reader.GetValues();
            FixedLengthSchema schema = reader.GetSchema();

            Assert.AreEqual(schema.ColumnDefinitions.Count, values.Length, "The wrong number of values were parsed.");
            object value = values[schema.ColumnDefinitions.Count - 1];

            Assert.AreEqual(expected, value, "The wrong extra value was found for the record.");
            return(values);
        }
        public void TestGetSchema_SchemaProvided_ParsesValues()
        {
            const string text = @"       123                      Bob 1/19/2013";
            FixedLengthSchema schema = new FixedLengthSchema();
            schema.AddColumn(new Int32Column("id"), new Window(10))
                .AddColumn(new StringColumn("name"), new Window(25))
                .AddColumn(new DateTimeColumn("created"), new Window(10));

            StringReader stringReader = new StringReader(text);
            IReader parser = new FixedLengthReader(stringReader, schema);
            ISchema actual = parser.GetSchema();
            Assert.Same(schema, actual);
        }
        public void TestGetSchema_SchemaProvided_ParsesValues()
        {
            const string      text   = @"       123                      Bob 1/19/2013";
            FixedLengthSchema schema = new FixedLengthSchema();

            schema.AddColumn(new Int32Column("id"), new Window(10))
            .AddColumn(new StringColumn("name"), new Window(25))
            .AddColumn(new DateTimeColumn("created"), new Window(10));
            IReader parser = new FixedLengthReader(new MemoryStream(Encoding.Default.GetBytes(text)), schema);
            ISchema actual = parser.GetSchema();

            Assert.AreSame(schema, actual, "The underlying schema was not returned.");
        }
        public void TestGetSchema_SchemaProvided_ParsesValues()
        {
            const string      text   = @"       123                      Bob 1/19/2013";
            FixedLengthSchema schema = new FixedLengthSchema();

            schema.AddColumn(new Int32Column("id"), new Window(10))
            .AddColumn(new StringColumn("name"), new Window(25))
            .AddColumn(new DateTimeColumn("created"), new Window(10));

            StringReader stringReader = new StringReader(text);
            IReader      parser       = new FixedLengthReader(stringReader, schema);
            ISchema      actual       = parser.GetSchema();

            Assert.AreSame(schema, actual);
        }
 public void TestGetSchema_SchemaProvided_ParsesValues()
 {
     const string text = @"       123                      Bob 1/19/2013";
     FixedLengthSchema schema = new FixedLengthSchema();
     schema.AddColumn(new Int32Column("id"), new Window(10))
         .AddColumn(new StringColumn("name"), new Window(25))
         .AddColumn(new DateTimeColumn("created"), new Window(10));
     IReader parser = new FixedLengthReader(new MemoryStream(Encoding.Default.GetBytes(text)), schema);
     ISchema actual = parser.GetSchema();
     Assert.AreSame(schema, actual, "The underlying schema was not returned.");
 }
Exemple #6
0
        public Core.ResultDefinition ValidateFile()
        {
            var schemaData        = string.Empty;
            var schemaParser      = new SchemaParser();
            var validationResults = new List <ValidationResult>();
            var validator         = new Validators.FixedLengthValidator();

            if (this.RawSchema == null)
            {
                try
                {
                    using (var sr = new StreamReader(File.OpenRead(this.SchemaFilePath)))
                    {
                        schemaData = sr.ReadToEnd();
                    }
                }
                catch (Exception ex)
                {
                    var errorMsg = String.Format(null, SharedResources.SchemaError, ex.Message);
                    Console.WriteLine($"{errorMsg}");
                }

                this.RawSchema = schemaParser.ParseSchema(schemaData);
            }

            var fixedLengthFileSchemaBuilder = new FixedLengthSchemaBuilder();
            var fixedFileSchema = fixedLengthFileSchemaBuilder.BuildSchema(this.RawSchema);
            var schemas         = fixedFileSchema.FixedLengthRecordSchemas;
            var count           = 0;

            foreach (var inputLine in File.ReadLines(this.FilePath))
            {
                Console.WriteLine($"Started parsing line...{count}");

                if (inputLine != null)
                {
                    FixedLengthSchema actualSchema;
                    TextReader        stringReader = new StringReader(inputLine);
                    var schema = schemas.FirstOrDefault(x => inputLine.StartsWith(x.RecordIdentifier));
                    count++;
                    var innerResults = new List <ValidationResult>();
                    var line         = inputLine;
                    if (schema != null)
                    {
                        var parser = new FixedLengthReader(stringReader, schema.FixedLengthSchema);

                        try
                        {
                            actualSchema = parser.GetSchema();
                            parser.Read();
                            var rawValues = GetRawValues(inputLine, schema.FixedLengthSchema);
                            var values    = parser.GetValues();

                            for (int i = 0; i < values.Length; i++)
                            {
                                var validationResult = new ValidationResult
                                {
                                    Record            = inputLine,
                                    ColumnName        = actualSchema.ColumnDefinitions[i].ColumnName,
                                    ColumnType        = actualSchema.ColumnDefinitions[i].ColumnType.Name,
                                    ActualRowLength   = inputLine.Length,
                                    MaxRowLength      = actualSchema.TotalWidth,
                                    ParsedValueLength = (values[i].ToString() ?? string.Empty).Length,
                                    RawValueLength    = rawValues[i].ToString().Length,
                                    ColumnLength      = actualSchema.Windows[i].Width,
                                    FillCharacter     = actualSchema.Windows[i].FillCharacter != null ? actualSchema.Windows[i].FillCharacter.ToString() : "Not specified",
                                    TextAlignment     = actualSchema.Windows[i].Alignment != null ? actualSchema.Windows[i].Alignment.ToString() : "Not specified",
                                    ParsedValue       = values[i].ToString() ?? string.Empty,
                                    RawValue          = rawValues[i].ToString() ?? string.Empty,
                                    RowNumber         = count
                                };
                                var failures = validator.Validate(validationResult);
                                if (failures.Errors != null && failures.Errors.Any() && !failures.IsValid)
                                {
                                    var errors = new List <string>();
                                    foreach (var error in failures.Errors)
                                    {
                                        errors.Add(error.ErrorMessage);
                                    }
                                    validationResult.ErrorMessages = errors;
                                    validationResult.HasErrors     = true;
                                    line = line.Replace(validationResult.RawValue, $"<strong style='color:{HtmlColors[i%HtmlColors.Length]}'>{validationResult.RawValue}</strong>");
                                    validationResult.Record = line;
                                    validationResults.Add(validationResult);
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            var errorMsg = string.Empty;
                            if (ex.InnerException != null && ex.InnerException.Message != null)
                            {
                                errorMsg = ex.InnerException.Message;
                                Console.WriteLine(ex.InnerException.Message);
                            }
                            else
                            {
                                errorMsg = ex.Message;
                                Console.WriteLine(ex.Message);
                            }

                            var error = new ValidationResult
                            {
                                RowNumber     = count,
                                Record        = inputLine,
                                ErrorMessages = new List <string>
                                {
                                    $"Exception: {errorMsg}"
                                },
                                HasErrors = true
                            };
                            validationResults.Add(error);
                        }
                    }
                    else
                    {
                        var error = new ValidationResult
                        {
                            RowNumber     = count,
                            Record        = inputLine,
                            ErrorMessages = new List <string>
                            {
                                $"Exception: The row does not contain a record identifier that match the schema."
                            },
                            HasErrors = true
                        };
                        validationResults.Add(error);
                    }
                }
            }
            var results = new Core.ResultDefinition
            {
                Results             = validationResults,
                TotalLinesProcessed = count,
                FileFormat          = this.RawSchema.FileFormat,
                FilePath            = this.FilePath,
                SchemaPath          = this.SchemaFilePath
            };

            return(results);
        }