Example #1
0
        /// <summary>
        /// Create field object after extracting the string from the underlying
        /// input data
        /// </summary>
        /// <param name="fieldString">Information extracted?</param>
        /// <param name="line">Underlying input data</param>
        /// <returns>Object to assign to field</returns>
        private AssignResult AssignFromString(ExtractedInfo fieldString, LineInfo line)
        {
            object val;

            var extractedString = fieldString.ExtractedString();

            try {
                if (this.Converter == null)
                {
                    if (IsStringField)
                    {
                        val = TrimString(extractedString);
                    }
                    else
                    {
                        extractedString = extractedString.Trim();

                        if (extractedString.Length == 0)
                        {
                            return(new AssignResult {
                                Value = GetNullValue(line),
                                NullValueUsed = true
                            });
                        }
                        else
                        {
                            val = Convert.ChangeType(extractedString, FieldTypeInternal, null);
                        }
                    }
                }
                else
                {
                    var trimmedString = extractedString.Trim();

                    if (this.Converter.CustomNullHandling == false &&
                        trimmedString.Length == 0)
                    {
                        return(new AssignResult {
                            Value = GetNullValue(line),
                            NullValueUsed = true
                        });
                    }
                    else
                    {
                        if (TrimMode == TrimMode.Both)
                        {
                            val = this.Converter.StringToField(trimmedString);
                        }
                        else
                        {
                            val = this.Converter.StringToField(TrimString(extractedString));
                        }

                        if (val == null)
                        {
                            return(new AssignResult {
                                Value = GetNullValue(line),
                                NullValueUsed = true
                            });
                        }
                    }
                }

                return(new AssignResult {
                    Value = val
                });
            }
            catch (ConvertException ex) {
                ex.FieldName    = FieldInfo.Name;
                ex.LineNumber   = line.mReader.LineNumber;
                ex.ColumnNumber = fieldString.ExtractedFrom + 1;
                throw;
            }
            catch (BadUsageException) {
                throw;
            }
            catch (Exception ex) {
                if (this.Converter == null ||
                    this.Converter.GetType().Assembly == typeof(FieldBase).Assembly)
                {
                    throw new ConvertException(extractedString,
                                               FieldTypeInternal,
                                               FieldInfo.Name,
                                               line.mReader.LineNumber,
                                               fieldString.ExtractedFrom + 1,
                                               ex.Message,
                                               ex);
                }
                else
                {
                    throw new ConvertException(extractedString,
                                               FieldTypeInternal,
                                               FieldInfo.Name,
                                               line.mReader.LineNumber,
                                               fieldString.ExtractedFrom + 1,
                                               "Your custom converter: " + this.Converter.GetType().Name + " throws an " + ex.GetType().Name +
                                               " with the message: " + ex.Message,
                                               ex);
                }
            }
        }
Example #2
0
        /// <summary>
        /// Create field object after extracting the string from the underlying
        /// input data
        /// </summary>
        /// <param name="fieldString">Information extracted?</param>
        /// <param name="line">Underlying input data</param>
        /// <returns>Object to assign to field</returns>
        private AssignResult AssignFromString(ExtractedInfo fieldString, LineInfo line)
        {
            object val;

            var extractedString = fieldString.ExtractedString();

            try {
                if (IsNotEmpty && String.IsNullOrEmpty(extractedString)) {
                    throw new InvalidOperationException("The value is empty and must be populated.");
                } else if (this.Converter == null) {
                    if (IsStringField)
                        val = TrimString(extractedString);
                    else {
                        extractedString = extractedString.Trim();

                        if (extractedString.Length == 0) {
                            return new AssignResult {
                                Value = GetNullValue(line),
                                NullValueUsed = true
                            };
                        }
                        else
                            val = Convert.ChangeType(extractedString, FieldTypeInternal, null);
                    }
                }
                else {
                    var trimmedString = extractedString.Trim();

                    if (this.Converter.CustomNullHandling == false &&
                        trimmedString.Length == 0) {
                        return new AssignResult {
                            Value = GetNullValue(line),
                            NullValueUsed = true
                        };
                    }
                    else {
                        if (TrimMode == TrimMode.Both)
                            val = this.Converter.StringToField(trimmedString);
                        else
                            val = this.Converter.StringToField(TrimString(extractedString));

                        if (val == null) {
                            return new AssignResult {
                                Value = GetNullValue(line),
                                NullValueUsed = true
                            };
                        }
                    }
                }

                return new AssignResult {
                    Value = val
                };
            }
            catch (ConvertException ex) {
                ex.FieldName = FieldInfo.Name;
                ex.LineNumber = line.mReader.LineNumber;
                ex.ColumnNumber = fieldString.ExtractedFrom + 1;
                throw;
            }
            catch (BadUsageException) {
                throw;
            }
            catch (Exception ex) {
                if (this.Converter == null ||
                    this.Converter.GetType().Assembly == typeof (FieldBase).Assembly) {
                    throw new ConvertException(extractedString,
                        FieldTypeInternal,
                        FieldInfo.Name,
                        line.mReader.LineNumber,
                        fieldString.ExtractedFrom + 1,
                        ex.Message,
                        ex);
                }
                else {
                    throw new ConvertException(extractedString,
                        FieldTypeInternal,
                        FieldInfo.Name,
                        line.mReader.LineNumber,
                        fieldString.ExtractedFrom + 1,
                        "Your custom converter: " + this.Converter.GetType().Name + " throws an " + ex.GetType().Name +
                        " with the message: " + ex.Message,
                        ex);
                }
            }
        }
Example #3
0
        internal object AssignFromString(ExtractedInfo fieldString, LineInfo line)
        {
            object val;

            switch (mTrimMode)
            {
            case TrimMode.None:
                break;

            case TrimMode.Both:
                fieldString.TrimBoth(mTrimChars);
                break;

            case TrimMode.Left:
                fieldString.TrimStart(mTrimChars);
                break;

            case TrimMode.Right:
                fieldString.TrimEnd(mTrimChars);
                break;
            }

            try
            {
                if (mConvertProvider == null)
                {
                    if (mIsStringField)
                    {
                        val = fieldString.ExtractedString();
                    }
                    else
                    {
                        // Trim it to use Convert.ChangeType
                        fieldString.TrimBoth(WhitespaceChars);

                        if (fieldString.Length == 0)
                        {
                            // Empty stand for null
                            val = GetNullValue();
                        }
                        else
                        {
                            val = Convert.ChangeType(fieldString.ExtractedString(), mFieldType, null);
                        }
                    }
                }

                else
                {
                    if (mConvertProvider.CustomNullHandling == false &&
                        fieldString.HasOnlyThisChars(WhitespaceChars))
                    {
                        val = GetNullValue();
                    }
                    else
                    {
                        string from = fieldString.ExtractedString();
                        val = mConvertProvider.StringToField(from);

                        if (val == null)
                        {
                            val = GetNullValue();
                        }
                    }
                }

                return(val);
            }
            catch (ConvertException ex)
            {
                throw ConvertException.ReThrowException(ex, mFieldInfo.Name, line.mReader.LineNumber, fieldString.ExtractedFrom + 1);
            }
        }
Example #4
0
        internal object AssignFromString(ExtractedInfo fieldString, LineInfo line)
        {
            object val;

            switch (mTrimMode)
            {
                case TrimMode.None:
                    break;

                case TrimMode.Both:
                    fieldString.TrimBoth(mTrimChars);
                    break;

                case TrimMode.Left:
                    fieldString.TrimStart(mTrimChars);
                    break;

                case TrimMode.Right:
                    fieldString.TrimEnd(mTrimChars);
                    break;
            }
            
            try
            {

                if (mConvertProvider == null)
                {
                    if (mIsStringField)
                        val = fieldString.ExtractedString();
                    else
                    {
                        // Trim it to use Convert.ChangeType
                        fieldString.TrimBoth(WhitespaceChars);

                        if (fieldString.Length == 0)
                        {
                            // Empty stand for null
                            val = GetNullValue();
                        }
                        else
                        {
                            val = Convert.ChangeType(fieldString.ExtractedString(), mFieldType, null);
                        }
                    }
                }

                else
                {
                    if (mConvertProvider.CustomNullHandling == false &&
                        fieldString.HasOnlyThisChars(WhitespaceChars))
                    {
                        val = GetNullValue();
                    }
                    else
                    {
                        string from = fieldString.ExtractedString();
                        val = mConvertProvider.StringToField(from);

                        if (val == null)
                            val = GetNullValue();

                    }
                }

                return val;
            }
            catch (ConvertException ex)
            {
                throw ConvertException.ReThrowException(ex, mFieldInfo.Name, line.mReader.LineNumber, fieldString.ExtractedFrom + 1);
            }
        }
Example #5
0
        internal object AssignFromString(ExtractedInfo fieldString, LineInfo line)
        {
            object val;

            var extractedString = fieldString.ExtractedString();
            var trimmedBoth = false;
            switch (TrimMode)
            {
                case TrimMode.None:
                    break;

                case TrimMode.Both:
                    extractedString = extractedString.Trim();
                    trimmedBoth = true;
                    //fieldString.TrimBoth(TrimChars);
                    break;

                case TrimMode.Left:
                    extractedString = extractedString.TrimStart();
                    //fieldString.TrimStart(TrimChars);
                    break;

                case TrimMode.Right:
                    extractedString = extractedString.TrimEnd();
                    //fieldString.TrimEnd(TrimChars);
                    break;
            }

            try
            {
                if (ConvertProvider == null)
                {
                    if (IsStringField)
                        val = extractedString;
                    else
                    {
                        // Trim it to use Convert.ChangeType
                        if (trimmedBoth == false)
                            extractedString = extractedString.Trim();

                        if (extractedString.Length == 0)
                        {
                            // Empty stand for null
                            val = GetNullValue(line);
                        }
                        else
                        {
                            val = Convert.ChangeType(extractedString, FieldTypeInternal, null);
                        }
                    }
                }
                else
                {
                    var trimmedString = extractedString;
                    if (trimmedBoth == false)
                    {
                        trimmedString = extractedString.Trim();
                    }

                    if (ConvertProvider.CustomNullHandling == false &&
                        trimmedString.Length == 0)
                    {
                        val = GetNullValue(line);
                    }
                    else
                    {
                        string from = extractedString;
                        val = ConvertProvider.StringToField(from);

                        if (val == null)
                            val = GetNullValue(line);

                    }
                }

                return val;
            }
            catch (ConvertException ex)
            {
                ex.FieldName = FieldInfo.Name;
                ex.LineNumber = line.mReader.LineNumber;
                ex.ColumnNumber = fieldString.ExtractedFrom + 1;
                throw;
            }
            catch (BadUsageException)
            {
                throw;
            }
            catch (Exception ex)
            {
                if (ConvertProvider == null || ConvertProvider.GetType().Assembly == typeof(FieldBase).Assembly)
                    throw new ConvertException(extractedString, FieldTypeInternal, FieldInfo.Name, line.mReader.LineNumber, fieldString.ExtractedFrom + 1, ex.Message, ex);
                else
                    throw new ConvertException(extractedString, FieldTypeInternal, FieldInfo.Name, line.mReader.LineNumber, fieldString.ExtractedFrom + 1, "Your custom converter: " + ConvertProvider.GetType().Name + " throws an " + ex.GetType().Name + " with the message: " + ex.Message, ex);
            }
        }
Example #6
0
        internal object AssignFromString(ExtractedInfo fieldString, LineInfo line)
        {
            object val;

            switch (mTrimMode)
            {
            case TrimMode.None:
                break;

            case TrimMode.Both:
                fieldString.TrimBoth(mTrimChars);
                break;

            case TrimMode.Left:
                fieldString.TrimStart(mTrimChars);
                break;

            case TrimMode.Right:
                fieldString.TrimEnd(mTrimChars);
                break;
            }

            try
            {
                if (mConvertProvider == null)
                {
                    if (mIsStringField)
                    {
                        val = fieldString.ExtractedString();
                    }
                    else
                    {
                        // Trim it to use Convert.ChangeType
                        fieldString.TrimBoth(WhitespaceChars);

                        if (fieldString.Length == 0)
                        {
                            // Empty stand for null
                            val = GetNullValue(line);
                        }
                        else
                        {
                            val = Convert.ChangeType(fieldString.ExtractedString(), mFieldTypeInternal, null);
                        }
                    }
                }
                else
                {
                    if (mConvertProvider.CustomNullHandling == false &&
                        fieldString.HasOnlyThisChars(WhitespaceChars))
                    {
                        val = GetNullValue(line);
                    }
                    else
                    {
                        string from = fieldString.ExtractedString();
                        val = mConvertProvider.StringToField(from);

                        if (val == null)
                        {
                            val = GetNullValue(line);
                        }
                    }
                }

                return(val);
            }
            catch (ConvertException ex)
            {
                throw ConvertException.ReThrowException(ex, mFieldInfo.Name, line.mReader.LineNumber, fieldString.ExtractedFrom + 1);
            }
            catch (BadUsageException)
            {
                throw;
            }
            catch (Exception ex)
            {
                if (mConvertProvider == null || mConvertProvider.GetType().Assembly == typeof(FieldBase).Assembly)
                {
                    throw new ConvertException(fieldString.ExtractedString(), mFieldTypeInternal, mFieldInfo.Name, line.mReader.LineNumber, fieldString.ExtractedFrom + 1, ex.Message, ex);
                }
                else
                {
                    throw new ConvertException(fieldString.ExtractedString(), mFieldTypeInternal, mFieldInfo.Name, line.mReader.LineNumber, fieldString.ExtractedFrom + 1, "Your custom converter: " + mConvertProvider.GetType().Name + " throws an " + ex.GetType().Name + " with the message: " + ex.Message, ex);
                }
            }
        }
Example #7
0
        internal object AssignFromString(ExtractedInfo fieldString, LineInfo line)
        {
            object val;

            switch (mTrimMode)
            {
                case TrimMode.None:
                    break;

                case TrimMode.Both:
                    fieldString.TrimBoth(mTrimChars);
                    break;

                case TrimMode.Left:
                    fieldString.TrimStart(mTrimChars);
                    break;

                case TrimMode.Right:
                    fieldString.TrimEnd(mTrimChars);
                    break;
            }

            try
            {

                if (mConvertProvider == null)
                {
                    if (mIsStringField)
                        val = fieldString.ExtractedString();
                    else
                    {
                        // Trim it to use Convert.ChangeType
                        fieldString.TrimBoth(WhitespaceChars);

                        if (fieldString.Length == 0)
                        {
                            // Empty stand for null
                            val = GetNullValue(line);
                        }
                        else
                        {
                            val = Convert.ChangeType(fieldString.ExtractedString(), mFieldTypeInternal, null);
                        }
                    }
                }
                else
                {
                    if (mConvertProvider.CustomNullHandling == false &&
                        fieldString.HasOnlyThisChars(WhitespaceChars))
                    {
                        val = GetNullValue(line);
                    }
                    else
                    {
                        string from = fieldString.ExtractedString();
                        val = mConvertProvider.StringToField(from);

                        if (val == null)
                            val = GetNullValue(line);

                    }
                }

                return val;
            }
            catch (ConvertException ex)
            {
                throw ConvertException.ReThrowException(ex, mFieldInfo.Name, line.mReader.LineNumber, fieldString.ExtractedFrom + 1);
            }
            catch (BadUsageException)
            {
                throw;
            }
            catch (Exception ex)
            {
                if (mConvertProvider == null || mConvertProvider.GetType().Assembly == typeof(FieldBase).Assembly)
                    throw new ConvertException(fieldString.ExtractedString(), mFieldTypeInternal, mFieldInfo.Name, line.mReader.LineNumber, fieldString.ExtractedFrom + 1, ex.Message, ex);
                else
                    throw new ConvertException(fieldString.ExtractedString(), mFieldTypeInternal, mFieldInfo.Name, line.mReader.LineNumber, fieldString.ExtractedFrom + 1, "Your custom converter: " + mConvertProvider.GetType().Name + " throws an " + ex.GetType().Name + " with the message: " + ex.Message, ex);
            }
        }