protected override ExtractedInfo ExtractFieldString(LineInfo line) { if (mIsOptional && line.IsEOL() ) return ExtractedInfo.Empty; if (mQuoteChar == '\0') return BasicExtractString(line); else { //TODO: UnComment and Fix if (mTrimMode == TrimMode.Both || mTrimMode == TrimMode.Left) { //int pos = line.mCurrentPos; line.TrimStart(mTrimChars); // from2 = from.TrimStart(mTrimChars); //res.CharsRemoved = line.mCurrentPos - pos; } string quotedStr = mQuoteChar.ToString(); if (line.StartsWith(quotedStr)) { // ExtractedInfo res = null; // res = new ExtractedInfo(line, line.mCurrentPos); return StringHelper.ExtractQuotedString(line, mQuoteChar, mQuoteMultiline == MultilineMode.AllowForBoth || mQuoteMultiline == MultilineMode.AllowForRead); // if (mQuoteMultiline == MultilineMode.AllowForBoth || mQuoteMultiline == MultilineMode.AllowForRead) // { // // //res.ExtractedString = ei.ExtractedString; // //res.CharsRemoved += ei.CharsRemoved; // //res.ExtraLines = ei.ExtraLines; // //res.NewRestOfLine = ei.NewRestOfLine; // } // else // { // return StringHelper.ExtractQuotedString(from2, mQuoteChar, out index); // //res.CharsRemoved += index; // } // return res; } else { if (mQuoteMode == QuoteMode.OptionalForBoth || mQuoteMode == QuoteMode.OptionalForRead) return BasicExtractString(line); else if (line.StartsWithTrim(quotedStr)) throw new BadUsageException("The field '" + this.mFieldInfo.Name + "' has spaces before the QuotedChar at line "+ line.mReader.LineNumber.ToString() + ". Use the TrimAttribute to by pass this error. Field String: " + line.CurrentString); else throw new BadUsageException("The field '" + this.mFieldInfo.Name + "' not begin with the QuotedChar at line "+ line.mReader.LineNumber.ToString() + ". You can use FieldQuoted(QuoteMode.OptionalForRead) to allow optional quoted field.. Field String: " + line.CurrentString); } } }
/// <summary> /// Extract the rest of the line into my variable /// </summary> /// <param name="line"></param> public ExtractedInfo(LineInfo line) { mLine = line; ExtractedFrom = line.mCurrentPos; ExtractedTo = line.mLineStr.Length - 1; mCustomExtractedString = null; }
/// <summary> /// Extract fields from record and assign values to the object /// </summary> /// <param name="record">Object to assign to</param> /// <param name="line">Line of data</param> /// <param name="values">Array of values extracted</param> /// <returns>true if we processed the line and updated object</returns> public bool StringToRecord(object record, LineInfo line, object[] values) { if (MustIgnoreLine(line.mLineStr)) return false; for (int i = 0; i < RecordInfo.FieldCount; i++) values[i] = RecordInfo.Fields[i].ExtractFieldValue(line); try { // Assign all values via dynamic method that AssignHandler(record, values); return true; } catch (InvalidCastException ex) { // Occurs when a custom converter returns an invalid value for the field. for (int i = 0; i < RecordInfo.FieldCount; i++) { if (values[i] != null && !RecordInfo.Fields[i].FieldTypeInternal.IsInstanceOfType(values[i])) { throw new ConvertException(null, RecordInfo.Fields[i].FieldTypeInternal, RecordInfo.Fields[i].FieldInfo.Name, line.mReader.LineNumber, -1, Messages.Errors.WrongConverter .FieldName(RecordInfo.Fields[i].FieldInfo.Name) .ConverterReturnedType(values[i].GetType().Name) .FieldType(RecordInfo.Fields[i].FieldInfo.FieldType.Name) .Text , ex); } } throw; } }
/// <summary> /// Extract field from current position to specified position /// </summary> /// <param name="line">Record information</param> /// <param name="extractTo">Position to extract to</param> public ExtractedInfo(LineInfo line, int extractTo) { mLine = line; ExtractedFrom = line.mCurrentPos; ExtractedTo = extractTo - 1; mCustomExtractedString = null; }
internal override ExtractedInfo ExtractFieldString(LineInfo line) { if (mIsOptional && line.IsEOL() ) return ExtractedInfo.Empty; if (mQuoteChar == '\0') return BasicExtractString(line); else { if (mTrimMode == TrimMode.Both || mTrimMode == TrimMode.Left) { line.TrimStart(mTrimChars); } string quotedStr = mQuoteChar.ToString(); if (line.StartsWith(quotedStr)) { return StringHelper.ExtractQuotedString(line, mQuoteChar, mQuoteMultiline == MultilineMode.AllowForBoth || mQuoteMultiline == MultilineMode.AllowForRead); } else { if (mQuoteMode == QuoteMode.OptionalForBoth || mQuoteMode == QuoteMode.OptionalForRead) return BasicExtractString(line); else if (line.StartsWithTrim(quotedStr)) throw new BadUsageException(string.Format("The field '{0}' has spaces before the QuotedChar at line {1}. Use the TrimAttribute to by pass this error. Field String: {2}", mFieldInfo.Name, line.mReader.LineNumber, line.CurrentString)); else throw new BadUsageException(string.Format("The field '{0}' not begin with the QuotedChar at line {1}. You can use FieldQuoted(QuoteMode.OptionalForRead) to allow optional quoted field.. Field String: {2}", mFieldInfo.Name, line.mReader.LineNumber, line.CurrentString)); } } }
internal override ExtractedInfo ExtractFieldString(LineInfo line) { if (line.CurrentLength == 0) { if (mIsOptional) return ExtractedInfo.Empty; else throw new BadUsageException("End Of Line found processing the field: " + mFieldInfo.Name + " at line "+ line.mReader.LineNumber.ToString() + ". (You need to mark it as [FieldOptional] if you want to avoid this exception)"); } ExtractedInfo res; if (line.CurrentLength < this.mFieldLength) if (mFixedMode == FixedMode.AllowLessChars || mFixedMode == FixedMode.AllowVariableLength) res = new ExtractedInfo(line); else throw new BadUsageException("The string '" + line.CurrentString + "' (length " + line.CurrentLength.ToString() + ") at line "+ line.mReader.LineNumber.ToString() + " has less chars than the defined for " + mFieldInfo.Name + " (" + mFieldLength.ToString() + "). You can use the [FixedLengthRecord(FixedMode.AllowLessChars)] to avoid this problem."); else if (line.CurrentLength > mFieldLength && mIsArray == false && mIsLast && mFixedMode != FixedMode.AllowMoreChars && mFixedMode != FixedMode.AllowVariableLength) throw new BadUsageException("The string '" + line.CurrentString + "' (length " + line.CurrentLength.ToString() + ") at line "+ line.mReader.LineNumber.ToString() + " has more chars than the defined for the last field " + mFieldInfo.Name + " (" + mFieldLength.ToString() + ").You can use the [FixedLengthRecord(FixedMode.AllowMoreChars)] to avoid this problem."); else res = new ExtractedInfo(line, line.mCurrentPos + mFieldLength); return res; }
public ExtractedInfo(LineInfo line, int extractTo) { mLine = line; ExtractedFrom = line.mCurrentPos; ExtractedTo = extractTo - 1; //CharsRemoved = ExtractedTo - ExtractedFrom + 1; //ExtraLines = 0; // NewRestOfLine = null; }
/// <summary> /// Extract the field from the delimited file, removing separators and quotes /// and any duplicate quotes within the record /// </summary> /// <param name="line">line containing record input</param> /// <returns>Extract information</returns> internal override ExtractedInfo ExtractFieldString(LineInfo line) { if (IsOptional && line.IsEOL()) return ExtractedInfo.Empty; if (QuoteChar == '\0') return BasicExtractString(line); else { if (TrimMode == TrimMode.Both || TrimMode == TrimMode.Left) line.TrimStart(TrimChars); string quotedStr = QuoteChar.ToString(); if (line.StartsWith(quotedStr)) { var res = StringHelper.ExtractQuotedString(line, QuoteChar, QuoteMultiline == MultilineMode.AllowForBoth || QuoteMultiline == MultilineMode.AllowForRead); if (TrimMode == TrimMode.Both || TrimMode == TrimMode.Right) line.TrimStart(TrimChars); if (!IsLast && !line.StartsWith(Separator) && !line.IsEOL()) { throw new BadUsageException(line, "The field " + this.FieldInfo.Name + " is quoted but the quoted char: " + quotedStr + " not is just before the separator (You can use [FieldTrim] to avoid this error)"); } return res; } else { if (QuoteMode == QuoteMode.OptionalForBoth || QuoteMode == QuoteMode.OptionalForRead) return BasicExtractString(line); else if (line.StartsWithTrim(quotedStr)) { throw new BadUsageException( string.Format( "The field '{0}' has spaces before the QuotedChar at line {1}. Use the TrimAttribute to by pass this error. Field String: {2}", FieldInfo.Name, line.mReader.LineNumber, line.CurrentString)); } else { throw new BadUsageException( string.Format( "The field '{0}' does not begin with the QuotedChar at line {1}. You can use FieldQuoted(QuoteMode.OptionalForRead) to allow optional quoted field. Field String: {2}", FieldInfo.Name, line.mReader.LineNumber, line.CurrentString)); } } } }
internal BadUsageException(LineInfo line, string message) : this(line.mReader.LineNumber, line.mCurrentPos, message) { }
internal object AssignFromString(ExtractedInfo fieldString, LineInfo line) { object val; switch (mTrimMode) { case TrimMode.None: break; case TrimMode.Both: fieldString.TrimBoth(mTrimChars); break; case TrimMode.Left: fieldString.TrimStart(mTrimChars); break; case TrimMode.Right: fieldString.TrimEnd(mTrimChars); break; } try { if (mConvertProvider == null) { if (mIsStringField) { val = fieldString.ExtractedString(); } else { // Trim it to use Convert.ChangeType fieldString.TrimBoth(WhitespaceChars); if (fieldString.Length == 0) { // Empty stand for null val = GetNullValue(); } else { val = Convert.ChangeType(fieldString.ExtractedString(), mFieldType, null); } } } else { if (mConvertProvider.CustomNullHandling == false && fieldString.HasOnlyThisChars(WhitespaceChars)) { val = GetNullValue(); } else { string from = fieldString.ExtractedString(); val = mConvertProvider.StringToField(from); if (val == null) { val = GetNullValue(); } } } return(val); } catch (ConvertException ex) { throw ConvertException.ReThrowException(ex, mFieldInfo.Name, line.mReader.LineNumber, fieldString.ExtractedFrom + 1); } }
internal object StringToRecord(LineInfo line) { if (MustIgnoreLine(line.mLineStr)) return null; object[] mValues = new object[mFieldCount]; // array that holds the fields values for (int i = 0; i < mFieldCount; i++) { mValues[i] = mFields[i].ExtractValue(line); } #if NET_1_1 || MINI object record = CreateRecordObject(); for (int i = 0; i < mFieldCount; i++) { mFields[i].mFieldInfo.SetValue(record, mValues[i]); } return record; #else CreateAssingMethods(); try { // Asign all values via dinamic method that creates an object and assign values return mCreateHandler(mValues); } catch (InvalidCastException) { // Occurrs when the a custom converter returns an invalid value for the field. for (int i = 0; i < mFieldCount; i++) { if (mValues[i] != null && ! mFields[i].mFieldType.IsInstanceOfType(mValues[i])) throw new ConvertException(null, mFields[i].mFieldType, mFields[i].mFieldInfo.Name, line.mReader.LineNumber, -1, "The converter for the field: " + mFields[i].mFieldInfo.Name + " returns an object of Type: " + mValues[i].GetType().Name + " and the field is of type: " + mFields[i].mFieldType.Name); } return null; } #endif }
/// <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, mRecordTypeName = RecordInfo.RecordType.Name }; 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()); }
/// <summary> /// Get the data out of the records /// </summary> /// <param name="line">Line handler containing text</param> /// <returns></returns> internal object ExtractFieldValue(LineInfo line) { //-> extract only what I need if (InNewLine) { // Any trailing characters, terminate if (line.EmptyFromPos() == false) { throw new BadUsageException(line, "Text '" + line.CurrentString + "' found before the new line of the field: " + FieldInfo.Name + " (this is not allowed when you use [FieldInNewLine])"); } line.ReLoad(line.mReader.ReadNextLine()); if (line.mLineStr == null) { throw new BadUsageException(line, "End of stream found parsing the field " + FieldInfo.Name + ". Please check the class record."); } } if (IsArray == false) { ExtractedInfo info = ExtractFieldString(line); if (info.mCustomExtractedString == null) { line.mCurrentPos = info.ExtractedTo + 1; } line.mCurrentPos += CharsToDiscard; //total; if (Discarded) { return(GetDiscardedNullValue()); } else { return(AssignFromString(info, line).Value); } } else { if (ArrayMinLength <= 0) { ArrayMinLength = 0; } int i = 0; var res = new ArrayList(Math.Max(ArrayMinLength, 10)); while (line.mCurrentPos - CharsToDiscard < line.mLineStr.Length && i < ArrayMaxLength) { ExtractedInfo info = ExtractFieldString(line); if (info.mCustomExtractedString == null) { line.mCurrentPos = info.ExtractedTo + 1; } line.mCurrentPos += CharsToDiscard; try { var value = AssignFromString(info, line); if (value.NullValueUsed && i == 0 && line.IsEOL()) { break; } res.Add(value.Value); } catch (NullValueNotFoundException) { if (i == 0) { break; } else { throw; } } i++; } if (res.Count < ArrayMinLength) { throw new InvalidOperationException( string.Format( "Line: {0} Column: {1} Field: {2}. The array has only {3} values, less than the minimum length of {4}", line.mReader.LineNumber.ToString(), line.mCurrentPos.ToString(), FieldInfo.Name, res.Count, ArrayMinLength)); } else if (IsLast && line.IsEOL() == false) { throw new InvalidOperationException( string.Format( "Line: {0} Column: {1} Field: {2}. The array has more values than the maximum length of {3}", line.mReader.LineNumber, line.mCurrentPos, FieldInfo.Name, ArrayMaxLength)); } // TODO: is there a reason we go through all the array processing then discard it if (Discarded) { return(null); } else { return(res.ToArray(ArrayType)); } } }
/// <summary>Creates an instance of an NullValueNotFoundException.</summary> /// <param name="message">The exception Message</param> /// <param name="line">Line to display in message</param> internal NullValueNotFoundException(LineInfo line, string message) : base(line, message) { }
/// <summary> /// Convert a null value into a representation, /// allows for a null value override /// </summary> /// <param name="line">input line to read, used for error messages</param> /// <returns>Null value for object</returns> private object GetNullValue(LineInfo line) { if (NullValue == null) { if (FieldTypeInternal.IsValueType) { if (IsNullableType) return null; string msg = "Not value found for the value type field: '" + FieldInfo.Name + "' Class: '" + FieldInfo.DeclaringType.Name + "'. " + Environment.NewLine + "You must use the [FieldNullValue] attribute because this is a value type and can't be null or use a Nullable Type instead of the current type."; throw new NullValueNotFoundException(line, msg); } else return null; } else return NullValue; }
private ExtractedInfo BasicExtractString(LineInfo line) { if (IsLast && !IsArray) { var sepPos = line.IndexOf(Separator); if (sepPos == -1) { return(new ExtractedInfo(line)); } // Now check for one extra separator var msg = string.Format( "Delimiter '{0}' found after the last field '{1}' (the file is wrong or you need to add a field to the record class)", Separator, this.FieldInfo.Name, line.mReader.LineNumber); throw new BadUsageException(line.mReader.LineNumber, line.mCurrentPos, msg); } else { int sepPos = line.IndexOf(Separator); if (sepPos == -1) { if (IsLast && IsArray) { return(new ExtractedInfo(line)); } if (NextIsOptional == false) { string msg; if (IsFirst && line.EmptyFromPos()) { msg = string.Format( "The line {0} is empty. Maybe you need to use the attribute [IgnoreEmptyLines] in your record class.", line.mReader.LineNumber); } else { msg = string.Format( "Delimiter '{0}' not found after field '{1}' (the record has less fields, the delimiter is wrong or the next field must be marked as optional).", Separator, this.FieldInfo.Name, line.mReader.LineNumber); } throw new FileHelpersException(line.mReader.LineNumber, line.mCurrentPos, msg); } else { sepPos = line.mLineStr.Length; } } return(new ExtractedInfo(line, sepPos)); } }
public ExtractedInfo(LineInfo line, int extractTo) { mLine = line; ExtractedFrom = line.mCurrentPos; ExtractedTo = extractTo - 1; }
public ExtractedInfo(LineInfo line) { mLine = line; ExtractedFrom = line.mCurrentPos; ExtractedTo = line.mLine.Length - 1; }
private void ReadNextRecord() { string currentLine = mAsyncReader.ReadNextLine(); mLineNumber++; bool byPass = false; #if !GENERICS mLastRecord = null; #else mLastRecord = default(T); #endif LineInfo line = new LineInfo(string.Empty); line.mReader = mAsyncReader; while (true) { if (currentLine != null) { try { mTotalRecords++; line.ReLoad(currentLine); #if !GENERICS mLastRecord = mRecordInfo.StringToRecord(line); #else mLastRecord = (T)mRecordInfo.StringToRecord(line); #endif 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: ErrorInfo err = new ErrorInfo(); err.mLineNumber = mAsyncReader.LineNumber; err.mExceptionInfo = ex; // err.mColumnNumber = mColumnNum; err.mRecordString = currentLine; mErrorManager.AddError(err); break; } } finally { if (byPass == false) { currentLine = mAsyncReader.ReadNextLine(); mLineNumber = mAsyncReader.LineNumber; } } } else { #if !GENERICS mLastRecord = null; #else mLastRecord = default(T); #endif if (mRecordInfo.mIgnoreLast > 0) { mFooterText = mAsyncReader.RemainingText; } try { mAsyncReader.Close(); //mAsyncReader = null; } catch { } return; } } }
internal object ExtractFieldValue(LineInfo line) { //-> extract only what I need if (InNewLine) { if (line.EmptyFromPos() == false) throw new BadUsageException(line, "Text '" + line.CurrentString + "' found before the new line of the field: " + FieldInfo.Name + " (this is not allowed when you use [FieldInNewLine])"); line.ReLoad(line.mReader.ReadNextLine()); if (line.mLineStr == null) throw new BadUsageException(line, "End of stream found parsing the field " + FieldInfo.Name + ". Please check the class record."); } if (IsArray == false) { ExtractedInfo info = ExtractFieldString(line); if (info.mCustomExtractedString == null) line.mCurrentPos = info.ExtractedTo + 1; line.mCurrentPos += CharsToDiscard; //total; return AssignFromString(info, line); } else { if (ArrayMinLength <= 0) ArrayMinLength = 0; int i = 0; var res = new ArrayList(Math.Max(ArrayMinLength, 10)); while (line.mCurrentPos - CharsToDiscard < line.mLine.Length && i < ArrayMaxLength) { ExtractedInfo info = ExtractFieldString(line); if (info.mCustomExtractedString == null) line.mCurrentPos = info.ExtractedTo + 1; line.mCurrentPos += CharsToDiscard; res.Add(AssignFromString(info, line)); i++; } if (res.Count < ArrayMinLength) throw new InvalidOperationException(string.Format("Line: {0} Column: {1} Field: {2}. The array has only {3} values, less than the minimum length of {4}", line.mReader.LineNumber.ToString(), line.mCurrentPos.ToString(), FieldInfo.Name, res.Count, ArrayMinLength)); else if (IsLast && line.IsEOL() == false) throw new InvalidOperationException(string.Format("Line: {0} Column: {1} Field: {2}. The array has more values than the maximum length of {3}", line.mReader.LineNumber, line.mCurrentPos, FieldInfo.Name, ArrayMaxLength)); return res.ToArray(ArrayType); } }
internal object AssignFromString(ExtractedInfo fieldString, LineInfo line) { object val; var extractedString = fieldString.ExtractedString(); var trimmedBoth = false; switch (TrimMode) { case TrimMode.None: break; case TrimMode.Both: extractedString = extractedString.Trim(); trimmedBoth = true; //fieldString.TrimBoth(TrimChars); break; case TrimMode.Left: extractedString = extractedString.TrimStart(); //fieldString.TrimStart(TrimChars); break; case TrimMode.Right: extractedString = extractedString.TrimEnd(); //fieldString.TrimEnd(TrimChars); break; } try { if (ConvertProvider == null) { if (IsStringField) val = extractedString; else { // Trim it to use Convert.ChangeType if (trimmedBoth == false) extractedString = extractedString.Trim(); if (extractedString.Length == 0) { // Empty stand for null val = GetNullValue(line); } else { val = Convert.ChangeType(extractedString, FieldTypeInternal, null); } } } else { var trimmedString = extractedString; if (trimmedBoth == false) { trimmedString = extractedString.Trim(); } if (ConvertProvider.CustomNullHandling == false && trimmedString.Length == 0) { val = GetNullValue(line); } else { string from = extractedString; val = ConvertProvider.StringToField(from); if (val == null) val = GetNullValue(line); } } return val; } catch (ConvertException ex) { ex.FieldName = FieldInfo.Name; ex.LineNumber = line.mReader.LineNumber; ex.ColumnNumber = fieldString.ExtractedFrom + 1; throw; } catch (BadUsageException) { throw; } catch (Exception ex) { if (ConvertProvider == null || ConvertProvider.GetType().Assembly == typeof(FieldBase).Assembly) throw new ConvertException(extractedString, FieldTypeInternal, FieldInfo.Name, line.mReader.LineNumber, fieldString.ExtractedFrom + 1, ex.Message, ex); else throw new ConvertException(extractedString, FieldTypeInternal, FieldInfo.Name, line.mReader.LineNumber, fieldString.ExtractedFrom + 1, "Your custom converter: " + ConvertProvider.GetType().Name + " throws an " + ex.GetType().Name + " with the message: " + ex.Message, ex); } }
/// <summary>Creates an instance of an BadUsageException.</summary> /// <param name="message">The exception Message</param> /// <param name="line">Line to display in message</param> internal BadUsageException(LineInfo line, string message) : this(line.mReader.LineNumber, line.mCurrentPos, message) { }
/// <summary> /// Get the data out of the records /// </summary> /// <param name="line">Line handler containing text</param> /// <returns></returns> internal object ExtractFieldValue(LineInfo line) { //-> extract only what I need if (InNewLine) { // Any trailing characters, terminate if (line.EmptyFromPos() == false) { throw new BadUsageException(line, "Text '" + line.CurrentString + "' found before the new line of the field: " + FieldInfo.Name + " (this is not allowed when you use [FieldInNewLine])"); } line.ReLoad(line.mReader.ReadNextLine()); if (line.mLineStr == null) { throw new BadUsageException(line, "End of stream found parsing the field " + FieldInfo.Name + ". Please check the class record."); } } if (IsArray == false) { ExtractedInfo info = ExtractFieldString(line); if (info.mCustomExtractedString == null) line.mCurrentPos = info.ExtractedTo + 1; line.mCurrentPos += CharsToDiscard; //total; if (Discarded) return GetDiscardedNullValue(); else return AssignFromString(info, line).Value; } else { if (ArrayMinLength <= 0) ArrayMinLength = 0; int i = 0; var res = new ArrayList(Math.Max(ArrayMinLength, 10)); while (line.mCurrentPos - CharsToDiscard < line.mLineStr.Length && i < ArrayMaxLength) { ExtractedInfo info = ExtractFieldString(line); if (info.mCustomExtractedString == null) line.mCurrentPos = info.ExtractedTo + 1; line.mCurrentPos += CharsToDiscard; try { var value = AssignFromString(info, line); if (value.NullValueUsed && i == 0 && line.IsEOL()) break; res.Add(value.Value); } catch (NullValueNotFoundException) { if (i == 0) break; else throw; } i++; } if (res.Count < ArrayMinLength) { throw new InvalidOperationException( string.Format( "Line: {0} Column: {1} Field: {2}. The array has only {3} values, less than the minimum length of {4}", line.mReader.LineNumber.ToString(), line.mCurrentPos.ToString(), FieldInfo.Name, res.Count, ArrayMinLength)); } else if (IsLast && line.IsEOL() == false) { throw new InvalidOperationException( string.Format( "Line: {0} Column: {1} Field: {2}. The array has more values than the maximum length of {3}", line.mReader.LineNumber, line.mCurrentPos, FieldInfo.Name, ArrayMaxLength)); } // TODO: is there a reason we go through all the array processing then discard it if (Discarded) return null; else return res.ToArray(ArrayType); } }
/// <summary> /// Extract the field from the delimited file, removing separators and quotes /// and any duplicate quotes within the record /// </summary> /// <param name="line">line containing record input</param> /// <returns>Extract information</returns> internal override ExtractedInfo ExtractFieldString(LineInfo line) { if (IsOptional && line.IsEOL()) { return(ExtractedInfo.Empty); } if (QuoteChar == '\0') { return(BasicExtractString(line)); } else { if (TrimMode == TrimMode.Both || TrimMode == TrimMode.Left) { line.TrimStart(TrimChars); } string quotedStr = QuoteChar.ToString(); if (line.StartsWith(quotedStr)) { var res = StringHelper.ExtractQuotedString(line, QuoteChar, QuoteMultiline == MultilineMode.AllowForBoth || QuoteMultiline == MultilineMode.AllowForRead); if (TrimMode == TrimMode.Both || TrimMode == TrimMode.Right) { line.TrimStart(TrimChars); } if (!IsLast && !line.StartsWith(Separator) && !line.IsEOL()) { throw new BadUsageException(line, "The field " + this.FieldInfo.Name + " is quoted but the quoted char: " + quotedStr + " not is just before the separator (You can use [FieldTrim] to avoid this error)"); } return(res); } else { if (QuoteMode == QuoteMode.OptionalForBoth || QuoteMode == QuoteMode.OptionalForRead) { return(BasicExtractString(line)); } else if (line.StartsWithTrim(quotedStr)) { throw new BadUsageException( string.Format( "The field '{0}' has spaces before the QuotedChar at line {1}. Use the TrimAttribute to by pass this error. Field String: {2}", FieldInfo.Name, line.mReader.LineNumber, line.CurrentString)); } else { throw new BadUsageException( string.Format( "The field '{0}' does not begin with the QuotedChar at line {1}. You can use FieldQuoted(QuoteMode.OptionalForRead) to allow optional quoted field. Field String: {2}", FieldInfo.Name, line.mReader.LineNumber, line.CurrentString)); } } } }
protected override ExtractedInfo ExtractFieldString(LineInfo line) { if (mIsOptional && line.IsEOL()) { return(ExtractedInfo.Empty); } if (mQuoteChar == '\0') { return(BasicExtractString(line)); } else { //TODO: UnComment and Fix if (mTrimMode == TrimMode.Both || mTrimMode == TrimMode.Left) { //int pos = line.mCurrentPos; line.TrimStart(mTrimChars); // from2 = from.TrimStart(mTrimChars); //res.CharsRemoved = line.mCurrentPos - pos; } string quotedStr = mQuoteChar.ToString(); if (line.StartsWith(quotedStr)) { // ExtractedInfo res = null; // res = new ExtractedInfo(line, line.mCurrentPos); return(StringHelper.ExtractQuotedString(line, mQuoteChar, mQuoteMultiline == MultilineMode.AllowForBoth || mQuoteMultiline == MultilineMode.AllowForRead)); // if (mQuoteMultiline == MultilineMode.AllowForBoth || mQuoteMultiline == MultilineMode.AllowForRead) // { // // //res.ExtractedString = ei.ExtractedString; // //res.CharsRemoved += ei.CharsRemoved; // //res.ExtraLines = ei.ExtraLines; // //res.NewRestOfLine = ei.NewRestOfLine; // } // else // { // return StringHelper.ExtractQuotedString(from2, mQuoteChar, out index); // //res.CharsRemoved += index; // } // return res; } else { if (mQuoteMode == QuoteMode.OptionalForBoth || mQuoteMode == QuoteMode.OptionalForRead) { return(BasicExtractString(line)); } else if (line.StartsWithTrim(quotedStr)) { throw new BadUsageException("The field '" + this.mFieldInfo.Name + "' has spaces before the QuotedChar at line " + line.mReader.LineNumber.ToString() + ". Use the TrimAttribute to by pass this error. Field String: " + line.CurrentString); } else { throw new BadUsageException("The field '" + this.mFieldInfo.Name + "' not begin with the QuotedChar at line " + line.mReader.LineNumber.ToString() + ". You can use FieldQuoted(QuoteMode.OptionalForRead) to allow optional quoted field.. Field String: " + line.CurrentString); } } } }
public object StringToRecord(LineInfo line, object[] values) { if (MustIgnoreLine(line.mLineStr)) return null; for (int i = 0; i < RecordInfo.FieldCount; i++) { values[i] = RecordInfo.Fields[i].ExtractFieldValue(line); } try { // Asign all values via dinamic method that creates an object and assign values return CreateHandler(values); } catch (InvalidCastException ex) { // Occurrs when the a custom converter returns an invalid value for the field. for (int i = 0; i < RecordInfo.FieldCount; i++) { if (values[i] != null && !RecordInfo.Fields[i].FieldTypeInternal.IsInstanceOfType(values[i])) throw new ConvertException(null, RecordInfo.Fields[i].FieldTypeInternal, RecordInfo.Fields[i].FieldInfo.Name, line.mReader.LineNumber, -1, Messages.Errors.WrongConverter .FieldName(RecordInfo.Fields[i].FieldInfo.Name) .ConverterReturnedType(values[i].GetType().Name) .FieldType(RecordInfo.Fields[i].FieldInfo.FieldType.Name) .Text , ex); } return null; } }
/// <summary> /// Extract the string from the underlying data, removes quotes /// characters for example /// </summary> /// <param name="line">Line to parse data from</param> /// <returns>Slightly processed string from the data</returns> internal abstract ExtractedInfo ExtractFieldString(LineInfo line);
/// <include file='MultiRecordEngine.docs.xml' path='doc/ReadStream/*'/> public object[] 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, 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 record = info.StringToRecord(line); #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()); }
/// <summary> /// Create field object after extracting the string from the underlying /// input data /// </summary> /// <param name="fieldString">Information extracted?</param> /// <param name="line">Underlying input data</param> /// <returns>Object to assign to field</returns> private AssignResult AssignFromString(ExtractedInfo fieldString, LineInfo line) { var extractedString = fieldString.ExtractedString(); try { object val; if (IsNotEmpty && String.IsNullOrEmpty(extractedString)) { throw new InvalidOperationException("The value is empty and must be populated."); } else if (Converter == null) { if (IsStringField) { val = TrimString(extractedString); } else { extractedString = extractedString.Trim(); if (extractedString.Length == 0) { return(new AssignResult { Value = GetNullValue(line), NullValueUsed = true }); } else { val = Convert.ChangeType(extractedString, FieldTypeInternal, null); } } } else { var trimmedString = extractedString.Trim(); if (Converter.CustomNullHandling == false && trimmedString.Length == 0) { return(new AssignResult { Value = GetNullValue(line), NullValueUsed = true }); } else { if (TrimMode == TrimMode.Both) { val = Converter.StringToField(trimmedString); } else { val = Converter.StringToField(TrimString(extractedString)); } if (val == null) { return(new AssignResult { Value = GetNullValue(line), NullValueUsed = true }); } } } return(new AssignResult { Value = val }); } catch (ConvertException ex) { ex.FieldName = FieldInfo.Name; ex.LineNumber = line.mReader.LineNumber; ex.ColumnNumber = fieldString.ExtractedFrom + 1; throw; } catch (BadUsageException) { throw; } catch (Exception ex) { if (Converter == null || Converter.GetType().Assembly == typeof(FieldBase).Assembly) { throw new ConvertException(extractedString, FieldTypeInternal, FieldInfo.Name, line.mReader.LineNumber, fieldString.ExtractedFrom + 1, ex.Message, ex); } else { throw new ConvertException(extractedString, FieldTypeInternal, FieldInfo.Name, line.mReader.LineNumber, fieldString.ExtractedFrom + 1, "Your custom converter: " + Converter.GetType().Name + " throws an " + ex.GetType().Name + " with the message: " + ex.Message, ex); } } }
private void ReadNextRecord() { string currentLine = mAsyncReader.ReadNextLine(); mLineNumber++; bool byPass = false; mLastRecord = null; LineInfo line = new LineInfo(currentLine); line.mReader = mAsyncReader; while (true) { if (currentLine != null) { try { mTotalRecords++; Type currType = mRecordSelector(this, currentLine); line.ReLoad(currentLine); if (currType != null) { RecordInfo info = (RecordInfo)mRecordInfoHash[currType]; mLastRecord = info.StringToRecord(line); 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: ErrorInfo err = new ErrorInfo(); err.mLineNumber = mAsyncReader.LineNumber; err.mExceptionInfo = ex; // err.mColumnNumber = mColumnNum; err.mRecordString = currentLine; mErrorManager.AddError(err); break; } } finally { if (byPass == false) { currentLine = mAsyncReader.ReadNextLine(); mLineNumber = mAsyncReader.LineNumber; } } } else { mLastRecord = null; if (mRecordInfo.mIgnoreLast > 0) { mFooterText = mAsyncReader.RemainingText; } try { mAsyncReader.Close(); } catch { } return; } } }
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, mRecordTypeName = RecordInfo.RecordType.Name }; 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; } } }
private ExtractedInfo BasicExtractString(LineInfo line) { ExtractedInfo res; if (mIsLast) res = new ExtractedInfo(line); else { int sepPos; sepPos = line.IndexOf(mSeparator); if (sepPos == -1) { if (this.mNextIsOptional == false) { string msg = null; if (mIsFirst && line.EmptyFromPos()) msg = "The line " + line.mReader.LineNumber.ToString() + " is empty. Maybe you need to use the attribute [IgnoreEmptyLines] in your record class."; else msg = "The delimiter '" + this.mSeparator + "' can´t be found after the field '" + this.mFieldInfo.Name + "' at line " + line.mReader.LineNumber.ToString() + " (the record has less fields, the delimiter is wrong or the next field must be marked as optional)."; throw new FileHelpersException(msg); } else sepPos = line.mLine.Length - 1; } res = new ExtractedInfo(line, sepPos); } return res; }
protected abstract ExtractedInfo ExtractFieldString(LineInfo line);
private ExtractedInfo BasicExtractString(LineInfo line) { if (IsLast && ! IsArray) return new ExtractedInfo(line); else { int sepPos; sepPos = line.IndexOf(mSeparator); if (sepPos == -1) { if (IsLast && IsArray) return new ExtractedInfo(line); if (NextIsOptional == false) { string msg; if (IsFirst && line.EmptyFromPos()) msg = string.Format("The line {0} is empty. Maybe you need to use the attribute [IgnoreEmptyLines] in your record class.", line.mReader.LineNumber); else msg = string.Format("Delimiter '{0}' not found after field '{1}' (the record has less fields, the delimiter is wrong or the next field must be marked as optional).", mSeparator, this.FieldInfo.Name, line.mReader.LineNumber); throw new FileHelpersException(line.mReader.LineNumber, line.mCurrentPos, msg); } else sepPos = line.mLine.Length; } return new ExtractedInfo(line, sepPos); } }
internal object ExtractFieldValue(LineInfo line) { //-> extract only what I need if (mInNewLine) { if (line.EmptyFromPos() == false) { throw new BadUsageException(line, "Text '" + line.CurrentString + "' found before the new line of the field: " + mFieldInfo.Name + " (this is not allowed when you use [FieldInNewLine])"); } line.ReLoad(line.mReader.ReadNextLine()); if (line.mLineStr == null) { throw new BadUsageException(line, "End of stream found parsing the field " + mFieldInfo.Name + ". Please check the class record."); } } if (mIsArray == false) { ExtractedInfo info = ExtractFieldString(line); if (info.mCustomExtractedString == null) { line.mCurrentPos = info.ExtractedTo + 1; } line.mCurrentPos += mCharsToDiscard; //total; return(AssignFromString(info, line)); } else { if (mArrayMinLength <= 0) { mArrayMinLength = 0; } int i = 0; ArrayList res = new ArrayList(Math.Max(mArrayMinLength, 10)); while (line.mCurrentPos - mCharsToDiscard < line.mLine.Length && i < mArrayMaxLength) { ExtractedInfo info = ExtractFieldString(line); if (info.mCustomExtractedString == null) { line.mCurrentPos = info.ExtractedTo + 1; } line.mCurrentPos += mCharsToDiscard; res.Add(AssignFromString(info, line)); i++; } if (res.Count < mArrayMinLength) { throw new InvalidOperationException(string.Format("Line: {0} Column: {1} Field: {2}. The array has only {3} values, less than the minimum length of {4}", line.mReader.LineNumber.ToString(), line.mCurrentPos.ToString(), mFieldInfo.Name, res.Count, mArrayMinLength)); } else if (mIsLast && line.IsEOL() == false) { throw new InvalidOperationException(string.Format("Line: {0} Column: {1} Field: {2}. The array has more values than the maximum length of {3}", line.mReader.LineNumber, line.mCurrentPos, mFieldInfo.Name, mArrayMaxLength)); } return(res.ToArray(mArrayType)); } }
internal object AssignFromString(ExtractedInfo fieldString, LineInfo line) { object val; switch (mTrimMode) { case TrimMode.None: break; case TrimMode.Both: fieldString.TrimBoth(mTrimChars); break; case TrimMode.Left: fieldString.TrimStart(mTrimChars); break; case TrimMode.Right: fieldString.TrimEnd(mTrimChars); break; } try { if (mConvertProvider == null) { if (mIsStringField) { val = fieldString.ExtractedString(); } else { // Trim it to use Convert.ChangeType fieldString.TrimBoth(WhitespaceChars); if (fieldString.Length == 0) { // Empty stand for null val = GetNullValue(line); } else { val = Convert.ChangeType(fieldString.ExtractedString(), mFieldTypeInternal, null); } } } else { if (mConvertProvider.CustomNullHandling == false && fieldString.HasOnlyThisChars(WhitespaceChars)) { val = GetNullValue(line); } else { string from = fieldString.ExtractedString(); val = mConvertProvider.StringToField(from); if (val == null) { val = GetNullValue(line); } } } return(val); } catch (ConvertException ex) { throw ConvertException.ReThrowException(ex, mFieldInfo.Name, line.mReader.LineNumber, fieldString.ExtractedFrom + 1); } catch (BadUsageException) { throw; } catch (Exception ex) { if (mConvertProvider == null || mConvertProvider.GetType().Assembly == typeof(FieldBase).Assembly) { throw new ConvertException(fieldString.ExtractedString(), mFieldTypeInternal, mFieldInfo.Name, line.mReader.LineNumber, fieldString.ExtractedFrom + 1, ex.Message, ex); } else { throw new ConvertException(fieldString.ExtractedString(), mFieldTypeInternal, mFieldInfo.Name, line.mReader.LineNumber, fieldString.ExtractedFrom + 1, "Your custom converter: " + mConvertProvider.GetType().Name + " throws an " + ex.GetType().Name + " with the message: " + ex.Message, ex); } } }
internal object AssignFromString(ExtractedInfo fieldString, LineInfo line) { object val; switch (mTrimMode) { case TrimMode.None: break; case TrimMode.Both: fieldString.TrimBoth(mTrimChars); break; case TrimMode.Left: fieldString.TrimStart(mTrimChars); break; case TrimMode.Right: fieldString.TrimEnd(mTrimChars); break; } try { if (mConvertProvider == null) { if (mIsStringField) val = fieldString.ExtractedString(); else { // Trim it to use Convert.ChangeType fieldString.TrimBoth(WhitespaceChars); if (fieldString.Length == 0) { // Empty stand for null val = GetNullValue(line); } else { val = Convert.ChangeType(fieldString.ExtractedString(), mFieldTypeInternal, null); } } } else { if (mConvertProvider.CustomNullHandling == false && fieldString.HasOnlyThisChars(WhitespaceChars)) { val = GetNullValue(line); } else { string from = fieldString.ExtractedString(); val = mConvertProvider.StringToField(from); if (val == null) val = GetNullValue(line); } } return val; } catch (ConvertException ex) { throw ConvertException.ReThrowException(ex, mFieldInfo.Name, line.mReader.LineNumber, fieldString.ExtractedFrom + 1); } catch (BadUsageException) { throw; } catch (Exception ex) { if (mConvertProvider == null || mConvertProvider.GetType().Assembly == typeof(FieldBase).Assembly) throw new ConvertException(fieldString.ExtractedString(), mFieldTypeInternal, mFieldInfo.Name, line.mReader.LineNumber, fieldString.ExtractedFrom + 1, ex.Message, ex); else throw new ConvertException(fieldString.ExtractedString(), mFieldTypeInternal, mFieldInfo.Name, line.mReader.LineNumber, fieldString.ExtractedFrom + 1, "Your custom converter: " + mConvertProvider.GetType().Name + " throws an " + ex.GetType().Name + " with the message: " + ex.Message, ex); } }
// object[] values, int index, ForwardReader reader internal object ExtractValue(LineInfo line) { //-> extract only what I need if (this.mInNewLine == true) { if (line.EmptyFromPos() == false) throw new BadUsageException("Text '" + line.CurrentString + "' found before the new line of the field: " + mFieldInfo.Name + " (this is not allowed when you use [FieldInNewLine])"); line.ReLoad(line.mReader.ReadNextLine()); if (line.mLineStr == null) throw new BadUsageException("End of stream found parsing the field " + mFieldInfo.Name + ". Please check the class record."); } ExtractedInfo info = ExtractFieldString(line); if (info.mCustomExtractedString == null) line.mCurrentPos = info.ExtractedTo + 1; line.mCurrentPos += mCharsToDiscard; //total; return AssignFromString(info, line); //-> discard the part that I use //TODO: Uncoment this for Quoted Handling // if (info.NewRestOfLine != null) // { // if (info.NewRestOfLine.Length < CharsToDiscard()) // return info.NewRestOfLine; // else // return info.NewRestOfLine.Substring(CharsToDiscard()); // } // else // { // int total; // if (info.CharsRemoved >= line.mLine.Length) // total = line.mLine.Length; // else // total = info.CharsRemoved + CharsToDiscard(); //return buffer.Substring(total); // } }
/// <summary> /// Allow a default string or a specific string for this /// variable to be applied /// </summary> /// <param name="customExtract"></param> public ExtractedInfo(string customExtract) { mLine = null; ExtractedFrom = 0; ExtractedTo = 0; mCustomExtractedString = customExtract; }
internal object AssignFromString(ExtractedInfo fieldString, LineInfo line) { object val; switch (mTrimMode) { case TrimMode.None: break; case TrimMode.Both: fieldString.TrimBoth(mTrimChars); break; case TrimMode.Left: fieldString.TrimStart(mTrimChars); break; case TrimMode.Right: fieldString.TrimEnd(mTrimChars); break; } try { if (mConvertProvider == null) { if (mIsStringField) val = fieldString.ExtractedString(); else { // Trim it to use Convert.ChangeType fieldString.TrimBoth(WhitespaceChars); if (fieldString.Length == 0) { // Empty stand for null val = GetNullValue(); } else { val = Convert.ChangeType(fieldString.ExtractedString(), mFieldType, null); } } } else { if (mConvertProvider.CustomNullHandling == false && fieldString.HasOnlyThisChars(WhitespaceChars)) { val = GetNullValue(); } else { string from = fieldString.ExtractedString(); val = mConvertProvider.StringToField(from); if (val == null) val = GetNullValue(); } } return val; } catch (ConvertException ex) { throw ConvertException.ReThrowException(ex, mFieldInfo.Name, line.mReader.LineNumber, fieldString.ExtractedFrom + 1); } }
/// <summary> /// Extract a string from a quoted string, allows for doubling the quotes /// for example 'o''clock' /// </summary> /// <param name="line">Line to extract from (with extra info)</param> /// <param name="quoteChar">Quote char to remove</param> /// <param name="allowMultiline">can we have a new line in middle of string</param> /// <returns>Extracted information</returns> internal static ExtractedInfo ExtractQuotedString(LineInfo line, char quoteChar, bool allowMultiline) { // if (line.mReader == null) // throw new BadUsageException("The reader can´t be null"); if (line.IsEOL()) { throw new BadUsageException("An empty String found. This can not be parsed like a QuotedString try to use SafeExtractQuotedString"); } if (line.mLineStr[line.mCurrentPos] != quoteChar) { throw new BadUsageException("The source string does not begin with the quote char: " + quoteChar); } StringBuilder res = new StringBuilder(32); bool firstFound = false; int i = line.mCurrentPos + 1; //bool mustContinue = true; while (line.mLineStr != null) { while (i < line.mLineStr.Length) { if (line.mLineStr[i] == quoteChar) { if (firstFound) { // Is an escaped quoted char res.Append(quoteChar); firstFound = false; } else { firstFound = true; } } else { if (firstFound) { // This was the end of the string line.mCurrentPos = i; return(new ExtractedInfo(res.ToString())); } else { res.Append(line.mLineStr[i]); } } i++; } if (firstFound) { line.mCurrentPos = i; return(new ExtractedInfo(res.ToString())); } else { if (allowMultiline == false) { throw new BadUsageException("The current field has an unclosed quoted string. Complete line: " + res.ToString()); } line.ReadNextLine(); res.Append(StringHelper.NewLine); //lines++; i = 0; } } throw new BadUsageException("The current field has an unclosed quoted string. Complete Filed String: " + res.ToString()); }
/// <summary> /// Create field object after extracting the string from the underlying /// input data /// </summary> /// <param name="fieldString">Information extracted?</param> /// <param name="line">Underlying input data</param> /// <returns>Object to assign to field</returns> private AssignResult AssignFromString(ExtractedInfo fieldString, LineInfo line) { object val; var extractedString = fieldString.ExtractedString(); try { if (IsNotEmpty && String.IsNullOrEmpty(extractedString)) { throw new InvalidOperationException("The value is empty and must be populated."); } else if (this.Converter == null) { if (IsStringField) val = TrimString(extractedString); else { extractedString = extractedString.Trim(); if (extractedString.Length == 0) { return new AssignResult { Value = GetNullValue(line), NullValueUsed = true }; } else val = Convert.ChangeType(extractedString, FieldTypeInternal, null); } } else { var trimmedString = extractedString.Trim(); if (this.Converter.CustomNullHandling == false && trimmedString.Length == 0) { return new AssignResult { Value = GetNullValue(line), NullValueUsed = true }; } else { if (TrimMode == TrimMode.Both) val = this.Converter.StringToField(trimmedString); else val = this.Converter.StringToField(TrimString(extractedString)); if (val == null) { return new AssignResult { Value = GetNullValue(line), NullValueUsed = true }; } } } return new AssignResult { Value = val }; } catch (ConvertException ex) { ex.FieldName = FieldInfo.Name; ex.LineNumber = line.mReader.LineNumber; ex.ColumnNumber = fieldString.ExtractedFrom + 1; throw; } catch (BadUsageException) { throw; } catch (Exception ex) { if (this.Converter == null || this.Converter.GetType().Assembly == typeof (FieldBase).Assembly) { throw new ConvertException(extractedString, FieldTypeInternal, FieldInfo.Name, line.mReader.LineNumber, fieldString.ExtractedFrom + 1, ex.Message, ex); } else { throw new ConvertException(extractedString, FieldTypeInternal, FieldInfo.Name, line.mReader.LineNumber, fieldString.ExtractedFrom + 1, "Your custom converter: " + this.Converter.GetType().Name + " throws an " + ex.GetType().Name + " with the message: " + ex.Message, ex); } } }
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"); } 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; if (maxRecords < 0) { maxRecords = int.MaxValue; } LineInfo line = new LineInfo(currentLine); line.mReader = freader; while (currentLine != null && currentRecord < maxRecords) { try { mTotalRecords++; currentRecord++; line.ReLoad(currentLine); bool skip = false; #if !MINI ProgressHelper.Notify(mNotifyHandler, mProgressMode, currentRecord, -1); skip = OnBeforeReadRecord(currentLine); #endif if (skip == false) { object record = mRecordInfo.StringToRecord(line); #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)); }
internal static ExtractedInfo ExtractQuotedString(LineInfo line, char quoteChar, bool allowMultiline) { // if (line.mReader == null) // throw new BadUsageException("The reader can´t be null"); if (line.IsEOL()) throw new BadUsageException("An empty String found and can be parsed like a QuotedString try to use SafeExtractQuotedString"); if (line.mLine[line.mCurrentPos] != quoteChar) throw new BadUsageException("The source string not begins with the quote char: " + quoteChar); StringBuilder res = new StringBuilder(32); //int lines = 0; bool firstFound = false; int i = line.mCurrentPos + 1; //bool mustContinue = true; while (line.mLineStr != null) { while (i < line.mLine.Length) { if (line.mLine[i] == quoteChar) { if (firstFound == true) { // Is an escaped quoted char res.Append(quoteChar); firstFound = false; } else { firstFound = true; } } else { if (firstFound) { // This was the end of the string line.mCurrentPos = i; return new ExtractedInfo(res.ToString()); // ExtractedInfo ei = ; // return ei; } else { res.Append(line.mLine[i]); } } i++; } if (firstFound) { line.mCurrentPos = i; return new ExtractedInfo(res.ToString()); } else { if (allowMultiline == false) throw new BadUsageException("The current field has an UnClosed quoted string. Complete line: " + res.ToString()); line.ReadNextLine(); res.Append(StringHelper.NewLine); //lines++; i = 0; } } throw new BadUsageException("The current field has an unclosed quoted string. Complete Filed String: " + res.ToString()); }
/// <summary> /// Extract fields from record and assign values to the object /// </summary> /// <param name="record">Object to assign to</param> /// <param name="line">Line of data</param> /// <param name="values">Array of values extracted</param> /// <returns>true if we processed the line and updated object</returns> public bool StringToRecord(object record, LineInfo line, object[] values) { if (MustIgnoreLine(line.mLineStr)) { return(false); } int maxCustomIndex = mRecordInfo.Fields.ToList().Max(x => x.CustomIndex) + 1; int skipCount = 0; for (int i = 0; i < maxCustomIndex; i++) { var setPos = i - skipCount; var field = mRecordInfo.Fields.FirstOrDefault(f => f.CustomIndex == i) ?? mRecordInfo.Fields[i - skipCount]; bool fieldNotInFile = !mRecordInfo.Fields.ToList().Any(f => f.CustomIndex == i); if (fieldNotInFile) { var tmpField = new DelimitedField(typeof(StringField).GetField("field"), mRecordInfo.Fields[0].Separator); var tmpVal = tmpField.ExtractFieldValue(line); field.ExtractFieldString(line); values[setPos] = field.NullValue; skipCount++;// discard extracted line value and set next iteration to values[i-1] } else { values[setPos] = field.ExtractFieldValue(line); } } try { // Assign all values via dynamic method that AssignHandler(record, values); return(true); } catch (InvalidCastException ex) { // Occurs when a custom converter returns an invalid value for the field. for (int i = 0; i < mRecordInfo.FieldCount; i++) { if (values[i] != null && !mRecordInfo.Fields[i].FieldTypeInternal.IsInstanceOfType(values[i])) { throw new ConvertException(null, mRecordInfo.Fields[i].FieldTypeInternal, mRecordInfo.Fields[i].FieldInfo.Name, line.mReader.LineNumber, -1, Messages.Errors.WrongConverter .FieldName(mRecordInfo.Fields[i].FieldInfo.Name) .ConverterReturnedType(values[i].GetType().Name) .FieldType(mRecordInfo.Fields[i].FieldInfo.FieldType.Name) .Text , ex); } } throw; } }