Class Parser
- Direct Known Subclasses:
GenericParser,PipeParser,XMLParser
- Author:
- Bryan Tripp (bryan_tripp@sourceforge.net), Christian Ohr
-
Constructor Summary
ConstructorsConstructorDescriptionParser()Uses DefaultModelClassFactory for model class lookup.Parser(HapiContext context) Creates a new parser, using theModelClassFactory, theParserConfigurationand theValidationContextas defined in the context.Parser(ModelClassFactory modelClassFactory) Initialize parser with custom ModelClassFactory and default ValidationContext -
Method Summary
Modifier and TypeMethodDescriptionprotected voidapplySuperStructureName(Message theMessage) static voidassertVersionExists(String version) LikevalidVersion(String)but throws an HL7Exception insteadprotected abstract StringCalled by encode(Message) to perform implementation-specific encoding work.protected abstract StringCalled by encode(Message, String) to perform implementation-specific encoding work.abstract StringdoEncode(Segment structure, EncodingCharacters encodingCharacters) Encodes a particular segment and returns the encoded structureabstract StringdoEncode(Type type, EncodingCharacters encodingCharacters) Encodes a particular type and returns the encoded structureprotected abstract MessageCalled by parse() to perform implementation-specific parsing work.protected abstract MessagedoParseForSpecificPackage(String message, String version, String packageName) Attempt the parse a message using a specific model packageFormats a Message object into an HL7 message string using this parser's default encoding.Formats a Message object into an HL7 message string using the given encoding.abstract StringFor response messages, returns the value of MSA-2 (the message ID of the message sent by the sending system).abstract SegmentgetCriticalResponseData(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.abstract Stringabstract StringgetEncoding(String message) Returns a String representing the encoding of the given message, if the encoding is recognized.getMessageStructureForEvent(String name, String version) Deprecated.Returns the parser configuration.abstract StringgetVersion(String message) Returns the version ID (MSH-12) from the given message, without fully parsing the message.protected MessageinstantiateMessage(String theName, String theVersion, boolean isExplicit) Note that the validation context of the resulting message is set to this parser's validation context.protected MessageinstantiateMessageInASpecificPackage(String theName, String theVersion, boolean isExplicit, String packageName) Instantiate a message type using a specific package namestatic SegmentmakeControlMSH(String version, ModelClassFactory factory) Creates a version-specific MSH object and returns it as a version-independent MSH interface.abstract voidParses a particular message and returns the encoded structureabstract voidparse(Segment segment, String string, EncodingCharacters encodingCharacters) Parses a particular segment and returns the encoded structureabstract voidparse(Type type, String string, EncodingCharacters encodingCharacters) Parses a particular type and returns the encoded structureParses a message string and returns the corresponding Message object.parseForSpecificPackage(String message, String packageName) Parse a message using a specific model package instead of the default, usingModelClassFactory.getMessageClassInASpecificPackage(String, String, boolean, String).voidsetParserConfiguration(ParserConfiguration configuration) Deprecated.use a dedicatedHapiContextand set its ParserConfiguration propertyvoidsetValidationContext(ValidationContext context) Deprecated.use a dedicatedHapiContextand set its ValidationContext propertybooleansupportsEncoding(String encoding) Returns true if and only if the given encoding is supported by this Parser.static booleanvalidVersion(String version) Deprecated.Methods inherited from class ca.uhn.hl7v2.HapiContextSupport
getHapiContext, setHapiContext
-
Constructor Details
-
Parser
public Parser()Uses DefaultModelClassFactory for model class lookup. -
Parser
Creates a new parser, using theModelClassFactory, theParserConfigurationand theValidationContextas defined in the context.- Parameters:
context- HapiContext
-
Parser
Initialize parser with custom ModelClassFactory and default ValidationContext- Parameters:
modelClassFactory- custom factory to use for model class lookup
-
-
Method Details
-
getFactory
- Returns:
- the factory used by this Parser for model class lookup
-
getValidationContext
- Returns:
- the set of validation rules that is applied to messages parsed or encoded by this
parser. Note that this method may return
null
-
setValidationContext
Deprecated.use a dedicatedHapiContextand set its ValidationContext property- Parameters:
context- the set of validation rules to be applied to messages parsed or encoded by this parser (defaults to ValidationContextFactory.DefaultValidation)
-
getParserConfiguration
Returns the parser configuration. This is a bean which contains configuration instructions relating to how a parser should be parsing or encoding messages it deals with.
Note that the parser configuration comes from the
HAPI Context. Changes to the configuration for one parser will affect all parsers which share the same context.- Returns:
- the current parser configuration
-
setParserConfiguration
Deprecated.use a dedicatedHapiContextand set its ParserConfiguration propertySets the parser configuration for this parser (may not be null). This is a bean which contains configuration instructions relating to how a parser should be parsing or encoding messages it deals with.- Parameters:
configuration- The parser configuration
-
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.- Parameters:
message- message string- Returns:
- string representing the encoding of the given message, i.e. "XML" or "ER7"
-
supportsEncoding
Returns true if and only if the given encoding is supported by this Parser.- Parameters:
encoding- the encoding, "XML" or "ER7"- Returns:
- true if this parser supports parsing message encoded this way
-
getDefaultEncoding
- Returns:
- the preferred encoding of this Parser ("XML" or "ER7")
-
parse
Parses a message string and returns the corresponding Message object.- Parameters:
message- 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.
-
doParse
Called by parse() to perform implementation-specific parsing work.- 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.
-
encode
Formats a Message object into an HL7 message string using the given encoding.- 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.
-
doEncode
Called by encode(Message, String) to perform implementation-specific encoding work.- 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.
-
encode
Formats a Message object into an HL7 message string using this parser's default encoding.- 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
Called by encode(Message) to perform implementation-specific encoding work.- 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)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
- Parameters:
message- the message- Returns:
- an MSH segment
- Throws:
HL7Exception- if no MSH segment could be created
-
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).- Parameters:
message- the message- Returns:
- the value of MSA-2
-
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.- Parameters:
message- the message- Returns:
- the value of MSH-12
- Throws:
HL7Exception- if the version field can not be found.
-
doEncode
public abstract String doEncode(Segment structure, EncodingCharacters encodingCharacters) throws HL7Exception Encodes a particular segment and returns the encoded structure- Parameters:
structure- The structure to encodeencodingCharacters- The encoding characters- Returns:
- The encoded segment
- Throws:
HL7Exception- If there is a problem encoding- Since:
- 1.0
-
doEncode
public abstract String doEncode(Type type, EncodingCharacters encodingCharacters) throws HL7Exception Encodes a particular type and returns the encoded structure- Parameters:
type- The type to encodeencodingCharacters- The encoding characters- Returns:
- The encoded type
- Throws:
HL7Exception- If there is a problem encoding- Since:
- 1.0
-
parse
public abstract void parse(Type type, String string, EncodingCharacters encodingCharacters) throws HL7Exception Parses a particular type and returns the encoded structure- Parameters:
string- The string to parsetype- The type to encodeencodingCharacters- The encoding characters- Throws:
HL7Exception- If there is a problem encoding- Since:
- 1.0
-
parseForSpecificPackage
Parse a message using a specific model package instead of the default, usingModelClassFactory.getMessageClassInASpecificPackage(String, String, boolean, String). WARNING: This method is only implemented in some parser implementations. Currently it will only work with the PipeParser parser implementation. Use with caution.- Parameters:
message- message stringpackageName- name of the package of the models- Returns:
- parsed message
- Throws:
HL7Exception- if an error occurred while parsing
-
doParseForSpecificPackage
protected abstract Message doParseForSpecificPackage(String message, String version, String packageName) throws HL7Exception Attempt the parse a message using a specific model package- Throws:
HL7Exception
-
instantiateMessageInASpecificPackage
protected Message instantiateMessageInASpecificPackage(String theName, String theVersion, boolean isExplicit, String packageName) throws HL7Exception Instantiate a message type using a specific package name -
parse
public abstract void parse(Segment segment, String string, EncodingCharacters encodingCharacters) throws HL7Exception Parses a particular segment and returns the encoded structure- Parameters:
segment- The segment to encodestring- The string to parseencodingCharacters- The encoding characters- Throws:
HL7Exception- If there is a problem encoding
-
parse
Parses a particular message and returns the encoded structure- Parameters:
message- The message to encodestring- The string to parse- Throws:
HL7Exception- If there is a problem encoding- Since:
- 1.0
-
makeControlMSH
Creates a version-specific MSH object and returns it as a version-independent MSH interface.
Since HAPI 2.1, if a version specific MSH segment can't be found (for example because the specific structure JAR is not found on the classpath), an instance of
GenericSegmentis returned.- Parameters:
version- HL7 versionfactory- model class factory to be used- Returns:
- MSH segment for this version returned by the model class factory
- Throws:
HL7Exception- if no matching segment could be found
-
validVersion
Deprecated.Returns true if the given string represents a valid 2.x version. Valid versions include "2.1", "2.2", "2.3", "2.3.1", "2.4", "2.5", "2.5.1", "2.6"- Parameters:
version- HL7 version string- Returns:
trueif version is known
-
assertVersionExists
LikevalidVersion(String)but throws an HL7Exception instead- Parameters:
version- HL7 version- Throws:
HL7Exception- if version is unknown
-
getMessageStructureForEvent
Deprecated.Given a concatenation of message type and event (e.g. ADT_A04), and the version, finds the corresponding message structure (e.g. ADT_A01). This is needed because some events share message structures, although it is not needed when the message structure is explicitly valued in MSH-9-3. If no mapping is found, returns the original name.- Throws:
HL7Exception- if there is an error retrieving the map, or if the given version is invalid
-
instantiateMessage
protected Message instantiateMessage(String theName, String theVersion, boolean isExplicit) throws HL7Exception Note that the validation context of the resulting message is set to this parser's validation context. The validation context is used within Primitive.setValue().- Parameters:
theName- name of the desired structure in the form XXX_YYYtheVersion- HL7 version (e.g. "2.3")isExplicit- true if the structure was specified explicitly in MSH-9-3, false if it was inferred from MSH-9-1 and MSH-9-2. If false, a lookup may be performed to find an alternate structure corresponding to that message type and event.- Returns:
- a Message instance
- Throws:
HL7Exception- if the version is not recognized or no appropriate class can be found or the Message class throws an exception on instantiation (e.g. if args are not as expected)
-
applySuperStructureName
- Throws:
HL7Exception
-
ModelClassFactory.getMessageStructureForEvent(String, Version)