Class XMLParser
- Direct Known Subclasses:
DefaultXMLParser
parse(Segment segment, ElementNode segmentNode) and
encode(Segment segment, ElementNode segmentNode)
as appropriate. XMLParser uses the Xerces parser, which must be installed in your
classpath.- Author:
- Bryan Tripp, Shawn Bellina
- See Also:
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionConstructorXMLParser(HapiContext context) XMLParser(ModelClassFactory theFactory) Constructor -
Method Summary
Modifier and TypeMethodDescriptionprotected voidprotected StringFormats a Message object into an HL7 message string using this parser's default encoding (XML encoding).protected StringFormats a Message object into an HL7 message string using the given encoding.doEncode(Segment structure, EncodingCharacters encodingCharacters) Throws unsupported operation exceptiondoEncode(Type type, EncodingCharacters encodingCharacters) Throws unsupported operation exceptionprotected MessageParses a message string and returns the corresponding Message object.protected MessagedoParseForSpecificPackage(String theMessage, String theVersion, String thePackageName) Throws unsupported operation exceptionbooleanPopulates the given Element with data from the given Segment, by inserting Elements corresponding to the Segment's fields, their components, etc.abstract DocumentencodeDocument(Message source) Creates an XML Document that corresponds to the given Message object.For 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.String[]Deprecated.Returns the text encoding to be used in generating new messages.getVersion(String message) Returns the version ID (MSH-12) from the given message, without fully parsing the message.protected booleankeepAsOriginal(Node node) Checks ifNodecontent should be kept as original (ie.: whitespaces won't be removed)voidparse(Segment segment, String string, EncodingCharacters encodingCharacters) Throws unsupported operation exceptionvoidPopulates the given Segment object with data from the given XML Element.voidparse(Type type, String string, EncodingCharacters encodingCharacters) Throws unsupported operation exceptionvoidPopulates the given Type object with data from the given XML Element.abstract MessageparseDocument(Document xmlMessage, String version) Creates and populates a Message object from an XML Document that contains an XML-encoded HL7 message.protected static StringAttempts to retrieve the value of a leaf tag without using DOM or SAX.protected DocumentparseStringIntoDocument(String message) Parses a string containing an XML document into a Document object.protected StringRemoves all unnecessary whitespace from the given String (intended to be used with Primitive values).voidsetKeepAsOriginalNodes(String[] keepAsOriginalNodes) Deprecated.voidsetTextEncoding(String textEncoding) Sets the text encoding to be used in generating new messages.Methods inherited from class ca.uhn.hl7v2.parser.Parser
applySuperStructureName, assertVersionExists, encode, encode, getFactory, getMessageStructureForEvent, getParserConfiguration, getValidationContext, instantiateMessage, instantiateMessageInASpecificPackage, makeControlMSH, parse, parse, parseForSpecificPackage, setParserConfiguration, setValidationContext, supportsEncoding, validVersionMethods inherited from class ca.uhn.hl7v2.HapiContextSupport
getHapiContext, setHapiContext
-
Field Details
-
NS
- See Also:
-
-
Constructor Details
-
XMLParser
public XMLParser()Constructor -
XMLParser
- Parameters:
context- the HAPI context
-
XMLParser
Constructor- Parameters:
theFactory- custom factory to use for model class lookup
-
-
Method Details
-
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"
-
getDefaultEncoding
- Specified by:
getDefaultEncodingin classParser- Returns:
- the preferred encoding of this Parser
-
setKeepAsOriginalNodes
Deprecated.UseParserConfiguration.setXmlDisableWhitespaceTrimmingOnNodeNames(Set)instead. That method works exactly the same as this one but has been renamed for a more clear meaning.Sets the keepAsOriginalNodes The nodes whose names match the keepAsOriginalNodes will be kept as original, meaning that no white space treaming will occur on them- Parameters:
keepAsOriginalNodes- of the nodes to be kept as original
-
getKeepAsOriginalNodes
Deprecated.Sets the keepAsOriginalNodes -
parseDocument
Creates and populates a Message object from an XML Document that contains an XML-encoded HL7 message.
The easiest way to implement this method for a particular message structure is as follows:
- Create an instance of the Message type you are going to handle with your subclass of XMLParser
- Go through the given Document and find the Elements that represent the top level of each message segment.
- For each of these segments, call
parse(Segment segmentObject, Element segmentElement), providing the appropriate Segment from your Message object, and the corresponding Element.
- Parameters:
xmlMessage- DOM message object to be parsedversion- HL7 version- Throws:
HL7Exception- if the message is not correctly formatted.EncodingNotSupportedException- if the message encoded is not supported by this parser.
-
doParse
Parses a message string and returns the corresponding Message object. This method checks that the given message string is XML encoded, creates an XML Document object (using Xerces) from the given String, and calls the abstract method
parse(Document XMLMessage)- 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.
-
parseStringIntoDocument
Parses a string containing an XML document into a Document object. Note that this method is synchronized currently, as the XML parser is not thread safe- Throws:
HL7Exception
-
doEncode
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.
-
doEncode
Formats a Message object into an HL7 message string using this parser's default encoding (XML encoding). This method calls the abstract methodencodeDocument(...)in order to obtain XML Document object representation of the Message, then serializes it to a String.- 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)
-
encodeDocument
Creates an XML Document that corresponds to the given Message object.
If you are implementing this method, you should create an XML Document, and insert XML Elements into it that correspond to the groups and segments that belong to the message type that your subclass of XMLParser supports. Then, for each segment in the message, call the method
encode(Segment segmentObject, Element segmentElement)using the Element for that segment and the corresponding Segment object from the given Message.- Parameters:
source- message- Returns:
- the DOM document object of the encoded message
- Throws:
HL7Exception
-
assertNamespaceURI
- Throws:
HL7Exception
-
parse
Populates the given Segment object with data from the given XML Element.- Parameters:
segmentObject- the segment to parse intosegmentElement- the DOM element to be parsed- Throws:
HL7Exception- if the XML Element does not have the correct name and structure for the given Segment, or if there is an error while setting individual field values.
-
encode
Populates the given Element with data from the given Segment, by inserting Elements corresponding to the Segment's fields, their components, etc. Returns true if there is at least one data value in the segment.- Parameters:
segmentObject- the segment to be encodedsegmentElement- the DOM element to encode into- Returns:
- true if there is at least one data value in the segment
- Throws:
HL7Exception- if an erro occurred while encoding
-
parse
Populates the given Type object with data from the given XML Element.- Parameters:
datatypeObject- the type to parse intodatatypeElement- the DOM element to be parsed- Throws:
DataTypeException- if the data did not match the expected type rulesHL7Exception
-
keepAsOriginal
Checks ifNodecontent should be kept as original (ie.: whitespaces won't be removed)- Parameters:
node- The targetNode- Returns:
- boolean
trueif whitespaces should not be removed from node content,falseotherwise
-
removeWhitespace
Removes all unnecessary whitespace from the given String (intended to be used with Primitive values). This includes leading and trailing whitespace, and repeated space characters. Carriage returns, line feeds, and tabs are replaced with spaces. -
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
-
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). Trims whitespace from around the MSA-2 field. -
getVersion
Description copied from class:ParserReturns 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.
-
parseLeaf
Attempts to retrieve the value of a leaf tag without using DOM or SAX. This method searches the given message string for the given tag name, and returns everything after the given tag and before the start of the next tag. Whitespace is stripped. This is intended only for lead nodes, as the value is considered to end at the start of the next tag, regardless of whether it is the matching end tag or some other nested tag.- Parameters:
message- a string message in XML formtagName- the name of the XML tag, e.g. "MSA.2"startAt- the character location at which to start searching- Throws:
HL7Exception- if the tag can not be found
-
doEncode
Throws unsupported operation exception- Specified by:
doEncodein classParser- Parameters:
structure- The structure to encodeencodingCharacters- The encoding characters- Returns:
- The encoded segment
- Throws:
UnsupportedOperationException
-
doParseForSpecificPackage
protected Message doParseForSpecificPackage(String theMessage, String theVersion, String thePackageName) Throws unsupported operation exception- Specified by:
doParseForSpecificPackagein classParser- Throws:
UnsupportedOperationException
-
doEncode
Throws unsupported operation exception- Specified by:
doEncodein classParser- Parameters:
type- The type to encodeencodingCharacters- The encoding characters- Returns:
- The encoded type
- Throws:
UnsupportedOperationException
-
parse
Throws unsupported operation exception- Specified by:
parsein classParser- Parameters:
type- The type to encodestring- The string to parseencodingCharacters- The encoding characters- Throws:
UnsupportedOperationException
-
parse
Throws unsupported operation exception- Specified by:
parsein classParser- Parameters:
segment- The segment to encodestring- The string to parseencodingCharacters- The encoding characters- Throws:
UnsupportedOperationException
-
getTextEncoding
Returns the text encoding to be used in generating new messages. Note that this affects encoding to string only, not parsing.- Returns:
- text encoding
-
setTextEncoding
Sets the text encoding to be used in generating new messages. Note that this affects encoding to string only, not parsing.- Parameters:
textEncoding- The encoding. Default is the platform default.
-
ParserConfiguration.getXmlDisableWhitespaceTrimmingOnNodeNames()instead