<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.XPath;
using System.Globalization;
using System.Reflection;

namespace Cus.WebApi
{
    /// <summary>
    /// xml文档提供者
    /// </summary>
    class XmlDocumentationProvider
    {
        private XPathNavigator _documentNavigator;
        private const string TypeExpression = "/doc/members/member[@name='T:{0}']";
        private const string MethodExpression = "/doc/members/member[@name='M:{0}']";
        private const string PropertyExpression = "/doc/members/member[@name='P:{0}']";
        private const string ParameterExpression = "param[@name='{0}']";

        public XmlDocumentationProvider(System.IO.Stream stream)
        {
            var xpath = new XPathDocument(stream);
            _documentNavigator = xpath.CreateNavigator();
        }

        public XmlDocumentationProvider(string documentPath)
        {
            var xpath = new XPathDocument(documentPath);
            _documentNavigator = xpath.CreateNavigator();
        }

        public void SetDocumentation(ApiDescriptor apiDescriptor, bool classDocOnly = false)
        {
            apiDescriptor.Documentation = GetApiDocumentation(apiDescriptor);

            if (classDocOnly) return;

            foreach (var methodDescriptor in apiDescriptor.Methods)
            {
                methodDescriptor.Documentation = GetMethodDocumentation(methodDescriptor);
                foreach (var paramDescriptor in methodDescriptor.Params)
                {
                    paramDescriptor.Documentation = GetParamDocumentation(methodDescriptor, paramDescriptor);
                    ScanParamDocumentation(paramDescriptor);
                }
                methodDescriptor.ResponseParam.Documentation = GetResponseDocumentation(methodDescriptor);
                ScanParamDocumentation(methodDescriptor.ResponseParam);
            }
        }

        private string GetApiDocumentation(ApiDescriptor apiDescriptor)
        {
            XPathNavigator typeNode = GetTypeNode(apiDescriptor.ApiType);
            return GetTagValue(typeNode, "summary");
        }

        private string GetMethodDocumentation(MethodDescriptor methodDescriptor)
        {
            XPathNavigator methodNode = GetMethodNode(methodDescriptor);
            return GetTagValue(methodNode, "summary");
        }

        private string GetParamDocumentation(MethodDescriptor methodDescriptor, ParamDescriptor paramDescriptor)
        {
            XPathNavigator methodNode = GetMethodNode(methodDescriptor);
            if (methodNode != null)
            {
                string parameterName = paramDescriptor.ParameterInfo.Name;
                XPathNavigator parameterNode = methodNode.SelectSingleNode(String.Format(CultureInfo.InvariantCulture, ParameterExpression, parameterName));
                if (parameterNode != null)
                {
                    return parameterNode.InnerXml.Trim();
                }
            }
            return null;
        }

        private string GetResponseDocumentation(MethodDescriptor methodDescriptor)
        {
            XPathNavigator methodNode = GetMethodNode(methodDescriptor);
            return GetTagValue(methodNode, "returns");
        }

        private void ScanParamDocumentation(TypeDescriptor typeDescriptor)
        {
            if (!typeDescriptor.Def.IsLeaf && typeDescriptor.Properties != null)
            {
                foreach (var property in typeDescriptor.Properties)
                {
                    ScanPropertyDocumentation(property);
                }
            }
        }

        private void ScanPropertyDocumentation(PropertyDescriptor propertyDescriptor)
        {
            XPathNavigator propertyNode = GetPropertyNode(propertyDescriptor);
            string documentation = GetTagValue(propertyNode, "summary");

            if (propertyDescriptor.Documentation == null && documentation != null)
                propertyDescriptor.Documentation = documentation;

            if (propertyDescriptor.Def.IsLeaf) return;
            if (propertyDescriptor.Def.IsArray && propertyDescriptor.SubType != null && propertyDescriptor.SubType.Def.IsObject)
            {
                foreach (PropertyDescriptor sub in propertyDescriptor.SubType.Properties)
                {
                    ScanPropertyDocumentation(sub);
                }
            }
            else if (propertyDescriptor.Def.IsObject)
            {
                foreach (PropertyDescriptor sub in propertyDescriptor.Properties)
                {
                    ScanPropertyDocumentation(sub);
                }
            }
        }

        private XPathNavigator GetPropertyNode(PropertyDescriptor propertyDescriptor)
        {
            string selectExpression = String.Format(CultureInfo.InvariantCulture, PropertyExpression, GetPropertyName(propertyDescriptor.PropertyInfo));
            return _documentNavigator.SelectSingleNode(selectExpression);
        }

         private static string GetPropertyName(PropertyInfo property)
         {
             var dtype = property.DeclaringType;<commit_msg>fix document bug<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.XPath;
using System.Globalization;
using System.Reflection;

namespace Cus.WebApi
{
    /// <summary>
    /// xml文档提供者
    /// </summary>
    class XmlDocumentationProvider
    {
        private XPathNavigator _documentNavigator;
        private const string TypeExpression = "/doc/members/member[@name='T:{0}']";
        private const string MethodExpression = "/doc/members/member[@name='M:{0}']";
        private const string PropertyExpression = "/doc/members/member[@name='P:{0}']";
        private const string ParameterExpression = "param[@name='{0}']";

        public XmlDocumentationProvider(System.IO.Stream stream)
        {
            var xpath = new XPathDocument(stream);
            _documentNavigator = xpath.CreateNavigator();
        }

        public XmlDocumentationProvider(string documentPath)
        {
            var xpath = new XPathDocument(documentPath);
            _documentNavigator = xpath.CreateNavigator();
        }

        public void SetDocumentation(ApiDescriptor apiDescriptor, bool classDocOnly = false)
        {
            if (apiDescriptor.Documentation == null)
                apiDescriptor.Documentation = GetApiDocumentation(apiDescriptor);

            if (classDocOnly) return;

            foreach (var methodDescriptor in apiDescriptor.Methods)
            {
                if (methodDescriptor.Documentation == null)
                    methodDescriptor.Documentation = GetMethodDocumentation(methodDescriptor);

                foreach (var paramDescriptor in methodDescriptor.Params)
                {
                    if (paramDescriptor.Documentation == null)
                        paramDescriptor.Documentation = GetParamDocumentation(methodDescriptor, paramDescriptor);

                    ScanParamDocumentation(paramDescriptor);
                }

                if (methodDescriptor.ResponseParam.Documentation == null)
                    methodDescriptor.ResponseParam.Documentation = GetResponseDocumentation(methodDescriptor);

                ScanParamDocumentation(methodDescriptor.ResponseParam);
            }
        }

        private string GetApiDocumentation(ApiDescriptor apiDescriptor)
        {
            XPathNavigator typeNode = GetTypeNode(apiDescriptor.ApiType);
            return GetTagValue(typeNode, "summary");
        }

        private string GetMethodDocumentation(MethodDescriptor methodDescriptor)
        {
            XPathNavigator methodNode = GetMethodNode(methodDescriptor);
            return GetTagValue(methodNode, "summary");
        }

        private string GetParamDocumentation(MethodDescriptor methodDescriptor, ParamDescriptor paramDescriptor)
        {
            XPathNavigator methodNode = GetMethodNode(methodDescriptor);
            if (methodNode != null)
            {
                string parameterName = paramDescriptor.ParameterInfo.Name;
                XPathNavigator parameterNode = methodNode.SelectSingleNode(String.Format(CultureInfo.InvariantCulture, ParameterExpression, parameterName));
                if (parameterNode != null)
                {
                    return parameterNode.InnerXml.Trim();
                }
            }
            return null;
        }

        private string GetResponseDocumentation(MethodDescriptor methodDescriptor)
        {
            XPathNavigator methodNode = GetMethodNode(methodDescriptor);
            return GetTagValue(methodNode, "returns");
        }

        private void ScanParamDocumentation(TypeDescriptor typeDescriptor)
        {
            if (!typeDescriptor.Def.IsLeaf && typeDescriptor.Properties != null)
            {
                foreach (var property in typeDescriptor.Properties)
                {
                    ScanPropertyDocumentation(property);
                }
            }
        }

        private void ScanPropertyDocumentation(PropertyDescriptor propertyDescriptor)
        {
            XPathNavigator propertyNode = GetPropertyNode(propertyDescriptor);

            if (propertyDescriptor.Documentation == null)
                propertyDescriptor.Documentation = GetTagValue(propertyNode, "summary");

            if (propertyDescriptor.Def.IsLeaf) return;
            if (propertyDescriptor.Def.IsArray && propertyDescriptor.SubType != null && propertyDescriptor.SubType.Def.IsObject)
            {
                foreach (PropertyDescriptor sub in propertyDescriptor.SubType.Properties)
                {
                    ScanPropertyDocumentation(sub);
                }
            }
            else if (propertyDescriptor.Def.IsObject)
            {
                foreach (PropertyDescriptor sub in propertyDescriptor.Properties)
                {
                    ScanPropertyDocumentation(sub);
                }
            }
        }

        private XPathNavigator GetPropertyNode(PropertyDescriptor propertyDescriptor)
        {
            string selectExpression = String.Format(CultureInfo.InvariantCulture, PropertyExpression, GetPropertyName(propertyDescriptor.PropertyInfo));
            return _documentNavigator.SelectSingleNode(selectExpression);
        }

         private static string GetPropertyName(PropertyInfo property)
         {
             var dtype = property.DeclaringType;
{'fraction_non_alphanumeric': 0.07287334593572779, 'fraction_numerical': 0.0007561436672967864, 'mean_word_length': 2.221985383678441, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7244179', 'n_tokens_mistral': 2383, 'n_tokens_neox': 2273, 'n_words': 456}