Exemplo n.º 1
0
        /// <summary>
        /// Flushes any remaining data before a control segment.
        /// </summary>
        /// <param name="segment">The control segment.</param>
        /// <returns>The parsed segments.</returns>
        protected EdiItem Flush(string segment)
        {
            if (!CurrentSegments.Any())
            {
                return(null);
            }

            var firstSegment = CurrentSegments.First().Value;

            if (Separators != null)
            {
                Buffer(segment + Separators.Segment);
            }
            else
            {
                Buffer(segment);
            }

            var result = ParseSegments() ?? new ReaderErrorContext(
                new Exception(string.Format("Invalid control structure beginning with {0}",
                                            firstSegment)), ReaderErrorCode.InvalidControlStructure);

            CurrentMessageContext = null;
            SegmentIndex          = 0;
            PartsIndex            = 0;

            return(result);
        }
Exemplo n.º 2
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.º 3
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);
        }