/// <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);
                }
            }
        }
        /// <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].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[] values = new object[info.mFieldCount];
                                object   record = info.StringToRecord(line, values);

#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());
        }
Exemplo n.º 3
0
        public void WriteStream(TextWriter writer, T[] 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.");
            }

            if (records.Length > 0 && records[0] != null && mRecordInfo.mRecordType.IsInstanceOfType(records[0]) == false)
            {
                throw new BadUsageException("This engine works with record of type " + mRecordInfo.mRecordType.Name + " and you use records of type " + records[0].GetType().Name);
            }

            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 = mRecordInfo.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);
                }
            }
        }
Exemplo n.º 4
0
        public T[] ReadStream(TextReader reader)
#endif
        {
            if (reader == null)
            {
                throw new ArgumentNullException("reader", "The reader of the Stream can´t be null");
            }

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

            ArrayList resArray      = new ArrayList();
            int       currentRecord = 0;

            ForwardReader freader = new ForwardReader(reader, mRecordInfo.mIgnoreLast);

            freader.DiscardForward = true;

            string currentLine, completeLine;

            mLineNumber = 1;

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

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

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

            bool byPass = false;

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

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

                    object record = mRecordInfo.StringToRecord(currentLine, freader);

                    if (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++;
                    }
                }
            }

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

#if !GENERICS
            return((object[])
#else
            return (T[])
#endif
                   resArray.ToArray(RecordType));
        }
        private T[] ReadStream(TextReader reader, int maxRecords, DataTable dt)
#endif
        {
#endif
            if (reader == null)
            {
                throw new ArgumentNullException("reader", "The reader of the Stream can´t be null");
            }
            NewLineDelimitedRecordReader recordReader = new NewLineDelimitedRecordReader(reader);

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

            ArrayList resArray      = new ArrayList();
            int       currentRecord = 0;

            using (ForwardReader freader = new ForwardReader(recordReader, mRecordInfo.mIgnoreLast))
            {
                freader.DiscardForward = true;


                string currentLine, completeLine;

                mLineNumber = 1;

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

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

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

                bool byPass = false;

                if (maxRecords < 0)
                {
                    maxRecords = int.MaxValue;
                }

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

                object[] values = new object[mRecordInfo.mFieldCount];
                while (currentLine != null && currentRecord < maxRecords)
                {
                    try
                    {
                        mTotalRecords++;
                        currentRecord++;

                        line.ReLoad(currentLine);

                        bool skip = false;
#if !MINI
                        ProgressHelper.Notify(mNotifyHandler, mProgressMode, currentRecord, -1);
#if !GENERICS
                        BeforeReadRecordEventArgs e = new BeforeReadRecordEventArgs(currentLine, LineNumber);
#else
                        BeforeReadRecordEventArgs <T> e = new BeforeReadRecordEventArgs <T>(currentLine, LineNumber);
#endif
                        skip = OnBeforeReadRecord(e);
                        if (e.RecordLineChanged)
                        {
                            line.ReLoad(e.RecordLine);
                        }
#endif

                        if (skip == false)
                        {
                            object record = mRecordInfo.StringToRecord(line, values);

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

                            if (skip == false && record != null)
                            {
#if MINI
                                resArray.Add(record);
#else
                                if (dt == null)
                                {
                                    resArray.Add(record);
                                }
                                else
                                {
                                    dt.Rows.Add(mRecordInfo.RecordToValues(record));
                                }
#endif
                            }
                        }
                    }
                    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++;
                        }
                    }
                }

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

#if !GENERICS
            return((object[])
#else
            return (T[])
#endif
                   resArray.ToArray(RecordType));
        }