private SeparatedValueReader(Stream stream, SeparatedValueSchema schema, SeparatedValueOptions options, bool hasSchema, bool ownsStream)
 {
     if (stream == null)
     {
         throw new ArgumentNullException("stream");
     }
     if (hasSchema && schema == null)
     {
         throw new ArgumentNullException("schema");
     }
     if (options == null)
     {
         throw new ArgumentNullException("options");
     }
     reader = new RecordReader(stream, options.Encoding, options.RecordSeparator, ownsStream);
     regex  = buildRegex(options.Separator);
     if (hasSchema)
     {
         if (options.IsFirstRecordSchema)
         {
             readNextRecord();  // skip header record
         }
         this.schema = schema;
     }
     else if (options.IsFirstRecordSchema)
     {
         string[] columnNames = readNextRecord();
         this.schema = new SeparatedValueSchema();
         foreach (string columnName in columnNames)
         {
             StringColumn column = new StringColumn(columnName);
             this.schema.AddColumn(column);
         }
     }
 }
Example #2
0
 /// <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(SeparatedValueSchema schema)
 {
     defaultMatcher = schema == null ? nonMatcher : new SchemaMatcher()
     {
         Predicate = (values) => true, Schema = schema
     };
 }
Example #3
0
        private async Task handleSchemaAsync()
        {
            if (recordCount != 0)
            {
                return;
            }
            if (!parser.Options.IsFirstRecordSchema)
            {
                return;
            }
            if (schema != null)
            {
                await skipAsync();

                return;
            }
            string[] columnNames = await readNextRecordAsync();

            schema = new SeparatedValueSchema();
            foreach (string columnName in columnNames)
            {
                StringColumn column = new StringColumn(columnName);
                schema.AddColumn(column);
            }
        }
Example #4
0
        private SeparatedValueReader(TextReader reader, SeparatedValueSchema schema, SeparatedValueOptions options, bool hasSchema)
        {
            if (reader == null)
            {
                throw new ArgumentNullException(nameof(reader));
            }
            if (hasSchema && schema == null)
            {
                throw new ArgumentNullException(nameof(schema));
            }
            if (options == null)
            {
                options = new SeparatedValueOptions();
            }
            if (options.RecordSeparator == options.Separator)
            {
                throw new ArgumentException(SharedResources.SameSeparator, nameof(options));
            }
            RetryReader retryReader = new RetryReader(reader);

            this.parser = new SeparatedValueRecordParser(retryReader, options);
            if (hasSchema)
            {
                this.schema = schema;
            }
        }
 /// <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 ISeparatedValueSchemaSelectorUseBuilder WithDefault(SeparatedValueSchema schema)
 {
     defaultMatcher = schema == null ? nonMatcher : new SchemaMatcher {
         Predicate = values => true, Schema = schema
     };
     return(new SeparatedValueSchemaSelectorUseBuilder(defaultMatcher));
 }
        private SeparatedValueReader(TextReader reader, SeparatedValueSchema schema, SeparatedValueOptions options, bool hasSchema)
        {
            if (reader == null)
            {
                throw new ArgumentNullException(nameof(reader));
            }
            if (hasSchema && schema == null)
            {
                throw new ArgumentNullException(nameof(schema));
            }
            if (options == null)
            {
                options = new SeparatedValueOptions();
            }
            if (options.RecordSeparator == options.Separator)
            {
                throw new ArgumentException(Resources.SameSeparator, nameof(options));
            }
            RetryReader retryReader = new RetryReader(reader);

            parser   = new SeparatedValueRecordParser(retryReader, options);
            metadata = new SeparatedValueRecordContext()
            {
                ExecutionContext = new SeparatedValueExecutionContext()
                {
                    Schema  = hasSchema ? schema : null,
                    Options = parser.Options
                }
            };
        }
 public void Use(SeparatedValueSchema schema)
 {
     if (schema == null)
     {
         throw new ArgumentNullException(nameof(schema));
     }
     selector.Add(schema, predicate);
 }
 public SeparatedValueRecordWriter(TextWriter writer, SeparatedValueSchema schema, SeparatedValueOptions options)
 {
     this.writer            = writer;
     this.schema            = schema;
     this.options           = options.Clone();
     this.quoteString       = String.Empty + options.Quote;
     this.doubleQuoteString = String.Empty + options.Quote + options.Quote;
 }
            public ISeparatedValueSchemaSelectorUseBuilder Use(SeparatedValueSchema schema)
            {
                if (schema == null)
                {
                    throw new ArgumentNullException(nameof(schema));
                }
                var matcher = selector.Add(schema, predicate);

                return(new SeparatedValueSchemaSelectorUseBuilder(matcher));
            }
        private void Add(SeparatedValueSchema schema, Func <object[], bool> predicate)
        {
            var matcher = new SchemaMatcher()
            {
                Schema    = schema,
                Predicate = predicate
            };

            matchers.Add(matcher);
        }
Example #11
0
 private SeparatedValueComplexColumn(string columnName, SeparatedValueSchema schema, SeparatedValueOptions options, bool hasSchema)
     : base(columnName)
 {
     if (hasSchema && schema == null)
     {
         throw new ArgumentNullException(nameof(schema));
     }
     this.schema = schema;
     Options     = options;
 }
Example #12
0
 public SeparatedValueRecordWriter(TextWriter writer, SeparatedValueSchema schema, SeparatedValueOptions options)
 {
     this.writer   = writer;
     this.metadata = new SeparatedValueMetadata()
     {
         Schema  = schema,
         Options = options.Clone()
     };
     this.quoteString       = String.Empty + options.Quote;
     this.doubleQuoteString = String.Empty + options.Quote + options.Quote;
 }
        private SchemaMatcher Add(SeparatedValueSchema schema, Func <string[], bool> predicate)
        {
            var matcher = new SchemaMatcher()
            {
                Schema    = schema,
                Predicate = predicate
            };

            matchers.Add(matcher);
            return(matcher);
        }
Example #14
0
 public SeparatedValueRecordWriter(TextWriter writer, SeparatedValueSchema schema, SeparatedValueOptions options)
 {
     this.writer = writer;
     Metadata    = new SeparatedValueRecordContext()
     {
         ExecutionContext = new SeparatedValueExecutionContext()
         {
             Schema  = schema,
             Options = options.Clone()
         }
     };
     quoteString       = String.Empty + options.Quote;
     doubleQuoteString = String.Empty + options.Quote + options.Quote;
 }
Example #15
0
 private SeparatedValueWriter(TextWriter writer, SeparatedValueSchema schema, SeparatedValueOptions options, bool hasSchema)
 {
     if (writer == null)
     {
         throw new ArgumentNullException("writer");
     }
     if (hasSchema && schema == null)
     {
         throw new ArgumentNullException("schema");
     }
     if (options == null)
     {
         options = new SeparatedValueOptions();
     }
     this.recordWriter = new SeparatedValueRecordWriter(writer, schema, options);
 }
 private SeparatedValueWriter(TextWriter writer, SeparatedValueSchema schema, SeparatedValueOptions options, bool hasSchema)
 {
     if (writer == null)
     {
         throw new ArgumentNullException("writer");
     }
     if (hasSchema && schema == null)
     {
         throw new ArgumentNullException("schema");
     }
     if (options == null)
     {
         options = new SeparatedValueOptions();
     }
     this.recordWriter = new SeparatedValueRecordWriter(writer, schema, options);
     this.isFirstLine = true;
 }
Example #17
0
        private SeparatedValueReader(TextReader reader, SeparatedValueSchema schema, SeparatedValueOptions options, bool hasSchema)
        {
            if (reader == null)
            {
                throw new ArgumentNullException("reader");
            }
            if (hasSchema && schema == null)
            {
                throw new ArgumentNullException("schema");
            }
            if (options == null)
            {
                options = new SeparatedValueOptions();
            }
            if (options.RecordSeparator == options.Separator)
            {
                throw new ArgumentException(SharedResources.SameSeparator, "options");
            }
            RetryReader retryReader = new RetryReader(reader);

            this.parser = new SeparatedValueRecordParser(retryReader, options);
            if (hasSchema)
            {
                if (options.IsFirstRecordSchema)
                {
                    skip();  // skip header record
                }
                this.schema = schema;
            }
            else if (options.IsFirstRecordSchema)
            {
                string[] columnNames = readNextRecord();
                this.schema = new SeparatedValueSchema();
                foreach (string columnName in columnNames)
                {
                    StringColumn column = new StringColumn(columnName);
                    this.schema.AddColumn(column);
                }
            }
        }
Example #18
0
 private SeparatedValueWriter(Stream stream, SeparatedValueSchema schema, SeparatedValueOptions 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.ownsStream          = ownsStream;
     this.schema              = schema;
     this.isFirstRecordSchema = options.IsFirstRecordSchema;
     this.separator           = options.Separator;
     this.recordSeparator     = options.RecordSeparator;
     this.isFirstLine         = true;
 }
 private SeparatedValueWriter(Stream stream, SeparatedValueSchema schema, SeparatedValueOptions 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.ownsStream = ownsStream;
     this.schema = schema;
     this.isFirstRecordSchema = options.IsFirstRecordSchema;
     this.separator = options.Separator;
     this.recordSeparator = options.RecordSeparator;
     this.isFirstLine = true;
 }
Example #20
0
 private object[] parseValues(SeparatedValueSchema schema, string[] rawValues)
 {
     if (schema == null)
     {
         return(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);
     }
 }
Example #21
0
 private string[] formatValues(SeparatedValueSchema schema, object[] values)
 {
     if (schema == null)
     {
         string[] results = new string[values.Length];
         for (int index = 0; index != results.Length; ++index)
         {
             results[index] = toString(values[index]);
         }
         return(results);
     }
     else
     {
         var metadata = injector == null ? Metadata : new SeparatedValueMetadata()
         {
             Schema             = schema,
             Options            = Metadata.Options,
             RecordCount        = Metadata.RecordCount,
             LogicalRecordCount = Metadata.LogicalRecordCount
         };
         return(schema.FormatValues(Metadata, values));
     }
 }
Example #22
0
 /// <summary>
 /// Initializes a new SeparatedValueWriter with the given schema.
 /// </summary>
 /// <param name="writer">A writer over the separated value document.</param>
 /// <param name="schema">The schema of the separated value 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 SeparatedValueWriter(TextWriter writer, SeparatedValueSchema schema, SeparatedValueOptions options = null)
     : this(writer, schema, options, true)
 {
 }
 /// <summary>
 /// Initializes a new instance of a SeparatedValueWriter.
 /// </summary>
 /// <param name="fileName">The name of the file to write to.</param>
 /// <param name="schema">The schema to use to build the output.</param>
 /// <param name="options">The options to use to format the output.</param>
 /// <exception cref="System.ArgumentNullException">The schema is null.</exception>
 public SeparatedValueWriter(string fileName, SeparatedValueSchema schema, SeparatedValueOptions options)
     : this(File.OpenWrite(fileName), schema, options, true)
 {
 }
 /// <summary>
 /// Initializes a new instance of a FixedLengthSchema.
 /// </summary>
 public FixedLengthSchema()
 {
     schema = new SeparatedValueSchema();
     windows = new List<Window>();
 }
 /// <summary>
 /// Initializes a new instance of a SeparatedValueParser.
 /// </summary>
 /// <param name="stream">A stream containing the records to parse.</param>
 /// <param name="schema">The predefined schema for the records.</param>
 /// <exception cref="System.ArgumentNullException">The stream is null.</exception>
 /// <exception cref="System.ArgumentNullException">The schema is null.</exception>
 public SeparatedValueReader(Stream stream, SeparatedValueSchema schema)
     : this(stream, schema, new SeparatedValueOptions(), true, false)
 {
 }
 /// <summary>
 /// Initializes a new instance of a SeparatedValueWriter.
 /// </summary>
 /// <param name="stream">The stream to write the output to.</param>
 /// <param name="schema">The schema to use to build the output.</param>
 /// <exception cref="System.ArgumentNullException">The schema is null.</exception>
 /// <exception cref="System.ArgumentNullException">The options is null.</exception>
 public SeparatedValueWriter(Stream stream, SeparatedValueSchema schema)
     : this(stream, schema, new SeparatedValueOptions(), false)
 {
 }
 /// <summary>
 /// Initializes a new instance of a SeparatedValueParser.
 /// </summary>
 /// <param name="stream">A stream containing the records to parse.</param>
 /// <param name="schema">The predefined schema for the records.</param>
 /// <exception cref="System.ArgumentNullException">The stream is null.</exception>
 /// <exception cref="System.ArgumentNullException">The schema is null.</exception>
 public SeparatedValueReader(Stream stream, SeparatedValueSchema schema)
     : this(stream, schema, new SeparatedValueOptions(), true, false)
 {
 }
 /// <summary>
 /// Initializes a new instance of a SeparatedValueParser.
 /// </summary>
 /// <param name="fileName">The path of the file containing the records to extract.</param>
 /// <param name="schema">The predefined schema for the records.</param>
 /// <param name="options">The options for configuring the parser's behavior.</param>
 /// <exception cref="System.ArgumentNullException">The schema is null.</exception>
 /// <exception cref="System.ArgumentNullException">The options object is null.</exception>
 public SeparatedValueReader(string fileName, SeparatedValueSchema schema, SeparatedValueOptions options)
     : this(File.OpenRead(fileName), schema, options, true, true)
 {
 }
 /// <summary>
 /// Initializes a new instance of a SeparatedValueWriter.
 /// </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>
 /// <exception cref="System.ArgumentNullException">The schema is null.</exception>
 /// <exception cref="System.ArgumentNullException">The options is null.</exception>
 public SeparatedValueWriter(Stream stream, SeparatedValueSchema schema, SeparatedValueOptions options)
     : this(stream, schema, options, false)
 {
 }
 /// <summary>
 /// Initializes a new instance of a SeparatedValueParser.
 /// </summary>
 /// <param name="stream">A stream containing the records to parse.</param>
 /// <param name="schema">The predefined schema for the records.</param>
 /// <param name="options">The options for configuring the parser's behavior.</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 SeparatedValueReader(Stream stream, SeparatedValueSchema schema, SeparatedValueOptions options)
     : this(stream, schema, options, true, false)
 {
 }
 /// <summary>
 /// Initializes a new instance of a FixedLengthSchema.
 /// </summary>
 public FixedLengthSchema()
 {
     schema  = new SeparatedValueSchema();
     windows = new List <Window>();
 }
Example #32
0
 /// <summary>
 /// Initializes a new SeparatedValueComplexColumn 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 SeparatedValueComplexColumn(string columnName, SeparatedValueSchema schema, SeparatedValueOptions options = null)
     : this(columnName, schema, options, true)
 {
 }
Example #33
0
 /// <summary>
 /// Initializes a new SeparatedValueComplexColumn 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 SeparatedValueComplexColumn(string columnName, SeparatedValueSchema schema)
     : this(columnName, schema, new SeparatedValueOptions(), true)
 {
 }
 /// <summary>
 /// Initializes a new instance of a SeparatedValueParser.
 /// </summary>
 /// <param name="stream">A stream containing the records to parse.</param>
 /// <param name="schema">The predefined schema for the records.</param>
 /// <param name="options">The options for configuring the parser's behavior.</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 SeparatedValueReader(Stream stream, SeparatedValueSchema schema, SeparatedValueOptions options)
     : this(stream, schema, options, true, false)
 {
 }
 /// <summary>
 /// Initializes a new SeparatedValueReader with the given schema.
 /// </summary>
 /// <param name="reader">A reader over the separated value document.</param>
 /// <param name="schema">The schema of the separated value document.</param>
 /// <param name="options">The options controlling how the separated value document is read.</param>
 /// <exception cref="ArgumentNullException">The reader is null.</exception>
 /// <exception cref="ArgumentNullException">The schema is null.</exception>
 public SeparatedValueReader(TextReader reader, SeparatedValueSchema schema, SeparatedValueOptions options = null)
     : this(reader, schema, options, true)
 {
 }
Example #36
0
 /// <summary>
 /// Initializes a new instance of a SeparatedValueWriter.
 /// </summary>
 /// <param name="fileName">The name of the file to write to.</param>
 /// <param name="schema">The schema to use to build the output.</param>
 /// <param name="options">The options to use to format the output.</param>
 /// <exception cref="System.ArgumentNullException">The schema is null.</exception>
 public SeparatedValueWriter(string fileName, SeparatedValueSchema schema, SeparatedValueOptions options)
     : this(File.OpenWrite(fileName), schema, options, true)
 {
 }
 private SeparatedValueReader(Stream stream, SeparatedValueSchema schema, SeparatedValueOptions options, bool hasSchema, bool ownsStream)
 {
     if (stream == null)
     {
         throw new ArgumentNullException("stream");
     }
     if (hasSchema && schema == null)
     {
         throw new ArgumentNullException("schema");
     }
     if (options == null)
     {
         throw new ArgumentNullException("options");
     }
     reader = new RecordReader(stream, options.Encoding, options.RecordSeparator, ownsStream);
     regex = buildRegex(options.Separator);
     if (hasSchema)
     {
         if (options.IsFirstRecordSchema)
         {
             readNextRecord();  // skip header record
         }
         this.schema = schema;
     }
     else if (options.IsFirstRecordSchema)
     {
         string[] columnNames = readNextRecord();
         this.schema = new SeparatedValueSchema();
         foreach (string columnName in columnNames)
         {
             StringColumn column = new StringColumn(columnName);
             this.schema.AddColumn(column);
         }
     }
 }
Example #38
0
 /// <summary>
 /// Initializes a new instance of a SeparatedValueWriter.
 /// </summary>
 /// <param name="stream">The stream to write the output to.</param>
 /// <param name="schema">The schema to use to build the output.</param>
 /// <exception cref="System.ArgumentNullException">The schema is null.</exception>
 /// <exception cref="System.ArgumentNullException">The options is null.</exception>
 public SeparatedValueWriter(Stream stream, SeparatedValueSchema schema)
     : this(stream, schema, new SeparatedValueOptions(), false)
 {
 }
Example #39
0
 /// <summary>
 /// Initializes a new instance of a SeparatedValueWriter.
 /// </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>
 /// <exception cref="System.ArgumentNullException">The schema is null.</exception>
 /// <exception cref="System.ArgumentNullException">The options is null.</exception>
 public SeparatedValueWriter(Stream stream, SeparatedValueSchema schema, SeparatedValueOptions options)
     : this(stream, schema, options, false)
 {
 }
 /// <summary>
 /// Initializes a new instance of a SeparatedValueParser.
 /// </summary>
 /// <param name="fileName">The path of the file containing the records to extract.</param>
 /// <param name="schema">The predefined schema for the records.</param>
 /// <param name="options">The options for configuring the parser's behavior.</param>
 /// <exception cref="System.ArgumentNullException">The schema is null.</exception>
 /// <exception cref="System.ArgumentNullException">The options object is null.</exception>
 public SeparatedValueReader(string fileName, SeparatedValueSchema schema, SeparatedValueOptions options)
     : this(File.OpenRead(fileName), schema, options, true, true)
 {
 }
 /// <summary>
 /// Initializes a new SeparatedValueWriter with the given schema.
 /// </summary>
 /// <param name="writer">A writer over the separated value document.</param>
 /// <param name="schema">The schema of the separated value 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 SeparatedValueWriter(TextWriter writer, SeparatedValueSchema schema, SeparatedValueOptions options = null)
     : this(writer, schema, options, true)
 {
 }