예제 #1
0
        /// <summary>
        /// Returns given <see cref="IType"/> as a pipe-encoded string, using the given encoding characters.
        /// <para>
        /// It is assumed that the Type represents a complete field rather than a component.
        /// </para>
        /// </summary>
        /// <param name="source">An <see cref="IType"/> object from which to construct an encoded string.</param>
        /// <param name="encodingChars">Encoding characters to be used.</param>
        /// <returns>The encoded type.</returns>
        /// <exception cref="HL7Exception">Thrown if the data fields in the group 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(IType source, EncodingCharacters encodingChars)
        {
            if (source is Varies)
            {
                var varies = (Varies)source;
                if (varies.Data != null)
                {
                    source = varies.Data;
                }
            }

            var field = new StringBuilder();

            for (var i = 1; i <= Terser.NumComponents(source); i++)
            {
                var comp = new StringBuilder();
                for (var j = 1; j <= Terser.NumSubComponents(source, i); j++)
                {
                    var 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));
        }