An internal class used to store information about the Record Type.
Is public to provide extensibility of DataStorage from outside the library.
Beispiel #1
0
        /// <summary>
        /// Create an engine on the record info provided
        /// </summary>
        /// <param name="ri">Record information</param>
        internal EngineBase(RecordInfo ri)
        {
            mRecordType = ri.RecordType;
            RecordInfo = ri;

            CreateRecordOptions();
        }
Beispiel #2
0
 public RecordOperations Clone(RecordInfo ri)
 {
     var res = new RecordOperations(ri);
     res.mCreateHandler = mCreateHandler;
     res.mFastConstructor = mFastConstructor;
     res.mObjectToValuesHandler = mObjectToValuesHandler;
     return res;
 }
Beispiel #3
0
		internal EngineBase(Type recordType, Encoding encoding)
		{
			if (recordType == null)
				throw new BadUsageException("The record type can't be null");
			if (recordType.IsValueType)
				throw new BadUsageException("The record type must be a class not a struct.");

			mRecordType = recordType;
			mRecordInfo = new RecordInfo(recordType);
			mEncoding = encoding;
		}
            /// <summary>
            /// Return the record information for the type
            /// </summary>
            /// <param name="type">Type we want settings for</param>
            /// <remarks>Threadsafe</remarks>
            /// <returns>Record Information (settings and functions)</returns>
            public static IRecordInfo Resolve(Type type)
            {
                RecordInfo res;

                // class check cache / lock / check cache  and create if null algorythm
                if (!mRecordInfoCache.TryGetValue(type, out res))
                {
                    lock (type)
                    {   // Double Check inside a lock
                        if (!mRecordInfoCache.TryGetValue(type, out res))
                        {

                            res = new RecordInfo(type);
                            mRecordInfoCache.Add(type, res);
                        }
                    }

                }
                return (IRecordInfo) res.Clone();
            }
            /// <summary>
            /// Return the record information for the type
            /// </summary>
            /// <param name="type">Type we want settings for</param>
            /// <remarks>Threadsafe</remarks>
            /// <returns>Record Information (settings and functions)</returns>
            public static IRecordInfo Resolve(Type type)
            {
                RecordInfo res;

                lock (type) {
                    lock (mRecordInfoCache) {
                        if (mRecordInfoCache.TryGetValue(type, out res))
                            return (IRecordInfo) res.Clone();
                    }

                    // class check cache / lock / check cache  and create if null algorythm

                    res = new RecordInfo(type);
                    lock (mRecordInfoCache) {
                        if (!mRecordInfoCache.ContainsKey(type))
                            mRecordInfoCache.Add(type, res);
                    }

                    return (IRecordInfo) res.Clone();
                }
            }
		internal DelimitedRecordOptions(RecordInfo info)
				:base(info)
		{
	}
 /// <summary>
 /// Event based upon supplied record information
 /// </summary>
 /// <param name="ri"></param>
 internal EventEngineBase(RecordInfo ri)
     : base(ri)
 {
 }
        private void ReadNextRecord()
        {
            string currentLine = mAsyncReader.ReadNextLine();

            mLineNumber++;

            bool byPass = false;

            mLastRecord = null;

            LineInfo line = new LineInfo(currentLine);

            line.mReader = mAsyncReader;


            while (true)
            {
                if (currentLine != null)
                {
                    try
                    {
                        mTotalRecords++;

                        Type currType = mRecordSelector(this, currentLine);

                        line.ReLoad(currentLine);

                        if (currType != null)
                        {
                            RecordInfo info = (RecordInfo)mRecordInfoHash[currType];
                            mLastRecord = info.StringToRecord(line);

                            if (mLastRecord != null)
                            {
                                byPass = true;
                                return;
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        switch (mErrorManager.ErrorMode)
                        {
                        case ErrorMode.ThrowException:
                            byPass = true;
                            throw;

                        case ErrorMode.IgnoreAndContinue:
                            break;

                        case ErrorMode.SaveAndContinue:
                            ErrorInfo err = new ErrorInfo();
                            err.mLineNumber    = mAsyncReader.LineNumber;
                            err.mExceptionInfo = ex;
                            //							err.mColumnNumber = mColumnNum;
                            err.mRecordString = currentLine;

                            mErrorManager.AddError(err);
                            break;
                        }
                    }
                    finally
                    {
                        if (byPass == false)
                        {
                            currentLine = mAsyncReader.ReadNextLine();
                            mLineNumber = mAsyncReader.LineNumber;
                        }
                    }
                }
                else
                {
                    mLastRecord = null;


                    if (mRecordInfo.mIgnoreLast > 0)
                    {
                        mFooterText = mAsyncReader.RemainingText;
                    }

                    try
                    {
                        mAsyncReader.Close();
                    }
                    catch
                    {
                    }

                    return;
                }
            }
        }
        /// <include file='MultiRecordEngine.docs.xml' path='doc/WriteStream2/*'/>
        public void WriteStream(TextWriter writer, IEnumerable records, int maxRecords)
        {
            if (writer == null)
            {
                throw new ArgumentNullException("writer", "The writer of the Stream can be null");
            }

            if (records == null)
            {
                throw new ArgumentNullException("records", "The records can be null. Try with an empty array.");
            }

            ResetFields();

            if (mHeaderText != null && mHeaderText.Length != 0)
            {
                if (mHeaderText.EndsWith(StringHelper.NewLine))
                {
                    writer.Write(mHeaderText);
                }
                else
                {
                    writer.WriteLine(mHeaderText);
                }
            }


            string currentLine = null;

            //ConstructorInfo constr = mType.GetConstructor(new Type[] {});
            int max = maxRecords;

            if (records is IList)
            {
                max = Math.Min(max < 0 ? int.MaxValue : max, ((IList)records).Count);
            }


#if !MINI
            ProgressHelper.Notify(mNotifyHandler, mProgressMode, 0, max);
#endif
            int recIndex = 0;

            foreach (object rec in records)
            {
                if (recIndex == maxRecords)
                {
                    break;
                }
                try
                {
                    if (rec == null)
                    {
                        throw new BadUsageException("The record at index " + recIndex.ToString() + " is null.");
                    }

                    bool skip = false;
#if !MINI
                    ProgressHelper.Notify(mNotifyHandler, mProgressMode, recIndex + 1, max);
                    skip = OnBeforeWriteRecord(rec);
#endif

                    RecordInfo info = (RecordInfo)mRecordInfoHash[rec.GetType()];

                    if (info == null)
                    {
                        throw new BadUsageException("The record at index " + recIndex.ToString() + " is of type '" + rec.GetType().Name + "' and the engine dont handle this type. You can add it to the constructor.");
                    }

                    if (skip == false)
                    {
                        currentLine = info.RecordToString(rec);
#if !MINI
                        currentLine = OnAfterWriteRecord(currentLine, rec);
#endif
                        writer.WriteLine(currentLine);
                    }
                }
                catch (Exception ex)
                {
                    switch (mErrorManager.ErrorMode)
                    {
                    case ErrorMode.ThrowException:
                        throw;

                    case ErrorMode.IgnoreAndContinue:
                        break;

                    case ErrorMode.SaveAndContinue:
                        ErrorInfo err = new ErrorInfo();
                        err.mLineNumber    = mLineNumber;
                        err.mExceptionInfo = ex;
                        //							err.mColumnNumber = mColumnNum;
                        err.mRecordString = currentLine;
                        mErrorManager.AddError(err);
                        break;
                    }
                }
                recIndex++;
            }

            mTotalRecords = recIndex;

            if (mFooterText != null && mFooterText != string.Empty)
            {
                if (mFooterText.EndsWith(StringHelper.NewLine))
                {
                    writer.Write(mFooterText);
                }
                else
                {
                    writer.WriteLine(mFooterText);
                }
            }
        }
        /// <summary>
        /// Generic extension method for arrays that returns the array records
        /// in a DataTable.
        /// </summary>
        /// <param name="records">The array to transform into a DataTable</param>
        /// <returns>The array records in a DataTable.</returns>
        public static DataTable ToDataTable <T>(this T[] records)
        {
            var ri = RecordInfo.Resolve(typeof(T));

            return(ri.Operations.RecordsToDataTable(records));
        }
Beispiel #11
0
 internal FileHelperEngine(RecordInfo ri)
     : base(ri)
 {
     CreateRecordOptions();
 }
Beispiel #12
0
        /// <summary>
        /// Converts any collection of records to a DataTable using reflection.
        /// If the number of records is 0 this methods returns an empty
        /// DataTable with the columns based on the fields of the
        /// Type.
        /// </summary>
        /// <param name="records">The records to be converted to a DataTable</param>
        /// <returns>The DataTable containing the records as DataRows</returns>
        /// <param name="maxRecords">The max number of records to add to the DataTable. -1 for all.</param>
        /// <param name="recordType">The type of the inner records.</param>
        public static DataTable RecordsToDataTable(ICollection records, Type recordType, int maxRecords)
        {
            IRecordInfo ri = RecordInfo.Resolve(recordType);

            return(ri.Operations.RecordsToDataTable(records, maxRecords));
        }
Beispiel #13
0
 internal DelimitedRecordOptions(RecordInfo info)
     : base(info)
 {
 }
        private void ReadNextRecord()
        {
            string currentLine = mAsyncReader.ReadNextLine();

            mLineNumber++;

            bool byPass = false;

            mLastRecord = null;

            LineInfo line = new LineInfo(currentLine);

            line.mReader = mAsyncReader;


            while (true)
            {
                if (currentLine != null)
                {
                    try
                    {
                        mTotalRecords++;

                        Type currType = mRecordSelector(this, currentLine);

                        line.ReLoad(currentLine);

                        if (currType != null)
                        {
                            RecordInfo info = (RecordInfo)mRecordInfoHash[currType];
                            if (info == null)
                            {
                                throw new BadUsageException("A record is of type '" + currType.Name +
                                                            "' which this engine is not configured to handle. Try adding this type to the constructor.");
                            }
                            object[] values = new object[info.FieldCount];
                            mLastRecord = info.Operations.StringToRecord(line, values);

                            if (mLastRecord != null)
                            {
                                byPass = true;
                                return;
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        switch (mErrorManager.ErrorMode)
                        {
                        case ErrorMode.ThrowException:
                            byPass = true;
                            throw;

                        case ErrorMode.IgnoreAndContinue:
                            break;

                        case ErrorMode.SaveAndContinue:
                            ErrorInfo err = new ErrorInfo();
                            err.mLineNumber    = mAsyncReader.LineNumber;
                            err.mExceptionInfo = ex;
                            //							err.mColumnNumber = mColumnNum;
                            err.mRecordString = currentLine;

                            mErrorManager.AddError(err);
                            break;
                        }
                    }
                    finally
                    {
                        if (byPass == false)
                        {
                            currentLine = mAsyncReader.ReadNextLine();
                            mLineNumber = mAsyncReader.LineNumber;
                        }
                    }
                }
                else
                {
                    mLastRecord = null;


                    if (mRecordInfo.IgnoreLast > 0)
                    {
                        mFooterText = mAsyncReader.RemainingText;
                    }

                    try
                    {
                        mAsyncReader.Close();
                    }
                    catch
                    {
                    }

                    return;
                }
            }
        }
        /// <include file='MultiRecordEngine.docs.xml' path='doc/ReadStream/*'/>
        public object[] ReadStream(IRecordReader reader)
        {
            if (reader == null)
            {
                throw new ArgumentNullException("reader", "The reader of the Stream can´t be null");
            }

            if (mRecordSelector == null)
            {
                throw new BadUsageException("The Recordselector can´t be null, please pass a not null Selector in the constructor.");
            }

            ResetFields();
            mHeaderText = String.Empty;
            mFooterText = String.Empty;

            ArrayList resArray = new ArrayList();

            using (ForwardReader freader = new ForwardReader(reader, mMultiRecordInfo[0].IgnoreLast))
            {
                freader.DiscardForward = true;

                string currentLine, completeLine;

                mLineNumber = 1;

                completeLine = freader.ReadNextLine();
                currentLine  = completeLine;

#if !MINI
                if (MustNotifyProgress) // Avoid object creation
                {
                    OnProgress(new ProgressEventArgs(0, -1));
                }
#endif
                int currentRecord = 0;

                if (mMultiRecordInfo[0].IgnoreFirst > 0)
                {
                    for (int i = 0; i < mMultiRecordInfo[0].IgnoreFirst && currentLine != null; i++)
                    {
                        mHeaderText += currentLine + StringHelper.NewLine;
                        currentLine  = freader.ReadNextLine();
                        mLineNumber++;
                    }
                }


                bool byPass = false;

                LineInfo line = new LineInfo(currentLine);
                line.mReader = freader;

                while (currentLine != null)
                {
                    try
                    {
                        mTotalRecords++;
                        currentRecord++;

                        line.ReLoad(currentLine);

                        var  skip     = false;
                        Type currType = null;
                        try
                        {
                            currType = mRecordSelector(this, currentLine);
                        }
                        catch (Exception ex)
                        {
                            throw new Exception("Selector failed to process correctly", ex);
                        }

                        if (currType != null)
                        {
                            RecordInfo info = (RecordInfo)mRecordInfoHash[currType];
                            if (info == null)
                            {
                                throw new BadUsageException("A record is of type '" + currType.Name +
                                                            "' which this engine is not configured to handle. Try adding this type to the constructor.");
                            }

                            var record = info.Operations.CreateRecordHandler();

#if !MINI
                            if (MustNotifyProgress) // Avoid object creation
                            {
                                OnProgress(new ProgressEventArgs(currentRecord, -1));
                            }

                            BeforeReadEventArgs <object> e = null;
                            if (MustNotifyRead) // Avoid object creation
                            {
                                e    = new BeforeReadEventArgs <object>(this, record, currentLine, LineNumber);
                                skip = OnBeforeReadRecord(e);
                                if (e.RecordLineChanged)
                                {
                                    line.ReLoad(e.RecordLine);
                                }
                            }
#endif

                            if (skip == false)
                            {
                                var values = new object[info.FieldCount];
                                if (info.Operations.StringToRecord(record, line, values))
                                {
#if !MINI
                                    if (MustNotifyRead) // Avoid object creation
                                    {
                                        skip = OnAfterReadRecord(currentLine, record, e.RecordLineChanged, LineNumber);
                                    }
#endif

                                    if (skip == false)
                                    {
                                        resArray.Add(record);
                                    }
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        switch (mErrorManager.ErrorMode)
                        {
                        case ErrorMode.ThrowException:
                            byPass = true;
                            throw;

                        case ErrorMode.IgnoreAndContinue:
                            break;

                        case ErrorMode.SaveAndContinue:
                            var err = new ErrorInfo();
                            err.mLineNumber    = freader.LineNumber;
                            err.mExceptionInfo = ex;
                            //							err.mColumnNumber = mColumnNum;
                            err.mRecordString = completeLine;

                            mErrorManager.AddError(err);
                            break;
                        }
                    }
                    finally
                    {
                        if (byPass == false)
                        {
                            currentLine  = freader.ReadNextLine();
                            completeLine = currentLine;
                            mLineNumber  = freader.LineNumber;
                        }
                    }
                }

                if (mMultiRecordInfo[0].IgnoreLast > 0)
                {
                    mFooterText = freader.RemainingText;
                }
            }
            return(resArray.ToArray());
        }
Beispiel #16
0
        /// <summary>
        /// Create an new instance of the record information
        /// </summary>
        /// <returns>Deep copy of the RecordInfo class</returns>
        public object Clone()
        {
            var res = new RecordInfo
            {
                CommentAnyPlace = CommentAnyPlace,
                CommentMarker = CommentMarker,
                IgnoreEmptyLines = IgnoreEmptyLines,
                IgnoreEmptySpaces = IgnoreEmptySpaces,
                IgnoreFirst = IgnoreFirst,
                IgnoreLast = IgnoreLast,
                NotifyRead = NotifyRead,
                NotifyWrite = NotifyWrite,
                RecordCondition = RecordCondition,
                RecordConditionRegEx = RecordConditionRegEx,
                RecordConditionSelector = RecordConditionSelector,
                RecordType = RecordType,
                SizeHint = SizeHint
            };

            res.Operations = Operations.Clone(res);
            
            res.Fields = new FieldBase[Fields.Length];
            for (int i = 0; i < Fields.Length; i++)
                res.Fields[i] = (FieldBase) ((ICloneable) Fields[i]).Clone();

            return res;
        }
 internal FixedRecordOptions(RecordInfo info)
     : base(info)
 {
 }
Beispiel #18
0
			internal IgnoreCommentInfo(RecordInfo ri)
			{
				mRecordInfo = ri;
			}
Beispiel #19
0
		internal EngineBase(RecordInfo ri)
		{
			mRecordType = ri.mRecordType;
			mRecordInfo = ri;
		}
Beispiel #20
0
 internal RecordConditionInfo(RecordInfo ri)
 {
     mRecordInfo = ri;
 }
		internal FixedRecordOptions(RecordInfo info)
				:base(info)
		{
		}
        /// <summary>Converts any collection of records to a DataTebla using reflection. If the number of records is 0 this methods returns an empty DataTable with the columns based on the fields of the Type.</summary>
        /// <param name="records">The records to be converted to a DataTable</param>
        /// <returns>The datatable containing the records as DataRows</returns>
        /// <param name="maxRecords">The max number of records to add to the datatable. -1 for all.</param>
        /// <param name="recordType">The type of the inner records.</param>
        public static DataTable RecordsToDataTable(ICollection records, Type recordType, int maxRecords)
        {
            RecordInfo ri = new RecordInfo(recordType);

            return(ri.RecordsToDataTable(records, maxRecords));
        }
Beispiel #23
0
		internal RecordOptions(RecordInfo info)
		{
			mRecordInfo = info;
            mRecordConditionInfo = new RecordConditionInfo(info);
			mIgnoreCommentInfo = new IgnoreCommentInfo(info);
		}
 /// <include file='FileHelperEngine.docs.xml' path='doc/FileHelperEngineCtr/*'/>
 /// <param name="ri">Record information on new type</param>
 internal FileHelperEngine(RecordInfo ri)
     : base(ri)
 {
 }
Beispiel #25
0
        /// <summary>
        /// Create an new instance of the record information
        /// </summary>
        /// <returns>Deep copy of the RecordInfo class</returns>
        public object Clone()
        {
            var res = new RecordInfo();

            res.CommentAnyPlace = CommentAnyPlace;
            res.CommentMarker = CommentMarker;
            res.IgnoreEmptyLines = IgnoreEmptyLines;
            res.IgnoreEmptySpaces = IgnoreEmptySpaces;
            res.IgnoreFirst = IgnoreFirst;
            res.IgnoreLast = IgnoreLast;
            res.NotifyRead = NotifyRead;
            res.NotifyWrite = NotifyWrite;
            res.Operations = Operations.Clone(res);

            res.RecordCondition = RecordCondition;
            res.RecordConditionRegEx = RecordConditionRegEx;
            res.RecordConditionSelector = RecordConditionSelector;
            res.RecordType = RecordType;
            res.SizeHint = SizeHint;

            res.Fields = new FieldBase[Fields.Length];
            for (int i = 0; i < Fields.Length; i++)
                res.Fields[i] = (FieldBase) Fields[i].Clone();

            return res;
        }
        /// <include file='MultiRecordEngine.docs.xml' path='doc/ReadStream/*'/>
        public object[] ReadStream(TextReader reader)
        {
            if (reader == null)
            {
                throw new ArgumentNullException("reader", "The reader of the Stream can´t be null");
            }

            if (mRecordSelector == null)
            {
                throw new BadUsageException("The Recordselector can´t be null, please pass a not null Selector in the constructor.");
            }

            ResetFields();
            mHeaderText = String.Empty;
            mFooterText = String.Empty;

            ArrayList resArray = new ArrayList();

            ForwardReader freader = new ForwardReader(reader, mMultiRecordInfo[0].mIgnoreLast);

            freader.DiscardForward = true;

            string currentLine, completeLine;

            mLineNumber = 1;

            completeLine = freader.ReadNextLine();
            currentLine  = completeLine;

#if !MINI
            ProgressHelper.Notify(mNotifyHandler, mProgressMode, 0, -1);
#endif
            int currentRecord = 0;

            if (mMultiRecordInfo[0].mIgnoreFirst > 0)
            {
                for (int i = 0; i < mMultiRecordInfo[0].mIgnoreFirst && currentLine != null; i++)
                {
                    mHeaderText += currentLine + StringHelper.NewLine;
                    currentLine  = freader.ReadNextLine();
                    mLineNumber++;
                }
            }


            bool byPass = false;

            //			MasterDetails record = null;
            ArrayList tmpDetails = new ArrayList();

            LineInfo line = new LineInfo(currentLine);
            line.mReader = freader;

            while (currentLine != null)
            {
                try
                {
                    mTotalRecords++;
                    currentRecord++;

                    line.ReLoad(currentLine);

                    bool skip = false;
#if !MINI
                    ProgressHelper.Notify(mNotifyHandler, mProgressMode, currentRecord, -1);
                    skip = OnBeforeReadRecord(currentLine);
#endif

                    Type currType = mRecordSelector(this, currentLine);

                    if (currType != null)
                    {
                        RecordInfo info = (RecordInfo)mRecordInfoHash[currType];

                        if (skip == false)
                        {
                            object record = info.StringToRecord(line);

#if !MINI
                            skip = OnAfterReadRecord(currentLine, record);
#endif

                            if (skip == false && record != null)
                            {
                                resArray.Add(record);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    switch (mErrorManager.ErrorMode)
                    {
                    case ErrorMode.ThrowException:
                        byPass = true;
                        throw;

                    case ErrorMode.IgnoreAndContinue:
                        break;

                    case ErrorMode.SaveAndContinue:
                        ErrorInfo err = new ErrorInfo();
                        err.mLineNumber    = freader.LineNumber;
                        err.mExceptionInfo = ex;
                        //							err.mColumnNumber = mColumnNum;
                        err.mRecordString = completeLine;

                        mErrorManager.AddError(err);
                        break;
                    }
                }
                finally
                {
                    if (byPass == false)
                    {
                        currentLine  = freader.ReadNextLine();
                        completeLine = currentLine;
                        mLineNumber  = freader.LineNumber;
                    }
                }
            }

            if (mMultiRecordInfo[0].mIgnoreLast > 0)
            {
                mFooterText = freader.RemainingText;
            }

            return(resArray.ToArray());
        }