Defines the expected format of a fixed-length file record.
상속: Schema
예제 #1
0
        private string[] partitionRecord(FixedLengthSchema schema, string record)
        {
            if (record.Length < schema.TotalWidth)
            {
                processError(new RecordProcessingException(metadata.RecordCount, Resources.FixedLengthRecordTooShort));
                return(null);
            }
            WindowCollection windows = schema.Windows;

            string[] values = new string[windows.Count - schema.ColumnDefinitions.MetadataCount];
            int      offset = 0;

            for (int index = 0; index != values.Length;)
            {
                var definition = schema.ColumnDefinitions[index];
                if (!(definition is IMetadataColumn metaColumn))
                {
                    Window window    = windows[index];
                    string value     = record.Substring(offset, window.Width);
                    var    alignment = window.Alignment ?? metadata.Options.Alignment;
                    if (alignment == FixedAlignment.LeftAligned)
                    {
                        value = value.TrimEnd(window.FillCharacter ?? metadata.Options.FillCharacter);
                    }
                    else
                    {
                        value = value.TrimStart(window.FillCharacter ?? metadata.Options.FillCharacter);
                    }
                    values[index] = value;
                    ++index;
                    offset += window.Width;
                }
            }
            return(values);
        }
 /// <summary>
 /// Provides the schema to use by default when no other matches are found.
 /// </summary>
 /// <param name="schema">The default schema to use.</param>
 /// <returns>The current selector to allow for further customization.</returns>
 public void WithDefault(FixedLengthSchema schema)
 {
     defaultMatcher = schema == null ? nonMatcher : new SchemaMatcher()
     {
         Predicate = (values) => true, Schema = schema
     };
 }
예제 #3
0
 private void fitWindows(FixedLengthSchema schema, string[] values)
 {
     for (int index = 0; index != values.Length; ++index)
     {
         var window = schema.Windows[index];
         values[index] = fitWidth(window, values[index]);
     }
 }
예제 #4
0
 public void Use(FixedLengthSchema schema)
 {
     if (schema == null)
     {
         throw new ArgumentNullException(nameof(schema));
     }
     selector.Add(schema, predicate);
 }
 /// <summary>
 /// Provides the schema to use by default when no other matches are found.
 /// </summary>
 /// <param name="schema">The default schema to use.</param>
 /// <returns>The current selector to allow for further customization.</returns>
 public IFixedLengthSchemaSelectorUseBuilder WithDefault(FixedLengthSchema schema)
 {
     defaultMatcher = schema == null ? nonMatcher : new SchemaMatcher()
     {
         Predicate = (values) => true, Schema = schema
     };
     return(new FixedLengthSchemaSelectorUseBuilder(defaultMatcher));
 }
예제 #6
0
 public FixedLengthRecordWriter(TextWriter writer, FixedLengthSchema schema, FixedLengthOptions options)
 {
     this.writer   = writer;
     this.metadata = new FixedLengthWriterMetadata()
     {
         Schema  = schema,
         Options = options.Clone()
     };
 }
예제 #7
0
        private void Add(FixedLengthSchema schema, Func <object[], bool> predicate)
        {
            var matcher = new SchemaMatcher()
            {
                Schema    = schema,
                Predicate = predicate
            };

            matchers.Add(matcher);
        }
            public IFixedLengthSchemaSelectorUseBuilder Use(FixedLengthSchema schema)
            {
                if (schema == null)
                {
                    throw new ArgumentNullException(nameof(schema));
                }
                var matcher = selector.Add(schema, predicate);

                return(new FixedLengthSchemaSelectorUseBuilder(matcher));
            }
예제 #9
0
 public FixedLengthRecordParser(TextReader reader, FixedLengthSchema schema, FixedLengthOptions options)
 {
     if (options.HasRecordSeparator)
     {
         this.recordReader = new SeparatorRecordReader(reader, options.RecordSeparator);
     }
     else
     {
         this.recordReader = new FixedLengthRecordReader(reader, schema.TotalWidth);
     }
 }
        private SchemaMatcher Add(FixedLengthSchema schema, Func <string, bool> predicate)
        {
            var matcher = new SchemaMatcher()
            {
                Schema    = schema,
                Predicate = predicate
            };

            matchers.Add(matcher);
            return(matcher);
        }
 public FixedLengthRecordParser(TextReader reader, FixedLengthSchema schema, FixedLengthOptions options)
 {
     if (String.IsNullOrEmpty(options.RecordSeparator))
     {
         this.recordReader = new FixedLengthRecordReader(reader, schema.TotalWidth);
     }
     else
     {
         this.recordReader = new SeparatorRecordReader(reader, options.RecordSeparator);
     }
 }
예제 #12
0
 public FixedLengthRecordWriter(TextWriter writer, FixedLengthSchema schema, FixedLengthOptions options)
 {
     this.writer = writer;
     Metadata    = new FixedLengthRecordContext()
     {
         ExecutionContext = new FixedLengthExecutionContext()
         {
             Schema  = schema,
             Options = options.Clone()
         }
     };
 }
예제 #13
0
        private string[] formatValues(object[] values, FixedLengthSchema schema)
        {
            var metadata = injector == null ? Metadata : new FixedLengthWriterMetadata()
            {
                Schema             = schema,
                Options            = Metadata.Options,
                RecordCount        = Metadata.RecordCount,
                LogicalRecordCount = Metadata.LogicalRecordCount
            };

            return(schema.FormatValues(metadata, values));
        }
 /// <summary>
 /// Initializes a new FixedLengthComplexColumn with the given schema and options.
 /// </summary>
 /// <param name="columnName">The name of the column.</param>
 /// <param name="schema">The schema of the data embedded in the column.</param>
 /// <param name="options">The options to use when parsing the embedded data.</param>
 public FixedLengthComplexColumn(string columnName, FixedLengthSchema schema, FixedLengthOptions options)
     : base(columnName)
 {
     if (schema == null)
     {
         throw new ArgumentNullException("schema");
     }
     if (options == null)
     {
         throw new ArgumentNullException("options");
     }
     this.schema = schema;
     this.options = options.Clone();
 }
예제 #15
0
 /// <summary>
 /// Initializes a new FixedLengthComplexColumn with the given schema and options.
 /// </summary>
 /// <param name="columnName">The name of the column.</param>
 /// <param name="schema">The schema of the data embedded in the column.</param>
 /// <param name="options">The options to use when parsing the embedded data.</param>
 public FixedLengthComplexColumn(string columnName, FixedLengthSchema schema, FixedLengthOptions options)
     : base(columnName)
 {
     if (schema == null)
     {
         throw new ArgumentNullException("schema");
     }
     if (options == null)
     {
         throw new ArgumentNullException("options");
     }
     this.schema  = schema;
     this.options = options.Clone();
 }
예제 #16
0
 public FixedLengthRecordParser(TextReader reader, FixedLengthSchema schema, FixedLengthOptions options)
 {
     if (options.HasRecordSeparator)
     {
         recordReader = new SeparatorRecordReader(reader, options.RecordSeparator);
     }
     else if (schema == null)
     {
         throw new FlatFileException(Resources.RecordSeparatorRequired);
     }
     else
     {
         recordReader = new FixedLengthRecordReader(reader, schema.TotalWidth);
     }
 }
예제 #17
0
 /// <summary>
 /// Initializes a new FixedLengthBuilder with the given schema.
 /// </summary>
 /// <param name="writer">A writer over the fixed-length document.</param>
 /// <param name="schema">The schema of the fixed-length document.</param>
 /// <param name="options">The options used to format the output.</param>
 /// <exception cref="ArgumentNullException">The writer is null.</exception>
 /// <exception cref="ArgumentNullException">The schema is null.</exception>
 public FixedLengthWriter(TextWriter writer, FixedLengthSchema schema, FixedLengthOptions options = null)
 {
     if (writer == null)
     {
         throw new ArgumentNullException(nameof(writer));
     }
     if (schema == null)
     {
         throw new ArgumentNullException(nameof(schema));
     }
     if (options == null)
     {
         options = new FixedLengthOptions();
     }
     recordWriter = new FixedLengthRecordWriter(writer, schema, options);
 }
예제 #18
0
 /// <summary>
 /// Initializes a new FixedLengthBuilder with the given schema.
 /// </summary>
 /// <param name="writer">A writer over the fixed-length document.</param>
 /// <param name="schema">The schema of the fixed-length document.</param>
 /// <param name="options">The options used to format the output.</param>
 /// <exception cref="ArgumentNullException">The writer is null.</exception>
 /// <exception cref="ArgumentNullException">The schema is null.</exception>
 public FixedLengthWriter(TextWriter writer, FixedLengthSchema schema, FixedLengthOptions options = null)
 {
     if (writer == null)
     {
         throw new ArgumentNullException("writer");
     }
     if (schema == null)
     {
         throw new ArgumentNullException("schema");
     }
     if (options == null)
     {
         options = new FixedLengthOptions();
     }
     this.recordWriter = new FixedLengthRecordWriter(writer, schema, options);
     this.isFirstLine  = true;
 }
예제 #19
0
 /// <summary>
 /// Initializes a new FixedLengthReader with the given schema.
 /// </summary>
 /// <param name="reader">A reader over the fixed-length document.</param>
 /// <param name="schema">The schema of the fixed-length document.</param>
 /// <param name="options">The options controlling how the fixed-length document is read.</param>
 /// <exception cref="ArgumentNullException">The reader is null.</exception>
 /// <exception cref="ArgumentNullException">The schema is null.</exception>
 public FixedLengthReader(TextReader reader, FixedLengthSchema schema, FixedLengthOptions options = null)
 {
     if (reader == null)
     {
         throw new ArgumentNullException("reader");
     }
     if (schema == null)
     {
         throw new ArgumentNullException("schema");
     }
     if (options == null)
     {
         options = new FixedLengthOptions();
     }
     parser       = new FixedLengthRecordParser(reader, schema, options);
     this.schema  = schema;
     this.options = options.Clone();
 }
예제 #20
0
 private FixedLengthReader(Stream stream, FixedLengthSchema schema, FixedLengthOptions options, bool ownsStream)
 {
     if (stream == null)
     {
         throw new ArgumentNullException("stream");
     }
     if (schema == null)
     {
         throw new ArgumentNullException("schema");
     }
     if (options == null)
     {
         throw new ArgumentNullException("options");
     }
     reader = new RecordReader(stream, options.Encoding, options.RecordSeparator, ownsStream);
     this.schema = schema;
     this.options = options.Clone();
 }
예제 #21
0
 private FixedLengthReader(Stream stream, FixedLengthSchema schema, FixedLengthOptions options, bool ownsStream)
 {
     if (stream == null)
     {
         throw new ArgumentNullException("stream");
     }
     if (schema == null)
     {
         throw new ArgumentNullException("schema");
     }
     if (options == null)
     {
         throw new ArgumentNullException("options");
     }
     reader       = new RecordReader(stream, options.Encoding, options.RecordSeparator, ownsStream);
     this.schema  = schema;
     this.options = options.Clone();
 }
예제 #22
0
 private object[] parseValues(FixedLengthSchema schema, string[] rawValues)
 {
     try
     {
         var metadata = schemaSelector == null ? this.metadata : new Metadata()
         {
             Schema             = schema,
             Options            = this.metadata.Options,
             RecordCount        = this.metadata.RecordCount,
             LogicalRecordCount = this.metadata.LogicalRecordCount
         };
         return(schema.ParseValues(metadata, rawValues));
     }
     catch (FlatFileException exception)
     {
         processError(new RecordProcessingException(metadata.RecordCount, Resources.InvalidRecordConversion, exception));
         return(null);
     }
 }
예제 #23
0
 private FixedLengthWriter(Stream stream, FixedLengthSchema schema, FixedLengthOptions options, bool ownsStream)
 {
     if (stream == null)
     {
         throw new ArgumentNullException("stream");
     }
     if (schema == null)
     {
         throw new ArgumentNullException("schema");
     }
     if (options == null)
     {
         throw new ArgumentNullException("options");
     }
     this.writer = new StreamWriter(stream, options.Encoding ?? Encoding.Default);
     this.schema = schema;
     this.ownsStream = ownsStream;
     this.options = options.Clone();
 }
예제 #24
0
 private FixedLengthWriter(Stream stream, FixedLengthSchema schema, FixedLengthOptions options, bool ownsStream)
 {
     if (stream == null)
     {
         throw new ArgumentNullException("stream");
     }
     if (schema == null)
     {
         throw new ArgumentNullException("schema");
     }
     if (options == null)
     {
         throw new ArgumentNullException("options");
     }
     this.writer     = new StreamWriter(stream, options.Encoding ?? Encoding.Default);
     this.schema     = schema;
     this.ownsStream = ownsStream;
     this.options    = options.Clone();
 }
예제 #25
0
 private FixedLengthReader(TextReader reader, FixedLengthSchema schema, FixedLengthOptions options = null, bool hasSchema = true)
 {
     if (reader == null)
     {
         throw new ArgumentNullException(nameof(reader));
     }
     if (hasSchema && schema == null)
     {
         throw new ArgumentNullException(nameof(schema));
     }
     if (options == null)
     {
         options = new FixedLengthOptions();
     }
     parser        = new FixedLengthRecordParser(reader, schema, options);
     this.metadata = new Metadata()
     {
         Schema  = schema,
         Options = options.Clone()
     };
 }
예제 #26
0
 /// <summary>
 /// Initializes a new FixedLengthComplexColumn with the given schema and default options.
 /// </summary>
 /// <param name="columnName">The name of the column.</param>
 /// <param name="schema">The schema of the data embedded in the column.</param>
 public FixedLengthComplexColumn(string columnName, FixedLengthSchema schema)
     : this(columnName, schema, new FixedLengthOptions())
 {
 }
예제 #27
0
 /// <summary>
 /// Initializes a new instance of a FixedLengthParser.
 /// </summary>
 /// <param name="stream">A stream containing the records to parse.</param>
 /// <param name="schema">The schema object defining which columns are in each record.</param>
 /// <param name="options">An object containing settings for configuring the parser.</param>
 /// <exception cref="System.ArgumentNullException">The stream is null.</exception>
 /// <exception cref="System.ArgumentNullException">The schema is null.</exception>
 /// <exception cref="System.ArgumentNullException">The options is null.</exception>
 public FixedLengthReader(Stream stream, FixedLengthSchema schema, FixedLengthOptions options)
     : this(stream, schema, options, false)
 {
 }
예제 #28
0
 /// <summary>
 /// Initializes a new instance of a FixedLengthParser.
 /// </summary>
 /// <param name="stream">A stream containing the records to parse.</param>
 /// <param name="schema">The schema object defining which columns are in each record.</param>
 /// <exception cref="System.ArgumentNullException">The stream is null.</exception>
 /// <exception cref="System.ArgumentNullException">The schema is null.</exception>
 public FixedLengthReader(Stream stream, FixedLengthSchema schema)
     : this(stream, schema, new FixedLengthOptions(), false)
 {
 }
예제 #29
0
 /// <summary>
 /// Initializes a new instance of a FixedLengthParser.
 /// </summary>
 /// <param name="fileName">The path to the file containing the records to parse.</param>
 /// <param name="schema">The schema object defining which columns are in each record.</param>
 /// <param name="options">An object containing settings for configuring the parser.</param>
 /// <exception cref="System.ArgumentNullException">The schema is null.</exception>
 /// <exception cref="System.ArgumentNullException">The options is null.</exception>
 public FixedLengthReader(string fileName, FixedLengthSchema schema, FixedLengthOptions options)
     : this(File.OpenRead(fileName), schema, options, true)
 {
 }
예제 #30
0
 public FixedLengthRecordWriter(TextWriter writer, FixedLengthSchema schema, FixedLengthOptions options)
 {
     this.writer  = writer;
     this.schema  = schema;
     this.options = options.Clone();
 }
 public FixedLengthRecordWriter(TextWriter writer, FixedLengthSchema schema, FixedLengthOptions options)
 {
     this.writer = writer;
     this.schema = schema;
     this.options = options.Clone();
 }
 /// <summary>
 /// Initializes a new FixedLengthComplexColumn with the given schema and default options.
 /// </summary>
 /// <param name="columnName">The name of the column.</param>
 /// <param name="schema">The schema of the data embedded in the column.</param>
 public FixedLengthComplexColumn(string columnName, FixedLengthSchema schema)
     : this(columnName, schema, new FixedLengthOptions())
 {
 }
예제 #33
0
 /// <summary>
 /// Initializes a new FixedLengthReader with the given schema.
 /// </summary>
 /// <param name="reader">A reader over the fixed-length document.</param>
 /// <param name="schema">The schema of the fixed-length document.</param>
 /// <param name="options">The options controlling how the fixed-length document is read.</param>
 /// <exception cref="ArgumentNullException">The reader is null.</exception>
 /// <exception cref="ArgumentNullException">The schema is null.</exception>
 public FixedLengthReader(TextReader reader, FixedLengthSchema schema, FixedLengthOptions options = null)
     : this(reader, schema, options, true)
 {
 }
예제 #34
0
 /// <summary>
 /// Initializes a new instance of a FixedLengthBuilder.
 /// </summary>
 /// <param name="stream">The stream to write the output to.</param>
 /// <param name="schema">The schema to use to build the output.</param>
 /// <param name="options">The options used to format the output.</param>
 public FixedLengthWriter(Stream stream, FixedLengthSchema schema, FixedLengthOptions options)
     : this(stream, schema, options, false)
 {
 }
예제 #35
0
 /// <summary>
 /// Initializes a new instance of a FixedLengthBuilder.
 /// </summary>
 /// <param name="stream">The stream to write the output to.</param>
 /// <param name="schema">The schema to use to build the output.</param>
 public FixedLengthWriter(Stream stream, FixedLengthSchema schema)
     : this(stream, schema, new FixedLengthOptions(), false)
 {
 }
예제 #36
0
 /// <summary>
 /// Initializes a new instance of a FixedLengthBuilder.
 /// </summary>
 /// <param name="fileName">The name of the file to write the output to.</param>
 /// <param name="schema">The schema to use to build the output.</param>
 /// <param name="options">The options used to format the output.</param>
 public FixedLengthWriter(string fileName, FixedLengthSchema schema, FixedLengthOptions options)
     : this(File.OpenWrite(fileName), schema, options, true)
 {
 }
예제 #37
0
 /// <summary>
 /// Initializes a new instance of a FixedLengthParser.
 /// </summary>
 /// <param name="fileName">The path of the file containing the records to parse.</param>
 /// <param name="schema">The schema object defining which columns are in each record.</param>
 /// <exception cref="System.ArgumentNullException">The schema is null.</exception>
 public FixedLengthReader(string fileName, FixedLengthSchema schema)
     : this(File.OpenRead(fileName), schema, new FixedLengthOptions(), true)
 {
 }
예제 #38
0
 /// <summary>
 /// Initializes a new instance of a FixedLengthBuilder.
 /// </summary>
 /// <param name="fileName">The name of the file to write the output to.</param>
 /// <param name="schema">The schema to use to build the output.</param>
 public FixedLengthWriter(string fileName, FixedLengthSchema schema)
     : this(File.OpenWrite(fileName), schema, new FixedLengthOptions(), true)
 {
 }
예제 #39
0
 /// <summary>
 /// Initializes a new FixedLengthComplexColumn with the given schema and options.
 /// </summary>
 /// <param name="columnName">The name of the column.</param>
 /// <param name="schema">The schema of the data embedded in the column.</param>
 /// <param name="options">The options to use when parsing the embedded data.</param>
 public FixedLengthComplexColumn(string columnName, FixedLengthSchema schema, FixedLengthOptions options = null)
     : base(columnName)
 {
     this.schema = schema ?? throw new ArgumentNullException(nameof(schema));
     Options     = options;
 }