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
        public void MoveCurrentToUsed()
        {
            List <IDocumentFragment> documentFragments;

            foreach (string path in CurrentSegments.Keys)
            {
                if (UsedSegments.TryGetValue(path, out documentFragments) == false)
                {
                    documentFragments = new List <IDocumentFragment>();
                    UsedSegments.Add(path, documentFragments);
                }
                documentFragments.Add(CurrentSegments[path]);
            }

            CurrentSegments.Clear();
        }
Exemplo n.º 4
0
        /// <summary>
        /// Parses the accumulated segments.
        /// </summary>
        protected EdiItem ParseSegments()
        {
            if (CurrentMessageContext == null)
            {
                CurrentSegments.Clear();
                return(null);
            }

            EdiItem result;

            try
            {
                var message      = new TransactionSet(CurrentMessageContext.MessageType);
                var errorContext = message.Analyze(CurrentSegments, CurrentMessageContext, Separators, PartsIndex,
                                                   SegmentIndex);

                var ediMessage = (EdiMessage)message.ToInstance();
                ediMessage.MessagePart   = PartsIndex;
                ediMessage.ControlNumber = CurrentMessageContext.ControlNumber;
                ediMessage.ErrorContext  = errorContext;
                result = ediMessage;
            }
            catch (Exception ex)
            {
                result = new ReaderErrorContext(ex, ReaderErrorCode.InvalidSpecOrAssembly,
                                                new MessageErrorContext(CurrentMessageContext.Name,
                                                                        CurrentMessageContext.ControlNumber, CurrentMessageContext.Version, PartsIndex, ex.Message,
                                                                        MessageErrorCode.MissingOrInvalidTransactionSet));
            }
            finally
            {
                SegmentIndex = SegmentIndex + CurrentSegments.Count;
                CurrentSegments.Clear();
            }

            return(result);
        }