Пример #1
0
        private static ToplistBlock ConvertXElementToBlock(XElement xmlBlock)
        {
            xmlBlock.ThrowIfNull(nameof(xmlBlock));

            string title  = XDocumentParser.GetAttributeValue(xmlBlock, nameof(ToplistBlock.Title));
            int    number = XDocumentParser.GetAttributeValue <int>(xmlBlock,
                                                                    nameof(ToplistBlock.Number));

            XElement?itemsXml = XDocumentParser.FindSubelement(xmlBlock,
                                                               nameof(ToplistBlock.Items));

            if (itemsXml is null)
            {
                throw new ArgumentException(
                          "Invalid strcuture of XML document: cannot find toplist items block.",
                          nameof(xmlBlock)
                          );
            }

            var result = new ToplistBlock(title, number);

            result.UpdateItems(
                XDocumentParser.FindSubelements(itemsXml)
                .Select(xmlItem => ConvertXElementToItem(xmlItem, result))
                .ToList()
                );

            return(result);
        }
        /// <summary>
        /// Creates message (sequential) handler instance depend on parameter value (could be get
        /// from config).
        /// </summary>
        /// <param name="messageHandlerElement">Element from XML config.</param>
        /// <returns>Fully initialized instance of message handler class.</returns>
        /// <exception cref="ArgumentOutOfRangeException">
        /// <paramref name="messageHandlerElement" /> isn't specified in XML config.
        /// </exception>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="messageHandlerElement" /> or its attribute with handler name is
        /// <c>null</c>.
        /// </exception>
        public Communication.IMessageHandler CreateMessageHandler(XElement messageHandlerElement)
        {
            messageHandlerElement.ThrowIfNull(nameof(messageHandlerElement));
            var handlerElement = messageHandlerElement.Attribute(_messageHandlerTypeParameterName);

            handlerElement.ThrowIfNull(nameof(handlerElement));

            switch (handlerElement.Value)
            {
            case _consoleMessageHandlerParameterName:
            {
                var messageHandlerParametersElement = XDocumentParser.FindSubelement(
                    messageHandlerElement, _setUnicodeParameterName
                    );
                var setUnicode = XDocumentParser.GetElementValue <bool>(
                    messageHandlerParametersElement
                    );

                return(new Communication.ConsoleMessageHandler(setUnicode));
            }

            default:
            {
                var ex = new ArgumentOutOfRangeException(
                    nameof(messageHandlerElement), messageHandlerElement,
                    "Couldn't recognize message handler type specified in XML config."
                    );
                _logger.Error(ex, "Passed incorrect data to method: " +
                              $"{handlerElement.Value}");
                throw ex;
            }
            }
        }
        /// <summary>
        /// Creates message (sequential) handler instance depend on parameter value (could be get
        /// from config).
        /// </summary>
        /// <param name="messageHandlerElement">Element from XML config.</param>
        /// <returns>Fully initialized instance of message handler class.</returns>
        /// <exception cref="ArgumentOutOfRangeException">
        /// <paramref name="messageHandlerElement" /> isn't specified in XML config.
        /// </exception>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="messageHandlerElement" /> or its attribute with handler name is
        /// <c>null</c>.
        /// </exception>
        public Communication.IMessageHandler CreateMessageHandler(XElement messageHandlerElement)
        {
            messageHandlerElement.ThrowIfNull(nameof(messageHandlerElement));

            _logger.Info("Creating message handler.");

            var handlerElement = messageHandlerElement.Attribute(_messageHandlerTypeParameterName);

            handlerElement.ThrowIfNull(nameof(handlerElement));

            switch (handlerElement.Value)
            {
            case _consoleMessageHandlerParameterName:
            {
                XElement?messageHandlerParametersElement = XDocumentParser.FindSubelement(
                    messageHandlerElement, _setUnicodeParameterName
                    );

                if (messageHandlerParametersElement is null)
                {
                    throw new ArgumentException(
                              "Invalid structure of XML document: cannot find message handler " +
                              "parameters block.",
                              nameof(messageHandlerElement)
                              );
                }

                var setUnicode = XDocumentParser.GetElementValue <bool>(
                    messageHandlerParametersElement
                    );

                return(new Communication.ConsoleMessageHandler(setUnicode));
            }

            default:
            {
                throw new ArgumentOutOfRangeException(
                          nameof(messageHandlerElement), messageHandlerElement,
                          "Couldn't recognize message handler type specified in XML config."
                          );
            }
            }
        }