Beispiel #1
0
        private static FieldBase[] CreateFields(FieldInfo[] fields, TypedRecordAttribute recordAttribute)
        {
            FieldBase curField;
            ArrayList arr          = new ArrayList();
            bool      someOptional = false;

            for (int i = 0; i < fields.Length; i++)
            {
                FieldInfo fieldInfo = fields[i];

                curField = FieldFactory.CreateField(fieldInfo, recordAttribute, someOptional);

                if (curField != null)
                {
                    someOptional = curField.mIsOptional;

                    arr.Add(curField);
                    if (arr.Count > 1)
                    {
                        ((FieldBase)arr[arr.Count - 2]).mNextIsOptional = ((FieldBase)arr[arr.Count - 1]).mIsOptional;
                    }
                }
            }

            if (arr.Count > 0)
            {
                ((FieldBase)arr[arr.Count - 1]).mIsLast = true;
            }

            return((FieldBase[])arr.ToArray(typeof(FieldBase)));
        }
		private void RecursiveGetFields(ArrayList fields, Type currentType, TypedRecordAttribute recordAttribute)
		{
			if (currentType.BaseType != null)
				RecursiveGetFields(fields, currentType.BaseType, recordAttribute);

			fields.AddRange(currentType.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly));
		}
Beispiel #3
0
        /// <summary>
        /// Parse the attributes on the class and create an ordered list of
        /// fields we are extracting from the record
        /// </summary>
        /// <param name="fields">Complete list of fields in class</param>
        /// <param name="recordAttribute">Type of record, fixed or delimited</param>
        /// <returns>List of fields we are extracting</returns>
        private static FieldBase[] CreateCoreFields(IList <FieldInfo> fields, TypedRecordAttribute recordAttribute)
        {
            var resFields = new List <FieldBase>();

            // count of Properties
            var automaticFields = 0;

            // count of normal fields
            var genericFields = 0;

            for (int i = 0; i < fields.Count; i++)
            {
                FieldBase currentField = FieldBase.CreateField(fields[i], recordAttribute);
                if (currentField == null)
                {
                    continue;
                }

                if (currentField.FieldInfo.IsDefined(typeof(CompilerGeneratedAttribute), false))
                {
                    automaticFields++;
                }
                else
                {
                    genericFields++;
                }

                // Add to the result
                resFields.Add(currentField);

                if (resFields.Count > 1)
                {
                    CheckForOrderProblems(currentField, resFields);
                }
            }

            if (automaticFields > 0 &&
                genericFields > 0)
            {
                throw new BadUsageException(Messages.Errors.MixOfStandardAndAutoPropertiesFields
                                            .ClassName(resFields[0].FieldInfo.DeclaringType.Name)
                                            .Text);
            }

            SortFieldsByOrder(resFields);

            if (resFields.Count > 0)
            {
                resFields[0].IsFirst = true;
                resFields[resFields.Count - 1].IsLast = true;
            }

            CheckForOptionalAndArrayProblems(resFields);

            return(resFields.ToArray());
        }
Beispiel #4
0
//		#region " TESTING "
//
//		internal RecordInfo()
//		{
//		}
//
//		internal void AddFields(FieldBase[] fields)
//		{
//			mFields = fields;
//		}
//
//		#endregion

        private void InitFields()
        {
            //-> Checked by the AttributeTargets
            //new BadUsageException("Structures are not supported in the FileHelperEngine only classes are allowed.");

            TypedRecordAttribute recordAttribute = null;

            if (mRecordType.IsDefined(typeof(TypedRecordAttribute), true) == false)
            {
                throw new BadUsageException("The class " + mRecordType.Name + " must be marked with the DelimitedRecord or FixedLengthRecord Attribute.");
            }
            else
            {
                object[] attbs = mRecordType.GetCustomAttributes(typeof(TypedRecordAttribute), true);
                recordAttribute = (TypedRecordAttribute)attbs[0];
            }

            if (mRecordType.GetConstructor(BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic, null, mEmptyTypeArr, new ParameterModifier[] {}) == null)
            {
                throw new BadUsageException("The record class don't have a default constructor.");
            }

            if (mRecordType.IsDefined(typeof(IgnoreFirstAttribute), false))
            {
                mIgnoreFirst = ((IgnoreFirstAttribute)mRecordType.GetCustomAttributes(typeof(IgnoreFirstAttribute), false)[0]).NumberOfLines;
            }

            if (mRecordType.IsDefined(typeof(IgnoreLastAttribute), false))
            {
                mIgnoreLast = ((IgnoreLastAttribute)mRecordType.GetCustomAttributes(typeof(IgnoreLastAttribute), false)[0]).NumberOfLines;
            }

            if (mRecordType.IsDefined(typeof(IgnoreEmptyLinesAttribute), false))
            {
                mIgnoreEmptyLines = true;
            }

            mRecordConstructor = mRecordType.GetConstructor(BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic, null, mEmptyTypeArr, new ParameterModifier[] {});


// Create fields

            FieldInfo[] fields;
            fields = mRecordType.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);

            mFields     = CreateFields(fields, recordAttribute);
            mFieldCount = mFields.Length;

            if (mFieldCount == 0)
            {
                throw new BadUsageException("The record class don't contains any field.");
            }
        }
Beispiel #5
0
        private static FieldBase[] CreateCoreFields(List <FieldInfo> fields, TypedRecordAttribute recordAttribute)
        {
            List <FieldBase> resFields = new List <FieldBase>();

            for (int i = 0; i < fields.Count; i++)
            {
                FieldBase currentField = FieldBase.CreateField((FieldInfo)fields[i], recordAttribute);

                if (currentField != null)
                {
                    // Add to the result
                    resFields.Add(currentField);

                    // Check some differences with the previous field
                    if (resFields.Count > 1)
                    {
                        FieldBase prevField = (FieldBase)resFields[resFields.Count - 2];

                        prevField.mNextIsOptional = currentField.mIsOptional;

                        // Check for optional problems
                        if (prevField.mIsOptional && currentField.mIsOptional == false)
                        {
                            throw new BadUsageException("The field: " + prevField.mFieldInfo.Name + " must be marked as optional bacause after a field with FieldOptional, the next fields must be marked with the same attribute. ( Try adding [FieldOptional] to " + currentField.mFieldInfo.Name + " )");
                        }

                        // Check for array problems
                        if (prevField.mIsArray)
                        {
                            if (prevField.mArrayMinLength == int.MinValue)
                            {
                                throw new BadUsageException("The field: " + prevField.mFieldInfo.Name + " is an array and must contain a [FieldArrayLength] attribute because not is the last field.");
                            }

                            if (prevField.mArrayMinLength != prevField.mArrayMaxLength)
                            {
                                throw new BadUsageException("The array field: " + prevField.mFieldInfo.Name + " must contain a fixed length, i.e. the min and max length of the [FieldArrayLength] attribute must be the same because not is the last field.");
                            }
                        }
                    }
                }
            }

            if (resFields.Count > 0)
            {
                ((FieldBase)resFields[0]).mIsFirst = true;
                ((FieldBase)resFields[resFields.Count - 1]).mIsLast = true;
            }

            return(resFields.ToArray());
        }
Beispiel #6
0
        /// <summary>
        /// Parse the attributes on the class and create an ordered list of
        /// fields we are extracting from the record
        /// </summary>
        /// <param name="fields">Complete list of fields in class</param>
        /// <param name="recordAttribute">Type of record, fixed or delimited</param>
        /// <returns>List of fields we are extracting</returns>
        private static FieldBase[] CreateCoreFields(IList <FieldInfo> fields, TypedRecordAttribute recordAttribute)
        {
            var resFields = new List <FieldBase>();

            // count of Properties
            var automaticFields = 0;

            // count of normal fields
            var genericFields = 0;

            for (int i = 0; i < fields.Count; i++)
            {
                FieldBase currentField = FieldBase.CreateField(fields[i], recordAttribute);
                if (currentField == null)
                {
                    continue;
                }

                if (currentField.FieldInfo.IsDefined(typeof(CompilerGeneratedAttribute), false))
                {
                    automaticFields++;
                }
                else
                {
                    genericFields++;
                }

                // Add to the result
                resFields.Add(currentField);

                if (resFields.Count > 1)
                {
                    CheckForOrderProblems(currentField, resFields);
                }
            }

            if (automaticFields > 0 &&
                genericFields > 0 && SumOrder(resFields) == 0)
            {
                throw new BadUsageException(
                          $"You can mix standard fields and automatic properties only if you use [FieldOrder()] over the fields and properties in the {resFields[0].FieldInfo.DeclaringType.Name} class.");
            }

            SortFieldsByOrder(resFields);

            CheckForOptionalAndArrayProblems(resFields);

            return(resFields.ToArray());
        }
Beispiel #7
0
        private void RecursiveGetFields(List <FieldInfo> fields, Type currentType, TypedRecordAttribute recordAttribute)
        {
            if (currentType.BaseType != null && !currentType.IsDefined(typeof(IgnoreInheritedClassAttribute), false))
            {
                RecursiveGetFields(fields, currentType.BaseType, recordAttribute);
            }

            if (currentType == typeof(object))
            {
                return;
            }

#if !MINI
            lock (mTypeCacheLock)
            {
                ClearFieldInfoCache();
#endif

            foreach (FieldInfo fi in currentType.GetFields(BindingFlags.Public |
                                                           BindingFlags.NonPublic |
                                                           BindingFlags.Instance |
                                                           BindingFlags.DeclaredOnly))
            {
                if ((typeof(Delegate)).IsAssignableFrom(fi.FieldType))
                {
                    continue;
                }

                fields.Add(fi);
            }
#if !MINI
        }
#endif
            //currentType.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly);
            //currentType.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly);
            //GC.Collect();
            //GC.WaitForPendingFinalizers();
        }
Beispiel #8
0
        /// <summary>
        /// Check the Attributes on the field and return a structure containing
        /// the settings for this file.
        /// </summary>
        /// <param name="fi">Information about this field</param>
        /// <param name="recordAttribute">Type of record we are reading</param>
        /// <returns>Null if not used</returns>
        public static FieldBase CreateField(FieldInfo fi, TypedRecordAttribute recordAttribute)
        {
            // If ignored, return null
#pragma warning disable 612,618 // disable obsole warning
            if (fi.IsDefined(typeof(FieldNotInFileAttribute), true) || fi.IsDefined(typeof(FieldIgnoredAttribute), true))
#pragma warning restore 612,618
            {
                return(null);
            }

            FieldBase res = null;

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

            // CHECK USAGE ERRORS !!!

            // Fixed length record and no attributes at all
            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 purpose.");
            }

            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)
            {
                FieldAttribute 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.");
                    }

                    var attbFixedLength = (FieldFixedLengthAttribute)fieldAttb;
                    var attbAlign       = Attributes.GetFirst <FieldAlignAttribute>(fi);

                    res = new FixedLengthField(fi, attbFixedLength.Length, attbAlign);
                    ((FixedLengthField)res).FixedMode = ((FixedLengthRecordAttribute)recordAttribute).FixedMode;
                }
                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).Delimiter);
                }
                else
                {
                    throw new BadUsageException("Custom field attributes are not currently supported. Unknown attribute: " + fieldAttb.GetType().Name + " on field: " + fi.Name);
                }
            }
            else // attributes.Length == 0
            {
                var delimitedRecordAttribute = recordAttribute as DelimitedRecordAttribute;

                if (delimitedRecordAttribute != null)
                {
                    res = new DelimitedField(fi, delimitedRecordAttribute.Separator);
                }
            }

            if (res != null)
            {
                // FieldDiscarded
                res.Discarded = fi.IsDefined(typeof(FieldValueDiscardedAttribute), false);


                // FieldTrim
                Attributes.WorkWithFirst <FieldTrimAttribute>(fi, (x) =>
                {
                    res.TrimMode  = x.TrimMode;
                    res.TrimChars = x.TrimChars;
                });

                // FieldQuoted
                Attributes.WorkWithFirst <FieldQuotedAttribute>(fi, (x) =>
                {
                    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).QuoteChar =
                        x.QuoteChar;
                    ((DelimitedField)res).QuoteMode =
                        x.QuoteMode;
                    ((DelimitedField)res).QuoteMultiline =
                        x.QuoteMultiline;
                });



                // FieldOrder
                Attributes.WorkWithFirst <FieldOrderAttribute>(fi, x => res.FieldOrder = x.Order);


                // FieldOptional
                res.IsOptional = fi.IsDefined(typeof(FieldOptionalAttribute), false);

                // FieldInNewLine
                res.InNewLine = fi.IsDefined(typeof(FieldInNewLineAttribute), false);

                // FieldArrayLength
                if (fi.FieldType.IsArray)
                {
                    res.IsArray   = true;
                    res.ArrayType = fi.FieldType.GetElementType();

                    // MinValue indicates that there is no FieldArrayLength in the array
                    res.ArrayMinLength = int.MinValue;
                    res.ArrayMaxLength = int.MaxValue;

                    Attributes.WorkWithFirst <FieldArrayLengthAttribute>(fi, (x) =>
                    {
                        res.ArrayMinLength = x.mMinLength;
                        res.ArrayMaxLength = x.mMaxLength;

                        if (res.ArrayMaxLength < res.ArrayMinLength ||
                            res.ArrayMinLength < 0 ||
                            res.ArrayMaxLength <= 0)
                        {
                            throw new BadUsageException("The field: " + fi.Name + " has invalid length values in the [FieldArrayLength] attribute.");
                        }
                    });
                }

                // NullFieldOnError
                res.NullFieldOnError = fi.IsDefined(typeof(FieldNullOnErrorAttribute), false);
            }

            if (fi.IsDefined(typeof(CompilerGeneratedAttribute), false))
            {
                if (fi.Name.EndsWith("__BackingField") && fi.Name.StartsWith("<") && fi.Name.Contains(">"))
                {
                    res.FieldFriendlyName = fi.Name.Substring(1, fi.Name.IndexOf(">") - 1);
                }
            }

            if (string.IsNullOrEmpty(res.FieldFriendlyName))
            {
                res.FieldFriendlyName = res.FieldName;
            }

            return(res);
        }
Beispiel #9
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);
        }
Beispiel #10
0
        /*
         * private static readonly char[] mWhitespaceChars = new[] {
         * '\t', '\n', '\v', '\f', '\r', ' ', '\x00a0', '\u2000', '\u2001', '\u2002', '\u2003', '\u2004', '\u2005',
         * '\u2006', '\u2007', '\u2008',
         * '\u2009', '\u200a', '\u200b', '\u3000', '\ufeff'
         */

        #endregion

        #region "  CreateField  "

        /// <summary>
        /// Check the Attributes on the field and return a structure containing
        /// the settings for this file.
        /// </summary>
        /// <param name="fi">Information about this field</param>
        /// <param name="recordAttribute">Type of record we are reading</param>
        /// <returns>Null if not used</returns>
        public static FieldBase CreateField(FieldInfo fi, TypedRecordAttribute recordAttribute)
        {
            FieldBase  res               = null;
            MemberInfo mi                = fi;
            var        memberName        = "The field: '" + fi.Name;
            Type       fieldType         = fi.FieldType;
            string     fieldFriendlyName = AutoPropertyName(fi);

            if (string.IsNullOrEmpty(fieldFriendlyName) == false)
            {
                var prop = fi.DeclaringType.GetProperty(fieldFriendlyName);
                if (prop != null)
                {
                    memberName = "The property: '" + prop.Name;
                    mi         = prop;
                }
                else
                {
                    fieldFriendlyName = null;
                }
            }
            // If ignored, return null
#pragma warning disable 612,618 // disable obsolete warning
            if (mi.IsDefined(typeof(FieldNotInFileAttribute), true) ||
                mi.IsDefined(typeof(FieldIgnoredAttribute), true) ||
                mi.IsDefined(typeof(FieldHiddenAttribute), true))
#pragma warning restore 612,618
            {
                return(null);
            }



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

            // CHECK USAGE ERRORS !!!

            // Fixed length record and no attributes at all
            if (recordAttribute is FixedLengthRecordAttribute &&
                attributes.Length == 0)
            {
                throw new BadUsageException(memberName +
                                            "' must be marked the FieldFixedLength attribute because the record class is marked with FixedLengthRecord.");
            }

            if (attributes.Length > 1)
            {
                throw new BadUsageException(memberName +
                                            "' has a FieldFixedLength and a FieldDelimiter attribute.");
            }

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

            if (fieldType.IsArray == false &&
                mi.IsDefined(typeof(FieldArrayLengthAttribute), false))
            {
                throw new BadUsageException(memberName +
                                            "' can't be marked with FieldArrayLength attribute is only valid for array fields.");
            }

            // PROCESS IN NORMAL CONDITIONS
            if (attributes.Length > 0)
            {
                FieldAttribute fieldAttb = attributes[0];

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

                    var attbFixedLength = (FieldFixedLengthAttribute)fieldAttb;
                    var attbAlign       = Attributes.GetFirst <FieldAlignAttribute>(mi);

                    res = new FixedLengthField(fi, attbFixedLength.Length, attbAlign);
                    ((FixedLengthField)res).FixedMode = ((FixedLengthRecordAttribute)recordAttribute).FixedMode;
                }
                else if (fieldAttb is FieldDelimiterAttribute)
                {
                    // Delimited Field
                    if (recordAttribute is FixedLengthRecordAttribute)
                    {
                        throw new BadUsageException(memberName +
                                                    "' can't be marked with FieldDelimiter attribute, it is only for DelimitedRecords not for fixed ones.");
                    }

                    res = new DelimitedField(fi, ((FieldDelimiterAttribute)fieldAttb).Delimiter);
                }
                else
                {
                    throw new BadUsageException(
                              "Custom field attributes are not currently supported. Unknown attribute: " +
                              fieldAttb.GetType().Name + " on field: " + fi.Name);
                }
            }
            else // attributes.Length == 0
            {
                var delimitedRecordAttribute = recordAttribute as DelimitedRecordAttribute;

                if (delimitedRecordAttribute != null)
                {
                    res = new DelimitedField(fi, delimitedRecordAttribute.Separator);
                }
            }

            if (res != null)
            {
                // FieldDiscarded
                res.Discarded = mi.IsDefined(typeof(FieldValueDiscardedAttribute), false);

                // FieldTrim
                Attributes.WorkWithFirst <FieldTrimAttribute>(mi,
                                                              (x) => {
                    res.TrimMode  = x.TrimMode;
                    res.TrimChars = x.TrimChars;
                });

                // FieldQuoted
                Attributes.WorkWithFirst <FieldQuotedAttribute>(mi,
                                                                (x) => {
                    if (res is FixedLengthField)
                    {
                        throw new BadUsageException(
                            memberName +
                            "' can't be marked with FieldQuoted attribute, it is only for the delimited records.");
                    }

                    ((DelimitedField)res).QuoteChar =
                        x.QuoteChar;
                    ((DelimitedField)res).QuoteMode =
                        x.QuoteMode;
                    ((DelimitedField)res).QuoteMultiline =
                        x.QuoteMultiline;
                });

                // FieldOrder
                Attributes.WorkWithFirst <FieldOrderAttribute>(mi, x => res.FieldOrder = x.Order);

                // FieldCaption
                Attributes.WorkWithFirst <FieldCaptionAttribute>(mi, x => res.FieldCaption = x.Caption);

                // FieldOptional
                res.IsOptional = mi.IsDefined(typeof(FieldOptionalAttribute), false);

                // FieldInNewLine
                res.InNewLine = mi.IsDefined(typeof(FieldInNewLineAttribute), false);

                // FieldValidatorAttributes - for use in custom validation of fields
                res.Validators = new List <Interfaces.IFieldValidate>((FieldValidateAttribute[])mi.GetCustomAttributes(typeof(FieldValidateAttribute), false));

                // FieldArrayLength
                if (fieldType.IsArray)
                {
                    res.IsArray   = true;
                    res.ArrayType = fieldType.GetElementType();

                    // MinValue indicates that there is no FieldArrayLength in the array
                    res.ArrayMinLength = int.MinValue;
                    res.ArrayMaxLength = int.MaxValue;

                    Attributes.WorkWithFirst <FieldArrayLengthAttribute>(mi,
                                                                         (x) => {
                        res.ArrayMinLength = x.MinLength;
                        res.ArrayMaxLength = x.MaxLength;

                        if (res.ArrayMaxLength < res.ArrayMinLength ||
                            res.ArrayMinLength < 0 ||
                            res.ArrayMaxLength <= 0)
                        {
                            throw new BadUsageException(memberName +
                                                        " has invalid length values in the [FieldArrayLength] attribute.");
                        }
                    });
                }
            }

            if (string.IsNullOrEmpty(res.FieldFriendlyName))
            {
                res.FieldFriendlyName = res.FieldName;
            }

            return(res);
        }
Beispiel #11
0
        public static FieldBase CreateField(FieldInfo fi, TypedRecordAttribute recordAttribute)
        {
            // If ignored, return null
            if (fi.IsDefined(typeof(FieldIgnoredAttribute), true))
                return null;

            FieldBase res = null;

            var 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)
            {
                FieldAttribute 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.");

                    var attbFixedLength = (FieldFixedLengthAttribute)fieldAttb;
                    var attbAlign = Attributes.GetFirst<FieldAlignAttribute>(fi);

                    res = new FixedLengthField(fi, attbFixedLength.Length, attbAlign);
                    ((FixedLengthField)res).FixedMode = ((FixedLengthRecordAttribute)recordAttribute).FixedMode;
                }
                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).Delimiter);

                }
                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)
            {

                // FieldTrim
                Attributes.WorkWithFirst<FieldTrimAttribute>(fi, (x) =>
                                                 {
                                                     res.TrimMode = x.TrimMode;
                                                     res.TrimChars = x.TrimChars;
                                                 });

                // FieldQuoted
                Attributes.WorkWithFirst<FieldQuotedAttribute>(fi, (x) =>
                                                                       {
                                                                           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).QuoteChar =
                                                                               x.QuoteChar;
                                                                           ((DelimitedField)res).QuoteMode =
                                                                               x.QuoteMode;
                                                                           ((DelimitedField)res).QuoteMultiline =
                                                                               x.QuoteMultiline;
                                                                       });

                // FieldOrder
                Attributes.WorkWithFirst<FieldOrderAttribute>(fi, (x) =>
                {
                    res.FieldOrder = x.Order;
                });

                // FieldOptional
                res.IsOptional = fi.IsDefined(typeof(FieldOptionalAttribute), false);

                // FieldInNewLine
                res.InNewLine = fi.IsDefined(typeof(FieldInNewLineAttribute), false);

                // FieldArrayLength
                if (fi.FieldType.IsArray)
                {
                    res.IsArray = true;
                    res.ArrayType = fi.FieldType.GetElementType();

                    // MinValue indicates that there is no FieldArrayLength in the array
                    res.ArrayMinLength = int.MinValue;
                    res.ArrayMaxLength = int.MaxValue;

                    Attributes.WorkWithFirst<FieldArrayLengthAttribute>(fi, (x) =>
                    {
                        res.ArrayMinLength = x.mMinLength;
                        res.ArrayMaxLength = x.mMaxLength;

                        if (res.ArrayMaxLength < res.ArrayMinLength ||
                            res.ArrayMinLength < 0 ||
                            res.ArrayMaxLength <= 0)
                            throw new BadUsageException("The field: " + fi.Name + " has invalid length values in the [FieldArrayLength] attribute.");
                    });
                }

            }

            return res;
        }
Beispiel #12
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);
        }
Beispiel #13
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);
					((FixedLengthField) res).mFixedMode = ((FixedLengthRecordAttribute)recordAttribute).mFixedMode;
				}
				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).mSeparator);

				}
				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;
		}
Beispiel #14
0
        private void InitFields()
        {
            //-> Checked by the AttributeTargets
            //new BadUsageException("Structures are not supported in the FileHelperEngine only classes are allowed.");

            TypedRecordAttribute recordAttribute = null;

            if (mRecordType.IsDefined(typeof(TypedRecordAttribute), true) == false)
            {
                throw new BadUsageException("The class " + mRecordType.Name + " must be marked with the [DelimitedRecord] or [FixedLengthRecord] Attribute.");
            }
            else
            {
                object[] attbs = mRecordType.GetCustomAttributes(typeof(TypedRecordAttribute), true);
                recordAttribute = (TypedRecordAttribute)attbs[0];
            }

            if (mRecordType.GetConstructor(BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic, null, mEmptyTypeArr, new ParameterModifier[] {}) == null)
            {
                throw new BadUsageException("The record class " + mRecordType.Name + " need a constructor with no args (public or private)");
            }

            if (mRecordType.IsDefined(typeof(IgnoreFirstAttribute), false))
            {
                mIgnoreFirst = ((IgnoreFirstAttribute)mRecordType.GetCustomAttributes(typeof(IgnoreFirstAttribute), false)[0]).NumberOfLines;
            }

            if (mRecordType.IsDefined(typeof(IgnoreLastAttribute), false))
            {
                mIgnoreLast = ((IgnoreLastAttribute)mRecordType.GetCustomAttributes(typeof(IgnoreLastAttribute), false)[0]).NumberOfLines;
            }

            if (mRecordType.IsDefined(typeof(IgnoreEmptyLinesAttribute), false))
            {
                mIgnoreEmptyLines  = true;
                mIgnoreEmptySpaces = ((IgnoreEmptyLinesAttribute)mRecordType.GetCustomAttributes(typeof(IgnoreEmptyLinesAttribute), false)[0]).
                                     mIgnoreSpaces;
            }

            if (mRecordType.IsDefined(typeof(IgnoreCommentedLinesAttribute), false))
            {
                IgnoreCommentedLinesAttribute ignoreComments =
                    (IgnoreCommentedLinesAttribute)mRecordType.GetCustomAttributes(typeof(IgnoreCommentedLinesAttribute), false)[0];
                mCommentMarker   = ignoreComments.mCommentMarker;
                mCommentAnyPlace = ignoreComments.mAnyPlace;
            }

            if (mRecordType.IsDefined(typeof(ConditionalRecordAttribute), false))
            {
                ConditionalRecordAttribute conditional =
                    (ConditionalRecordAttribute)mRecordType.GetCustomAttributes(typeof(ConditionalRecordAttribute), false)[0];

                mRecordCondition         = conditional.mCondition;
                mRecordConditionSelector = conditional.mConditionSelector;

                                #if !MINI
                if (mRecordCondition == RecordCondition.ExcludeIfMatchRegex ||
                    mRecordCondition == RecordCondition.IncludeIfMatchRegex)
                {
                    mConditionRegEx = new Regex(mRecordConditionSelector, RegexOptions.Compiled | RegexOptions.CultureInvariant | RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture);
                }
                                #endif
            }



#if !MINI
            if (typeof(INotifyRead).IsAssignableFrom(mRecordType))
            {
                mNotifyRead = true;
            }

            if (typeof(INotifyWrite).IsAssignableFrom(mRecordType))
            {
                mNotifyWrite = true;
            }
#endif


            mRecordConstructor = mRecordType.GetConstructor(BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic, null, mEmptyTypeArr, new ParameterModifier[] {});

            // Create fields

            ArrayList fields = new ArrayList();
            RecursiveGetFields(fields, mRecordType, recordAttribute);

            mFields     = CreateCoreFields(fields, recordAttribute);
            mFieldCount = mFields.Length;

            if (recordAttribute is FixedLengthRecordAttribute)
            {
                // Defines the initial size of the StringBuilder
                mSizeHint = 0;
                for (int i = 0; i < mFieldCount; i++)
                {
                    mSizeHint += ((FixedLengthField)mFields[i]).mFieldLength;
                }
            }


            if (mFieldCount == 0)
            {
                throw new BadUsageException("The record class " + mRecordType.Name + " don't contains any field.");
            }
        }
Beispiel #15
0
		private void RecursiveGetFields(ArrayList fields, Type currentType, TypedRecordAttribute recordAttribute)
		{
			if (currentType.BaseType != null)
				RecursiveGetFields(fields, currentType.BaseType, recordAttribute);

			fields.AddRange(currentType.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly));
		}
Beispiel #16
0
        /// <summary>
        /// Check the Attributes on the field and return a structure containing
        /// the settings for this file.
        /// </summary>
        /// <param name="fi">Information about this field</param>
        /// <param name="recordAttribute">Type of record we are reading</param>
        /// <returns>Null if not used</returns>
        public static FieldBase CreateField(FieldInfo fi, TypedRecordAttribute recordAttribute)
        {
            // If ignored, return null
            #pragma warning disable 612,618 // disable obsole warning
            if (fi.IsDefined(typeof (FieldNotInFileAttribute), true) ||
                fi.IsDefined(typeof (FieldIgnoredAttribute), true) ||
                fi.IsDefined(typeof (FieldHiddenAttribute), true))
            #pragma warning restore 612,618
                return null;

            FieldBase res = null;

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

            // CHECK USAGE ERRORS !!!

            // Fixed length record and no attributes at all
            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 purpose.");
            }

            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) {
                FieldAttribute 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.");
                    }

                    var attbFixedLength = (FieldFixedLengthAttribute) fieldAttb;
                    var attbAlign = Attributes.GetFirst<FieldAlignAttribute>(fi);

                    res = new FixedLengthField(fi, attbFixedLength.Length, attbAlign);
                    ((FixedLengthField) res).FixedMode = ((FixedLengthRecordAttribute) recordAttribute).FixedMode;
                }
                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).Delimiter);
                }
                else {
                    throw new BadUsageException(
                        "Custom field attributes are not currently supported. Unknown attribute: " +
                        fieldAttb.GetType().Name + " on field: " + fi.Name);
                }
            }
            else // attributes.Length == 0
            {
                var delimitedRecordAttribute = recordAttribute as DelimitedRecordAttribute;

                if (delimitedRecordAttribute != null)
                    res = new DelimitedField(fi, delimitedRecordAttribute.Separator);
            }

            if (res != null) {
                // FieldDiscarded
                res.Discarded = fi.IsDefined(typeof (FieldValueDiscardedAttribute), false);

                // FieldTrim
                Attributes.WorkWithFirst<FieldTrimAttribute>(fi,
                    (x) => {
                        res.TrimMode = x.TrimMode;
                        res.TrimChars = x.TrimChars;
                    });

                // FieldQuoted
                Attributes.WorkWithFirst<FieldQuotedAttribute>(fi,
                    (x) => {
                        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).QuoteChar =
                            x.QuoteChar;
                        ((DelimitedField) res).QuoteMode =
                            x.QuoteMode;
                        ((DelimitedField) res).QuoteMultiline =
                            x.QuoteMultiline;
                    });

                // FieldOrder
                Attributes.WorkWithFirst<FieldOrderAttribute>(fi, x => res.FieldOrder = x.Order);

                // FieldCaption
                Attributes.WorkWithFirst<FieldCaptionAttribute>(fi, x => res.FieldCaption = x.Caption);

                // FieldOptional
                res.IsOptional = fi.IsDefined(typeof(FieldOptionalAttribute), false);

                // FieldInNewLine
                res.InNewLine = fi.IsDefined(typeof(FieldInNewLineAttribute), false);

                // FieldNotEmpty
                res.IsNotEmpty = fi.IsDefined(typeof(FieldNotEmptyAttribute), false);

                // FieldArrayLength
                if (fi.FieldType.IsArray) {
                    res.IsArray = true;
                    res.ArrayType = fi.FieldType.GetElementType();

                    // MinValue indicates that there is no FieldArrayLength in the array
                    res.ArrayMinLength = int.MinValue;
                    res.ArrayMaxLength = int.MaxValue;

                    Attributes.WorkWithFirst<FieldArrayLengthAttribute>(fi,
                        (x) => {
                            res.ArrayMinLength = x.MinLength;
                            res.ArrayMaxLength = x.MaxLength;

                            if (res.ArrayMaxLength < res.ArrayMinLength ||
                                res.ArrayMinLength < 0 ||
                                res.ArrayMaxLength <= 0) {
                                throw new BadUsageException("The field: " + fi.Name +
                                                            " has invalid length values in the [FieldArrayLength] attribute.");
                            }
                        });
                }
            }

            if (fi.IsDefined(typeof (CompilerGeneratedAttribute), false))
            {
                if (fi.Name.EndsWith("__BackingField") &&
                    fi.Name.StartsWith("<") &&
                    fi.Name.Contains(">"))

                res.FieldFriendlyName = fi.Name.Substring(1, fi.Name.IndexOf(">") - 1);
                res.IsAutoProperty = true;

                var prop = fi.DeclaringType.GetProperty(res.FieldFriendlyName);
                if (prop != null)
                {
                    Attributes.WorkWithFirst<FieldOrderAttribute>(prop, x => res.FieldOrder = x.Order);
                }
            }

            if (string.IsNullOrEmpty(res.FieldFriendlyName))
                res.FieldFriendlyName = res.FieldName;

            return res;
        }
Beispiel #17
0
		private static FieldBase[] CreateCoreFields(ArrayList fields, TypedRecordAttribute recordAttribute)
		{
			FieldBase curField;
			ArrayList arr = new ArrayList();
			bool someOptional = false;
			for (int i = 0; i < fields.Count; i++)
			{
				FieldInfo fieldInfo = (FieldInfo) fields[i];

				curField = FieldFactory.CreateField(fieldInfo, recordAttribute, someOptional);

				if (curField != null)
				{
					someOptional = curField.mIsOptional;

					arr.Add(curField);
					if (arr.Count > 1)
						((FieldBase)arr[arr.Count-2]).mNextIsOptional = ((FieldBase)arr[arr.Count-1]).mIsOptional;
				}
			}

			if (arr.Count > 0)
			{
				((FieldBase) arr[0]).mIsFirst = true;
				((FieldBase) arr[arr.Count - 1]).mIsLast = true;

			}

			return (FieldBase[]) arr.ToArray(typeof (FieldBase));

		}
Beispiel #18
0
        /// <summary>
        /// Parse the attributes on the class and create an ordered list of
        /// fields we are extracting from the record
        /// </summary>
        /// <param name="fields">Complete list of fields in class</param>
        /// <param name="recordAttribute">Type of record, fixed or delimited</param>
        /// <returns>List of fields we are extracting</returns>
        private static FieldBase[] CreateCoreFields(IList<FieldInfo> fields, TypedRecordAttribute recordAttribute)
        {
            var resFields = new List<FieldBase>();

            // count of Properties
            var automaticFields = 0;

            // count of normal fields
            var genericFields = 0;
            for (int i = 0; i < fields.Count; i++) {
                FieldBase currentField = FieldBase.CreateField(fields[i], recordAttribute);
                if (currentField == null)
                    continue;

                if (currentField.FieldInfo.IsDefined(typeof (CompilerGeneratedAttribute), false))
                    automaticFields++;
                else
                    genericFields++;

                // Add to the result
                resFields.Add(currentField);

                if (resFields.Count > 1)
                    CheckForOrderProblems(currentField, resFields);
            }

            if (automaticFields > 0 &&
                genericFields > 0) {
                throw new BadUsageException(Messages.Errors.MixOfStandardAndAutoPropertiesFields
                    .ClassName(resFields[0].FieldInfo.DeclaringType.Name)
                    .Text);
            }

            SortFieldsByOrder(resFields);

            if (resFields.Count > 0) {
                resFields[0].IsFirst = true;
                resFields[resFields.Count - 1].IsLast = true;
            }

            CheckForOptionalAndArrayProblems(resFields);

            return resFields.ToArray();
        }
Beispiel #19
0
        private static FieldBase[] CreateCoreFields(IList<FieldInfo> fields, TypedRecordAttribute recordAttribute)
        {
            var resFields = new List<FieldBase>();

            for (int i = 0; i < fields.Count; i++)
            {
                FieldBase currentField = FieldBase.CreateField(fields[i], recordAttribute);

                if (currentField == null)
                    continue;

                // Add to the result
                resFields.Add(currentField);

                if (resFields.Count > 1)
                {
                    CheckForOrderProblems(currentField, resFields);
                }

            }

            SortFieldsByOrder(resFields);

            if (resFields.Count > 0)
            {
                resFields[0].mIsFirst = true;
                resFields[resFields.Count - 1].mIsLast = true;
            }

            CheckForOptionalAndArrayProblems(resFields);

            return resFields.ToArray();
        }