/// <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)); }
/// <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)); }
/// <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)); }
/// <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); }
/// <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)); }
/// <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)); }
/// <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); }
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); }
/// <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(); }
/// <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); }
/// <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()); }
/// <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)); }
/// <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); }
/// <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)); }
/// <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()); }
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); } }
/// <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)); }
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); }
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); } }
/// <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)); }
/// <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)); }
/// <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(); }
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); }
/// <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)); }
/// <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); }
/// <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); }
/// <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(); }
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; } }
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; } }
/// <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)); }