private static FieldBase[] CreateCoreFields(ArrayList fields, TypedRecordAttribute recordAttribute) { FieldBase curField; var arr = new ArrayList(); var someOptional = false; for (var i = 0; i < fields.Count; i++) { var 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))); }
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)); }
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."); } var attb = ((FieldFixedLengthAttribute)fieldAttb); var 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) { var trim = (FieldTrimAttribute[])fi.GetCustomAttributes(typeof(FieldTrimAttribute), true); if (trim.Length > 0) { res.mTrimMode = trim[0].TrimMode; res.mTrimChars = trim[0].TrimChars; } var 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; } var 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); }
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 { var 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)) { var ignoreComments = (IgnoreCommentedLinesAttribute)mRecordType.GetCustomAttributes(typeof(IgnoreCommentedLinesAttribute), false)[0]; mCommentMarker = ignoreComments.mCommentMarker; mCommentAnyPlace = ignoreComments.mAnyPlace; } if (mRecordType.IsDefined(typeof(ConditionalRecordAttribute), false)) { var 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 var 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 (var 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."); } }
private static FieldBase[] CreateCoreFields(ArrayList fields, TypedRecordAttribute recordAttribute) { FieldBase curField; var arr = new ArrayList(); var someOptional = false; for (var i = 0; i < fields.Count; i++) { var 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)); }
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)); }
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."); var attb = ((FieldFixedLengthAttribute) fieldAttb); var 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) { var trim = (FieldTrimAttribute[]) fi.GetCustomAttributes(typeof (FieldTrimAttribute), true); if (trim.Length > 0) { res.mTrimMode = trim[0].TrimMode; res.mTrimChars = trim[0].TrimChars; } var 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; } var 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; }