ca.uhn.hl7v2.parser
Class XMLParser

java.lang.Object
  extended by ca.uhn.hl7v2.parser.Parser
      extended by ca.uhn.hl7v2.parser.XMLParser
Direct Known Subclasses:
DefaultXMLParser

public abstract class XMLParser
extends Parser

Parses and encodes HL7 messages in XML form, according to HL7's normative XML encoding specification. This is an abstract class that handles datatype and segment parsing/encoding, but not the parsing/encoding of entire messages. To use the XML parser, you should create a subclass for a certain message structure. This subclass must be able to identify the Segment objects that correspond to various Segment nodes in an XML document, and call the methods 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

Constructor Summary
XMLParser()
          Constructor
XMLParser(ModelClassFactory theFactory)
          Constructor
 
Method Summary
protected  java.lang.String doEncode(Message source)
          Formats a Message object into an HL7 message string using this parser's default encoding (XML 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)
          Throws unsupported operation exception
 java.lang.String doEncode(Type type, EncodingCharacters encodingCharacters)
          Throws unsupported operation exception
protected  Message doParse(java.lang.String message, java.lang.String version)
          Parses a message string and returns the corresponding Message object.
 boolean encode(Segment segmentObject, org.w3c.dom.Element segmentElement)
          Populates the given Element with data from the given Segment, by inserting Elements corresponding to the Segment's fields, their components, etc.
abstract  org.w3c.dom.Document encodeDocument(Message source)
          Creates an XML Document that corresponds to the given 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[] getKeepAsOriginalNodes()
          Sets the keepAsOriginalNodes
 java.lang.String getTextEncoding()
          Returns the text encoding to be used in generating new messages.
 java.lang.String getVersion(java.lang.String message)
          Returns the version ID (MSH-12) from the given message, without fully parsing the message.
protected  boolean keepAsOriginal(org.w3c.dom.Node node)
          Checks if Node content should be kept as original (ie.: whitespaces won't be removed)
static void main(java.lang.String[] args)
          Test harness
 void parse(Message message, java.lang.String string)
          Throws unsupported operation exception
 void parse(Segment segmentObject, org.w3c.dom.Element segmentElement)
          Populates the given Segment object with data from the given XML Element.
 void parse(Segment segment, java.lang.String string, EncodingCharacters encodingCharacters)
          Throws unsupported operation exception
 void parse(Type datatypeObject, org.w3c.dom.Element datatypeElement)
          Populates the given Type object with data from the given XML Element.
 void parse(Type type, java.lang.String string, EncodingCharacters encodingCharacters)
          Throws unsupported operation exception
abstract  Message parseDocument(org.w3c.dom.Document XMLMessage, java.lang.String version)
          Creates and populates a Message object from an XML Document that contains an XML-encoded HL7 message.
protected  java.lang.String parseLeaf(java.lang.String message, java.lang.String tagName, int startAt)
          Attempts to retrieve the value of a leaf tag without using DOM or SAX.
protected  java.lang.String removeWhitespace(java.lang.String s)
          Removes all unecessary whitespace from the given String (intended to be used with Primitive values).
 void setKeepAsOriginalNodes(java.lang.String[] keepAsOriginalNodes)
          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
 void setTextEncoding(java.lang.String textEncoding)
          Sets the text encoding to be used in generating new messages.
 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, setValidationContext, validVersion
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

XMLParser

public XMLParser()
Constructor


XMLParser

public XMLParser(ModelClassFactory theFactory)
Constructor

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

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

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 this Parser

setKeepAsOriginalNodes

public void setKeepAsOriginalNodes(java.lang.String[] keepAsOriginalNodes)
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


getKeepAsOriginalNodes

public java.lang.String[] getKeepAsOriginalNodes()
Sets the keepAsOriginalNodes


parseDocument

public abstract Message parseDocument(org.w3c.dom.Document XMLMessage,
                                      java.lang.String version)
                               throws HL7Exception

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:

  1. Create an instance of the Message type you are going to handle with your subclass of XMLParser
  2. Go through the given Document and find the Elements that represent the top level of each message segment.
  3. For each of these segments, call parse(Segment segmentObject, Element segmentElement), providing the appropriate Segment from your Message object, and the corresponding Element.
At the end of this process, your Message object should be populated with data from the XML Document.

Throws:
HL7Exception - if the message is not correctly formatted.
EncodingNotSupportedException - if the message encoded is not supported by this 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. 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:
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,
                                    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.

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 (XML encoding). This method calls the abstract method encodeDocument(...) in order to obtain XML Document object representation of the Message, then serializes it to a String.

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)

encodeDocument

public abstract org.w3c.dom.Document encodeDocument(Message source)
                                             throws HL7Exception

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.

Throws:
HL7Exception

parse

public void parse(Segment segmentObject,
                  org.w3c.dom.Element segmentElement)
           throws HL7Exception
Populates the given Segment object with data from the given XML Element.

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

public boolean encode(Segment segmentObject,
                      org.w3c.dom.Element segmentElement)
               throws HL7Exception
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.

Throws:
HL7Exception

parse

public void parse(Type datatypeObject,
                  org.w3c.dom.Element datatypeElement)
           throws DataTypeException
Populates the given Type object with data from the given XML Element.

Throws:
DataTypeException

keepAsOriginal

protected boolean keepAsOriginal(org.w3c.dom.Node node)
Checks if Node content should be kept as original (ie.: whitespaces won't be removed)

Parameters:
node - The target Node
Returns:
boolean true if whitespaces should not be removed from node content, false otherwise

removeWhitespace

protected java.lang.String removeWhitespace(java.lang.String s)
Removes all unecessary 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

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:

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

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). Trims whitespace from around the MSA-2 field.

Specified by:
getAckID in class Parser

getVersion

public java.lang.String getVersion(java.lang.String message)
                            throws HL7Exception
Description copied from class: Parser
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.

parseLeaf

protected java.lang.String parseLeaf(java.lang.String message,
                                     java.lang.String tagName,
                                     int startAt)
                              throws HL7Exception
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 form
tagName - 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

public java.lang.String doEncode(Segment structure,
                                 EncodingCharacters encodingCharacters)
                          throws HL7Exception
Throws unsupported operation exception

Specified by:
doEncode in class Parser
Parameters:
structure - The structure to encode
encodingCharacters - The encoding characters
Returns:
The encoded segment
Throws:
Unsupported - operation exception
HL7Exception - If there is a problem encoding

parse

public void parse(Message message,
                  java.lang.String string)
           throws HL7Exception
Throws unsupported operation exception

Specified by:
parse in class Parser
Parameters:
message - The message to encode
string - The string to parse
Throws:
Unsupported - operation exception
HL7Exception - If there is a problem encoding

doEncode

public java.lang.String doEncode(Type type,
                                 EncodingCharacters encodingCharacters)
                          throws HL7Exception
Throws unsupported operation exception

Specified by:
doEncode in class Parser
Parameters:
type - The type to encode
encodingCharacters - The encoding characters
Returns:
The encoded type
Throws:
Unsupported - operation exception
HL7Exception - If there is a problem encoding

parse

public void parse(Type type,
                  java.lang.String string,
                  EncodingCharacters encodingCharacters)
           throws HL7Exception
Throws unsupported operation exception

Specified by:
parse in class Parser
Parameters:
type - The type to encode
string - The string to parse
encodingCharacters - The encoding characters
Throws:
Unsupported - operation exception
HL7Exception - If there is a problem encoding

parse

public void parse(Segment segment,
                  java.lang.String string,
                  EncodingCharacters encodingCharacters)
           throws HL7Exception
Throws unsupported operation exception

Specified by:
parse in class Parser
Parameters:
segment - The segment to encode
string - The string to parse
encodingCharacters - The encoding characters
Throws:
Unsupported - operation exception
HL7Exception - If there is a problem encoding

main

public static void main(java.lang.String[] args)
Test harness


getTextEncoding

public java.lang.String getTextEncoding()
Returns the text encoding to be used in generating new messages. Note that this affects encoding to string only, not parsing.

Returns:

setTextEncoding

public void setTextEncoding(java.lang.String textEncoding)
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.


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