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