Class Hl7Message

java.lang.Object
com.framed.interop.hl7.hl7v2.Hl7Message

public final class Hl7Message extends Object
A lightweight, read-oriented HL7 v2 pipe-encoded message: a list of segments, each split into fields on |. Components (^) and repetitions (~) are exposed as raw field text for callers to split as needed.

Field numbering follows HL7's convention. For most segments field n is at array index n (index 0 being the segment id). MSH is special: its first | is field 1 (the field separator), so MSH-n for n >= 2 lives at array index n - 1. field(String, int) hides this difference.

  • Method Details

    • parse

      public static Hl7Message parse(String raw)
      Parses a raw HL7 message. Segment separators may be \r, \n or \r\n.
      Parameters:
      raw - the raw message text
      Returns:
      the parsed message
    • segment

      public String[] segment(String id)
      Returns:
      the first segment with the given id, or null.
    • segments

      public List<String[]> segments(String id)
      Returns:
      all segments with the given id, in order.
    • field

      public String field(String id, int field)
      Returns field n of the first id segment, honouring the MSH offset.
      Parameters:
      id - the segment id (e.g. "MSH", "OBR")
      field - the HL7 field number (e.g. 9 for MSH-9)
      Returns:
      the field text, or "" if absent
    • field

      public static String field(String[] seg, String id, int field)
      Returns field n of a specific segment instance, honouring the MSH offset.
      Parameters:
      seg - the segment fields array (as returned by segment(String))
      id - the segment id
      field - the HL7 field number
      Returns:
      the field text, or "" if absent
    • firstComponent

      public static String firstComponent(String field)
      Returns:
      the first component (before ^) of a field value.
    • messageType

      public String messageType()
      Returns:
      the message type triplet from MSH-9, e.g. "ORU^R01" (trailing structure dropped).
    • controlId

      public String controlId()
      Returns:
      the message control id (MSH-10).