private void WriteRecord(object record)
        {
            string currentLine = null;

            try
            {
                bool skip = false;
//#if !MINI
//				ProgressHelper.Notify(mNotifyHandler, mProgressMode, i+1, max);
//				skip = OnBeforeWriteRecord(records[i]);
//#endif

                mLineNumber++;
                mTotalRecords++;

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

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

                if (skip == false)
                {
                    currentLine = info.RecordToString(record);
                    mAsyncWriter.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;
                }
            }
        }
        /// <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);
                }
            }
        }
Esempio n. 3
0
        /// <include file='MasterDetailEngine.docs.xml' path='doc/WriteStream2/*'/>
        public void WriteStream(TextWriter writer, object[] 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

                    RecordInfo info = (RecordInfo)mRecordInfoHash[records[i].GetType()];

                    currentLine = info.RecordToString(records[i]);
                    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);
                }
            }
        }