public void WriteStream(TextWriter writer, IEnumerable <MasterDetails <M, D> > records, int maxRecords)
#endif
        {
            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;

            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;

#if !GENERICS
            foreach (MasterDetails rec in records)
#else
            foreach (MasterDetails <M, D> rec in records)
#endif
            {
                if (recIndex == maxRecords)
                {
                    break;
                }

                try
                {
                    if (rec == null)
                    {
                        throw new BadUsageException("The record at index " + recIndex.ToString() + " is null.");
                    }

#if !MINI
                    ProgressHelper.Notify(mNotifyHandler, mProgressMode, recIndex + 1, max);
#endif

                    currentLine = mMasterInfo.RecordToString(rec.mMaster);
                    writer.WriteLine(currentLine);

                    if (rec.mDetails != null)
                    {
                        for (int d = 0; d < rec.mDetails.Length; d++)
                        {
                            currentLine = mRecordInfo.RecordToString(rec.mDetails[d]);
                            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;
                    }
                }
            }

            mTotalRecords = recIndex;

            if (mFooterText != null && mFooterText != string.Empty)
            {
                if (mFooterText.EndsWith(StringHelper.NewLine))
                {
                    writer.Write(mFooterText);
                }
                else
                {
                    writer.WriteLine(mFooterText);
                }
            }
        }
        public MasterDetails <M, D>[] ReadStream(TextReader reader)
#endif
        {
            if (reader == null)
            {
                throw new ArgumentNullException("reader", "The reader of the Stream can't be null");
            }

            if (RecordSelector == null)
            {
                throw new BadUsageException("The RecordSelector can't be null on read operations.");
            }

            NewLineDelimitedRecordReader recordReader = new NewLineDelimitedRecordReader(reader);

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

            ArrayList resArray = new ArrayList();

            using (ForwardReader freader = new ForwardReader(recordReader, mMasterInfo.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 (mMasterInfo.mIgnoreFirst > 0)
                {
                    for (int i = 0; i < mMasterInfo.mIgnoreFirst && currentLine != null; i++)
                    {
                        mHeaderText += currentLine + StringHelper.NewLine;
                        currentLine  = freader.ReadNextLine();
                        mLineNumber++;
                    }
                }


                bool byPass = false;

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

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


                object[] valuesMaster = new object[mMasterInfo.mFieldCount];
                object[] valuesDetail = new object[mRecordInfo.mFieldCount];

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

                        line.ReLoad(currentLine);

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

                        RecordAction action = RecordSelector(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
                            object lastMaster = mMasterInfo.StringToRecord(line, valuesMaster);
#else
                            M lastMaster = (M)mMasterInfo.StringToRecord(line, valuesMaster);
#endif

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

                            break;

                        case RecordAction.Detail:
#if !GENERICS
                            object lastChild = mRecordInfo.StringToRecord(line, valuesDetail);
#else
                            D lastChild = (D)mRecordInfo.StringToRecord(line, valuesDetail);
#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:
                            ErrorInfo 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
        }
Ejemplo n.º 3
0
        /// <include file='MasterDetailEngine.docs.xml' path='doc/WriteStream2/*'/>
        public void WriteStream(TextWriter writer, MasterDetails[] 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 = records.Length;

            if (maxRecords >= 0)
            {
                max = Math.Min(records.Length, maxRecords);
            }


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

            for (int i = 0; i < max; i++)
            {
                try
                {
                    if (records[i] == null)
                    {
                        throw new BadUsageException("The record at index " + i.ToString() + " is null.");
                    }

                                        #if !MINI
                    ProgressHelper.Notify(mNotifyHandler, mProgressMode, i + 1, max);
                                        #endif

                    currentLine = mMasterInfo.RecordToString(records[i].mMaster);
                    writer.WriteLine(currentLine);

                    if (records[i].mDetails != null)
                    {
                        for (int d = 0; d < records[i].mDetails.Length; d++)
                        {
                            currentLine = mRecordInfo.RecordToString(records[i].mDetails[d]);
                            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;
                    }
                }
            }

            mTotalRecords = records.Length;

            if (mFooterText != null && mFooterText != string.Empty)
            {
                if (mFooterText.EndsWith(StringHelper.NewLine))
                {
                    writer.Write(mFooterText);
                }
                else
                {
                    writer.WriteLine(mFooterText);
                }
            }
        }
Ejemplo n.º 4
0
 protected void Notify(ProgressChangeHandler handler, ProgressMode mode, int current, int total)
 {
     ProgressHelper.Notify(handler, mode, current, total);
 }
Ejemplo n.º 5
0
        /// <include file='MasterDetailEngine.docs.xml' path='doc/ReadStream/*'/>
        public MasterDetails[] 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, mMasterInfo.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 (mMasterInfo.mIgnoreFirst > 0)
            {
                for (int i = 0; i < mMasterInfo.mIgnoreFirst && currentLine != null; i++)
                {
                    mHeaderText += currentLine + StringHelper.NewLine;
                    currentLine  = freader.ReadNextLine();
                    mLineNumber++;
                }
            }


            bool byPass = false;

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

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

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

                    RecordAction action = mRecordSelector(currentLine);

                    switch (action)
                    {
                    case RecordAction.Master:
                        if (record != null)
                        {
                            record.mDetails = tmpDetails.ToArray();
                            resArray.Add(record);
                        }

                        mTotalRecords++;
                        record = new MasterDetails();
                        tmpDetails.Clear();
                        object lastMaster = mMasterInfo.StringToRecord(currentLine, freader);

                        if (lastMaster != null)
                        {
                            record.mMaster = mMasterInfo.StringToRecord(currentLine, freader);
                        }

                        break;

                    case RecordAction.Detail:
                        object lastChild = mRecordInfo.StringToRecord(currentLine, freader);

                        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:
                        ErrorInfo 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)
            {
                record.mDetails = tmpDetails.ToArray();
                resArray.Add(record);
            }

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

            return((MasterDetails[])resArray.ToArray(typeof(MasterDetails)));
        }