/// <summary> Iterates through the message tree to the next segment/group location (regardless /// of whether an instance of the segment exists). If the end of the tree is /// reached, starts over at the root. Only enters the first repetition of a /// repeating group -- explicit navigation (using the drill...() methods) is /// necessary to get to subsequent reps. /// </summary> /// <param name="segmentsOnly">if true, only stops at segments (not groups). /// </param> /// <param name="loop">if true, loops back to beginning when end of msg reached; if false, /// throws HL7Exception if end of msg reached. /// </param> public virtual void Iterate(bool segmentsOnly, bool loop) { IStructure start = null; if (currentChild == -1) { start = CurrentGroup; } else { start = CurrentGroup.GetStructure(childNames[currentChild]); } // using a non-existent direction and not allowing segment creation means that only // the first rep of anything is traversed. IEnumerator it = new MessageIterator(start, "doesn't exist", false); if (segmentsOnly) { FilterIterator.IPredicate predicate = new AnonymousClassPredicate(this); it = new FilterIterator(it, predicate); } if (it.MoveNext()) { var next = (IStructure)it.Current; DrillHere(next); } else if (loop) { Reset(); } else { throw new HL7Exception( "End of message reached while iterating without loop", ErrorCode.APPLICATION_INTERNAL_ERROR); } }
/// <summary> Parses a message string and returns the corresponding Message /// object. Unexpected segments added at the end of their group. /// /// </summary> /// <throws> HL7Exception if the message is not correctly formatted. </throws> /// <throws> EncodingNotSupportedException if the message encoded </throws> /// <summary> is not supported by this parser. /// </summary> protected internal override IMessage DoParse(String message, String version) { //try to instantiate a message object of the right class MessageStructure structure = GetStructure(message); IMessage m = InstantiateMessage(structure.messageStructure, version, structure.explicitlyDefined); //MessagePointer ptr = new MessagePointer(this, m, getEncodingChars(message)); MessageIterator messageIter = new MessageIterator(m, "MSH", true); FilterIterator.IPredicate segmentsOnly = new AnonymousClassPredicate(this); FilterIterator segmentIter = new FilterIterator(messageIter, segmentsOnly); String[] segments = Split(message, segDelim); EncodingCharacters encodingChars = GetEncodingChars(message); for (int i = 0; i < segments.Length; i++) { //get rid of any leading whitespace characters ... if (segments[i] != null && segments[i].Length > 0 && Char.IsWhiteSpace(segments[i][0])) segments[i] = StripLeadingWhitespace(segments[i]); //sometimes people put extra segment delimiters at end of msg ... if (segments[i] != null && segments[i].Length >= 3) { String name = segments[i].Substring(0, (3) - (0)); log.Debug("Parsing segment " + name); messageIter.Direction = name; FilterIterator.IPredicate byDirection = new AnonymousClassPredicate1(name, this); FilterIterator dirIter = new FilterIterator(segmentIter, byDirection); if (dirIter.MoveNext()) { Parse((ISegment) dirIter.Current, segments[i], encodingChars); } } } return m; }
/// <summary> /// Constructor /// </summary> /// <param name="iter"></param> /// <param name="predicate"></param> public FilterIterator(System.Collections.IEnumerator iter, FilterIterator.IPredicate predicate) { this.iter = iter; this.predicate = predicate; }
/// <summary> Parses a message string and returns the corresponding Message /// object. Unexpected segments added at the end of their group. /// /// </summary> /// <throws> HL7Exception if the message is not correctly formatted. </throws> /// <throws> EncodingNotSupportedException if the message encoded </throws> /// <summary> is not supported by this parser. /// </summary> protected internal override IMessage DoParse(String message, String version) { //try to instantiate a message object of the right class MessageStructure structure = GetStructure(message); IMessage m = InstantiateMessage(structure.messageStructure, version, structure.explicitlyDefined); //MessagePointer ptr = new MessagePointer(this, m, getEncodingChars(message)); MessageIterator messageIter = new MessageIterator(m, "MSH", true); FilterIterator.IPredicate segmentsOnly = new AnonymousClassPredicate(this); FilterIterator segmentIter = new FilterIterator(messageIter, segmentsOnly); string lastSegmentName = null; String[] segments = Split(message, segDelim); EncodingCharacters encodingChars = GetEncodingChars(message); for (int i = 0; i < segments.Length; i++) { //get rid of any leading whitespace characters ... if (segments[i] != null && segments[i].Length > 0 && Char.IsWhiteSpace(segments[i][0])) segments[i] = StripLeadingWhitespace(segments[i]); //sometimes people put extra segment delimiters at end of msg ... if (segments[i] != null && segments[i].Length >= 3) { String name = segments[i].Substring(0, (3) - (0)); log.Debug("Parsing segment " + name); if (!name.Equals(lastSegmentName, StringComparison.CurrentCultureIgnoreCase)) { // If the message iterator passes a segment that is later encountered the message object won't be properly parsed. // Rebuild the iterator for each segment, or fix iterator logic in handling unexpected segments. messageIter = new MessageIterator(m, "MSH", true); segmentsOnly = new AnonymousClassPredicate(this); segmentIter = new FilterIterator(messageIter, segmentsOnly); lastSegmentName = name; } messageIter.Direction = name; FilterIterator.IPredicate byDirection = new AnonymousClassPredicate1(name, this); FilterIterator dirIter = new FilterIterator(segmentIter, byDirection); if (dirIter.MoveNext()) { Parse((ISegment) dirIter.Current, segments[i], encodingChars); } } } return m; }
/// <summary> Iterates through the message tree to the next segment/group location (regardless /// of whether an instance of the segment exists). If the end of the tree is /// reached, starts over at the root. Only enters the first repetition of a /// repeating group -- explicit navigation (using the drill...() methods) is /// necessary to get to subsequent reps. /// </summary> /// <param name="segmentsOnly">if true, only stops at segments (not groups) /// </param> /// <param name="loop">if true, loops back to beginning when end of msg reached; if false, /// throws HL7Exception if end of msg reached /// </param> public virtual void iterate(bool segmentsOnly, bool loop) { IStructure start = null; if (this.currentChild == -1) { start = this.currentGroup; } else { start = (this.currentGroup.GetStructure(this.childNames[this.currentChild])); } //using a non-existent direction and not allowing segment creation means that only //the first rep of anything is traversed. System.Collections.IEnumerator it = new MessageIterator(start, "doesn't exist", false); if (segmentsOnly) { FilterIterator.IPredicate predicate = new AnonymousClassPredicate(this); it = new FilterIterator(it, predicate); } if (it.MoveNext()) { IStructure next = (IStructure)it.Current; drillHere(next); } else if (loop) { this.reset(); } else { throw new HL7Exception("End of message reached while iterating without loop", HL7Exception.APPLICATION_INTERNAL_ERROR); } }