Package ca.uhn.hl7v2.parser
Class GenericParser
java.lang.Object
ca.uhn.hl7v2.HapiContextSupport
ca.uhn.hl7v2.parser.Parser
ca.uhn.hl7v2.parser.GenericParser
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 Summary
ConstructorsConstructorDescriptionCreates a new instance of GenericParserGenericParser(HapiContext context) GenericParser(ModelClassFactory theFactory) Creates a new instance of GenericParser -
Method Summary
Modifier and TypeMethodDescriptionprotected StringFormats a Message object into an HL7 message string using this parser's default encoding.protected StringFormats a Message object into an HL7 message string using the given encoding.doEncode(Segment structure, EncodingCharacters encodingCharacters) Encodes a particular segment and returns the encoded structuredoEncode(Type type, EncodingCharacters encodingCharacters) Encodes a particular type and returns the encoded structureprotected MessageParses a message string and returns the corresponding Message object.protected MessagedoParseForSpecificPackage(String theMessage, String theVersion, String thePackageName) Attempt the parse a message using a specific model packageFor response messages, returns the value of MSA-2 (the message ID of the message sent by the sending system).getCriticalResponseData(String message) Returns a minimal amount of data from a message string, including only the data needed to send a response to the remote system.getEncoding(String message) Returns a String representing the encoding of the given message, if the encoding is recognized.static GenericParserConvenience factory method which returns an instance that has aNoValidation validation context.getVersion(String message) Returns the version ID (MSH-12) from the given message, without fully parsing the message.booleanReturns true if the pipe parser is primarystatic voidvoidParses a particular message and returns the encoded structurevoidparse(Segment segment, String string, EncodingCharacters encodingCharacters) Parses a particular segment and returns the encoded structurevoidparse(Type type, String string, EncodingCharacters encodingCharacters) Parses a particular type and returns the encoded structureParses a message string and returns the corresponding Message object.voidSets 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.voidsetValidationContext(ValidationContext theContext) Deprecated.voidSets 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.booleansupportsEncoding(String encoding) Returns true if and only if the given encoding is supported by this Parser.Methods inherited from class ca.uhn.hl7v2.parser.Parser
applySuperStructureName, assertVersionExists, encode, encode, getFactory, getMessageStructureForEvent, getParserConfiguration, getValidationContext, instantiateMessage, instantiateMessageInASpecificPackage, makeControlMSH, parseForSpecificPackage, setParserConfiguration, validVersionMethods inherited from class ca.uhn.hl7v2.HapiContextSupport
getHapiContext, setHapiContext
-
Constructor Details
-
GenericParser
public GenericParser()Creates a new instance of GenericParser -
GenericParser
- Parameters:
context- the HapiContext to be used
-
GenericParser
Creates a new instance of GenericParser- Parameters:
theFactory- custom factory to use for model class lookup
-
-
Method Details
-
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
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
Returns true if the pipe parser is primary- Returns:
- true if the pipe parser is primary
-
setValidationContext
Deprecated.use a dedicatedHapiContextand set its ValidationContext property- Overrides:
setValidationContextin classParser- Parameters:
theContext- the set of validation rules to be applied to messages parsed or encoded by this parser (defaults to ValidationContextFactory.DefaultValidation)
-
doEncode
protected String doEncode(Message source, String encoding) throws HL7Exception, EncodingNotSupportedException Formats a Message object into an HL7 message string using the given encoding.- Specified by:
doEncodein classParser- Parameters:
source- a Message object from which to construct an encoded message stringencoding- 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
- Specified by:
getCriticalResponseDatain classParser- Parameters:
message- the message- Returns:
- an MSH segment
- Throws:
HL7Exception- if no MSH segment could be created
-
getVersion
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:
getVersionin classParser- Parameters:
message- the message- Returns:
- the value of MSH-12
- Throws:
HL7Exception- if the version field can not be found.
-
getEncoding
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:
getEncodingin classParser- Parameters:
message- message string- Returns:
- string representing the encoding of the given message, i.e. "XML" or "ER7"
-
getAckID
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). -
supportsEncoding
Returns true if and only if the given encoding is supported by this Parser.- Overrides:
supportsEncodingin classParser- Parameters:
encoding- the encoding, "XML" or "ER7"- Returns:
- true if this parser supports parsing message encoded this way
-
getDefaultEncoding
- Specified by:
getDefaultEncodingin classParser- Returns:
- the preferred encoding of the current primary Parser
-
doParse
Parses a message string and returns the corresponding Message object.- Specified by:
doParsein classParser- Parameters:
message- a String that contains an HL7 messageversion- 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
Parses a message string and returns the corresponding Message object.- Overrides:
parsein classParser- 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
Formats a Message object into an HL7 message string using this parser's default encoding.- Specified by:
doEncodein classParser- 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:
doEncodein classParser- Parameters:
structure- The structure to encodeencodingCharacters- The encoding characters- Returns:
- The encoded segment
- Throws:
HL7Exception- If there is a problem encoding
-
doEncode
Encodes a particular type and returns the encoded structure- Specified by:
doEncodein classParser- Parameters:
type- The type to encodeencodingCharacters- 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:
parsein classParser- Parameters:
type- The type to encodestring- The string to parseencodingCharacters- 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:
parsein classParser- Parameters:
segment- The segment to encodestring- The string to parseencodingCharacters- The encoding characters- Throws:
HL7Exception- If there is a problem encoding
-
parse
Description copied from class:ParserParses a particular message and returns the encoded structure- Specified by:
parsein classParser- Parameters:
message- The message to encodestring- The string to parse- Throws:
HL7Exception- If there is a problem encoding
-
getInstanceWithNoValidation
Convenience factory method which returns an instance that has aNoValidation validation context.- Returns:
- instance of GenericParser without validation
-
doParseForSpecificPackage
protected Message doParseForSpecificPackage(String theMessage, String theVersion, String thePackageName) throws HL7Exception Description copied from class:ParserAttempt the parse a message using a specific model package- Specified by:
doParseForSpecificPackagein classParser- Throws:
HL7Exception
-
main
- Throws:
HL7Exception
-
HapiContextand set its ValidationContext property