private static System.String encodePrimitive(Primitive p, EncodingCharacters encodingChars) { System.String val = ((Primitive)p).Value; if (val == null) { val = ""; } else { val = Escape.escape(val, encodingChars); } return(val); }
public static System.String encode(Segment source, EncodingCharacters encodingChars) { System.Text.StringBuilder result = new System.Text.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 { 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.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(Type 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; } } }