hl7.serialize.lua
Verified Featured
Added by iNTERFACEWARE
Serializes an HL7 message using specified non-standard delimiters and/or escape characters
Source Code
-- hl7.serialize module -- http://help.interfaceware.com/code/details/hl7-serialize-lua -- Will search for instances of Pattern in S. -- Will call replacement function onUnmatched -- on unmatched parts of S, and onMatched on -- matched parts of S. local function search(S, Pattern, onUnmatched, onMatched) Pattern = '^(.-)('..Pattern..')(.*)$' local Out = {} local function loop(S) local Head, Match, Tail = select(3, S:find(Pattern)) if Match then table.insert(Out, (onUnmatched(Head)) ) table.insert(Out, (onMatched(Match)) ) return loop(Tail) else table.insert(Out, (onUnmatched(S)) ) return table.concat(Out) end end return loop(S) end local charsToEscape = {['(']=1, [')']=1, ['.']=1, ['%']=1, ['+']=1, ['-']=1, ['*']=1, ['?']=1, ['[']=1, ['^']=1, ['$']=1} local function charToPattern(C) if charsToEscape[C] then return '%'..C else return C end end -- Only for HL7 Messages -- Accepts a table which must contain the following parameter: -- data: the HL7 message to be serialized. -- Also accepts the following optional parameters: -- delimiters: a list of delimiters to use in the message, -- if different from the default {'\r', '|', '^', '~', '\\', '&'}. -- escaped: a list representing the character used to represent -- an escaped delimiter character, if different from the default -- {'F', 'S', 'R', 'E', 'T'}. -- function hl7.serialize(Params) Params = Params or {} local Msg = Params.data local NodeType, ProtocolType = Msg:nodeType() if NodeType ~= 'message' or ProtocolType ~= 'hl7' then error('Expected hl7 message, got '..ProtocolType..' '..NodeType, 2) end local Serialized = tostring(Msg) local Delimiters = Params.delimiters or {'\r', '|', '^', '~', '\\', '&'} if Delimiters and #Delimiters ~= 6 then error('Expected delimiter list of size 6, got '..#Delimiters, 2) end for N,D in ipairs(Delimiters) do if #D ~= 1 then error('Delimiters must be exactly one character, "'..D..'" is '..#D, 2) end end local Escaped = Params.escaped or {'F', 'S', 'R', 'E', 'T'} trace(Escaped) if #Escaped ~= 5 then error('Expected escaped list of size 5, got '..#Escaped, 2) end local DelimiterMap = { ['\r']=Delimiters[1], ['|']=Delimiters[2], ['^']=Delimiters[3], ['~']=Delimiters[4], ['\\']=Delimiters[5], ['&']=Delimiters[6] } local UnescapeMap = { ['\\F\\']='|', ['\\S\\']='^', ['\\R\\']='~', ['\\E\\']='\\', ['\\T\\']='&' } local EscapeNewDelimiterMap = { [Delimiters[1]]=Delimiters[5]..'X'..Delimiters[1]:byte(1)..Delimiters[5], [Delimiters[2]]=Delimiters[5]..Escaped[1]..Delimiters[5], [Delimiters[3]]=Delimiters[5]..Escaped[2]..Delimiters[5], [Delimiters[4]]=Delimiters[5]..Escaped[3]..Delimiters[5], [Delimiters[5]]=Delimiters[5]..Escaped[4]..Delimiters[5], [Delimiters[6]]=Delimiters[5]..Escaped[5]..Delimiters[5] } trace(EscapeNewDelimiterMap) local DelimiterPattern = '[\r|%^~\\&'.. charToPattern(Delimiters[1]).. charToPattern(Delimiters[2]).. charToPattern(Delimiters[3]).. charToPattern(Delimiters[4]).. charToPattern(Delimiters[5]).. charToPattern(Delimiters[6])..']' Serialized = search(Serialized, '\\[FSRET]\\', function(Unmatched) return Unmatched:gsub(DelimiterPattern, function(Match) return DelimiterMap[Match] or EscapeNewDelimiterMap[Match] end ) end, function(Matched) local Unescaped = UnescapeMap[Matched] local NewEscape = EscapeNewDelimiterMap[Unescaped] if NewEscape then return NewEscape else return Unescaped end end) return Serialized end local hl7_serialize = { Title="hl7.serialize"; Usage="hl7.serialize{data [, {delimeters}] [, {escapes}]", SummaryLine="Generate an HL7 message with non-standard delimiters", Desc=[[Generates an HL7 message with non-standard delimiters, and optionally non-standard escape characters (for embedded delimiters). <p>Serializes an HL7 node tree (message) using the supplied delimiters, if no delimiters are supplied the HL7 defaults are used. Non-standard escapes for embedded delimiters can also be supplied, if no escapes are supplied it uses the HL7 defaults. <p><b>Note</b>: This function only works with HL7 messages. ]]; ["Returns"] = { {Desc="The serialized HL7 message <u>string</u>."}, }; ParameterTable= true, Parameters= { {data= {Desc='An HL7 message (node tree) to be serialized <u>hl7 node tree</u>.'}}, {delimiters= {Desc='A list of delimiters to use in the message <u>table</u>.'}}, {escapes= {Desc='A list of delimiter escape characters <u>table</u>.'}}, }; Examples={ [[ Msg = hl7.parse{vmd='demo.vmd', data=Data} hl7.serialize{data=Msg} hl7.serialize{data=Msg, delimiters = {'\n', '#', '.', '&', '\'', '*'}} hl7.serialize{data=Msg, escaped = {'A', 'B', 'C', 'D', 'E'}} hl7.serialize{data = Msg, delimiters = {'\n', '&', '\\', '}', '~', '^'}, escaped = {'A', 'B', 'C', 'D', 'E'}} hl7.serialize{data = Msg, delimiters = {'A', '|', '^', '~', '\\', '&'}} ]], }; SeeAlso={ { Title="hl7.serialize.lua - in our code repository", Link="http://help.interfaceware.com/code/details/hl7-serialize-lua" }, { Title="hl7.serialize", Link="http://help.interfaceware.com/v6/hl7-serialize" } } } help.set{input_function=hl7.serialize, help_data=hl7_serialize}
Description
Serializes an HL7 message using specified non-standard delimiters and/or escape characters
Attachments
Usage Details
Have you ever needed to generate an HL7 message with non-standard delimiters? Sometimes the receiving party is expecting their own flavor of HL7 with strange delimiters, so what do you do? You can use this function, hl7.serialize{}
, which will serialize an HL7 message with a provided set of delimiters (and/or a provided set of “escape characters”).
How to use hl7.serialize.lua:
- Add the module to a Filter or To Translator script
- You can also load the example project
- Use
hl7.serialize{}
to serialize the code with the desired delimiters and/or escape characters - Inspect the code and annotations to see how it works
Here is some sample code for main()
:
-- This module goes into the hl7 namespace require 'hl7.serialize' -- The module allows us to tightly control what delimiters and escape sequences are used to encode an HL7 message -- https://help.interfaceware.com/code/details/hl7-serialize-lua function main(Data) Msg = hl7.parse{vmd='demo.vmd', data=Data} hl7.serialize{data=Msg} hl7.serialize{data=Msg, delimiters = {'\n', '#', '.', '&', '\'', '*'}} hl7.serialize{data=Msg, escaped = {'A', 'B', 'C', 'D', 'E'}} hl7.serialize{data = Msg, delimiters = {'\n', '&', '\\', '}', '~', '^'}, escaped = {'A', 'B', 'C', 'D', 'E'}} hl7.serialize{data = Msg, delimiters = {'A', '|', '^', '~', '\\', '&'}} end