ca.uhn.hl7v2.parser
Class GenericParser

java.lang.Object
  extended by ca.uhn.hl7v2.parser.Parser
      extended by ca.uhn.hl7v2.parser.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 Summary
GenericParser()
          Creates a new instance of GenericParser
GenericParser(ModelClassFactory theFactory)
          Creates a new instance of GenericParser
 
Method Summary
protected  java.lang.String doEncode(Message source)
          Formats a Message object into an HL7 message string using this parser's default encoding.
protected  java.lang.String doEncode(Message source, java.lang.String encoding)
          Formats a Message object into an HL7 message string using the given encoding.
 java.lang.String doEncode(Segment structure, EncodingCharacters encodingCharacters)
          Encodes a particular segment and returns the encoded structure
 java.lang.String doEncode(Type type, EncodingCharacters encodingCharacters)
          Encodes a particular type and returns the encoded structure
protected  Message doParse(java.lang.String message, java.lang.String version)
          Parses a message string and returns the corresponding Message object.
 java.lang.String getAckID(java.lang.String message)
          For response messages, returns the value of MSA-2 (the message ID of the message sent by the sending system).
 Segment getCriticalResponseData(java.lang.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.
 java.lang.String getDefaultEncoding()
           
 java.lang.String getEncoding(java.lang.String message)
          Returns a String representing the encoding of the given message, if the encoding is recognized.
 java.lang.String getVersion(java.lang.String message)
          Returns the version ID (MSH-12) from the given message, without fully parsing the message.
 void parse(Message message, java.lang.String string)
          Parses a particular message and returns the encoded structure
 void parse(Segment segment, java.lang.String string, EncodingCharacters encodingCharacters)
          Parses a particular segment and returns the encoded structure
 void parse(Type type, java.lang.String string, EncodingCharacters encodingCharacters)
          Parses a particular type and returns the encoded structure
 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.
 void setValidationContext(ValidationContext theContext)
           
 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.
 boolean supportsEncoding(java.lang.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
encode, encode, getFactory, getMessageStructureForEvent, getMessageStructures, getValidationContext, getValidVersions, instantiateMessage, makeControlMSH, parse, validVersion
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

GenericParser

public GenericParser()
Creates a new instance of GenericParser


GenericParser

public GenericParser(ModelClassFactory theFactory)
Creates a new instance of GenericParser

Parameters:
theFactory - custom factory to use for model class lookup
Method Detail

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.


setValidationContext

public void setValidationContext(ValidationContext theContext)
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

protected java.lang.String doEncode(Message source,
                                    java.lang.String encoding)
                             throws HL7Exception,
                                    EncodingNotSupportedException
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

public Segment getCriticalResponseData(java.lang.String message)
                                throws HL7Exception

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:

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
Returns:
an MSH segment
Throws:
HL7Exception

getVersion

public java.lang.String getVersion(java.lang.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
Throws:
HL7Exception - if the version field can not be found.

getEncoding

public java.lang.String getEncoding(java.lang.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

getAckID

public java.lang.String getAckID(java.lang.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

supportsEncoding

public boolean supportsEncoding(java.lang.String encoding)
Returns true if and only if the given encoding is supported by this Parser.

Specified by:
supportsEncoding in class Parser

getDefaultEncoding

public java.lang.String getDefaultEncoding()
Specified by:
getDefaultEncoding in class Parser
Returns:
the preferred encoding of the current primary Parser

doParse

protected Message doParse(java.lang.String message,
                          java.lang.String version)
                   throws HL7Exception,
                          EncodingNotSupportedException
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.

doEncode

protected java.lang.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 java.lang.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 java.lang.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,
                  java.lang.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,
                  java.lang.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,
                  java.lang.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


Copyright © 2001-2011 University Health Network. All Rights Reserved.