/// <summary>Initializes the converter with an attribute</summary>
        public override void Initialize(CsvConverterAttribute attribute,
                                        IDefaultTypeConverterFactory defaultFactory)
        {
            base.Initialize(attribute, defaultFactory);

            if (!(attribute is CsvConverterNumberAttribute oneAttribute))
            {
                throw new CsvConverterAttributeException(
                          $"Please use the {nameof(CsvConverterNumberAttribute)} " +
                          $"attribute with the {nameof(CsvConverterPercentage)} converter.");
            }

            if (string.IsNullOrWhiteSpace(oneAttribute.StringFormat))
            {
                int precession = oneAttribute.NumberOfDecimalPlaces - 2;
                if (precession < 0)
                {
                    precession = 0;
                }
                oneAttribute.StringFormat = $"P{precession}";
            }

            _decimalConverter = defaultFactory.CreateConverter(typeof(decimal));
            _decimalConverter.Initialize(oneAttribute, defaultFactory);
        }
Exemplo n.º 2
0
 /// <summary>Constructor.</summary>
 /// <param name="configuration">Configuration information</param>
 /// <param name="defaultFactory">The default type converter factory, which is passed into any
 /// attributes found during the mapping process so that they can create a default converter if necessary.</param>
 /// <param name="columnIndexDefaultValue">Default value for a column index</param>
 public ColumnToPropertyMapper(CsvConverterConfiguration configuration, IDefaultTypeConverterFactory defaultFactory,
                               int columnIndexDefaultValue)
 {
     _columnIndexDefaultValue = columnIndexDefaultValue;
     _configuration           = configuration;
     _defaultFactory          = defaultFactory;
 }
        private void CreateConverter(Type inputType,
                                     CsvConverterNumberAttribute oneAttribute,
                                     IDefaultTypeConverterFactory defaultFactory)
        {
            ICsvConverter converter = defaultFactory.CreateConverter(inputType);

            converter.Initialize(oneAttribute, defaultFactory);
            _converterDictionary.Add(inputType, converter);
        }
Exemplo n.º 4
0
        /// <summary>Initializes the converter with an attribute</summary>
        public override void Initialize(CsvConverterAttribute attribute,
                                        IDefaultTypeConverterFactory defaultFactory)
        {
            base.Initialize(attribute, defaultFactory);

            if (attribute is CsvConverterStringTrimAttribute oneAttribute)
            {
                TrimAction = oneAttribute.TrimAction;
            }
        }
Exemplo n.º 5
0
 /// <summary>Initializes the converter with an attribute</summary>
 public override void Initialize(CsvConverterAttribute attribute, IDefaultTypeConverterFactory defaultFactory)
 {
     base.Initialize(attribute, defaultFactory);
     if (attribute != null)
     {
         if (attribute is CsvConverterNumberAttribute settings)
         {
             StringFormat = settings.StringFormat;
         }
     }
 }
 /// <summary>Initializes the converter with an attribute</summary>
 public override void Initialize(CsvConverterAttribute attribute, IDefaultTypeConverterFactory defaultFactory)
 {
     base.Initialize(attribute, defaultFactory);
     if (attribute != null)
     {
         if (attribute is CsvConverterBooleanAttribute settings)
         {
             TrueValue  = settings.TrueValue;
             FalseValue = settings.FalseValue;
         }
     }
 }
Exemplo n.º 7
0
 /// <summary>Initializes the converter with an attribute</summary>
 public override void Initialize(CsvConverterAttribute attribute, IDefaultTypeConverterFactory defaultFactory)
 {
     base.Initialize(attribute, defaultFactory);
     if (attribute != null)
     {
         if (attribute is CsvConverterDateTimeAttribute settings)
         {
             DateFormat = settings.StringFormat;
             DateStyle  = settings.DateStyle;
         }
     }
 }
Exemplo n.º 8
0
        public override void Initialize(CsvConverterAttribute attribute, IDefaultTypeConverterFactory defaultFactory)
        {
            var myAttribute = attribute as CsvConverterTextLengthEnforcerAttribute;

            if (myAttribute == null)
            {
                throw new ArgumentException($"Please use the {nameof(CsvConverterTextLengthEnforcerAttribute)} attribute with this pre-converter!");
            }

            _maximumLength = myAttribute.MaximumLength;
            _minimumLength = myAttribute.MinimumLength;
            _characterToAddToShortStrings = myAttribute.CharacterToAddToShortStrings;
        }
        /// <summary>Initializes the converter with an attribute</summary>
        public override void Initialize(CsvConverterAttribute attribute, IDefaultTypeConverterFactory defaultFactory)
        {
            base.Initialize(attribute, defaultFactory);

            if (!(attribute is CsvConverterStringAttribute oneAttribute))
            {
                throw new CsvConverterAttributeException(
                          $"All string converters should be used with attributes that derive from the " +
                          $"{nameof(CsvConverterStringAttribute)}!");
            }

            Order = oneAttribute.Order;
        }
 /// <summary>Initializes the converter with an attribute</summary>
 public override void Initialize(CsvConverterAttribute attribute, IDefaultTypeConverterFactory defaultFactory)
 {
     base.Initialize(attribute, defaultFactory);
     if (attribute != null)
     {
         if (attribute is CsvConverterNumberAttribute settings)
         {
             StringFormat          = settings.StringFormat;
             AllowRounding         = settings.AllowRounding;
             NumberOfDecimalPlaces = settings.NumberOfDecimalPlaces;
             Mode = settings.Mode;
         }
     }
 }
Exemplo n.º 11
0
        /// <summary>Initializes the converter with an attribute</summary>
        public override void Initialize(CsvConverterAttribute attribute,
                                        IDefaultTypeConverterFactory defaultFactory)
        {
            base.Initialize(attribute, defaultFactory);

            if (!(attribute is CsvConverterStringOldAndNewAttribute oneAttribute))
            {
                throw new CsvConverterAttributeException(
                          $"Please use the {nameof(CsvConverterStringOldAndNewAttribute)} " +
                          $"attribute with the {nameof(CsvConverterStringReplaceTextExactMatch)} converter.");
            }

            _newValue = oneAttribute.NewValue;
        }
Exemplo n.º 12
0
        public override void Initialize(CsvConverterAttribute attribute,
                                        IDefaultTypeConverterFactory defaultFactory)
        {
            base.Initialize(attribute, defaultFactory);

            if (!(attribute is CsvConverterNumericRangeAttribute oneAttribute))
            {
                throw new ArgumentException($"Please use the {nameof(CsvConverterNumericRangeAttribute)} attribute with this converter!");
            }

            _intConverter = defaultFactory.CreateConverter(typeof(int));
            _intConverter.Initialize(oneAttribute, defaultFactory);

            _minimum = oneAttribute.Minimum;
            _maximum = oneAttribute.Maximum;
        }
Exemplo n.º 13
0
        public override void Initialize(CsvConverterAttribute attribute,
                                        IDefaultTypeConverterFactory defaultFactory)
        {
            base.Initialize(attribute, defaultFactory);
            if (!(attribute is CsvConverterNumberAttribute oneAttribute))
            {
                throw new CsvConverterAttributeException(
                          $"Please use the {nameof(CsvConverterNumberAttribute)} " +
                          $"attribute with the {nameof(CsvConverterMoney)} converter.");
            }

            CreateConverter(typeof(decimal), oneAttribute, defaultFactory);
            CreateConverter(typeof(decimal?), oneAttribute, defaultFactory);
            CreateConverter(typeof(double), oneAttribute, defaultFactory);
            CreateConverter(typeof(double?), oneAttribute, defaultFactory);
        }
        /// <summary>Creates a converter for a property on a class based on the type specified by ConverterType OR ou can
        /// dynamically create a converter based on the property information passed into the method.</summary>
        /// <param name="theClassType">The class were the property resides so that it can be named to help the user
        /// find the particular property in question if they have more than class decorated with converter attributes.</param>
        /// <param name="propInfo">Property information about the property that this attribute was on.</param>
        /// <param name="defaultFactory">The default type converter factory.</param>
        /// <returns>A converter or null if one is not specified.</returns>
        public virtual ICsvConverter CreateConverterForProperty(Type theClassType, PropertyInfo propInfo,
                                                                IDefaultTypeConverterFactory defaultFactory)
        {
            // If no converter type was specified by the user, use a default converter based on the property's type.
            if (this.ConverterType == null)
            {
                ConverterType = defaultFactory.FindConverterType(propInfo.PropertyType);
            }

            string errorMessage = GetErrorMessageForCreateConverterForProperty(theClassType, propInfo);

            var oneTypeConverter = ConverterType.HelpCreateAndCastToInterface <ICsvConverter>(errorMessage);

            oneTypeConverter.Initialize(this, defaultFactory);

            return(oneTypeConverter);
        }
Exemplo n.º 15
0
        /// <summary>Initializes the converter with an attribute</summary>
        public override void Initialize(CsvConverterAttribute attribute,
                                        IDefaultTypeConverterFactory defaultFactory)
        {
            base.Initialize(attribute, defaultFactory);

            if (!(attribute is CsvConverterStringOldAndNewAttribute oneAttribute))
            {
                throw new CsvConverterAttributeException(
                          $"Please use the {nameof(CsvConverterStringOldAndNewAttribute)} " +
                          $"attribute with the {nameof(CsvConverterStringReplaceTextExactMatch)} converter.");
            }

            _newValue = oneAttribute.NewValue;
            _oldValue = oneAttribute.OldValue;
            _oldValueCannotBeProcessByStringReplace = _oldValue == null || _oldValue.Length == 0;
            _regexOptions = oneAttribute.IsCaseSensitive ? RegexOptions.None : RegexOptions.IgnoreCase;
        }
        /// <summary>Creates a converter for a property on a class based on the type specified by ConverterType OR you can
        /// dynamically create a converter based on the property information passed into the method.</summary>
        /// <param name="theClassType">The class were the property resides so that it can be named to help the user
        /// find the particular property in question if they have more than class decorated with converter attributes.</param>
        /// <param name="defaultFactory">The default type converter factory.</param>
        /// <returns>A converter</returns>
        public virtual ICsvConverter CreateConverterForClass(Type theClassType, IDefaultTypeConverterFactory defaultFactory)
        {
            // When decorating a class with this attribute, you MUST specify a target property type!
            if (TargetPropertyType == null)
            {
                throw new ArgumentException($"The {theClassType.Name} class specified an attribute that inherits from " +
                                            $" {nameof(CsvConverterAttribute)}, but a {nameof(TargetPropertyType)} was NOT specified.");
            }

            if (IsColumnIndexSpecified())
            {
                throw new CsvConverterAttributeException($"The {theClassType.Name} class has a class level attribute " +
                                                         $"that inherits {nameof(CsvConverterAttribute)} that is specifying a ColumnIndex.  You can only " +
                                                         $"specify ColumnIndex if the attribute is on the property!");
            }

            if (AreAltColumnNamesSpecified())
            {
                throw new CsvConverterAttributeException($"The {theClassType.Name} class has a class level attribute " +
                                                         $"that inherits {nameof(CsvConverterAttribute)} that is specifying a AltColumnNames.  You can only " +
                                                         $"specify AltColumnNames if the attribute is on the property!");
            }

            if (IsColumnNameSpecified())
            {
                throw new CsvConverterAttributeException($"The {theClassType.Name} class has a class level attribute " +
                                                         $"that inherits {nameof(CsvConverterAttribute)} that is specifying a ColumnName.  You can only " +
                                                         $"specify ColumnName if the attribute is on the property!");
            }


            // If no converter type was specified by the user, use a default converter base on the target property type.
            if (ConverterType == null)
            {
                ConverterType = defaultFactory.FindConverterType(this.TargetPropertyType);
            }

            string errorMessage = GetErrorMessageForCreateConverterForClass(theClassType);

            var oneTypeConverter = ConverterType.HelpCreateAndCastToInterface <ICsvConverter>(errorMessage);

            oneTypeConverter.Initialize(this, defaultFactory);

            return(oneTypeConverter);
        }
        /// <summary>Initializes the converter with an attribute</summary>
        public override void Initialize(CsvConverterAttribute attribute, IDefaultTypeConverterFactory defaultFactory)
        {
            base.Initialize(attribute, defaultFactory);

            if (!(attribute is CsvConverterNumberAttribute oneAttribute))
            {
                throw new CsvConverterAttributeException(
                          $"Please use the {nameof(CsvConverterNumberAttribute)} " +
                          $"attribute with the {nameof(CsvConverterDecimalToInt)} converter.");
            }

            _oneAttribute          = oneAttribute;
            _writeDecimalConverter = defaultFactory.CreateConverter(typeof(decimal));
            _writeDecimalConverter.Initialize(oneAttribute, defaultFactory);

            oneAttribute.NumberOfDecimalPlaces = 0;
            _readDecimalConverter = defaultFactory.CreateConverter(typeof(decimal));
            _readDecimalConverter.Initialize(oneAttribute, defaultFactory);
        }
        /// <summary>Creates a converter for a property on a class based on the type specified by ConverterType OR ou can
        /// dynamically create a converter based on the property information passed into the method.</summary>
        /// <param name="theClassType">The class were the property resides so that it can be named to help the user
        /// find the particular property in question if they have more than class decorated with converter attributes.</param>
        /// <param name="propInfo">Property information about the property that this attribute was on.</param>
        /// <param name="defaultFactory">The default type converter factory.</param>
        /// <returns>A converter or null if one is not specified.</returns>
        public override ICsvConverter CreateConverterForProperty(Type theClassType, PropertyInfo propInfo,
                                                                 IDefaultTypeConverterFactory defaultFactory)
        {
            bool isPreOrPostConverter = IsPreConverter || IsPostConverter;

            if (isPreOrPostConverter && IsColumnIndexSpecified())
            {
                ThrowDoNotUseAttributePropertyException(theClassType, propInfo, "ColumnIndex");
            }

            if (isPreOrPostConverter && AreAltColumnNamesSpecified())
            {
                ThrowDoNotUseAttributePropertyException(theClassType, propInfo, "AltColumnNames");
            }

            if (isPreOrPostConverter && IsColumnNameSpecified())
            {
                ThrowDoNotUseAttributePropertyException(theClassType, propInfo, "ColumnName");
            }

            return(base.CreateConverterForProperty(theClassType, propInfo, defaultFactory));
        }
Exemplo n.º 19
0
 /// <summary>Initializes the converter with an attribute</summary>
 public virtual void Initialize(CsvConverterAttribute attribute, IDefaultTypeConverterFactory defaultFactory)
 {
 }