/TechEdWebApiDemo/Areas/HelpPage/XmlDocumentationProvider.cs
C# | 112 lines | 90 code | 13 blank | 9 comment | 17 complexity | de53d5fa3e889f7083e2f7cf5319aa5d MD5 | raw file
- using System;
- using System.Linq;
- using System.Reflection;
- using System.Web.Http.Controllers;
- using System.Web.Http.Description;
- using System.Xml.XPath;
- using System.Globalization;
- namespace TechEdWebApiDemo.Areas.HelpPage
- {
- /// <summary>
- /// A custom <see cref="IDocumentationProvider"/> that reads the API documentation from an XML documentation file.
- /// </summary>
- public class XmlDocumentationProvider : IDocumentationProvider
- {
- private XPathNavigator _documentNavigator;
- private const string _methodExpression = "/doc/members/member[@name='M:{0}']";
- private const string _parameterExpression = "param[@name='{0}']";
- /// <summary>
- /// Initializes a new instance of the <see cref="XmlDocumentationProvider"/> class.
- /// </summary>
- /// <param name="documentPath">The physical path to XML document.</param>
- public XmlDocumentationProvider(string documentPath)
- {
- if (documentPath == null)
- {
- throw new ArgumentNullException("documentPath");
- }
- XPathDocument xpath = new XPathDocument(documentPath);
- _documentNavigator = xpath.CreateNavigator();
- }
- public virtual string GetDocumentation(HttpActionDescriptor actionDescriptor)
- {
- XPathNavigator methodNode = GetMethodNode(actionDescriptor);
- if (methodNode != null)
- {
- XPathNavigator summaryNode = methodNode.SelectSingleNode("summary");
- if (summaryNode != null)
- {
- return summaryNode.Value.Trim();
- }
- }
- return null;
- }
- public virtual string GetDocumentation(HttpParameterDescriptor parameterDescriptor)
- {
- ReflectedHttpParameterDescriptor reflectedParameterDescriptor = parameterDescriptor as ReflectedHttpParameterDescriptor;
- if (reflectedParameterDescriptor != null)
- {
- XPathNavigator methodNode = GetMethodNode(reflectedParameterDescriptor.ActionDescriptor);
- if (methodNode != null)
- {
- string parameterName = reflectedParameterDescriptor.ParameterInfo.Name;
- XPathNavigator parameterNode = methodNode.SelectSingleNode(String.Format(CultureInfo.InvariantCulture, _parameterExpression, parameterName));
- if (parameterNode != null)
- {
- return parameterNode.Value.Trim();
- }
- }
- }
- return null;
- }
- private XPathNavigator GetMethodNode(HttpActionDescriptor actionDescriptor)
- {
- ReflectedHttpActionDescriptor reflectedActionDescriptor = actionDescriptor as ReflectedHttpActionDescriptor;
- if (reflectedActionDescriptor != null)
- {
- string selectExpression = String.Format(CultureInfo.InvariantCulture, _methodExpression, GetMemberName(reflectedActionDescriptor.MethodInfo));
- return _documentNavigator.SelectSingleNode(selectExpression);
- }
- return null;
- }
- private static string GetMemberName(MethodInfo method)
- {
- string name = String.Format(CultureInfo.InvariantCulture, "{0}.{1}", method.DeclaringType.FullName, method.Name);
- ParameterInfo[] parameters = method.GetParameters();
- if (parameters.Length != 0)
- {
- string[] parameterTypeNames = parameters.Select(param => GetTypeName(param.ParameterType)).ToArray();
- name += String.Format(CultureInfo.InvariantCulture, "({0})", String.Join(",", parameterTypeNames));
- }
- return name;
- }
- private static string GetTypeName(Type type)
- {
- if (type.IsGenericType)
- {
- // Format the generic type name to something like: Generic{System.Int32,System.String}
- Type genericType = type.GetGenericTypeDefinition();
- Type[] genericArguments = type.GetGenericArguments();
- string typeName = genericType.FullName;
- // Trim the generic parameter counts from the name
- typeName = typeName.Substring(0, typeName.IndexOf('`'));
- string[] argumentTypeNames = genericArguments.Select(t => GetTypeName(t)).ToArray();
- return String.Format(CultureInfo.InvariantCulture, "{0}{{{1}}}", typeName, String.Join(",", argumentTypeNames));
- }
- return type.FullName;
- }
- }
- }