java - How to find if HL7 Segment has ended or not if Carriage return is not present -
i working on tool construct hl7 message in following way :
message start : 0b segment end : od , message end : 1c0d
so, here have reached far, able add ob , add 1c0d in end of hl7 message. able add od before @ end of segment. accomplishing of code check if character before segment name 0d or not.
but issue if text in message ...pid| code add 0d before pid| not correct should check if start of segment or not.
please if has worked on similar requirement.
link code : arraylist sublist indexoutofbounds exception
thanks in advance
i had time @ problem. far understand, have piece of code generates hl7v2 segments , want create message following delimiters:
- segment delimiter: 0x0d (or 13 in ascii), carriage return. it's segment separator, per hl7v2 standard;
- message start delimiter: 0x0b (ascii 11 - vertical tab);
- message finish delimiter: 0x1c0d. guess value supposed concatenation of 0x1c (ascii 28 - file separator) , 0x0d (ascii 13 - carriage return).
with #1 hl7v2 messages standard-compliant. #2 , #3 able define delimiters message can processed , parsed later custom processor.
so took shot writing simple code , here's result:
public class app { public static void main( string[] args ) throws exception { string msg = "msh|^~\\&|his|rih|ekg|ekg|199904140038||adt^a01||p|2.5" + "pid|0001|00009874|00001122|a00977|smith^john^m|mom|19581119|f|notreal^linda^m|c|564 spring st^^needham^ma^02494^us" + "al1||sev|001^pollen"; string[] segments = msg.split("(?=pid|al1)"); system.out.println("initial message:"); (string s : segments) system.out.println(s); byte hexstartmessage = 0x0b; byte hexfinishmessage1 = 0x1c; byte hexfinishmessage2 = 0x0d; byte hexfinishsegment = 0x0d; string finalmessage = byte.tostring(hexstartmessage) + intersperse(segments, hexfinishsegment) + byte.tostring(hexfinishmessage1) + byte.tostring(hexfinishmessage2); system.out.println("\nfinal message:\n" + finalmessage); } public static string intersperse(string[] segments, byte delimiter) throws unsupportedencodingexception { // uncomment line if wish show delimiter in output //system.out.printf("byte delimiter: %s", string.format("%04x", (int)delimiter)); stringbuilder sb = new stringbuilder(); string defaultdelimiter = ""; (string segment : segments) { sb.append(defaultdelimiter).append(segment); defaultdelimiter = byte.tostring(delimiter); } return sb.tostring(); } }
i picked simple hl7v2 message , splitted in segments, according segments (name) used in message, of regex lookahead strategy. means that, messages you'll need know segments going used (you can standard).
i interspersed segment delimiter between each segment (at end) , added message start , end delimiters. in case, message end delimiters, used 0x1c , 0x0d values separated, if need use single value need change final appends.
here's output:
initial message: msh|^~\&|his|rih|ekg|ekg|199904140038||adt^a01||p|2.5 pid|0001|00009874|00001122|a00977|smith^john^m|mom|19581119|f|notreal^linda^m|c|564 spring st^^needham^ma^02494^us al1||sev|001^pollen final message: 11msh|^~\&|his|rih|ekg|ekg|199904140038||adt^a01||p|2.5 pid|0001|00009874|00001122|a00977|smith^john^m|mom|19581119|f|notreal^linda^m|c|564 spring st^^needham^ma^02494^us al1||sev|001^pollen2813
as see, final message begins value 11 (0x0b) , ends 28 (0x1c) , 13 (0x0d). 13 (0x0d) @ end of each segment not shown because java's system.out.println()
recognizes being '\r'
character , starts new line because i'm running in mac os x. if try intersperse segments other character (ex: 0x25 = '%') you'll notice final message printed in single line:
11msh|^~\&|his|rih|ekg|ekg|199904140038||adt^a01||p|2.5%pid|0001|00009874|00001122|a00977|smith^john^m|mom|19581119|f|notreal^linda^m|c|564 spring st^^needham^ma^02494^us%al1||sev|001^pollen2813
if run in ubuntu, see message in 1 line segment delimiter:
11msh|^~\&|his|rih|ekg|ekg|199904140038||adt^a01||p|2.513pid|0001|00009874|00001122|a00977|smith^john^m|mom|19581119|f|notreal^linda^m|c|564 spring st^^needham^ma^02494^us13al1||sev|001^pollen2813
Comments
Post a Comment