Exemplo n.º 1
0
        /// <summary>
        /// Retorna la representación XML en una cadena de caracteres de un
        /// mensaje.
        /// </summary>
        /// <param name="renderingMap">
        /// Es el mapa de todos los objetos que representan objetos. Vea log4net.
        /// </param>
        /// <param name="component">
        /// Es el mensaje a ser representado en XML.
        /// </param>
        /// <param name="indent">
        /// Es la indentación a emplear en la representación XML.
        /// </param>
        /// <returns>
        /// Retorna una cadena de caracteres con la representación en XML
        /// del mensaje.
        /// </returns>
        public override string DoRender(RendererMap renderingMap,
                                        MessagingComponent component, string indent)
        {
            if (!(component is Message))
            {
                throw new ArgumentException(SR.ComponentIsNotAMessage, "component");
            }

            Message message = ( Message)component;

            message.CorrectBitMapsValues();

            StringBuilder render = new StringBuilder();
            Field         field;

            render.Append(indent);
            render.Append("<");
            render.Append(MessagingComponentXmlRendering.XmlMessageTag);
            render.Append(">");
            render.Append(Environment.NewLine);

            string newIndent = indent + "   ";

            if (message.Header != null)
            {
                render.Append(message.Header.XmlRendering(renderingMap).DoRender(
                                  renderingMap, message.Header, indent));
            }


            int j = message.Fields.MaximumFieldNumber;

            for (int i = 0; i <= j; i++)
            {
                if ((field = message.Fields[i]) != null)
                {
                    render.Append(field.XmlRendering(renderingMap).DoRender(
                                      renderingMap, field, newIndent));
                }
            }

            render.Append(indent +
                          "</" + MessagingComponentXmlRendering.XmlMessageTag + ">\r\n");

            return(render.ToString());
        }
        /// <summary>
        /// It formats a message.
        /// </summary>
        /// <param name="message">
        /// It's the message to be formatted.
        /// </param>
        /// <param name="formatterContext">
        /// It's the formatter context to be used in the format.
        /// </param>
        public virtual void Format(Message message,
                                   ref FormatterContext formatterContext)
        {
            formatterContext.CurrentMessage = message;

            // Write header.
            if (_packetHeader != null)
            {
                formatterContext.Write(_packetHeader);
            }

            // Format header if we have one header formatter.
            if (_headerFormatter != null)
            {
                try {
                    _headerFormatter.Format(message.Header, ref formatterContext);
                } catch (Exception e) {
                    throw new MessagingException(SR.CantFormatMessageHeader, e);
                }
            }

            // Allow subclasses to put information between header data and
            // fields data.
            BeforeFieldsFormatting(message, ref formatterContext);

            // If bitmaps are present in the message formatter, check if we
            // must add them to the message.
            bool atLeastOne  = false;
            int  firstBitmap = -1;

            for (int i = _bitmaps.Length - 1; (i >= 0) && (_bitmaps[i] >= 0); i--)
            {
                firstBitmap = i;
                if (message.Fields.Contains(_bitmaps[i]))
                {
                    // Check if present field is a bitmap.
                    if (!(message[_bitmaps[i]] is BitMapField))
                    {
                        throw new MessagingException(SR.FieldMustBeBitmap(_bitmaps[i]));
                    }
                    atLeastOne = true;
                }
                else
                {
                    // Get bitmap from field formatters collection.
                    BitMapFieldFormatter bitmap =
                        ( BitMapFieldFormatter)_fieldsFormatters[_bitmaps[i]];

                    // Add bitmap to message if required.
                    if (message.Fields.ContainsAtLeastOne(bitmap.LowerFieldNumber,
                                                          bitmap.UpperFieldNumber))
                    {
                        message.Fields.Add(new BitMapField(bitmap.FieldNumber,
                                                           bitmap.LowerFieldNumber, bitmap.UpperFieldNumber));
                        atLeastOne = true;
                    }
                }
            }
            if (!atLeastOne && firstBitmap > -1)
            {
                // In a bitmaped message, at least the first bitmap must be present.
                BitMapFieldFormatter bitmap = ( BitMapFieldFormatter )_fieldsFormatters[_bitmaps[firstBitmap]];

                message.Fields.Add(new BitMapField(bitmap.FieldNumber,
                                                   bitmap.LowerFieldNumber, bitmap.UpperFieldNumber));
            }

            // Correct bitmap values.
            message.CorrectBitMapsValues();

            // Format fields.
            for (int i = 0; i <= message.Fields.MaximumFieldNumber; i++)
            {
                if (message.Fields.Contains(i))
                {
                    // If we haven't the field formatter, throw an exception.
                    if (!_fieldsFormatters.Contains(i))
                    {
                        throw new MessagingException(SR.UnknownFieldFormatter(i));
                    }

                    // Set parent message.
                    if (message.Fields[i] is InnerMessageField)
                    {
                        InnerMessageField innerMessageField = message.Fields[i] as InnerMessageField;
                        if (innerMessageField != null)
                        {
                            Message innerMessage = innerMessageField.Value as Message;
                            if (innerMessage != null)
                            {
                                innerMessage.Parent = message;
                            }
                        }
                    }

                    try {
                        _fieldsFormatters[i].Format(message.Fields[i],
                                                    ref formatterContext);
                    } catch (Exception e) {
                        throw new MessagingException(SR.CantFormatField(i), e);
                    }
                }
            }
        }