PageRenderTime 15ms CodeModel.GetById 10ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/src/Server/DeviceHive.API/Business/XmlCommentReader.cs

https://github.com/oryol/devicehive-.net
C# | 122 lines | 101 code | 20 blank | 1 comment | 11 complexity | 3217c4d5bdf2efce34a02a517f5e1079 MD5 | raw file
  1using System;
  2using System.Collections.Generic;
  3using System.IO;
  4using System.Linq;
  5using System.Reflection;
  6using System.Text.RegularExpressions;
  7using System.Web;
  8using System.Web.Http.Controllers;
  9using System.Web.Http.Description;
 10using System.Xml.Linq;
 11using System.Xml.XPath;
 12
 13namespace DeviceHive.API.Business
 14{
 15    public class XmlCommentReader
 16    {
 17        private XDocument _xml;
 18        private static Regex nullableTypeNameRegex = new Regex(@"(.*\.Nullable)" + Regex.Escape("`1[[") + "([^,]*),.*");
 19
 20        #region Constructor
 21
 22        public XmlCommentReader(string fileName)
 23        {
 24            var path = HttpContext.Current.Server.MapPath("~/Bin/" + fileName);
 25            using (var reader = new StreamReader(path))
 26            {
 27                _xml = XDocument.Load(reader);
 28            }
 29        }
 30        #endregion
 31
 32        #region Public Methods
 33
 34        public XElement GetTypeElement(Type type)
 35        {
 36            return _xml.XPathSelectElement(string.Format("/doc/members/member[@name='T:{0}']", type.FullName));
 37        }
 38
 39        public XElement GetPropertyElement(PropertyInfo property)
 40        {
 41            return _xml.XPathSelectElement(string.Format("/doc/members/member[@name='P:{0}.{1}']", property.DeclaringType.FullName, property.Name));
 42        }
 43
 44        public XElement GetMethodElement(MethodInfo method)
 45        {
 46            return _xml.XPathSelectElement(string.Format("/doc/members/member[@name='M:{0}']", GetMethodName(method)));
 47        }
 48
 49        public XElement GetMethodParameterElement(MethodInfo method, string parameterName)
 50        {
 51            var methodElement = GetMethodElement(method);
 52            if (methodElement == null)
 53                return null;
 54
 55            return methodElement.XPathSelectElement(string.Format("param[@name='{0}']", parameterName));
 56        }
 57
 58        public XElement GetMethodReturnsElement(MethodInfo method)
 59        {
 60            var methodElement = GetMethodElement(method);
 61            if (methodElement == null)
 62                return null;
 63
 64            return methodElement.XPathSelectElement("returns");
 65        }
 66        #endregion
 67
 68        #region Private Methods
 69
 70        private static string GetMethodName(MethodInfo method)
 71        {
 72            var name = string.Format("{0}.{1}", method.DeclaringType.FullName, method.Name);
 73            var parameters = method.GetParameters();
 74            if (parameters.Length != 0)
 75            {
 76                var parameterTypeNames = parameters.Select(p => ProcessTypeName(p.ParameterType.FullName)).ToArray();
 77                name += string.Format("({0})", string.Join(",", parameterTypeNames));
 78            }
 79            return name;
 80        }
 81
 82        private static string ProcessTypeName(string typeName)
 83        {
 84            var result = nullableTypeNameRegex.Match(typeName);
 85            if (result.Success)
 86            {
 87                return string.Format("{0}{{{1}}}", result.Groups[1].Value, result.Groups[2].Value);
 88            }
 89            return typeName;
 90        }
 91        #endregion
 92    }
 93
 94    public static class XmlCommentExtensions
 95    {
 96        #region Public Methods
 97
 98        public static string ElementContents(this XElement element, string name)
 99        {
100            if (element == null)
101                return null;
102
103            return element.Element(name).Contents();
104        }
105
106        public static string Contents(this XElement element)
107        {
108            if (element == null)
109                return null;
110
111            // fix cref link in see elements
112            foreach (var see in element.XPathSelectElements("//see[@cref]"))
113            {
114                var cref = see.Attribute("cref");
115                cref.Value = cref.Value.Substring(cref.Value.LastIndexOf(".") + 1);
116            }
117
118            return string.Concat(element.Nodes()).Trim();
119        }
120        #endregion
121    }
122}