/**
  * parse the mdata
  *
  * @param string $data       data
  * @param bool   $parse_body parse the body
  *
  * @throws CHL7v2Exception
  *
  * @return void
  */
 function parse($data, $parse_body = true)
 {
     try {
         self::isWellFormed($data, $this->strict_segment_terminator);
     } catch (CHL7v2Exception $e) {
         $this->error($e->getMessage(), $e->extraData);
         //return false;
     }
     // remove all chars before H
     $h_pos = strpos($data, self::$header_segment_name);
     if ($h_pos === false) {
         throw new CHL7v2Exception(CHL7v2Exception::SEGMENT_INVALID_SYNTAX, $data);
     }
     $data = substr($data, $h_pos);
     $data = self::fixRawER7($data, $this->strict_segment_terminator);
     // handle "A" segments
     $field_sep = preg_quote($this->fieldSeparator);
     $patt = "/[\r\n]+A{$field_sep}([^\r\n]+)/s";
     $data = preg_replace($patt, "\\1", $data);
     // remove "C" segments
     $patt = "/[\r\n]+C{$field_sep}([^\r\n]+)/s";
     $data = preg_replace($patt, "", $data);
     parent::parse($data);
     $message = $this->data;
     // 2 to 5
     if (!isset($message[5])) {
         throw new CHL7v2Exception(CHL7v2Exception::SEGMENT_INVALID_SYNTAX, $message);
     }
     $this->fieldSeparator = $message[1];
     $nextDelimiter = strpos($message, $this->fieldSeparator, 2);
     if ($nextDelimiter > 4) {
         // usually ^
         $this->componentSeparator = $message[2];
     }
     if ($nextDelimiter > 3) {
         // usually ~
         $this->repetitionSeparator = $message[3];
     }
     if ($nextDelimiter > 4) {
         // usually \
         $this->escapeCharacter = $message[4];
     }
     if ($nextDelimiter > 5) {
         // usually &
         $this->subcomponentSeparator = $message[5];
     }
     // replace the special case of ^~& with ^~\&
     if ("^~&|" == substr($message, 2, 4)) {
         $this->escapeCharacter = "\\";
         $this->subcomponentSeparator = "&";
         $this->repetitionSeparator = "~";
         $this->componentSeparator = "^";
     }
     $this->initEscapeSequences();
     $this->lines = CHL7v2::split($this->segmentTerminator, $this->data);
     // we extract the first line info "by hand"
     $first_line = CHL7v2::split($this->fieldSeparator, reset($this->lines));
     if (!isset($first_line[12])) {
         throw new CHL7v2Exception(CHL7v2Exception::SEGMENT_INVALID_SYNTAX, $message);
     }
     // version
     $this->parseRawVersion($first_line[12]);
     // message type
     $message_type = explode($this->componentSeparator, $first_line[6]);
     $this->name = $message_type[0];
     $this->event_name = $message_type[0];
     if (!($spec = $this->getSpecs())) {
         throw new CHL7v2Exception(CHL7v2Exception::UNKNOWN_MSG_CODE);
     }
     $this->description = $spec->queryTextNode("description");
     $this->readHeader();
     // type liaison
     //$type_liaison
     if ($parse_body) {
         $this->readSegments();
     }
 }
 function parse($data, $parse_body = true, CInteropActor $actor = null)
 {
     $this->actor = $actor;
     try {
         self::isWellFormed($data, $this->strict_segment_terminator);
     } catch (CHL7v2Exception $e) {
         $this->error($e->getMessage(), $e->extraData);
         //return false;
     }
     // remove all chars before MSH
     $msh_pos = strpos($data, self::$header_segment_name);
     if ($msh_pos === false) {
         throw new CHL7v2Exception(CHL7v2Exception::SEGMENT_INVALID_SYNTAX, $data);
     }
     $data = substr($data, $msh_pos);
     $data = self::fixRawER7($data, $this->strict_segment_terminator);
     parent::parse($data);
     $message = $this->data;
     // 4 to 7
     if (!isset($message[7])) {
         throw new CHL7v2Exception(CHL7v2Exception::SEGMENT_INVALID_SYNTAX, $message);
     }
     $this->fieldSeparator = $message[3];
     $nextDelimiter = strpos($message, $this->fieldSeparator, 4);
     if ($nextDelimiter > 4) {
         // usually ^
         $this->componentSeparator = $message[4];
     }
     if ($nextDelimiter > 5) {
         // usually ~
         $this->repetitionSeparator = $message[5];
     }
     if ($nextDelimiter > 6) {
         // usually \
         $this->escapeCharacter = $message[6];
     }
     if ($nextDelimiter > 7) {
         // usually &
         $this->subcomponentSeparator = $message[7];
     }
     // replace the special case of ^~& with ^~\&
     if ("^~&|" == substr($message, 4, 4)) {
         $this->escapeCharacter = "\\";
         $this->subcomponentSeparator = "&";
         $this->repetitionSeparator = "~";
         $this->componentSeparator = "^";
     }
     $this->initEscapeSequences();
     $this->lines = CHL7v2::split($this->segmentTerminator, $this->data);
     // we extract the first line info "by hand"
     $first_line = CHL7v2::split($this->fieldSeparator, reset($this->lines));
     if (!isset($first_line[11])) {
         throw new CHL7v2Exception(CHL7v2Exception::SEGMENT_INVALID_SYNTAX, $message);
     }
     // version
     if (array_key_exists(16, $first_line)) {
         $this->parseRawVersion($first_line[11], $first_line[16]);
     } else {
         $this->parseRawVersion($first_line[11]);
     }
     // message type
     $message_type = explode($this->componentSeparator, $first_line[8]);
     if ($message_type[0]) {
         $this->name = $message_type[0];
         if ($this->name == "ACK") {
             $this->event_name = $message_type[0];
         } else {
             if (isset($message_type[2]) && $message_type[2] && !preg_match("/^[A-Z]{3}_[A-Z\\d]{2}\\d\$/", $message_type[2])) {
                 throw new CHL7v2Exception(CHL7v2Exception::WRONG_MESSAGE_TYPE, $message_type[2]);
             }
             if (strlen($message_type[0]) != 3 || strlen($message_type[1]) != 3) {
                 $msg = $message_type[0] . $this->componentSeparator . $message_type[1];
                 throw new CHL7v2Exception(CHL7v2Exception::WRONG_MESSAGE_TYPE, $msg);
             }
             $this->event_name = $message_type[0] . $message_type[1];
         }
     } else {
         $this->event_name = preg_replace("/[^A-Z0-9]/", "", $message_type[2]);
         $this->name = substr($message_type[2], 0, 3);
     }
     if (!($spec = $this->getSpecs())) {
         throw new CHL7v2Exception(CHL7v2Exception::UNKNOWN_MSG_CODE);
     }
     $this->description = $spec->queryTextNode("description");
     $this->readHeader();
     if ($parse_body) {
         $this->readSegments();
     }
 }