// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. #if !DNXCORE50 using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Text; using System.Xml.Linq; namespace Microsoft.AspNet.Razor.Runtime { /// /// Extracts summary and remarks XML documentation from an XML documentation file. /// public class XmlDocumentationProvider { private readonly IEnumerable _members; /// /// Instantiates a new instance of the . /// /// Path to the XML documentation file to read. public XmlDocumentationProvider(string xmlFileLocation) { // XML file processing is defined by: https://msdn.microsoft.com/en-us/library/fsbx0t7x.aspx var xmlDocumentation = XDocument.Load(xmlFileLocation); var documentationRootMembers = xmlDocumentation.Root.Element("members"); _members = documentationRootMembers.Elements("member"); } /// /// Retrieves the <summary> documentation for the given . /// /// The id to lookup. /// <summary> documentation for the given . public string GetSummary(string id) { var associatedMemeber = GetMember(id); var summaryElement = associatedMemeber?.Element("summary"); if (summaryElement != null) { var summaryValue = GetElementValue(summaryElement); return summaryValue; } return null; } /// /// Retrieves the <remarks> documentation for the given . /// /// The id to lookup. /// <remarks> documentation for the given . public string GetRemarks(string id) { var associatedMemeber = GetMember(id); var remarksElement = associatedMemeber?.Element("remarks"); if (remarksElement != null) { var remarksValue = GetElementValue(remarksElement); return remarksValue; } return null; } /// /// Generates the identifier for the given . /// /// The to get the identifier for. /// The identifier for the given . public static string GetId(Type type) { if (type == null) { throw new ArgumentNullException(nameof(type)); } return $"T:{type.FullName}"; } /// /// Generates the identifier for the given . /// /// The to get the identifier for. /// The identifier for the given . public static string GetId(PropertyInfo propertyInfo) { if (propertyInfo == null) { throw new ArgumentNullException(nameof(propertyInfo)); } var declaringTypeInfo = propertyInfo.DeclaringType; return $"P:{declaringTypeInfo.FullName}.{propertyInfo.Name}"; } private XElement GetMember(string id) { var associatedMemeber = _members .FirstOrDefault(element => string.Equals(element.Attribute("name").Value, id, StringComparison.Ordinal)); return associatedMemeber; } private static string GetElementValue(XElement element) { var stringBuilder = new StringBuilder(); var node = element.FirstNode; while (node != null) { stringBuilder.Append(node.ToString(SaveOptions.DisableFormatting)); node = node.NextNode; } return stringBuilder.ToString().Trim(); } } } #endif