/// <summary>
        /// Formatea un campo cuyo valor es una cadena de caracteres.
        /// </summary>
        /// <param name="header">
        /// Es el cabezal a formatear.
        /// </param>
        /// <param name="formatterContext">
        /// Es el contexto de formateo que debe ser empleado.
        /// </param>
        public virtual void Format(MessageHeader header,
                                   ref FormatterContext formatterContext)
        {
            string headerValue = null;

            if (header != null)
            {
                if (!(header is StringMessageHeader))
                {
                    throw new ArgumentException(SR.StringHeaderExpected, "header");
                }

                headerValue = (( StringMessageHeader)header).Value;
            }

            // Pad if padding available.
            if (_padding != null)
            {
                headerValue = _padding.Pad(headerValue,
                                           _lengthManager.MaximumLength);
            }

            if (headerValue == null)
            {
                _lengthManager.WriteLength(header, 0, 0, ref formatterContext);
                _lengthManager.WriteLengthTrailer(header, 0, 0, ref formatterContext);
            }
            else
            {
                _lengthManager.WriteLength(header, headerValue.Length,
                                           _encoder.GetEncodedLength(headerValue.Length),
                                           ref formatterContext);
                _encoder.Encode(headerValue, ref formatterContext);
                _lengthManager.WriteLengthTrailer(header, headerValue.Length,
                                                  _encoder.GetEncodedLength(headerValue.Length),
                                                  ref formatterContext);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Formatea un campo cuyo valor es una cadena de caracteres.
        /// </summary>
        /// <param name="field">
        /// Es el campo a formatear.
        /// </param>
        /// <param name="formatterContext">
        /// Es el contexto de formateo que debe ser empleado.
        /// </param>
        public override void Format(Field field,
                                    ref FormatterContext formatterContext)
        {
            if (!(field is StringField))
            {
                throw new ArgumentException(SR.StringMessageFieldExpected, "field");
            }

            string fieldValue = (( StringField)field).FieldValue;

            // Pad if padding available.
            if (_padding != null)
            {
                fieldValue = _padding.Pad(fieldValue, _lengthManager.MaximumLength);
            }

            if (_validator != null)
            {
                _validator.Validate(fieldValue);
            }

            if (fieldValue == null)
            {
                _lengthManager.WriteLength(field, 0, 0, ref formatterContext);
                _lengthManager.WriteLengthTrailer(field, 0, 0, ref formatterContext);
            }
            else
            {
                _lengthManager.WriteLength(field, fieldValue.Length,
                                           _encoder.GetEncodedLength(fieldValue.Length),
                                           ref formatterContext);
                _encoder.Encode(fieldValue, ref formatterContext);
                _lengthManager.WriteLengthTrailer(field, fieldValue.Length,
                                                  _encoder.GetEncodedLength(fieldValue.Length),
                                                  ref formatterContext);
            }
        }