public abstract class XMLParser extends Parser
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.for configuration options which may affect parser encoding and decoding behaviour
Constructor and Description |
---|
XMLParser()
Constructor
|
XMLParser(HapiContext context) |
XMLParser(ModelClassFactory theFactory)
Constructor
|
Modifier and Type | Method and Description |
---|---|
protected void |
assertNamespaceURI(String ns) |
protected String |
doEncode(Message source)
Formats a Message object into an HL7 message string using this parser's default encoding (XML
encoding).
|
protected String |
doEncode(Message source,
String encoding)
Formats a Message object into an HL7 message string using the given encoding.
|
String |
doEncode(Segment structure,
EncodingCharacters encodingCharacters)
Throws unsupported operation exception
|
String |
doEncode(Type type,
EncodingCharacters encodingCharacters)
Throws unsupported operation exception
|
protected Message |
doParse(String message,
String version)
Parses a message string and returns the corresponding Message object.
|
protected Message |
doParseForSpecificPackage(String theMessage,
String theVersion,
String thePackageName)
Throws unsupported operation exception
|
boolean |
encode(Segment segmentObject,
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 Document |
encodeDocument(Message source)
Creates an XML Document that corresponds to the given Message object.
|
String |
getAckID(String message)
For response messages, returns the value of MSA-2 (the message ID of the message sent by the
sending system).
|
Segment |
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.
|
String |
getDefaultEncoding() |
String |
getEncoding(String message)
Returns a String representing the encoding of the given message, if the encoding is
recognized.
|
String[] |
getKeepAsOriginalNodes()
Deprecated.
|
String |
getTextEncoding()
Returns the text encoding to be used in generating new messages.
|
String |
getVersion(String message)
Returns the version ID (MSH-12) from the given message, without fully parsing the message.
|
protected boolean |
keepAsOriginal(Node node)
Checks if
Node content should be kept as original (ie.: whitespaces won't be
removed) |
void |
parse(Segment segmentObject,
Element segmentElement)
Populates the given Segment object with data from the given XML Element.
|
void |
parse(Segment segment,
String string,
EncodingCharacters encodingCharacters)
Throws unsupported operation exception
|
void |
parse(Type datatypeObject,
Element datatypeElement)
Populates the given Type object with data from the given XML Element.
|
void |
parse(Type type,
String string,
EncodingCharacters encodingCharacters)
Throws unsupported operation exception
|
abstract Message |
parseDocument(Document xmlMessage,
String version)
Creates and populates a Message object from an XML Document that contains an XML-encoded HL7
message.
|
protected static String |
parseLeaf(String message,
String tagName,
int startAt)
Attempts to retrieve the value of a leaf tag without using DOM or SAX.
|
protected Document |
parseStringIntoDocument(String message)
Parses a string containing an XML document into a Document object.
|
protected String |
removeWhitespace(String s)
Removes all unnecessary whitespace from the given String (intended to be used with Primitive
values).
|
void |
setKeepAsOriginalNodes(String[] keepAsOriginalNodes)
Deprecated.
Use
ParserConfiguration.setXmlDisableWhitespaceTrimmingOnNodeNames(Set) instead. That method works exactly the same as this one but has been renamed for a more clear meaning. |
void |
setTextEncoding(String textEncoding)
Sets the text encoding to be used in generating new messages.
|
applySuperStructureName, assertVersionExists, encode, encode, getFactory, getMessageStructureForEvent, getParserConfiguration, getValidationContext, instantiateMessage, instantiateMessageInASpecificPackage, makeControlMSH, parse, parse, parseForSpecificPackage, setParserConfiguration, setValidationContext, supportsEncoding, validVersion
getHapiContext, setHapiContext
protected static final String NS
public XMLParser()
public XMLParser(HapiContext context)
context
- the HAPI contextpublic XMLParser(ModelClassFactory theFactory)
theFactory
- custom factory to use for model class lookuppublic String getEncoding(String message)
getEncoding
in class Parser
message
- message stringpublic String getDefaultEncoding()
getDefaultEncoding
in class Parser
@Deprecated public void setKeepAsOriginalNodes(String[] keepAsOriginalNodes)
ParserConfiguration.setXmlDisableWhitespaceTrimmingOnNodeNames(Set)
instead. That method works exactly the same as this one but has been renamed for a more clear meaning.keepAsOriginalNodes
- of the nodes to be kept as original@Deprecated public String[] getKeepAsOriginalNodes()
ParserConfiguration.getXmlDisableWhitespaceTrimmingOnNodeNames()
insteadpublic abstract Message parseDocument(Document xmlMessage, 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:
parse(Segment segmentObject, Element segmentElement)
, providing the appropriate
Segment from your Message object, and the corresponding Element.xmlMessage
- DOM message object to be parsedversion
- HL7 versionHL7Exception
- if the message is not correctly formatted.EncodingNotSupportedException
- if the message encoded is not supported by this parser.protected Message doParse(String message, String version) throws HL7Exception
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)
doParse
in class Parser
message
- a String that contains an HL7 messageversion
- the name of the HL7 version to which the message belongs (eg "2.5")HL7Exception
- if the message is not correctly formatted.EncodingNotSupportedException
- if the message encoded is not supported by this parser.protected Document parseStringIntoDocument(String message) throws HL7Exception
HL7Exception
protected String doEncode(Message source, String encoding) throws HL7Exception
doEncode
in class Parser
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)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.protected String doEncode(Message source) throws HL7Exception
encodeDocument(...)
in order to
obtain XML Document object representation of the Message, then serializes it to a String.doEncode
in class Parser
source
- a Message object from which to construct an encoded message stringHL7Exception
- if the data fields in the message do not permit encoding (e.g. required
fields are null)public abstract 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.
source
- messageHL7Exception
protected void assertNamespaceURI(String ns) throws HL7Exception
HL7Exception
public void parse(Segment segmentObject, Element segmentElement) throws HL7Exception
segmentObject
- the segment to parse intosegmentElement
- the DOM element to be parsedHL7Exception
- 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.public boolean encode(Segment segmentObject, Element segmentElement) throws HL7Exception
segmentObject
- the segment to be encodedsegmentElement
- the DOM element to encode intoHL7Exception
- if an erro occurred while encodingpublic void parse(Type datatypeObject, Element datatypeElement) throws HL7Exception
datatypeObject
- the type to parse intodatatypeElement
- the DOM element to be parsedDataTypeException
- if the data did not match the expected type rulesHL7Exception
protected boolean keepAsOriginal(Node node)
Node
content should be kept as original (ie.: whitespaces won't be
removed)node
- The target Node
true
if whitespaces should not be removed from node content,
false
otherwiseprotected String removeWhitespace(String s)
public Segment getCriticalResponseData(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:
getCriticalResponseData
in class Parser
message
- the messageHL7Exception
- if no MSH segment could be createdpublic String getAckID(String message)
public String getVersion(String message) throws HL7Exception
Parser
getVersion
in class Parser
message
- the messageHL7Exception
- if the version field can not be found.protected static String parseLeaf(String message, String tagName, int startAt) throws HL7Exception
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 searchingHL7Exception
- if the tag can not be foundpublic String doEncode(Segment structure, EncodingCharacters encodingCharacters) throws HL7Exception
doEncode
in class Parser
structure
- The structure to encodeencodingCharacters
- The encoding charactersUnsupportedOperationException
HL7Exception
- If there is a problem encodingprotected Message doParseForSpecificPackage(String theMessage, String theVersion, String thePackageName) throws HL7Exception
doParseForSpecificPackage
in class Parser
UnsupportedOperationException
HL7Exception
public String doEncode(Type type, EncodingCharacters encodingCharacters) throws HL7Exception
doEncode
in class Parser
type
- The type to encodeencodingCharacters
- The encoding charactersUnsupportedOperationException
HL7Exception
- If there is a problem encodingpublic void parse(Type type, String string, EncodingCharacters encodingCharacters) throws HL7Exception
parse
in class Parser
type
- The type to encodestring
- The string to parseencodingCharacters
- The encoding charactersUnsupportedOperationException
HL7Exception
- If there is a problem encodingpublic void parse(Segment segment, String string, EncodingCharacters encodingCharacters) throws HL7Exception
parse
in class Parser
segment
- The segment to encodestring
- The string to parseencodingCharacters
- The encoding charactersUnsupportedOperationException
HL7Exception
- If there is a problem encodingpublic String getTextEncoding()
public void setTextEncoding(String textEncoding)
textEncoding
- The encoding. Default is the platform default.Copyright © 2001–2017 University Health Network. All rights reserved.