コード例 #1
0
ファイル: DateTimeColumn.cs プロジェクト: hbdbim/FlatFiles
        /// <summary>
        /// Parses the given value and returns a DateTime instance.
        /// </summary>
        /// <param name="context">Holds information about the column current being processed.</param>
        /// <param name="value">The value to parse.</param>
        /// <returns>The parsed DateTime instance.</returns>
        protected override DateTime OnParse(IColumnContext context, string value)
        {
            var provider = FormatProvider ?? CultureInfo.CurrentCulture;

            if (InputFormat == null)
            {
                return(DateTime.Parse(value, provider));
            }
            return(DateTime.ParseExact(value, InputFormat, provider));
        }
コード例 #2
0
ファイル: Int32Column.cs プロジェクト: xmxth001/FlatFiles
        /// <summary>
        /// Formats the given object.
        /// </summary>
        /// <param name="context">Holds information about the column current being processed.</param>
        /// <param name="value">The object to format.</param>
        /// <returns>The formatted value.</returns>
        protected override string OnFormat(IColumnContext context, int value)
        {
            var provider = GetFormatProvider(context, FormatProvider);

            if (OutputFormat == null)
            {
                return(value.ToString(provider));
            }
            return(value.ToString(OutputFormat, provider));
        }
コード例 #3
0
        /// <inheritdoc />
        protected override DateTimeOffset OnParse(IColumnContext context, string value)
        {
            var provider = GetFormatProvider(context, FormatProvider);

            if (InputFormat == null)
            {
                return(DateTimeOffset.Parse(value, provider));
            }
            return(DateTimeOffset.ParseExact(value, InputFormat, provider));
        }
コード例 #4
0
        /// <summary>
        /// Extracts a single record from the embedded data.
        /// </summary>
        /// <param name="context">Holds information about the column current being processed.</param>
        /// <param name="value">The value containing the embedded data.</param>
        /// <returns>
        /// An object array containing the values read from the embedded data -or- null if there is no embedded data.
        /// </returns>
        protected override object[] OnParse(IColumnContext context, string value)
        {
            var stringReader = new StringReader(value);
            var reader       = GetReader(stringReader);

            if (reader.Read())
            {
                return(reader.GetValues());
            }
            return(null);
        }
コード例 #5
0
        /// <summary>
        /// Provides a textual representation for the value.
        /// </summary>
        /// <param name="context">Holds information about the column current being processed.</param>
        /// <returns>The formatted value.</returns>
        protected override string OnFormat(IColumnContext context)
        {
            var recordNumber = GetRecordNumber(context);
            var provider     = GetFormatProvider(context, FormatProvider);

            if (OutputFormat == null)
            {
                return(recordNumber.ToString(provider));
            }
            return(recordNumber.ToString(OutputFormat, provider));
        }
コード例 #6
0
        /// <summary>
        /// Formats the given object.
        /// </summary>
        /// <param name="context">Holds information about the column current being processed.</param>
        /// <param name="value">The object to format.</param>
        /// <returns>The formatted value.</returns>
        public override string Format(IColumnContext context, object value)
        {
            if (value == null)
            {
                return(NullHandler.GetNullRepresentation());
            }
            byte[]   actual         = (byte[])value;
            Encoding actualEncoding = Encoding ?? new UTF8Encoding(false);

            return(actualEncoding.GetString(actual));
        }
コード例 #7
0
        /// <summary>
        /// Extracts a single record from the embedded data.
        /// </summary>
        /// <param name="context">Holds information about the column current being processed.</param>
        /// <param name="value">The value containing the embedded data.</param>
        /// <returns>
        /// An object array containing the values read from the embedded data -or- null if there is no embedded data.
        /// </returns>
        protected override object[] OnParse(IColumnContext context, string value)
        {
            var stringReader = new StringReader(value);
            var reader       = new FixedLengthReader(stringReader, schema, Options);

            if (reader.Read())
            {
                return(reader.GetValues());
            }
            return(null);
        }
コード例 #8
0
            protected override GeoLocation OnParse(IColumnContext context, string value)
            {
                string[] parts  = value.Substring(1, value.Length - 2).Split(',', 2);
                var      result = new GeoLocation()
                {
                    Latitude  = Convert.ToDecimal(parts[0].Trim()),
                    Longitude = Convert.ToDecimal(parts[1].Trim())
                };

                return(result);
            }
コード例 #9
0
ファイル: BooleanColumn.cs プロジェクト: xmxth001/FlatFiles
 /// <summary>
 /// Parses the given value into its equivilent boolean value.
 /// </summary>
 /// <param name="context">Holds information about the column current being processed.</param>
 /// <param name="value">The value to parse.</param>
 /// <returns>True if the value equals the TrueString; otherwise, false.</returns>
 protected override bool OnParse(IColumnContext context, string value)
 {
     if (String.Equals(value, TrueString, StringComparison.CurrentCultureIgnoreCase))
     {
         return(true);
     }
     if (String.Equals(value, FalseString, StringComparison.CurrentCultureIgnoreCase))
     {
         return(false);
     }
     throw new InvalidCastException();
 }
コード例 #10
0
        /// <inheritdoc />
        public override object Parse(IColumnContext context, string value)
        {
            var sourceValue = columnDefinition.Parse(context, value);

            if (sourceValue == null)
            {
                return(null);
            }
            var destinationValue = parser((TSource)sourceValue);

            return(destinationValue);
        }
コード例 #11
0
 /// <summary>
 /// Parses the given value as a char array.
 /// </summary>
 /// <param name="context">Holds information about the column current being processed.</param>
 /// <param name="value">The value to parse.</param>
 /// <returns>The parsed char array.</returns>
 public override object Parse(IColumnContext context, string value)
 {
     if (Preprocessor != null)
     {
         value = Preprocessor(value);
     }
     if (NullHandler.IsNullRepresentation(value))
     {
         return(null);
     }
     value = TrimValue(value);
     return(value.ToCharArray());
 }
コード例 #12
0
        /// <summary>
        /// Formats the given object.
        /// </summary>
        /// <param name="context">Holds information about the column current being processed.</param>
        /// <param name="value">The object to format.</param>
        /// <returns>The formatted value.</returns>
        public override string Format(IColumnContext context, object value)
        {
            if (value == null)
            {
                return(NullHandler.GetNullRepresentation());
            }

            ushort actual = (ushort)value;

            return(OutputFormat == null
                ? actual.ToString(FormatProvider ?? CultureInfo.CurrentCulture)
                : actual.ToString(OutputFormat, FormatProvider ?? CultureInfo.CurrentCulture));
        }
コード例 #13
0
ファイル: IgnoredColumn.cs プロジェクト: xmxth001/FlatFiles
        /// <summary>
        /// Returns null so nothing is written to the document.
        /// </summary>
        /// <param name="context">Holds information about the column current being processed.</param>
        /// <param name="value">The value that needs written to the document.</param>
        /// <returns>A null.</returns>
        public override string Format(IColumnContext context, object value)
        {
            if (OnFormatting != null)
            {
                value = OnFormatting(context, value);
            }
            string result = NullFormatter.FormatNull(context);

            if (OnFormatted != null)
            {
                result = OnFormatted(context, result);
            }
            return(result);
        }
コード例 #14
0
        /// <summary>
        /// Parses the given value into a byte.
        /// </summary>
        /// <param name="context">Holds information about the column current being processed.</param>
        /// <param name="value">The value to parse.</param>
        /// <returns>The parsed byte value.</returns>
        public override object Parse(IColumnContext context, string value)
        {
            if (Preprocessor != null)
            {
                value = Preprocessor(value);
            }
            if (NullHandler.IsNullRepresentation(value))
            {
                return(null);
            }
            IFormatProvider provider = FormatProvider ?? CultureInfo.CurrentCulture;

            return(Byte.Parse(value, NumberStyles, provider));
        }
コード例 #15
0
        /// <summary>
        /// Formats the given object array into an embedded record.
        /// </summary>
        /// <param name="context">Holds information about the column current being processed.</param>
        /// <param name="value">The object array containing the values of the embedded record.</param>
        /// <returns>A formatted string containing the embedded data.</returns>
        public override string Format(IColumnContext context, object value)
        {
            var values = value as object[];

            if (values == null)
            {
                return(NullHandler.GetNullRepresentation());
            }
            var writer       = new StringWriter();
            var recordWriter = GetWriter(writer);

            recordWriter.WriteRecord(values);
            return(writer.ToString());
        }
コード例 #16
0
 private static string Format(IColumnContext columnContext, IColumnDefinition definition, int position, object value)
 {
     try
     {
         return(definition.Format(columnContext, value));
     }
     catch (Exception exception) when(columnContext == null)
     {
         throw new ColumnProcessingException(definition, position, value, exception);
     }
     catch (Exception exception)
     {
         throw new ColumnProcessingException(columnContext, value, exception);
     }
 }
コード例 #17
0
        /// <summary>
        /// Parses the given value as a byte array.
        /// </summary>
        /// <param name="context">Holds information about the column current being processed.</param>
        /// <param name="value">The value to parse.</param>
        /// <returns>The parsed byte array.</returns>
        public override object Parse(IColumnContext context, string value)
        {
            if (Preprocessor != null)
            {
                value = Preprocessor(value);
            }
            if (NullHandler.IsNullRepresentation(value))
            {
                return(null);
            }
            value = TrimValue(value);
            Encoding actualEncoding = Encoding ?? new UTF8Encoding(false);

            return(actualEncoding.GetBytes(value));
        }
コード例 #18
0
            public bool IsNullValue(IColumnContext context, string value)
            {
                if (_NullValues != null && _NullValues.Length > 0)
                {
                    for (int i = 0; i < _NullValues.Length; i++)
                    {
                        if (string.Compare(_NullValues[i], value, _StringComparison) == 0)
                        {
                            return(true);
                        }
                    }
                }

                return(false);
            }
コード例 #19
0
 private object Parse(IColumnContext columnContext, IColumnDefinition definition, int position, string rawValue)
 {
     try
     {
         object parsedValue = definition.Parse(columnContext, rawValue);
         return(parsedValue);
     }
     catch (Exception exception) when(columnContext == null)
     {
         throw new ColumnProcessingException(definition, position, rawValue, exception);
     }
     catch (Exception exception)
     {
         throw new ColumnProcessingException(columnContext, rawValue, exception);
     }
 }
コード例 #20
0
        /// <summary>
        /// Formats the given object.
        /// </summary>
        /// <param name="context">Holds information about the column current being processed.</param>
        /// <param name="value">The object to format.</param>
        /// <returns>The formatted value.</returns>
        public override string Format(IColumnContext context, object value)
        {
            if (value == null)
            {
                return(NullHandler.GetNullRepresentation());
            }

            Guid actual = (Guid)value;

            if (OutputFormat == null)
            {
                return(actual.ToString());
            }

            return(actual.ToString(OutputFormat));
        }
コード例 #21
0
        /// <summary>
        /// Parses the given value and returns a Guid instance.
        /// </summary>
        /// <param name="context">Holds information about the column current being processed.</param>
        /// <param name="value">The value to parse.</param>
        /// <returns>The parsed Guid.</returns>
        public override object Parse(IColumnContext context, string value)
        {
            if (Preprocessor != null)
            {
                value = Preprocessor(value);
            }
            if (NullHandler.IsNullRepresentation(value))
            {
                return(null);
            }
            if (InputFormat == null)
            {
                return(Guid.Parse(value));
            }

            return(Guid.ParseExact(value, InputFormat));
        }
コード例 #22
0
        /// <summary>
        /// Parses the given value as a char.
        /// </summary>
        /// <param name="context">Holds information about the column current being processed.</param>
        /// <param name="value">The value to parse.</param>
        /// <returns>The parsed char.</returns>
        public override object Parse(IColumnContext context, string value)
        {
            if (Preprocessor != null)
            {
                value = Preprocessor(value);
            }
            if (NullHandler.IsNullRepresentation(value))
            {
                return(null);
            }
            value = TrimValue(value);
            if (AllowTrailing || value.Length == 1)
            {
                return(value[0]);
            }

            throw new InvalidCastException();
        }
コード例 #23
0
        private int GetRecordNumber(IColumnContext context)
        {
            if (IncludeSkippedRecords)
            {
                int recordCount = context.RecordContext.PhysicalRecordNumber;
                if (context.RecordContext.ExecutionContext.Options.IsFirstRecordSchema && !IncludeSchema)
                {
                    --recordCount;
                }
                return(recordCount);
            }

            // We only incrememnt the logical count after we are sure the record is not filtered out.
            // Since the value for the column is generated beforehand, we must increase it by one.
            int offset = (IncludeSchema && context.RecordContext.ExecutionContext.Options.IsFirstRecordSchema) ? 2 : 1;

            return(context.RecordContext.LogicalRecordNumber + offset);
        }
コード例 #24
0
        /// <summary>
        /// Parses the given value and returns a DateTime instance.
        /// </summary>
        /// <param name="context">Holds information about the column current being processed.</param>
        /// <param name="value">The value to parse.</param>
        /// <returns>The parsed DateTime instance.</returns>
        public override object Parse(IColumnContext context, string value)
        {
            if (Preprocessor != null)
            {
                value = Preprocessor(value);
            }
            if (NullHandler.IsNullRepresentation(value))
            {
                return(null);
            }
            IFormatProvider provider = FormatProvider ?? CultureInfo.CurrentCulture;

            if (InputFormat == null)
            {
                return(DateTime.Parse(value, provider));
            }

            return(DateTime.ParseExact(value, InputFormat, provider));
        }
コード例 #25
0
ファイル: IgnoredColumn.cs プロジェクト: xmxth001/FlatFiles
        /// <summary>
        /// Ignores the values that was parsed from the document.
        /// </summary>
        /// <param name="context">Holds information about the column current being processed.</param>
        /// <param name="value">The value that was parsed from the document.</param>
        /// <returns>A null.</returns>
        public override object Parse(IColumnContext context, string value)
        {
#pragma warning disable CS0618 // Type or member is obsolete
            if (Preprocessor != null)
            {
                value = Preprocessor(value);
            }
#pragma warning restore CS0618 // Type or member is obsolete
            if (OnParsing != null)
            {
                value = OnParsing(context, value);
            }
            object result = null;
            if (OnParsed != null)
            {
                result = OnParsed(context, result);
            }
            return(result);
        }
コード例 #26
0
        /// <summary>
        /// Extracts a single record from the embedded data.
        /// </summary>
        /// <param name="context">Holds information about the column current being processed.</param>
        /// <param name="value">The value containing the embedded data.</param>
        /// <returns>
        /// An object array containing the values read from the embedded data -or- null if there is no embedded data.
        /// </returns>
        public override object Parse(IColumnContext context, string value)
        {
            if (Preprocessor != null)
            {
                value = Preprocessor(value);
            }
            if (NullHandler.IsNullRepresentation(value))
            {
                return(null);
            }

            var stringReader = new StringReader(value);
            var reader       = GetReader(stringReader);

            if (reader.Read())
            {
                return(reader.GetValues());
            }
            return(null);
        }
コード例 #27
0
 /// <summary>
 /// Parses the given value into its equivilent boolean value.
 /// </summary>
 /// <param name="context">Holds information about the column current being processed.</param>
 /// <param name="value">The value to parse.</param>
 /// <returns>True if the value equals the TrueString; otherwise, false.</returns>
 public override object Parse(IColumnContext context, string value)
 {
     if (Preprocessor != null)
     {
         value = Preprocessor(value);
     }
     if (NullHandler.IsNullRepresentation(value))
     {
         return(null);
     }
     value = TrimValue(value);
     if (String.Equals(value, TrueString, StringComparison.CurrentCultureIgnoreCase))
     {
         return(true);
     }
     if (String.Equals(value, FalseString, StringComparison.CurrentCultureIgnoreCase))
     {
         return(false);
     }
     throw new InvalidCastException();
 }
コード例 #28
0
ファイル: Schema.cs プロジェクト: vandenbergjp/FlatFiles
 private string FormatWithContext(IColumnContext columnContext, object value)
 {
     try
     {
         var definition = columnContext.ColumnDefinition;
         return(definition.Format(columnContext, value));
     }
     catch (Exception exception)
     {
         var columnException = new ColumnProcessingException(columnContext, value, exception);
         if (columnContext.RecordContext is IRecoverableRecordContext recordContext && recordContext.HasHandler)
         {
             var e = new ColumnErrorEventArgs(columnException);
             recordContext.ProcessError(this, e);
             if (e.IsHandled)
             {
                 return((string)e.Substitution);
             }
         }
         throw columnException;
     }
 }
コード例 #29
0
ファイル: Schema.cs プロジェクト: vandenbergjp/FlatFiles
 private object ParseWithContext(IColumnContext columnContext, string rawValue)
 {
     try
     {
         var    definition  = columnContext.ColumnDefinition;
         object parsedValue = definition.Parse(columnContext, rawValue);
         return(parsedValue);
     }
     catch (Exception exception)
     {
         var columnException = new ColumnProcessingException(columnContext, rawValue, exception);
         if (columnContext.RecordContext is IRecoverableRecordContext recordContext && recordContext.HasHandler)
         {
             var e = new ColumnErrorEventArgs(columnException);
             recordContext.ProcessError(this, e);
             if (e.IsHandled)
             {
                 return(e.Substitution);
             }
         }
         throw columnException;
     }
 }
コード例 #30
0
ファイル: DoubleColumn.cs プロジェクト: xmxth001/FlatFiles
        /// <summary>
        /// Parses the given value, returning a Double.
        /// </summary>
        /// <param name="context">Holds information about the column current being processed.</param>
        /// <param name="value">The value to parse.</param>
        /// <returns>The parsed Double.</returns>
        protected override double OnParse(IColumnContext context, string value)
        {
            var provider = GetFormatProvider(context, FormatProvider);

            return(Double.Parse(value, NumberStyles, provider));
        }