devicehive-.net /src/Server/DeviceHive.API/Business/XmlCommentReader.cs

Language C# Lines 122
MD5 Hash 3217c4d5bdf2efce34a02a517f5e1079
Repository https://github.com/oryol/devicehive-.net.git View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Http.Controllers;
using System.Web.Http.Description;
using System.Xml.Linq;
using System.Xml.XPath;

namespace DeviceHive.API.Business
{
    public class XmlCommentReader
    {
        private XDocument _xml;
        private static Regex nullableTypeNameRegex = new Regex(@"(.*\.Nullable)" + Regex.Escape("`1[[") + "([^,]*),.*");

        #region Constructor

        public XmlCommentReader(string fileName)
        {
            var path = HttpContext.Current.Server.MapPath("~/Bin/" + fileName);
            using (var reader = new StreamReader(path))
            {
                _xml = XDocument.Load(reader);
            }
        }
        #endregion

        #region Public Methods

        public XElement GetTypeElement(Type type)
        {
            return _xml.XPathSelectElement(string.Format("/doc/members/member[@name='T:{0}']", type.FullName));
        }

        public XElement GetPropertyElement(PropertyInfo property)
        {
            return _xml.XPathSelectElement(string.Format("/doc/members/member[@name='P:{0}.{1}']", property.DeclaringType.FullName, property.Name));
        }

        public XElement GetMethodElement(MethodInfo method)
        {
            return _xml.XPathSelectElement(string.Format("/doc/members/member[@name='M:{0}']", GetMethodName(method)));
        }

        public XElement GetMethodParameterElement(MethodInfo method, string parameterName)
        {
            var methodElement = GetMethodElement(method);
            if (methodElement == null)
                return null;

            return methodElement.XPathSelectElement(string.Format("param[@name='{0}']", parameterName));
        }

        public XElement GetMethodReturnsElement(MethodInfo method)
        {
            var methodElement = GetMethodElement(method);
            if (methodElement == null)
                return null;

            return methodElement.XPathSelectElement("returns");
        }
        #endregion

        #region Private Methods

        private static string GetMethodName(MethodInfo method)
        {
            var name = string.Format("{0}.{1}", method.DeclaringType.FullName, method.Name);
            var parameters = method.GetParameters();
            if (parameters.Length != 0)
            {
                var parameterTypeNames = parameters.Select(p => ProcessTypeName(p.ParameterType.FullName)).ToArray();
                name += string.Format("({0})", string.Join(",", parameterTypeNames));
            }
            return name;
        }

        private static string ProcessTypeName(string typeName)
        {
            var result = nullableTypeNameRegex.Match(typeName);
            if (result.Success)
            {
                return string.Format("{0}{{{1}}}", result.Groups[1].Value, result.Groups[2].Value);
            }
            return typeName;
        }
        #endregion
    }

    public static class XmlCommentExtensions
    {
        #region Public Methods

        public static string ElementContents(this XElement element, string name)
        {
            if (element == null)
                return null;

            return element.Element(name).Contents();
        }

        public static string Contents(this XElement element)
        {
            if (element == null)
                return null;

            // fix cref link in see elements
            foreach (var see in element.XPathSelectElements("//see[@cref]"))
            {
                var cref = see.Attribute("cref");
                cref.Value = cref.Value.Substring(cref.Value.LastIndexOf(".") + 1);
            }

            return string.Concat(element.Nodes()).Trim();
        }
        #endregion
    }
}
Back to Top