/// <summary> Formats a Message object into an HL7 message string using this parser's /// default encoding ("VB"). /// </summary> /// <throws> HL7Exception if the data fields in the message do not permit encoding </throws> /// <summary> (e.g. required fields are null) /// </summary> protected internal override System.String doEncode(Message source) { //get encoding characters ... Segment msh = (Segment)source.get_Renamed("MSH"); System.String fieldSepString = Terser.get_Renamed(msh, 1, 0, 1, 1); if (fieldSepString == null) { throw new NuGenHL7Exception("Can't encode message: MSH-1 (field separator) is missing"); } char fieldSep = '|'; if (fieldSepString != null && fieldSepString.Length > 0) { fieldSep = fieldSepString[0]; } System.String encCharString = Terser.get_Renamed(msh, 2, 0, 1, 1); if (encCharString == null) { throw new NuGenHL7Exception("Can't encode message: MSH-2 (encoding characters) is missing"); } if (encCharString.Length != 4) { throw new NuGenHL7Exception("Encoding characters '" + encCharString + "' invalid -- must be 4 characters", NuGenHL7Exception.DATA_TYPE_ERROR); } NuGenEncodingCharacters en = new NuGenEncodingCharacters(fieldSep, encCharString); //pass down to group encoding method which will operate recursively on children ... return(encode((Group)source, en)); }
public static System.String escape(System.String text, NuGenEncodingCharacters encChars) { System.Text.StringBuilder result = new System.Text.StringBuilder(); int textLength = text.Length; System.Collections.Hashtable esc = getEscapeSequences(encChars); SupportClass.SetSupport keys = new SupportClass.HashSetSupport(esc.Keys); System.String escChar = System.Convert.ToString(encChars.EscapeCharacter); int position = 0; while (position < textLength) { System.Collections.IEnumerator it = keys.GetEnumerator(); bool isReplaced = false; while (it.MoveNext() && !isReplaced) { System.String seq = (System.String)it.Current; System.String val = (System.String)esc[seq]; if (text.Substring(position, (position + 1) - (position)).Equals(val)) { result.Append(seq); isReplaced = true; } } if (!isReplaced) { result.Append(text.Substring(position, ((position + 1)) - (position))); } position++; } return(result.ToString()); }
public static System.String unescape(System.String text, NuGenEncodingCharacters encChars) { System.Text.StringBuilder result = new System.Text.StringBuilder(); int textLength = text.Length; System.Collections.Hashtable esc = getEscapeSequences(encChars); SupportClass.SetSupport keys = new SupportClass.HashSetSupport(esc.Keys); System.String escChar = System.Convert.ToString(encChars.EscapeCharacter); int position = 0; while (position < textLength) { System.Collections.IEnumerator it = keys.GetEnumerator(); bool isReplaced = false; while (it.MoveNext() && !isReplaced) { System.String seq = (System.String) it.Current; System.String val = (System.String) esc[seq]; int seqLength = seq.Length; if (position + seqLength <= textLength) { if (text.Substring(position, (position + seqLength) - (position)).Equals(seq)) { result.Append(val); isReplaced = true; position = position + seq.Length; } } } if (!isReplaced) { result.Append(text.Substring(position, ((position + 1)) - (position))); position++; } } return result.ToString(); }
/// <summary> Creates new GroupPointer </summary> public NuGenMessagePointer(NuGenPipeParser parser, Message message, NuGenEncodingCharacters encodingChars) { this.parser = parser; this.message = message; this.encodingChars = encodingChars; makeChildren(); }
/// <summary> Returns given group serialized as a pipe-encoded string - this method is called /// by encode(Message source, String encoding). /// </summary> public static System.String encode(Group source, NuGenEncodingCharacters encodingChars) { System.Text.StringBuilder result = new System.Text.StringBuilder(); System.String[] names = source.Names; for (int i = 0; i < names.Length; i++) { Structure[] reps = source.getAll(names[i]); for (int rep = 0; rep < reps.Length; rep++) { if (reps[rep] is Group) { result.Append(encode((Group)reps[rep], encodingChars)); } else { System.String segString = encode((Segment)reps[rep], encodingChars); if (segString.Length >= 4) { result.Append(segString); result.Append('\r'); } } } } return(result.ToString()); }
/// <summary> Creates new SegmentPointer </summary> /// <param name="parser">the PipeParser used to parse segments /// </param> /// <param name="parent">the Group object that would be the parent of any instances of the /// Segment underlying this SegmentPointer /// </param> /// <param name="position">the position (among siblings; from 0) of the underlying Segment /// </param> /// <param name="encodingChars">array of encoding characters, starting w/ field delim /// </param> public NuGenSegmentPointer(NuGenPipeParser parser, Group parent, int position, NuGenEncodingCharacters encodingChars) { this.parser = parser; this.parent = parent; this.encodingChars = encodingChars; this.name = parent.Names[position]; this.repeating = parent.isRepeating(this.name); }
/// <summary> Creates new GroupPointer </summary> public NuGenGroupPointer(NuGenPipeParser parser, Group parent, int position, NuGenEncodingCharacters encodingChars) { this.parser = parser; this.parent = parent; this.name = parent.Names[position]; this.repeating = parent.isRepeating(this.name); this.encodingChars = encodingChars; //this.createNewInstance(); }
/// <returns>s the message structure from MSH-9-3 /// </returns> private MessageStructure getStructure(System.String message) { NuGenEncodingCharacters ec = getEncodingChars(message); System.String messageStructure = null; bool explicityDefined = true; System.String wholeFieldNine; try { System.String[] fields = split(message.Substring(0, (System.Math.Max(message.IndexOf(segDelim), message.Length)) - (0)), System.Convert.ToString(ec.FieldSeparator)); wholeFieldNine = fields[8]; //message structure is component 3 but we'll accept a composite of 1 and 2 if there is no component 3 ... // if component 1 is ACK, then the structure is ACK regardless of component 2 System.String[] comps = split(wholeFieldNine, System.Convert.ToString(ec.ComponentSeparator)); if (comps.Length >= 3) { messageStructure = comps[2]; } else if (comps.Length > 0 && comps[0] != null && comps[0].Equals("ACK")) { messageStructure = "ACK"; } else if (comps.Length == 2) { explicityDefined = false; messageStructure = comps[0] + "_" + comps[1]; } /*else if (comps.length == 1 && comps[0] != null && comps[0].equals("ACK")) { * messageStructure = "ACK"; //it's common for people to only populate component 1 in an ACK msg * }*/ else { System.Text.StringBuilder buf = new System.Text.StringBuilder("Can't determine message structure from MSH-9: "); buf.Append(wholeFieldNine); if (comps.Length < 3) { buf.Append(" HINT: there are only "); buf.Append(comps.Length); buf.Append(" of 3 components present"); } throw new NuGenHL7Exception(buf.ToString(), NuGenHL7Exception.UNSUPPORTED_MESSAGE_TYPE); } } catch (IndexOutOfRangeException e) { throw new NuGenHL7Exception("Can't find message structure (MSH-9-3): " + e.Message, NuGenHL7Exception.UNSUPPORTED_MESSAGE_TYPE); } return(new MessageStructure(messageStructure, explicityDefined)); }
private static System.String encodePrimitive(Primitive p, NuGenEncodingCharacters encodingChars) { System.String val = ((Primitive)p).Value; if (val == null) { val = ""; } else { val = NuGenEscape.escape(val, encodingChars); } return(val); }
/// <summary>Returns the component or subcomponent separator from the given encoding characters. </summary> private static char getSeparator(bool subComponents, NuGenEncodingCharacters encodingChars) { char separator; if (subComponents) { separator = encodingChars.SubcomponentSeparator; } else { separator = encodingChars.ComponentSeparator; } return(separator); }
/// <summary>copies contents of "other" </summary> public NuGenEncodingCharacters(NuGenEncodingCharacters other) { this.fieldSep = other.FieldSeparator; this.encChars = new char[4]; this.encChars[0] = other.ComponentSeparator; this.encChars[1] = other.RepetitionSeparator; this.encChars[2] = other.EscapeCharacter; this.encChars[3] = other.SubcomponentSeparator; }
public static void Main(System.String[] args) { System.String testString = "foo$r$this is $ $p$test$r$r$ string"; //System.out.println(testString); //System.out.println(replace(testString, "$r$", "***")); //System.out.println(replace(testString, "$", "+")); //test speed gain with cache int n = 100000; System.Collections.Hashtable seqs; NuGenEncodingCharacters ec = new NuGenEncodingCharacters('|', "^~\\&"); //warm up the JIT for (int i = 0; i < n; i++) { seqs = makeEscapeSequences(ec); } for (int i = 0; i < n; i++) { seqs = getEscapeSequences(ec); } //time long start = (System.DateTime.Now.Ticks - 621355968000000000) / 10000; for (int i = 0; i < n; i++) { seqs = makeEscapeSequences(ec); } System.Console.Out.WriteLine("Time to make " + n + " times: " + ((System.DateTime.Now.Ticks - 621355968000000000) / 10000 - start)); start = (System.DateTime.Now.Ticks - 621355968000000000) / 10000; for (int i = 0; i < n; i++) { seqs = getEscapeSequences(ec); } System.Console.Out.WriteLine("Time to get " + n + " times: " + ((System.DateTime.Now.Ticks - 621355968000000000) / 10000 - start)); start = (System.DateTime.Now.Ticks - 621355968000000000) / 10000; for (int i = 0; i < n; i++) { seqs = makeEscapeSequences(ec); } System.Console.Out.WriteLine("Time to make " + n + " times: " + ((System.DateTime.Now.Ticks - 621355968000000000) / 10000 - start)); //test escape: testString = "this | is ^ a field \\T\\ with & some ~ bad stuff \\T\\"; System.Console.Out.WriteLine("Original: " + testString); System.String escaped = escape(testString, ec); System.Console.Out.WriteLine("Escaped: " + escaped); System.Console.Out.WriteLine("Unescaped: " + unescape(escaped, ec)); }
/// <summary> Constructs escape sequences using the given escape character - this should only /// be called by getEscapeCharacter(), which will cache the results for subsequent use. /// </summary> private static System.Collections.Hashtable makeEscapeSequences(NuGenEncodingCharacters ec) { System.Collections.Hashtable seqs = new System.Collections.Hashtable(); char[] codes = new char[] { 'F', 'S', 'T', 'R', 'E' }; char[] values = new char[] { ec.FieldSeparator, ec.ComponentSeparator, ec.SubcomponentSeparator, ec.RepetitionSeparator, ec.EscapeCharacter }; for (int i = 0; i < codes.Length; i++) { System.Text.StringBuilder seq = new System.Text.StringBuilder(); seq.Append(ec.EscapeCharacter); seq.Append(codes[i]); seq.Append(ec.EscapeCharacter); seqs[seq.ToString()] = System.Convert.ToString(values[i]); } seqs["\\X000d\\"] = System.Convert.ToString('\r'); return(seqs); }
/// <summary> Fills a field with values from an unparsed string representing the field. </summary> /// <param name="destinationField">the field Type /// </param> /// <param name="data">the field string (including all components and subcomponents; not including field delimiters) /// </param> /// <param name="encodingCharacters">the encoding characters used in the message /// </param> private static void parse(Type destinationField, System.String data, NuGenEncodingCharacters encodingCharacters) { System.String[] components = split(data, System.Convert.ToString(encodingCharacters.ComponentSeparator)); for (int i = 0; i < components.Length; i++) { System.String[] subcomponents = split(components[i], System.Convert.ToString(encodingCharacters.SubcomponentSeparator)); for (int j = 0; j < subcomponents.Length; j++) { System.String val = subcomponents[j]; if (val != null) { val = NuGenEscape.unescape(val, encodingCharacters); } Terser.getPrimitive(destinationField, i + 1, j + 1).Value = val; } } }
public static System.String encode(Segment source, NuGenEncodingCharacters encodingChars) { System.Text.StringBuilder result = new System.Text.StringBuilder(); result.Append(source.getName()); result.Append(encodingChars.FieldSeparator); //start at field 2 for MSH segment because field 1 is the field delimiter int startAt = 1; if (isDelimDefSegment(source.getName())) { startAt = 2; } //loop through fields; for every field delimit any repetitions and add field delimiter after ... int numFields = source.numFields(); for (int i = startAt; i <= numFields; i++) { try { Type[] reps = source.getField(i); for (int j = 0; j < reps.Length; j++) { System.String fieldText = encode(reps[j], encodingChars); //if this is MSH-2, then it shouldn't be escaped, so unescape it again if (isDelimDefSegment(source.getName()) && i == 2) { fieldText = NuGenEscape.unescape(fieldText, encodingChars); } result.Append(fieldText); if (j < reps.Length - 1) { result.Append(encodingChars.RepetitionSeparator); } } } catch (NuGenHL7Exception) { } result.Append(encodingChars.FieldSeparator); } //strip trailing delimiters ... return(stripExtraDelimiters(result.ToString(), encodingChars.FieldSeparator)); }
/// <summary> Encodes the given Type, using the given encoding characters. /// It is assumed that the Type represents a complete field rather than a component. /// </summary> public static System.String encode(Type source, NuGenEncodingCharacters encodingChars) { System.Text.StringBuilder field = new System.Text.StringBuilder(); for (int i = 1; i <= Terser.numComponents(source); i++) { System.Text.StringBuilder comp = new System.Text.StringBuilder(); for (int j = 1; j <= Terser.numSubComponents(source, i); j++) { Primitive p = Terser.getPrimitive(source, i, j); comp.Append(encodePrimitive(p, encodingChars)); comp.Append(encodingChars.SubcomponentSeparator); } field.Append(stripExtraDelimiters(comp.ToString(), encodingChars.SubcomponentSeparator)); field.Append(encodingChars.ComponentSeparator); } return(stripExtraDelimiters(field.ToString(), encodingChars.ComponentSeparator)); //return encode(source, encodingChars, false); }
/// <seealso cref="java.lang.Object.equals"> /// </seealso> public override bool Equals(System.Object o) { if (o is NuGenEncodingCharacters) { NuGenEncodingCharacters other = (NuGenEncodingCharacters)o; if (this.FieldSeparator == other.FieldSeparator && this.ComponentSeparator == other.ComponentSeparator && this.EscapeCharacter == other.EscapeCharacter && this.RepetitionSeparator == other.RepetitionSeparator && this.SubcomponentSeparator == other.SubcomponentSeparator) { return(true); } else { return(false); } } else { return(false); } }
/// <summary> Returns a HashTable with escape sequences as keys, and corresponding /// Strings as values. /// </summary> private static System.Collections.Hashtable getEscapeSequences(NuGenEncodingCharacters encChars) { //escape sequence strings must be assembled using the given escape character //see if this has already been done for this set of encoding characters System.Collections.Hashtable escapeSequences = null; System.Object o = variousEncChars[encChars]; if (o == null) { //this means we haven't got the sequences for these encoding characters yet - let's make them escapeSequences = makeEscapeSequences(encChars); variousEncChars[encChars] = escapeSequences; } else { //we already have escape sequences for these encoding characters escapeSequences = (System.Collections.Hashtable)o; } return(escapeSequences); }
/// <seealso cref="Genetibase.NuGenHL7.parser.Parser.doParse(java.lang.String, java.lang.String)"> /// </seealso> protected internal override Message doParse(System.String message, System.String version) { Message result = null; char fieldSep = message[3]; NuGenEncodingCharacters ec = new NuGenEncodingCharacters(fieldSep, message.Substring(4, (8) - (4))); SupportClass.Tokenizer tok = new SupportClass.Tokenizer(message.Substring(4), System.Convert.ToString(new char[] { fieldSep, ourSegmentSeparator }), true); System.String[] mshFields = getMSHFields(tok, fieldSep); System.Object[] structure = getStructure(mshFields[8], ec.ComponentSeparator); StructRef root = (StructRef)myEventGuideMap[structure[0]]; if (root == null) { result = myPipeParser.parse(message); } else { int csIndex = mshFields[11].IndexOf((System.Char)ec.ComponentSeparator); result = instantiateMessage((System.String)structure[1], version, ((System.Boolean)structure[2])); StructRef mshRef = null; lock (root) { mshRef = root.getSuccessor("MSH"); root.reset(); } Segment msh = (Segment)result.get_Renamed("MSH"); for (int i = 0; i < mshRef.Fields.Length; i++) { int fieldNum = mshRef.Fields[i]; parse(mshFields[fieldNum - 1], msh, fieldNum, ec); } parse(tok, result, root, ec); } return(result); }
private void parse(System.String field, Segment segment, int num, NuGenEncodingCharacters ec) { if (field != null) { int rep = 0; int component = 1; int subcomponent = 1; Type type = segment.getField(num, rep); System.String delim = System.Convert.ToString(new char[] { ec.RepetitionSeparator, ec.ComponentSeparator, ec.SubcomponentSeparator }); for (SupportClass.Tokenizer tok = new SupportClass.Tokenizer(field, delim, true); tok.HasMoreTokens();) { System.String token = tok.NextToken(); char c = token[0]; if (c == ec.RepetitionSeparator) { rep++; component = 1; subcomponent = 1; type = segment.getField(num, rep); } else if (c == ec.ComponentSeparator) { component++; subcomponent = 1; } else if (c == ec.SubcomponentSeparator) { subcomponent++; } else { Primitive p = Terser.getPrimitive(type, component, subcomponent); p.Value = token; } } } }
public static System.String encode(Segment source, NuGenEncodingCharacters encodingChars) { System.Text.StringBuilder result = new System.Text.StringBuilder(); result.Append(source.getName()); result.Append(encodingChars.FieldSeparator); //start at field 2 for MSH segment because field 1 is the field delimiter int startAt = 1; if (isDelimDefSegment(source.getName())) startAt = 2; //loop through fields; for every field delimit any repetitions and add field delimiter after ... int numFields = source.numFields(); for (int i = startAt; i <= numFields; i++) { try { Type[] reps = source.getField(i); for (int j = 0; j < reps.Length; j++) { System.String fieldText = encode(reps[j], encodingChars); //if this is MSH-2, then it shouldn't be escaped, so unescape it again if (isDelimDefSegment(source.getName()) && i == 2) fieldText = NuGenEscape.unescape(fieldText, encodingChars); result.Append(fieldText); if (j < reps.Length - 1) result.Append(encodingChars.RepetitionSeparator); } } catch (NuGenHL7Exception) { } result.Append(encodingChars.FieldSeparator); } //strip trailing delimiters ... return stripExtraDelimiters(result.ToString(), encodingChars.FieldSeparator); }
/// <summary> Returns given group serialized as a pipe-encoded string - this method is called /// by encode(Message source, String encoding). /// </summary> public static System.String encode(Group source, NuGenEncodingCharacters encodingChars) { System.Text.StringBuilder result = new System.Text.StringBuilder(); System.String[] names = source.Names; for (int i = 0; i < names.Length; i++) { Structure[] reps = source.getAll(names[i]); for (int rep = 0; rep < reps.Length; rep++) { if (reps[rep] is Group) { result.Append(encode((Group) reps[rep], encodingChars)); } else { System.String segString = encode((Segment) reps[rep], encodingChars); if (segString.Length >= 4) { result.Append(segString); result.Append('\r'); } } } } return result.ToString(); }
private NuGenEncodingCharacters enc; //used to check for content in parts of a message /// <summary>Creates a new instance of DefaultValidator </summary> public NuGenDefaultValidator() { enc = new NuGenEncodingCharacters('|', null); //the | is assumed later -- don't change }
/// <summary> Returns a HashTable with escape sequences as keys, and corresponding /// Strings as values. /// </summary> private static System.Collections.Hashtable getEscapeSequences(NuGenEncodingCharacters encChars) { //escape sequence strings must be assembled using the given escape character //see if this has already been done for this set of encoding characters System.Collections.Hashtable escapeSequences = null; System.Object o = variousEncChars[encChars]; if (o == null) { //this means we haven't got the sequences for these encoding characters yet - let's make them escapeSequences = makeEscapeSequences(encChars); variousEncChars[encChars] = escapeSequences; } else { //we already have escape sequences for these encoding characters escapeSequences = (System.Collections.Hashtable) o; } return escapeSequences; }
/// <summary>Returns the component or subcomponent separator from the given encoding characters. </summary> private static char getSeparator(bool subComponents, NuGenEncodingCharacters encodingChars) { char separator; if (subComponents) { separator = encodingChars.SubcomponentSeparator; } else { separator = encodingChars.ComponentSeparator; } return separator; }
/// <summary> Parses a segment string and populates the given Segment object. Unexpected fields are /// added as Varies' at the end of the segment. /// /// </summary> /// <throws> HL7Exception if the given string does not contain the </throws> /// <summary> given segment or if the string is not encoded properly /// </summary> public virtual void parse(Segment destination, System.String segment, NuGenEncodingCharacters encodingChars) { int fieldOffset = 0; if (isDelimDefSegment(destination.getName())) { fieldOffset = 1; //set field 1 to fourth character of string Terser.set_Renamed(destination, 1, 0, 1, 1, System.Convert.ToString(encodingChars.FieldSeparator)); } System.String[] fields = split(segment, System.Convert.ToString(encodingChars.FieldSeparator)); //destination.setName(fields[0]); for (int i = 1; i < fields.Length; i++) { System.String[] reps = split(fields[i], System.Convert.ToString(encodingChars.RepetitionSeparator)); //MSH-2 will get split incorrectly so we have to fudge it ... bool isMSH2 = isDelimDefSegment(destination.getName()) && i + fieldOffset == 2; if (isMSH2) { reps = new System.String[1]; reps[0] = fields[i]; } for (int j = 0; j < reps.Length; j++) { try { System.Text.StringBuilder statusMessage = new System.Text.StringBuilder("Parsing field "); statusMessage.Append(i + fieldOffset); statusMessage.Append(" repetition "); statusMessage.Append(j); //parse(destination.getField(i + fieldOffset, j), reps[j], encodingChars, false); Type field = destination.getField(i + fieldOffset, j); if (isMSH2) { Terser.getPrimitive(field, 1, 1).Value = reps[j]; } else { parse(field, reps[j], encodingChars); } } catch (NuGenHL7Exception e) { //set the field location and throw again ... e.FieldPosition = i; e.SegmentRepetition = MessageIterator.getIndex(destination.Parent, destination).rep; e.SegmentName = destination.getName(); throw e; } } } //set data type of OBX-5 if (destination.GetType().FullName.IndexOf("OBX") >= 0) { Varies.fixOBX5(destination, Factory); } }
/// <seealso cref="Genetibase.NuGenHL7.parser.Parser.doParse(java.lang.String, java.lang.String)"> /// </seealso> protected internal override Message doParse(System.String message, System.String version) { Message result = null; char fieldSep = message[3]; NuGenEncodingCharacters ec = new NuGenEncodingCharacters(fieldSep, message.Substring(4, (8) - (4))); SupportClass.Tokenizer tok = new SupportClass.Tokenizer(message.Substring(4), System.Convert.ToString(new char[]{fieldSep, ourSegmentSeparator}), true); System.String[] mshFields = getMSHFields(tok, fieldSep); System.Object[] structure = getStructure(mshFields[8], ec.ComponentSeparator); StructRef root = (StructRef) myEventGuideMap[structure[0]]; if (root == null) { result = myPipeParser.parse(message); } else { int csIndex = mshFields[11].IndexOf((System.Char) ec.ComponentSeparator); result = instantiateMessage((System.String) structure[1], version, ((System.Boolean) structure[2])); StructRef mshRef = null; lock (root) { mshRef = root.getSuccessor("MSH"); root.reset(); } Segment msh = (Segment) result.get_Renamed("MSH"); for (int i = 0; i < mshRef.Fields.Length; i++) { int fieldNum = mshRef.Fields[i]; parse(mshFields[fieldNum - 1], msh, fieldNum, ec); } parse(tok, result, root, ec); } return result; }
private void parse(SupportClass.Tokenizer tok, Message message, StructRef root, NuGenEncodingCharacters ec) { Terser t = new Terser(message); lock (root) { StructRef ref_Renamed = root.getSuccessor("MSH"); int field = 0; Segment segment = null; int[] fields = new int[0]; while (tok.HasMoreTokens()) { System.String token = tok.NextToken(); if (token[0] == ec.FieldSeparator) { field++; } else if (token[0] == ourSegmentSeparator) { field = 0; } else if (field == 0) { StructRef newref = drill(ref_Renamed, token); if (newref == null) { segment = null; fields = new int[0]; } else { ref_Renamed = newref; segment = t.getSegment(ref_Renamed.FullPath); fields = ref_Renamed.Fields; } } else if (segment != null && System.Array.BinarySearch(fields, (System.Object) field) >= 0) { parse(token, segment, field, ec); } } root.reset(); } }
private void parse(System.String field, Segment segment, int num, NuGenEncodingCharacters ec) { if (field != null) { int rep = 0; int component = 1; int subcomponent = 1; Type type = segment.getField(num, rep); System.String delim = System.Convert.ToString(new char[]{ec.RepetitionSeparator, ec.ComponentSeparator, ec.SubcomponentSeparator}); for (SupportClass.Tokenizer tok = new SupportClass.Tokenizer(field, delim, true); tok.HasMoreTokens(); ) { System.String token = tok.NextToken(); char c = token[0]; if (c == ec.RepetitionSeparator) { rep++; component = 1; subcomponent = 1; type = segment.getField(num, rep); } else if (c == ec.ComponentSeparator) { component++; subcomponent = 1; } else if (c == ec.SubcomponentSeparator) { subcomponent++; } else { Primitive p = Terser.getPrimitive(type, component, subcomponent); p.Value = token; } } } }
private void parse(SupportClass.Tokenizer tok, Message message, StructRef root, NuGenEncodingCharacters ec) { Terser t = new Terser(message); lock (root) { StructRef ref_Renamed = root.getSuccessor("MSH"); int field = 0; Segment segment = null; int[] fields = new int[0]; while (tok.HasMoreTokens()) { System.String token = tok.NextToken(); if (token[0] == ec.FieldSeparator) { field++; } else if (token[0] == ourSegmentSeparator) { field = 0; } else if (field == 0) { StructRef newref = drill(ref_Renamed, token); if (newref == null) { segment = null; fields = new int[0]; } else { ref_Renamed = newref; segment = t.getSegment(ref_Renamed.FullPath); fields = ref_Renamed.Fields; } } else if (segment != null && System.Array.BinarySearch(fields, (System.Object)field) >= 0) { parse(token, segment, field, ec); } } root.reset(); } }
/// <summary> Encodes the given Type, using the given encoding characters. /// It is assumed that the Type represents a complete field rather than a component. /// </summary> public static System.String encode(Type source, NuGenEncodingCharacters encodingChars) { System.Text.StringBuilder field = new System.Text.StringBuilder(); for (int i = 1; i <= Terser.numComponents(source); i++) { System.Text.StringBuilder comp = new System.Text.StringBuilder(); for (int j = 1; j <= Terser.numSubComponents(source, i); j++) { Primitive p = Terser.getPrimitive(source, i, j); comp.Append(encodePrimitive(p, encodingChars)); comp.Append(encodingChars.SubcomponentSeparator); } field.Append(stripExtraDelimiters(comp.ToString(), encodingChars.SubcomponentSeparator)); field.Append(encodingChars.ComponentSeparator); } return stripExtraDelimiters(field.ToString(), encodingChars.ComponentSeparator); //return encode(source, encodingChars, false); }
private static System.String encodePrimitive(Primitive p, NuGenEncodingCharacters encodingChars) { System.String val = ((Primitive) p).Value; if (val == null) { val = ""; } else { val = NuGenEscape.escape(val, encodingChars); } return val; }
/// <summary> Constructs escape sequences using the given escape character - this should only /// be called by getEscapeCharacter(), which will cache the results for subsequent use. /// </summary> private static System.Collections.Hashtable makeEscapeSequences(NuGenEncodingCharacters ec) { System.Collections.Hashtable seqs = new System.Collections.Hashtable(); char[] codes = new char[]{'F', 'S', 'T', 'R', 'E'}; char[] values = new char[]{ec.FieldSeparator, ec.ComponentSeparator, ec.SubcomponentSeparator, ec.RepetitionSeparator, ec.EscapeCharacter}; for (int i = 0; i < codes.Length; i++) { System.Text.StringBuilder seq = new System.Text.StringBuilder(); seq.Append(ec.EscapeCharacter); seq.Append(codes[i]); seq.Append(ec.EscapeCharacter); seqs[seq.ToString()] = System.Convert.ToString(values[i]); } seqs["\\X000d\\"] = System.Convert.ToString('\r'); return seqs; }
/// <summary> Formats a Message object into an HL7 message string using this parser's /// default encoding ("VB"). /// </summary> /// <throws> HL7Exception if the data fields in the message do not permit encoding </throws> /// <summary> (e.g. required fields are null) /// </summary> protected internal override System.String doEncode(Message source) { //get encoding characters ... Segment msh = (Segment) source.get_Renamed("MSH"); System.String fieldSepString = Terser.get_Renamed(msh, 1, 0, 1, 1); if (fieldSepString == null) throw new NuGenHL7Exception("Can't encode message: MSH-1 (field separator) is missing"); char fieldSep = '|'; if (fieldSepString != null && fieldSepString.Length > 0) fieldSep = fieldSepString[0]; System.String encCharString = Terser.get_Renamed(msh, 2, 0, 1, 1); if (encCharString == null) throw new NuGenHL7Exception("Can't encode message: MSH-2 (encoding characters) is missing"); if (encCharString.Length != 4) throw new NuGenHL7Exception("Encoding characters '" + encCharString + "' invalid -- must be 4 characters", NuGenHL7Exception.DATA_TYPE_ERROR); NuGenEncodingCharacters en = new NuGenEncodingCharacters(fieldSep, encCharString); //pass down to group encoding method which will operate recursively on children ... return encode((Group) source, en); }