/// <summary> /// Tries to parse what had been collected before a new message starts. /// </summary> /// <param name="segmentContext">The current segment.</param> /// <param name="tag">The start new message tag.</param> /// <returns>If flushed.</returns> internal bool Flush(SegmentContext segmentContext, SegmentTags tag) { if ((segmentContext.IsControl || segmentContext.Tag == tag) && CurrentMessage.Any()) { foreach (var c in segmentContext.Value) { Buffer.Enqueue(c); } Buffer.Enqueue(Separators.Segment); try { if (CurrentGroupHeader != null) { CurrentMessage.Add(CurrentGroupHeader); } Item = CurrentMessage.Analyze(Separators, BuildContext()); } finally { CurrentMessage.Clear(); } return(true); } return(false); }
/// <summary> /// Reads an EDI item from the stream. /// An EDI item is an IEdiControl or a message or a ParsingException. /// </summary> /// <returns>Indication if an item was read.</returns> public bool Read() { Item = null; try { while ((!StreamReader.EndOfStream || Buffer.Any()) && Item == null) { ProcessSegment(ReadSegment()); if (Separators != null) { continue; } Item = new ParsingException(ErrorCodes.InvalidControlStructure, "No valid interchange header was found."); } } catch (ParsingException ex) { Item = ex; } catch (Exception ex) { Item = new ParsingException(ErrorCodes.Unknown, ex.Message, ex); } if (StreamReader.EndOfStream && CurrentMessage.Any()) { Item = new ParsingException(ErrorCodes.ImproperEndOfFile, "Unprocessed segments before the end of file."); CurrentMessage.Clear(); } return(Item != null); }