Пример #1
0
        public static FieldBase CreateField(FieldInfo fi, TypedRecordAttribute recordAttribute, bool someOptional)
        {
            // If ignored, return null
            if (fi.IsDefined(typeof(FieldIgnoredAttribute), true))
            {
                return(null);
            }

            FieldBase res = null;

            FieldAttribute[] attributes;
            FieldAttribute   fieldAttb;

            attributes = (FieldAttribute[])fi.GetCustomAttributes(typeof(FieldAttribute), true);

            // CHECK USAGE ERRORS !!!

            if (attributes.Length > 1)
            {
                throw new BadUsageException("The field: " + fi.Name + " has more than one FieldAttribute (left only one or none)");
            }

            if (attributes.Length == 0 && recordAttribute is FixedLengthRecordAttribute)
            {
                throw new BadUsageException("The record class marked with the FixedLengthRecord attribute must include a FixedLength attribute in each field.");
            }

            if (recordAttribute is DelimitedRecordAttribute && fi.IsDefined(typeof(FieldAlignAttribute), true))
            {
                throw new BadUsageException("The AlignAttribute is only valid for fixed length records and are used only for write purpouse.");
            }


            // PROCESS IN NORMAL CONDITIONS

            if (attributes.Length > 0)
            {
                fieldAttb = attributes[0];

                if (fieldAttb is FieldFixedLengthAttribute)
                {
                    if (recordAttribute is DelimitedRecordAttribute)
                    {
                        throw new BadUsageException("The FieldFixedLengthAttribute is only for the FixedLengthRecords not for the delimited ones.");
                    }


                    FieldFixedLengthAttribute attb = ((FieldFixedLengthAttribute)fieldAttb);

                    FieldAlignAttribute[] alignAttbs = (FieldAlignAttribute[])fi.GetCustomAttributes(typeof(FieldAlignAttribute), true);
                    FieldAlignAttribute   align      = null;

                    if (alignAttbs.Length > 0)
                    {
                        align = alignAttbs[0];
                    }

                    res = new FixedLengthField(fi, attb.Length, align);
                }
                else if (fieldAttb is FieldDelimiterAttribute)
                {
                    if (recordAttribute is FixedLengthRecordAttribute)
                    {
                        throw new BadUsageException("The DelimitedAttribute is only for DelimitedRecords not for the fixed ones.");
                    }

                    res = new DelimitedField(fi, ((FieldDelimiterAttribute)fieldAttb).Separator);
                }
                else
                {
                    throw new BadUsageException("Custom TypedRecords not currently supported.");
                }
            }
            else             // attributes.Length == 0
            {
                if (recordAttribute is DelimitedRecordAttribute)
                {
                    res = new DelimitedField(fi, ((DelimitedRecordAttribute)recordAttribute).Separator);
                }
            }

            //-----  TRIMMING

            if (res != null)
            {
                FieldTrimAttribute[] trim = (FieldTrimAttribute[])fi.GetCustomAttributes(typeof(FieldTrimAttribute), true);
                if (trim.Length > 0)
                {
                    res.mTrimMode  = trim[0].TrimMode;
                    res.mTrimChars = trim[0].TrimChars;
                }

                FieldQuotedAttribute[] quotedAttributes = (FieldQuotedAttribute[])fi.GetCustomAttributes(typeof(FieldQuotedAttribute), true);
                if (quotedAttributes.Length > 0)
                {
                    if (res is FixedLengthField)
                    {
                        throw new BadUsageException("The QuotedAttribute can't be used in FixedLength fields.");
                    }

                    ((DelimitedField)res).mQuoteChar      = quotedAttributes[0].QuoteChar;
                    ((DelimitedField)res).mQuoteMode      = quotedAttributes[0].QuoteMode;
                    ((DelimitedField)res).mQuoteMultiline = quotedAttributes[0].QuoteMultiline;
                }

                FieldOptionalAttribute[] optionalAttribs = (FieldOptionalAttribute[])fi.GetCustomAttributes(typeof(FieldOptionalAttribute), true);

                if (optionalAttribs.Length > 0)
                {
                    res.mIsOptional = true;
                }
                else if (someOptional)
                {
                    throw new BadUsageException("When you define a field as FieldOptional, the next fields must be marked with the same attribute. ( Try adding [FieldOptional] to " + res.mFieldInfo.Name + " )");
                }


                res.mInNewLine = fi.IsDefined(typeof(FieldInNewLineAttribute), true);
            }


            return(res);
        }
Пример #2
0
        public static FieldBase CreateField(FieldInfo fi, TypedRecordAttribute recordAttribute)
        {
            // If ignored, return null
            if (fi.IsDefined(typeof(FieldIgnoredAttribute), true))
            {
                return(null);
            }

            FieldBase res = null;

            FieldAttribute[] attributes;
            FieldAttribute   fieldAttb;

            attributes = (FieldAttribute[])fi.GetCustomAttributes(typeof(FieldAttribute), true);

            // CHECK USAGE ERRORS !!!

            if (recordAttribute is FixedLengthRecordAttribute && attributes.Length == 0)
            {
                throw new BadUsageException("The field: '" + fi.Name + "' must be marked the FieldFixedLength attribute because the record class is marked with FixedLengthRecord.");
            }

            if (attributes.Length > 1)
            {
                throw new BadUsageException("The field: '" + fi.Name + "' has a FieldFixedLength and a FieldDelimiter attribute.");
            }

            if (recordAttribute is DelimitedRecordAttribute && fi.IsDefined(typeof(FieldAlignAttribute), false))
            {
                throw new BadUsageException("The field: '" + fi.Name + "' can't be marked with FieldAlign attribute, it is only valid for fixed length records and are used only for write purpouse.");
            }

            if (fi.FieldType.IsArray == false && fi.IsDefined(typeof(FieldArrayLengthAttribute), false))
            {
                throw new BadUsageException("The field: '" + fi.Name + "' can't be marked with FieldArrayLength attribute is only valid for array fields.");
            }


            // PROCESS IN NORMAL CONDITIONS

            if (attributes.Length > 0)
            {
                fieldAttb = attributes[0];

                if (fieldAttb is FieldFixedLengthAttribute)
                {
                    // Fixed Field
                    if (recordAttribute is DelimitedRecordAttribute)
                    {
                        throw new BadUsageException("The field: '" + fi.Name + "' can't be marked with FieldFixedLength attribute, it is only for the FixedLengthRecords not for delimited ones.");
                    }

                    FieldFixedLengthAttribute attb = ((FieldFixedLengthAttribute)fieldAttb);

                    FieldAlignAttribute[] alignAttbs = (FieldAlignAttribute[])fi.GetCustomAttributes(typeof(FieldAlignAttribute), false);
                    FieldAlignAttribute   align      = null;

                    if (alignAttbs.Length > 0)
                    {
                        align = alignAttbs[0];
                    }

                    res = new FixedLengthField(fi, attb.Length, align);
                    ((FixedLengthField)res).mFixedMode = ((FixedLengthRecordAttribute)recordAttribute).mFixedMode;
                }
                else if (fieldAttb is FieldDelimiterAttribute)
                {
                    // Delimited Field
                    if (recordAttribute is FixedLengthRecordAttribute)
                    {
                        throw new BadUsageException("The field: '" + fi.Name + "' can't be marked with FieldDelimiter attribute, it is only for DelimitedRecords not for fixed ones.");
                    }

                    res = new DelimitedField(fi, ((FieldDelimiterAttribute)fieldAttb).mSeparator);
                }
                else
                {
                    throw new BadUsageException("Custom Record Types are not currently supported. And sure will never be :P (You must not be here)");
                }
            }
            else // attributes.Length == 0
            {
                if (recordAttribute is DelimitedRecordAttribute)
                {
                    res = new DelimitedField(fi, ((DelimitedRecordAttribute)recordAttribute).Separator);
                }
            }

            if (res != null)
            {
                // Trim Related
                FieldTrimAttribute[] trim = (FieldTrimAttribute[])fi.GetCustomAttributes(typeof(FieldTrimAttribute), false);
                if (trim.Length > 0)
                {
                    res.mTrimMode  = trim[0].TrimMode;
                    res.mTrimChars = trim[0].TrimChars;
                }

                // Quote Related
                FieldQuotedAttribute[] quotedAttributes = (FieldQuotedAttribute[])fi.GetCustomAttributes(typeof(FieldQuotedAttribute), false);
                if (quotedAttributes.Length > 0)
                {
                    if (res is FixedLengthField)
                    {
                        throw new BadUsageException("The field: '" + fi.Name + "' can't be marked with FieldQuoted attribute, it is only for the delimited records.");
                    }

                    ((DelimitedField)res).mQuoteChar      = quotedAttributes[0].QuoteChar;
                    ((DelimitedField)res).mQuoteMode      = quotedAttributes[0].QuoteMode;
                    ((DelimitedField)res).mQuoteMultiline = quotedAttributes[0].QuoteMultiline;
                }

                // Optional Related
                FieldOptionalAttribute[] optionalAttribs = (FieldOptionalAttribute[])fi.GetCustomAttributes(typeof(FieldOptionalAttribute), false);

                if (optionalAttribs.Length > 0)
                {
                    res.mIsOptional = true;
                }

                // NewLine Related
                res.mInNewLine = fi.IsDefined(typeof(FieldInNewLineAttribute), true);

                // Array Related
                if (fi.FieldType.IsArray)
                {
                    res.mIsArray   = true;
                    res.mArrayType = fi.FieldType.GetElementType();

                    FieldArrayLengthAttribute[] arrayAttribs = (FieldArrayLengthAttribute[])fi.GetCustomAttributes(typeof(FieldArrayLengthAttribute), false);

                    if (arrayAttribs.Length > 0)
                    {
                        res.mArrayMinLength = arrayAttribs[0].mMinLength;
                        res.mArrayMaxLength = arrayAttribs[0].mMaxLength;

                        if (res.mArrayMaxLength < res.mArrayMinLength ||
                            res.mArrayMinLength < 0 ||
                            res.mArrayMaxLength <= 0)
                        {
                            throw new BadUsageException("The field: " + fi.Name + " has invalid length values in the [FieldArrayLength] attribute.");
                        }
                    }
                    else
                    {
                        // MinValue indicates that there is no FieldArrayLength in the array
                        res.mArrayMinLength = int.MinValue;
                        res.mArrayMaxLength = int.MaxValue;
                    }
                }
            }

            return(res);
        }