Class GenericParser


public class GenericParser extends Parser
A Parser that delegates parsing tasks to an underlying PipeParser and DefaultXMLParser as needed. Messages to be parsed are inspected in order to determine which Parser to use. If a message is to be encoded, the "primary" Parser will be used. The primary parser defaults to PipeParser, and can be configured using the set...AsPrimary() methods.
Author:
Bryan Tripp
  • Constructor Details

    • GenericParser

      public GenericParser()
      Creates a new instance of GenericParser
    • GenericParser

      public GenericParser(HapiContext context)
      Parameters:
      context - the HapiContext to be used
    • GenericParser

      public GenericParser(ModelClassFactory theFactory)
      Creates a new instance of GenericParser
      Parameters:
      theFactory - custom factory to use for model class lookup
  • Method Details

    • setXMLParserAsPrimary

      public void setXMLParserAsPrimary()
      Sets the underlying XMLParser as the primary parser, so that subsequent calls to encode() will return XML encoded messages, and an attempt will be made to use the XMLParser first for parsing.
    • setPipeParserAsPrimary

      public void setPipeParserAsPrimary()
      Sets the underlying PipeParser as the primary parser, so that subsequent calls to encode() will return traditionally encoded messages, and an attempt will be made to use the PipeParser first for parsing. This is the default setting.
    • isPipeParserPrimary

      public boolean isPipeParserPrimary()
      Returns true if the pipe parser is primary
      Returns:
      true if the pipe parser is primary
    • setValidationContext

      public void setValidationContext(ValidationContext theContext)
      Deprecated.
      use a dedicated HapiContext and set its ValidationContext property
      Overrides:
      setValidationContext in class Parser
      Parameters:
      theContext - the set of validation rules to be applied to messages parsed or encoded by this parser (defaults to ValidationContextFactory.DefaultValidation)
    • doEncode

      Formats a Message object into an HL7 message string using the given encoding.
      Specified by:
      doEncode in class Parser
      Parameters:
      source - a Message object from which to construct an encoded message string
      encoding - the name of the HL7 encoding to use (eg "XML"; most implementations support only one encoding)
      Returns:
      the encoded message
      Throws:
      HL7Exception - if the data fields in the message do not permit encoding (e.g. required fields are null)
      EncodingNotSupportedException - if the requested encoding is not supported by this parser.
    • getCriticalResponseData

      Returns a minimal amount of data from a message string, including only the data needed to send a response to the remote system. This includes the following fields:

      • field separator
      • encoding characters
      • processing ID
      • message control ID
      This method is intended for use when there is an error parsing a message, (so the Message object is unavailable) but an error message must be sent back to the remote system including some of the information in the inbound message. This method parses only that required information, hopefully avoiding the condition that caused the original error.

      Specified by:
      getCriticalResponseData in class Parser
      Parameters:
      message - the message
      Returns:
      an MSH segment
      Throws:
      HL7Exception - if no MSH segment could be created
    • getVersion

      public String getVersion(String message) throws HL7Exception
      Returns the version ID (MSH-12) from the given message, without fully parsing the message. The version is needed prior to parsing in order to determine the message class into which the text of the message should be parsed.
      Specified by:
      getVersion in class Parser
      Parameters:
      message - the message
      Returns:
      the value of MSH-12
      Throws:
      HL7Exception - if the version field can not be found.
    • getEncoding

      public String getEncoding(String message)
      Returns a String representing the encoding of the given message, if the encoding is recognized. For example if the given message appears to be encoded using HL7 2.x XML rules then "XML" would be returned. If the encoding is not recognized then null is returned. That this method returns a specific encoding does not guarantee that the message is correctly encoded (e.g. well formed XML) - just that it is not encoded using any other encoding than the one returned. Returns null if the encoding is not recognized.
      Specified by:
      getEncoding in class Parser
      Parameters:
      message - message string
      Returns:
      string representing the encoding of the given message, i.e. "XML" or "ER7"
    • getAckID

      public String getAckID(String message)
      For response messages, returns the value of MSA-2 (the message ID of the message sent by the sending system). This value may be needed prior to main message parsing, so that (particularly in a multi-threaded scenario) the message can be routed to the thread that sent the request. We need this information first so that any parse exceptions are thrown to the correct thread. Implementers of Parsers should take care to make the implementation of this method very fast and robust. Returns null if MSA-2 can not be found (e.g. if the message is not a response message).
      Specified by:
      getAckID in class Parser
      Parameters:
      message - the message
      Returns:
      the value of MSA-2
    • supportsEncoding

      public boolean supportsEncoding(String encoding)
      Returns true if and only if the given encoding is supported by this Parser.
      Overrides:
      supportsEncoding in class Parser
      Parameters:
      encoding - the encoding, "XML" or "ER7"
      Returns:
      true if this parser supports parsing message encoded this way
    • getDefaultEncoding

      Specified by:
      getDefaultEncoding in class Parser
      Returns:
      the preferred encoding of the current primary Parser
    • doParse

      protected Message doParse(String message, String version) throws HL7Exception
      Parses a message string and returns the corresponding Message object.
      Specified by:
      doParse in class Parser
      Parameters:
      message - a String that contains an HL7 message
      version - the name of the HL7 version to which the message belongs (eg "2.5")
      Returns:
      a HAPI Message object parsed from the given String
      Throws:
      HL7Exception - if the message is not correctly formatted.
      EncodingNotSupportedException - if the message encoded is not supported by this parser.
    • parse

      public Message parse(String theMessage) throws HL7Exception
      Parses a message string and returns the corresponding Message object.
      Overrides:
      parse in class Parser
      Parameters:
      theMessage - a String that contains an HL7 message
      Returns:
      a HAPI Message object parsed from the given String
      Throws:
      HL7Exception - if the message is not correctly formatted.
      EncodingNotSupportedException - if the message encoded is not supported by this parser.
    • doEncode

      protected String doEncode(Message source) throws HL7Exception
      Formats a Message object into an HL7 message string using this parser's default encoding.
      Specified by:
      doEncode in class Parser
      Parameters:
      source - a Message object from which to construct an encoded message string
      Returns:
      the encoded message
      Throws:
      HL7Exception - if the data fields in the message do not permit encoding (e.g. required fields are null)
    • doEncode

      public String doEncode(Segment structure, EncodingCharacters encodingCharacters) throws HL7Exception
      Encodes a particular segment and returns the encoded structure
      Specified by:
      doEncode in class Parser
      Parameters:
      structure - The structure to encode
      encodingCharacters - The encoding characters
      Returns:
      The encoded segment
      Throws:
      HL7Exception - If there is a problem encoding
    • doEncode

      public String doEncode(Type type, EncodingCharacters encodingCharacters) throws HL7Exception
      Encodes a particular type and returns the encoded structure
      Specified by:
      doEncode in class Parser
      Parameters:
      type - The type to encode
      encodingCharacters - The encoding characters
      Returns:
      The encoded type
      Throws:
      HL7Exception - If there is a problem encoding
    • parse

      public void parse(Type type, String string, EncodingCharacters encodingCharacters) throws HL7Exception
      Parses a particular type and returns the encoded structure
      Specified by:
      parse in class Parser
      Parameters:
      type - The type to encode
      string - The string to parse
      encodingCharacters - The encoding characters
      Throws:
      HL7Exception - If there is a problem encoding
    • parse

      public void parse(Segment segment, String string, EncodingCharacters encodingCharacters) throws HL7Exception
      Parses a particular segment and returns the encoded structure
      Specified by:
      parse in class Parser
      Parameters:
      segment - The segment to encode
      string - The string to parse
      encodingCharacters - The encoding characters
      Throws:
      HL7Exception - If there is a problem encoding
    • parse

      public void parse(Message message, String string) throws HL7Exception
      Description copied from class: Parser
      Parses a particular message and returns the encoded structure
      Specified by:
      parse in class Parser
      Parameters:
      message - The message to encode
      string - The string to parse
      Throws:
      HL7Exception - If there is a problem encoding
    • getInstanceWithNoValidation

      Convenience factory method which returns an instance that has a NoValidation validation context.
      Returns:
      instance of GenericParser without validation
    • doParseForSpecificPackage

      protected Message doParseForSpecificPackage(String theMessage, String theVersion, String thePackageName) throws HL7Exception
      Description copied from class: Parser
      Attempt the parse a message using a specific model package
      Specified by:
      doParseForSpecificPackage in class Parser
      Throws:
      HL7Exception
    • main

      public static void main(String[] args) throws HL7Exception
      Throws:
      HL7Exception