/// <summary> /// Parse the HL7 message in text format, throws HL7Exception if error occurs /// </summary> /// <returns>boolean</returns> public bool ParseMessage() { bool isValid = false; bool isParsed = false; try { isValid = this.validateMessage(); } catch (HL7Exception ex) { throw ex; } catch (Exception ex) { throw new HL7Exception("Unhandled Exception in validation - " + ex.Message, HL7Exception.BAD_MESSAGE); } if (isValid) { try { if (this.allSegments == null || this.allSegments.Count <= 0) { this.allSegments = MessageHelper.SplitMessage(HL7Message); } short SegSeqNo = 0; foreach (string strSegment in this.allSegments) { if (string.IsNullOrWhiteSpace(strSegment)) { continue; } Segment newSegment = new Segment(this.Encoding); string segmentName = strSegment.Substring(0, 3); newSegment.Name = segmentName; newSegment.Value = strSegment; newSegment.SequenceNo = SegSeqNo++; this.AddNewSegment(newSegment); } this.SegmentCount = SegSeqNo; string strSerializedMessage = string.Empty; try { strSerializedMessage = this.SerializeMessage(false); } catch (HL7Exception ex) { throw new HL7Exception("Failed to serialize parsed message with error - " + ex.Message, HL7Exception.PARSING_ERROR); } if (!string.IsNullOrEmpty(strSerializedMessage)) { if (this.Equals(strSerializedMessage)) { isParsed = true; } } else { throw new HL7Exception("Unable to serialize to original message - ", HL7Exception.PARSING_ERROR); } } catch (Exception ex) { throw new HL7Exception("Failed to parse the message with error - " + ex.Message, HL7Exception.PARSING_ERROR); } } return(isParsed); }
/// <summary> /// Validates the HL7 message for basic syntax /// </summary> /// <returns>A boolean indicating whether the whole message is valid or not</returns> private bool validateMessage() { try { if (!string.IsNullOrEmpty(HL7Message)) { //check message length - MSH+Delimeters+12Fields in MSH if (HL7Message.Length < 20) { throw new HL7Exception("Message Length too short: " + HL7Message.Length + " chars.", HL7Exception.BAD_MESSAGE); } //check if message starts with header segment if (!HL7Message.StartsWith("MSH")) { throw new HL7Exception("MSH segment not found at the beggining of the message", HL7Exception.BAD_MESSAGE); } this.Encoding.EvaluateSegmentDelimiter(this.HL7Message); this.HL7Message = string.Join(this.Encoding.SegmentDelimiter, MessageHelper.SplitMessage(this.HL7Message)) + this.Encoding.SegmentDelimiter; //check Segment Name & 4th character of each segment char fourthCharMSH = HL7Message[3]; this.allSegments = MessageHelper.SplitMessage(HL7Message); foreach (string strSegment in this.allSegments) { if (string.IsNullOrWhiteSpace(strSegment)) { continue; } bool isValidSegName = false; string segmentName = strSegment.Substring(0, 3); string segNameRegEx = "[A-Z][A-Z][A-Z1-9]"; isValidSegName = System.Text.RegularExpressions.Regex.IsMatch(segmentName, segNameRegEx); if (!isValidSegName) { throw new HL7Exception("Invalid segment name found: " + strSegment, HL7Exception.BAD_MESSAGE); } char fourthCharSEG = strSegment[3]; if (fourthCharMSH != fourthCharSEG) { throw new HL7Exception("Invalid segment found: " + strSegment, HL7Exception.BAD_MESSAGE); } } string _fieldDelimiters_Message = this.allSegments[0].Substring(3, 8 - 3); this.Encoding.EvaluateDelimiters(_fieldDelimiters_Message); // Count field separators, MSH.12 is required so there should be at least 11 field separators in MSH int countFieldSepInMSH = this.allSegments[0].Count(f => f == Encoding.FieldDelimiter); if (countFieldSepInMSH < 11) { throw new HL7Exception("MSH segment doesn't contain all the required fields", HL7Exception.BAD_MESSAGE); } // Find Message Version var MSHFields = MessageHelper.SplitString(this.allSegments[0], Encoding.FieldDelimiter); if (MSHFields.Count >= 12) { this.Version = MessageHelper.SplitString(MSHFields[11], Encoding.ComponentDelimiter)[0]; } else { throw new HL7Exception("HL7 version not found in the MSH segment", HL7Exception.REQUIRED_FIELD_MISSING); } //Find Message Type & Trigger Event try { string MSH_9 = MSHFields[8]; if (!string.IsNullOrEmpty(MSH_9)) { var MSH_9_comps = MessageHelper.SplitString(MSH_9, this.Encoding.ComponentDelimiter); if (MSH_9_comps.Count >= 3) { this.MessageStructure = MSH_9_comps[2]; } else if (MSH_9_comps.Count > 0 && MSH_9_comps[0] != null && MSH_9_comps[0].Equals("ACK")) { this.MessageStructure = "ACK"; } else if (MSH_9_comps.Count == 2) { this.MessageStructure = MSH_9_comps[0] + "_" + MSH_9_comps[1]; } else { throw new HL7Exception("Message Type & Trigger Event value not found in message", HL7Exception.UNSUPPORTED_MESSAGE_TYPE); } } else { throw new HL7Exception("MSH.10 not available", HL7Exception.UNSUPPORTED_MESSAGE_TYPE); } } catch (System.IndexOutOfRangeException e) { throw new HL7Exception("Can't find message structure (MSH.9.3) - " + e.Message, HL7Exception.UNSUPPORTED_MESSAGE_TYPE); } try { this.MessageControlID = MSHFields[9]; if (string.IsNullOrEmpty(this.MessageControlID)) { throw new HL7Exception("MSH.10 - Message Control ID not found", HL7Exception.REQUIRED_FIELD_MISSING); } } catch (Exception ex) { throw new HL7Exception("Error occured while accessing MSH.10 - " + ex.Message, HL7Exception.REQUIRED_FIELD_MISSING); } try { this.ProcessingID = MSHFields[10]; if (string.IsNullOrEmpty(this.ProcessingID)) { throw new HL7Exception("MSH.11 - Processing ID not found", HL7Exception.REQUIRED_FIELD_MISSING); } } catch (Exception ex) { throw new HL7Exception("Error occured while accessing MSH.11 - " + ex.Message, HL7Exception.REQUIRED_FIELD_MISSING); } } else { throw new HL7Exception("No Message Found", HL7Exception.BAD_MESSAGE); } } catch (Exception ex) { throw new HL7Exception("Failed to validate the message with error - " + ex.Message, HL7Exception.BAD_MESSAGE); } return(true); }