예제 #1
0
        /// <summary>
        /// Formats a <see cref="IMessage"/> object into an HL7 message string using the given encoding.
        /// </summary>
        /// <param name="source">An <see cref="IMessage"/> object from which to construct an encoded message string.</param>
        /// <param name="encodingChars">Encoding characters to be used.</param>
        /// <returns>The encoded message.</returns>
        /// <exception cref="HL7Exception">Thrown if the data fields in the message do not permit encoding (e.g. required fields are null).</exception>
        /// <exception cref="EncodingNotSupportedException">Thrown if the requested encoding is not supported by this parser.</exception>
        public static string Encode(ISegment source, EncodingCharacters encodingChars)
        {
            var 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
            var startAt = 1;

            if (IsDelimDefSegment(source.GetStructureName()))
            {
                startAt = 2;
            }

            // loop through fields; for every field delimit any repetitions and add field delimiter after ...
            var numFields = source.NumFields();

            for (var i = startAt; i <= numFields; i++)
            {
                try
                {
                    var reps = source.GetField(i);
                    for (var j = 0; j < reps.Length; j++)
                    {
                        var fieldText = Encode(reps[j], encodingChars);

                        // if this is MSH-2, then it shouldn't be escaped, so un-escape it again
                        if (IsDelimDefSegment(source.GetStructureName()) && i == 2)
                        {
                            fieldText = Escape.UnescapeText(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));
        }
예제 #2
0
        /// <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)
        {
            var components = Split(data, Convert.ToString(encodingCharacters.ComponentSeparator));

            for (var i = 0; i < components.Length; i++)
            {
                var subcomponents = Split(components[i], Convert.ToString(encodingCharacters.SubcomponentSeparator));
                for (var j = 0; j < subcomponents.Length; j++)
                {
                    var val = subcomponents[j];
                    if (val != null)
                    {
                        val = Escape.UnescapeText(val, encodingCharacters);
                    }

                    Terser.GetPrimitive(destinationField, i + 1, j + 1).Value = val;
                }
            }
        }