/// <summary>Add the specified ErrorInfo to the contained collection.</summary> /// <param name="error"></param> internal void AddError(ErrorInfo error) { mErrorsArray.Add(error); }
/// <summary>Add the specified ErrorInfo to the contained collection.</summary> /// <param name="error"></param> internal void AddError(ErrorInfo error) { if (mErrorsArray.Count <= mErrorLimit) mErrorsArray.Add(error); }
/// <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; var resArray = new ArrayList(); using (var freader = new ForwardReader(reader, mMultiRecordInfo[0].IgnoreLast)) { freader.DiscardForward = true; mLineNumber = 1; var completeLine = freader.ReadNextLine(); var currentLine = completeLine; if (MustNotifyProgress) // Avoid object creation { OnProgress(new ProgressEventArgs(0, -1)); } 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; var line = new LineInfo(currentLine) { mReader = freader }; while (currentLine != null) { try { mTotalRecords++; currentRecord++; line.ReLoad(currentLine); var skip = false; Type currType; try { currType = mRecordSelector(this, currentLine); } catch (Exception ex) { throw new Exception("Selector failed to process correctly", ex); } if (currType != null) { var 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 (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); } } if (skip == false) { var values = new object[info.FieldCount]; if (info.Operations.StringToRecord(record, line, values)) { if (MustNotifyRead) // Avoid object creation { skip = OnAfterReadRecord(currentLine, record, e.RecordLineChanged, LineNumber); } 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 { mLineNumber = freader.LineNumber, mExceptionInfo = ex, 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()); }
/// <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 (!string.IsNullOrEmpty(mHeaderText)) { 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 (MustNotifyProgress) // Avoid object creation { OnProgress(new ProgressEventArgs(0, max)); } int recIndex = 0; foreach (var rec in records) { if (recIndex == maxRecords) { break; } try { if (rec == null) { throw new BadUsageException("The record at index " + recIndex + " is null."); } bool skip = false; if (MustNotifyProgress) // Avoid object creation { OnProgress(new ProgressEventArgs(recIndex + 1, max)); } if (MustNotifyWrite) { skip = OnBeforeWriteRecord(rec, LineNumber); } var info = (IRecordInfo)mRecordInfoHash[rec.GetType()]; if (info == null) { throw new BadUsageException("The record at index " + recIndex + " 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.Operations.RecordToString(rec); if (MustNotifyWrite) { currentLine = OnAfterWriteRecord(currentLine, rec); } writer.WriteLine(currentLine); } } catch (Exception ex) { switch (mErrorManager.ErrorMode) { case ErrorMode.ThrowException: throw; case ErrorMode.IgnoreAndContinue: break; case ErrorMode.SaveAndContinue: var err = new ErrorInfo { mLineNumber = mLineNumber, mExceptionInfo = ex, mRecordString = currentLine }; mErrorManager.AddError(err); break; } } recIndex++; } mTotalRecords = recIndex; if (!string.IsNullOrEmpty(mFooterText)) { if (mFooterText.EndsWith(StringHelper.NewLine)) { writer.Write(mFooterText); } else { writer.WriteLine(mFooterText); } } }
private void ReadNextRecord() { string currentLine = mAsyncReader.ReadNextLine(); mLineNumber++; bool byPass = false; mLastRecord = null; var line = new LineInfo(currentLine) { mReader = mAsyncReader }; while (true) { if (currentLine != null) { try { mTotalRecords++; Type currType = mRecordSelector(this, currentLine); line.ReLoad(currentLine); if (currType != null) { var 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 values = new object[info.FieldCount]; mLastRecord = info.Operations.StringToRecord(line, values); if (MustNotifyRead) { OnAfterReadRecord(currentLine, mLastRecord, false, LineNumber); } 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: var err = new ErrorInfo { mLineNumber = mAsyncReader.LineNumber, mExceptionInfo = ex, mRecordString = currentLine }; mErrorManager.AddError(err); break; } } finally { if (byPass == false) { currentLine = mAsyncReader.ReadNextLine(); mLineNumber = mAsyncReader.LineNumber; } } } else { mLastRecord = null; if (RecordInfo.IgnoreLast > 0) { mFooterText = mAsyncReader.RemainingText; } try { mAsyncReader.Close(); } catch {} return; } } }