Ejemplo n.º 1
0
 public void AppendToFile(string fileName, MasterDetails <M, D> record)
 {
     AppendToFile(fileName, new MasterDetails <M, D>[] { record });
 }
Ejemplo n.º 2
0
        public MasterDetails <M, D>[] ReadStream(TextReader reader)
#endif
        {
            if (reader == null)
            {
                throw new ArgumentNullException("reader", "The reader of the Stream cant be null");
            }

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

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

            var resArray = new ArrayList();

            var freader = new ForwardReader(reader, mMasterInfo.mIgnoreLast);

            freader.DiscardForward = true;

            string currentLine, completeLine;

            mLineNumber = 1;

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

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

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


            var byPass = false;

#if !GENERICS
            MasterDetails record = null;
#else
            MasterDetails <M, D> record = null;
#endif
            var tmpDetails = new ArrayList();

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

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

                    line.ReLoad(currentLine);

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

                    var action = mRecordSelector(currentLine);

                    switch (action)
                    {
                    case RecordAction.Master:
                        if (record != null)
                        {
#if !GENERICS
                            record.mDetails = tmpDetails.ToArray();
#else
                            record.mDetails = (D[])tmpDetails.ToArray();
                                                                #endif
                            resArray.Add(record);
                        }

                        mTotalRecords++;
#if !GENERICS
                        record = new MasterDetails();
#else
                        record = new MasterDetails <M, D>();
#endif
                        tmpDetails.Clear();
#if !GENERICS
                        var lastMaster = mMasterInfo.StringToRecord(line);
#else
                        M lastMaster = (M)mMasterInfo.StringToRecord(line);
#endif

                        if (lastMaster != null)
                        {
                            record.mMaster = lastMaster;
                        }

                        break;

                    case RecordAction.Detail:
#if !GENERICS
                        var lastChild = mRecordInfo.StringToRecord(line);
#else
                        D lastChild = (D)mRecordInfo.StringToRecord(line);
#endif

                        if (lastChild != null)
                        {
                            tmpDetails.Add(lastChild);
                        }
                        break;

                    default:
                        break;
                    }
                }
                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    = mLineNumber;
                        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 (record != null)
            {
#if !GENERICS
                record.mDetails = tmpDetails.ToArray();
#else
                record.mDetails = (D[])tmpDetails.ToArray();
#endif
                resArray.Add(record);
            }

            if (mMasterInfo.mIgnoreLast > 0)
            {
                mFooterText = freader.RemainingText;
            }

#if !GENERICS
            return((MasterDetails[])resArray.ToArray(typeof(MasterDetails)));
#else
            return((MasterDetails <M, D>[])resArray.ToArray(typeof(MasterDetails <M, D>)));
#endif
        }