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 }; }
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]); } }
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)); }
public FixedLengthRecordWriter(TextWriter writer, FixedLengthSchema schema, FixedLengthOptions options) { this.writer = writer; this.metadata = new FixedLengthWriterMetadata() { Schema = schema, Options = options.Clone() }; }
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)); }
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); } }
public FixedLengthRecordWriter(TextWriter writer, FixedLengthSchema schema, FixedLengthOptions options) { this.writer = writer; Metadata = new FixedLengthRecordContext() { ExecutionContext = new FixedLengthExecutionContext() { Schema = schema, Options = options.Clone() } }; }
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(); }
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); } }
/// <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); }
/// <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; }
/// <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(); }
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(); }
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); } }
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(); }
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() }; }
/// <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()) { }
/// <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) { }
/// <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) { }
/// <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) { }
public FixedLengthRecordWriter(TextWriter writer, FixedLengthSchema schema, FixedLengthOptions options) { this.writer = writer; this.schema = schema; this.options = options.Clone(); }
/// <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) { }
/// <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) { }
/// <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) { }
/// <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) { }
/// <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) { }
/// <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) { }
/// <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; }