Exemplo n.º 1
0
        /// <summary>
        /// Reads an EDI item from the stream.
        /// </summary>
        /// <returns>Indication if an item was read.</returns>
        public override bool Read()
        {
            if (Item is ReaderErrorContext && !ContinueOnError)
            {
                return(false);
            }

            Item = null;

            try
            {
                while ((!StreamReader.EndOfStream || InternalBuffer.Any()) && Item == null)
                {
                    var segment = ReadSegment();

                    if (Separators == null)
                    {
                        throw new ReaderException("No valid separator set was found.", ReaderErrorCode.InvalidControlStructure);
                    }

                    if (string.IsNullOrEmpty(segment))
                    {
                        continue;
                    }

                    Item = Split(segment) ?? Process(segment);
                }
            }
            catch (ReaderException ex)
            {
                Item = new ReaderErrorContext(ex, ex.ErrorCode);
            }
            catch (ParserMessageException ex)
            {
                Item = new ReaderErrorContext(ex, ReaderErrorCode.InvalidSpecOrAssembly, ex.MessageErrorContext);
            }
            catch (Exception ex)
            {
                Item = new ReaderErrorContext(ex, ReaderErrorCode.Unknown);
            }

            if (StreamReader.EndOfStream && CurrentSegments.Any())
            {
                Item = new ReaderErrorContext(new Exception("Improper end of file."), ReaderErrorCode.ImproperEndOfFile);
            }

            var readerErrorContext = Item as ReaderErrorContext;

            if (readerErrorContext != null)
            {
                CurrentSegments.Clear();
                if (readerErrorContext.MessageErrorContext == null)
                {
                    Separators = null;
                }
            }

            return(Item != null);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Reads an item from the stream.
        /// </summary>
        /// <returns>Indication if an item was read.</returns>
        public override bool Read()
        {
            if (Item is ReaderErrorContext && !ContinueOnError)
            {
                return(false);
            }

            Item = null;

            try
            {
                while ((!StreamReader.EndOfStream || InternalBuffer.Any()) && Item == null)
                {
                    var segment = ReadSegment();

                    if (string.IsNullOrEmpty(segment))
                    {
                        continue;
                    }

                    Item = Process(segment);
                }
            }
            catch (ReaderException ex)
            {
                Item = new ReaderErrorContext(ex, ex.ErrorCode);
            }
            catch (ParserMessageException ex)
            {
                Item = new ReaderErrorContext(ex, ReaderErrorCode.InvalidSpecOrAssembly, ex.MessageErrorContext);
            }
            catch (Exception ex)
            {
                Item = new ReaderErrorContext(ex, ReaderErrorCode.Unknown);
            }

            if (StreamReader.EndOfStream && CurrentSegments.Any())
            {
                Item = Flush(null);
            }

            var readerErrorContext = Item as ReaderErrorContext;

            if (readerErrorContext != null)
            {
                CurrentSegments.Clear();
            }

            return(Item != null);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Reads from the stream until a non-escaped segment terminator was reached.
        /// Breaks if no segment terminator was encountered after 5000 symbols were read.
        /// This is to avoid loading large and corrupt files.
        /// </summary>
        /// <returns>
        /// The segment.
        /// </returns>
        protected override string ReadSegment()
        {
            var line    = "";
            var postFix = "";

            while ((!StreamReader.EndOfStream || InternalBuffer.Any()) && line.Length < MaxSegmentLength)
            {
                line = line + Read(1);
            }

            while (!string.IsNullOrEmpty(_postFix) && !StreamReader.EndOfStream && postFix.Length < _postFix.Length)
            {
                postFix = postFix + Read(1);
            }

            if (!string.IsNullOrEmpty(_postFix) && postFix != _postFix)
            {
                throw new Exception(string.Format("Postfix {0} is different than the expected {1}", postFix, _postFix));
            }

            return(line);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Reads from the stream until a non-escaped segment terminator was reached.
        /// Breaks if no segment terminator was encountered after 5000 symbols were read.
        /// This is to avoid loading large and corrupt files.
        /// </summary>
        /// <returns>
        /// An EDI segment.
        /// </returns>
        protected override string ReadSegment()
        {
            var line = "";

            while (!StreamReader.EndOfStream || InternalBuffer.Any())
            {
                line = line + Read(1);
                if (line.Length > 2)
                {
                    var        last3 = line.Substring(line.Length - 3);
                    Separators separators;
                    string     probed;
                    if (TryReadHeader(last3, out probed, out separators))
                    {
                        Separators = separators;
                        Trims      = Trims.Except(new[] { Separators.Segment }).ToArray();
                        line       = probed;
                    }
                    else
                    {
                        if (!string.IsNullOrEmpty(probed))
                        {
                            Buffer(probed.Skip(3));
                        }
                    }
                }

                // Segment terminator may never be reached
                if (line.Length > MaxSegmentLength)
                {
                    //  Reset and continue or break
                    if (ContinueOnError)
                    {
                        line = "";
                    }
                    else
                    {
                        throw new ReaderException(
                                  string.Format("No segment was found before the buffer reached the allowed maximum of {0}.", MaxSegmentLength),
                                  ReaderErrorCode.InvalidInterchangeContent);
                    }
                }

                if (Separators == null)
                {
                    continue;
                }

                if (!line.EndsWith(Separators.Segment.ToString(), StringComparison.Ordinal))
                {
                    continue;
                }

                if (Separators.Escape.HasValue &&
                    line.EndsWith(string.Concat(Separators.Escape.Value, Separators.Segment),
                                  StringComparison.Ordinal))
                {
                    continue;
                }

                var index = line.LastIndexOf(Separators.Segment.ToString(), StringComparison.Ordinal);
                if (index > 0)
                {
                    line = line.Remove(index);
                }

                if (!string.IsNullOrEmpty(line))
                {
                    break;
                }
            }

            return(line.Trim(Trims));
        }