Пример #1
0
        private bool LoadLine(Tuple <long, string> pair, ref object rec)
        {
            Type recType = rec.GetType();

            try
            {
                if (!RaiseBeforeRecordLoad(rec, ref pair))
                {
                    ChoETLFramework.WriteLog(TraceSwitch.TraceVerbose, "Skipping...");
                    rec = null;
                    return(true);
                }

                if (pair.Item2 == null)
                {
                    rec = null;
                    return(true);
                }
                else if (pair.Item2 == String.Empty)
                {
                    return(true);
                }

                if (!pair.Item2.IsNullOrWhiteSpace())
                {
                    var config = GetConfiguration(rec.GetType());
                    if (config == null)
                    {
                        throw new ChoParserException("No parser found to parse record line.");
                    }

                    if (config.GetType() == typeof(ChoCSVRecordConfiguration))
                    {
                        var r = ChoCSVReader.LoadText(rec.GetType(), pair.Item2, config as ChoCSVRecordConfiguration, Configuration.Encoding, Configuration.BufferSize, TraceSwitch, Reader);
                        rec = r.FirstOrDefault <object>();
                    }
                    else if (config.GetType() == typeof(ChoFixedLengthRecordConfiguration))
                    {
                        var r = ChoFixedLengthReader.LoadText(rec.GetType(), pair.Item2, config as ChoFixedLengthRecordConfiguration, Configuration.Encoding, Configuration.BufferSize, TraceSwitch, Reader);
                        rec = r.FirstOrDefault <object>();
                    }
                    else
                    {
                        throw new ChoParserException("Unsupported record line found to parse.");
                    }
                }

                bool skip = false;
                if (!RaiseAfterRecordLoad(rec, pair, ref skip))
                {
                    return(false);
                }
                else if (skip)
                {
                    rec = null;
                    return(true);
                }
            }
            catch (ChoParserException pEx)
            {
                throw new ChoParserException($"Failed to parse line to '{recType}' object.", pEx);
            }
            catch (ChoMissingRecordFieldException mEx)
            {
                throw new ChoParserException($"Failed to parse line to '{recType}' object.", mEx);
            }
            catch (Exception ex)
            {
                Reader.IsValid = false;
                ChoETLFramework.HandleException(ref ex);
                if (Configuration.ErrorMode == ChoErrorMode.IgnoreAndContinue)
                {
                    ChoETLFramework.WriteLog(TraceSwitch.TraceVerbose, "Error [{0}] found. Ignoring record...".FormatString(ex.Message));
                    rec = null;
                }
                else if (Configuration.ErrorMode == ChoErrorMode.ReportAndContinue)
                {
                    if (!RaiseRecordLoadError(rec, pair, ex))
                    {
                        throw;
                    }
                    else
                    {
                        ChoETLFramework.WriteLog(TraceSwitch.TraceVerbose, "Error [{0}] found. Ignoring record...".FormatString(ex.Message));
                        rec = null;
                    }
                }
                else
                {
                    throw;
                }
                //if (Configuration.ErrorMode == ChoErrorMode.IgnoreAndContinue)
                //{
                //    rec = null;
                //}
                //else if (Configuration.ErrorMode == ChoErrorMode.ReportAndContinue)
                //{
                //    if (!RaiseRecordLoadError(rec, pair, ex))
                //        throw new ChoReaderException($"Failed to parse line to '{recType}' object.", ex);
                //}
                //else
                //    throw new ChoReaderException($"Failed to parse line to '{recType}' object.", ex);

                return(true);
            }

            return(true);
        }
Пример #2
0
 public static ChoCSVReader <T> LoadLines(IEnumerable <string> inputLines, ChoCSVRecordConfiguration configuration = null, TraceSwitch traceSwitch = null)
 {
     return(ChoCSVReader <T> .LoadLines(inputLines, configuration, traceSwitch).WithDelimiter(delimiter));
 }
        private bool LoadLine(Tuple <int, string> pair, ref object rec)
        {
            Type recType = rec.GetType();

            try
            {
                if (!RaiseBeforeRecordLoad(rec, ref pair))
                {
                    return(false);
                }

                if (pair.Item2 == null)
                {
                    rec = null;
                    return(true);
                }
                else if (pair.Item2 == String.Empty)
                {
                    return(true);
                }

                if (!pair.Item2.IsNullOrWhiteSpace())
                {
                    var config = GetConfiguration(rec.GetType());
                    if (config == null)
                    {
                        throw new ChoParserException("No parser found to parse record line.");
                    }

                    if (config.GetType() == typeof(ChoCSVRecordConfiguration))
                    {
                        var r = ChoCSVReader.LoadText(rec.GetType(), pair.Item2, config as ChoCSVRecordConfiguration, Configuration.Encoding, Configuration.BufferSize);
                        rec = r.FirstOrDefault <object>();
                    }
                    else if (config.GetType() == typeof(ChoFixedLengthRecordConfiguration))
                    {
                        var r = ChoFixedLengthReader.LoadText(rec.GetType(), pair.Item2, config as ChoFixedLengthRecordConfiguration, Configuration.Encoding, Configuration.BufferSize);
                        rec = r.FirstOrDefault <object>();
                    }
                    else
                    {
                        throw new ChoParserException("Unsupported record line found to parse.");
                    }
                }

                if (!RaiseAfterRecordLoad(rec, pair))
                {
                    return(false);
                }
            }
            catch (ChoParserException pEx)
            {
                throw new ChoParserException($"Failed to parse line to '{recType}' object.", pEx);
            }
            catch (ChoMissingRecordFieldException mEx)
            {
                throw new ChoParserException($"Failed to parse line to '{recType}' object.", mEx);
            }
            catch (Exception ex)
            {
                ChoETLFramework.HandleException(ex);
                if (Configuration.ErrorMode == ChoErrorMode.IgnoreAndContinue)
                {
                    rec = null;
                }
                else if (Configuration.ErrorMode == ChoErrorMode.ReportAndContinue)
                {
                    if (!RaiseRecordLoadError(rec, pair, ex))
                    {
                        throw new ChoParserException($"Failed to parse line to '{recType}' object.", ex);
                    }
                }
                else
                {
                    throw new ChoParserException($"Failed to parse line to '{recType}' object.", ex);
                }

                return(true);
            }

            return(true);
        }
Пример #4
0
 public static ChoCSVReader <T> LoadText(string inputText, ChoCSVRecordConfiguration configuration, TraceSwitch traceSwitch = null)
 {
     return(ChoCSVReader <T> .LoadText(inputText, configuration, traceSwitch).WithDelimiter(delimiter));
 }