XmlConversionUtil.cs :  » Build-Systems » CruiseControl.NET » ThoughtWorks » CruiseControl » Remote » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Build Systems » CruiseControl.NET 
CruiseControl.NET » ThoughtWorks » CruiseControl » Remote » XmlConversionUtil.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using System.Xml;
using ThoughtWorks.CruiseControl.Remote.Messages;
using System.Reflection;
using System.IO;

namespace ThoughtWorks.CruiseControl.Remote{
    /// <summary>
    /// Helper class for converting XML to objects.
    /// </summary>
    public static class XmlConversionUtil
    {
        #region Private fields
        private static Dictionary<string, Type> messageTypes = null;
        private static Dictionary<Type, XmlSerializer> messageSerialisers = new Dictionary<Type, XmlSerializer>();
        #endregion

        #region Public methods
        #region ProcessResponse()
        /// <summary>
        /// Converts a response string into a response object.
        /// </summary>
        /// <param name="response"></param>
        /// <returns></returns>
        public static Response ProcessResponse(string response)
        {
            // Find the type of message
            XmlDocument messageXml = new XmlDocument();
            messageXml.LoadXml(response);
            Type messageType = FindMessageType(messageXml.DocumentElement.Name);
            if (messageType == null)
            {
                throw new CommunicationsException(
                    string.Format(
                        "Unable to translate message: '{0}' is unknown",
                        messageXml.DocumentElement.Name));
            }

            // Convert the message and invoke the action
            object result = ConvertXmlToObject(messageType, response);
            return result as Response;
        }
        #endregion

        #region FindMessageType()
        /// <summary>
        /// Finds the type of object that a message is.
        /// </summary>
        /// <param name="messageName">The name of the message.</param>
        /// <returns>The message type, if found, null otherwise.</returns>
        public static Type FindMessageType(string messageName)
        {
            Type messageType = null;

            // If the message types have not been loaded, load them into a dictionary
            if (messageTypes == null)
            {
                messageTypes = new Dictionary<string, Type>();

                // Messages will only come from the communications library
                Assembly remotingLibrary = typeof(IServerConnection).Assembly;
                foreach (Type remotingType in remotingLibrary.GetExportedTypes())
                {
                    XmlRootAttribute[] attributes = remotingType.GetCustomAttributes(
                        typeof(XmlRootAttribute), false) as XmlRootAttribute[];
                    foreach (XmlRootAttribute attribute in attributes)
                    {
                        if (messageTypes.ContainsKey(attribute.ElementName))
                        {
                            throw new ApplicationException(
                                string.Format("Duplicate message type found: '{0}'.\r\nFirst type: {1}\r\nSecond type: {2}",
                                    attribute.ElementName,
                                    messageTypes[attribute.ElementName].FullName,
                                    remotingType.FullName));
                        }
                        else
                        {
                            messageTypes.Add(attribute.ElementName, remotingType);
                        }
                    }
                }
            }

            // Attempt to find the message within the message types
            if (messageTypes.ContainsKey(messageName))
            {
                messageType = messageTypes[messageName];
            }

            return messageType;
        }
        #endregion

        #region ConvertXmlToObject()
        /// <summary>
        /// Converts a message string into an object.
        /// </summary>
        /// <param name="messageType">The type of message.</param>
        /// <param name="message">The XML of the message.</param>
        /// <returns>The object of the message.</returns>
        public static object ConvertXmlToObject(Type messageType, string message)
        {
            object messageObj = null;

            // Make sure the serialiser has been loaded
            if (!messageSerialisers.ContainsKey(messageType))
            {
                messageSerialisers[messageType] = new XmlSerializer(messageType);
            }

            // Perform the actual conversion
            using (StringReader reader = new StringReader(message))
            {
                messageObj = messageSerialisers[messageType].Deserialize(reader);
            }

            return messageObj;
        }
        #endregion

        #region ConvertXmlToRequest()
        /// <summary>
        /// Converts a message string into a request.
        /// </summary>
        /// <param name="message">The XML of the message.</param>
        /// <returns>The converted <see cref="ServerRequest"/>.</returns>
        public static ServerRequest ConvertXmlToRequest(string message)
        {
            var messageXml = new XmlDocument();
            messageXml.LoadXml(message);
            var messageType = XmlConversionUtil.FindMessageType(messageXml.DocumentElement.Name);
            if (messageType == null) throw new CommunicationsException("Unknown message type");

            var request = ConvertXmlToObject(messageType, message) as ServerRequest;
            return request;
        }
        #endregion
        #endregion
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.