/// <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(IType destinationField, String data, EncodingCharacters encodingCharacters) { String[] components = Split(data, Convert.ToString(encodingCharacters.ComponentSeparator)); for (int i = 0; i < components.Length; i++) { int index = i + 1; if (components[i] != null && (Terser.isPrimitive(destinationField, index) && components[i].Contains(Convert.ToString(encodingCharacters.SubcomponentSeparator)))) { // sometimes someone sends us bad data and if the destinition is a primitive we should escape what needs to be escaped components[i] = Escape.escape(components[i], encodingCharacters); } String[] subcomponents = Split(components[i], Convert.ToString(encodingCharacters.SubcomponentSeparator)); for (int j = 0; j < subcomponents.Length; j++) { String val = subcomponents[j]; if (val != null) { val = Escape.unescape(val, encodingCharacters); } Terser.getPrimitive(destinationField, index, j + 1).Value = val; } } }
public static String Encode(ISegment source, EncodingCharacters encodingChars) { StringBuilder result = new StringBuilder(); result.Append(source.GetStructureName()); result.Append(encodingChars.FieldSeparator); //start at field 2 for MSH segment because field 1 is the field delimiter int startAt = 1; if (IsDelimDefSegment(source.GetStructureName())) { 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 { IType[] reps = source.GetField(i); for (int j = 0; j < reps.Length; j++) { String fieldText = Encode(reps[j], encodingChars); //if this is MSH-2, then it shouldn't be escaped, so unescape it again if (IsDelimDefSegment(source.GetStructureName()) && i == 2) { fieldText = Escape.unescape(fieldText, encodingChars); } result.Append(fieldText); if (j < reps.Length - 1) { result.Append(encodingChars.RepetitionSeparator); } } } catch (HL7Exception e) { log.Error("Error while encoding segment: ", e); } result.Append(encodingChars.FieldSeparator); } //strip trailing delimiters ... return(StripExtraDelimiters(result.ToString(), encodingChars.FieldSeparator)); }
/// <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(IType destinationField, System.String data, EncodingCharacters 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 = Escape.unescape(val, encodingCharacters); } Terser.getPrimitive(destinationField, i + 1, j + 1).Value = val; } } }