NetRepository.Core.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. using System;
  2. using System.Reflection;
  3. using System.Xml;
  4. using System.Collections;
  5. using System.IO;
  6. using System.Text.RegularExpressions;
  7. // From SyntaxParsers/NetRepository.cs
  8. namespace Editor.Syntax.Parsers.ReflectionRepository
  9. {
  10. public class DescriptionHelper
  11. {
  12. private static Hashtable assemblies = new Hashtable();
  13. private static string systemAssemblyFolder = string.Empty;
  14. private static Regex regex = new Regex(@"((\r\n)|(\n)|(\r))(\s)+", RegexOptions.Multiline);
  15. private static bool enabled = true;
  16. private static string GetInnerText(XmlNode node)
  17. {
  18. string result = string.Empty;
  19. if (node is XmlText)
  20. {
  21. result = node.InnerText;
  22. int p = result.IndexOf(":");
  23. if (p >= 0)
  24. result = result.Substring(p + 1);
  25. p = result.IndexOf(".#ctor");
  26. if (p >= 0)
  27. result = result.Substring(0, p);
  28. }
  29. else
  30. {
  31. if (node.Attributes != null)
  32. {
  33. foreach (XmlAttribute attr in node.Attributes)
  34. result = result + GetInnerText(attr);
  35. }
  36. // TZ: param name and description was displayed in one word
  37. if (node.Name == "param" && !String.IsNullOrEmpty(result))
  38. result = "<b>" + result + "</b>:\t";
  39. //
  40. if (node.HasChildNodes)
  41. {
  42. foreach (XmlNode child in node.ChildNodes)
  43. result = result + GetInnerText(child);
  44. }
  45. }
  46. return result;
  47. }
  48. private static XmlNode GetNodeByName(XmlNode node, string nodeName)
  49. {
  50. if (node != null)
  51. {
  52. XmlNode result = node.Name.Equals(nodeName) ? node : null;
  53. if (result != null)
  54. return result;
  55. if (node.HasChildNodes)
  56. {
  57. foreach (XmlNode child in node.ChildNodes)
  58. {
  59. result = GetNodeByName(child, nodeName);
  60. if (result != null)
  61. return result;
  62. }
  63. }
  64. }
  65. return null;
  66. }
  67. private static string GetNodeName(XmlNode node)
  68. {
  69. if (node.Attributes != null)
  70. {
  71. foreach (XmlAttribute attr in node.Attributes)
  72. {
  73. if (attr.Name == "name")
  74. return attr.InnerText;
  75. }
  76. }
  77. return string.Empty;
  78. }
  79. private static string GetNodeSummary(XmlNode node)
  80. {
  81. XmlNode result = GetNodeByName(node, "summary");
  82. return (result != null) ? GetInnerText(result) : string.Empty;
  83. }
  84. private static void LoadParameters(string name, XmlNode node, Hashtable descriptions)
  85. {
  86. if (node.HasChildNodes)
  87. {
  88. foreach (XmlNode child in node.ChildNodes)
  89. {
  90. if (child.Name.Equals("param"))
  91. {
  92. XmlAttribute attr = child.Attributes["name"];
  93. if (attr != null)
  94. descriptions[name + ".param:" + attr.Value] = GetInnerText(child);
  95. }
  96. }
  97. }
  98. }
  99. private static void LoadXmlFile(string fileName, Hashtable descriptions)
  100. {
  101. System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
  102. doc.Load(fileName);
  103. XmlNode root = GetNodeByName(doc, "members");
  104. if (root != null)
  105. {
  106. XmlNode node = root.FirstChild;
  107. while (node != null)
  108. {
  109. string name = GetNodeName(node);
  110. if (name.IndexOf(".Clear") >= 0)
  111. descriptions[name] = GetNodeSummary(node);
  112. else
  113. descriptions[name] = GetNodeSummary(node);
  114. if (node.HasChildNodes)
  115. LoadParameters(name, node, descriptions);
  116. node = node.NextSibling;
  117. }
  118. }
  119. }
  120. internal static object LoadAssembly(Assembly assembly)
  121. {
  122. object result = assemblies[assembly];
  123. if (result == null)
  124. {
  125. result = new Hashtable();
  126. if ((assembly.Location != null) && (assembly.Location != string.Empty))
  127. {
  128. try
  129. {
  130. FileInfo fileInfo = new FileInfo(Path.ChangeExtension(assembly.Location, "xml"));
  131. if (!fileInfo.Exists && SystemAssemblyFolder != string.Empty)
  132. fileInfo = new FileInfo(SystemAssemblyFolder + Path.ChangeExtension(Path.GetFileName(assembly.Location), "xml"));
  133. if (fileInfo.Exists)
  134. LoadXmlFile(fileInfo.FullName, (Hashtable)result);
  135. }
  136. catch
  137. {
  138. //
  139. }
  140. }
  141. }
  142. assemblies[assembly] = result;
  143. return result;
  144. }
  145. internal protected static object GetDescriptions(Assembly assembly)
  146. {
  147. object result = assemblies[assembly];
  148. if (result == null)
  149. result = LoadAssembly(assembly);
  150. return result;
  151. }
  152. protected static string GetDescription(string prefix, Type type, string postfix, string paramName)
  153. {
  154. if (type.Assembly == null)
  155. return null;
  156. // TZ: search descriptions in base members
  157. do
  158. {
  159. Hashtable descriptions = (Hashtable)GetDescriptions(type.Assembly);
  160. object result = descriptions[prefix + ":" + type.FullName + ((postfix != string.Empty) ? "." + postfix : string.Empty) + paramName];
  161. if (result != null && !String.IsNullOrEmpty(result.ToString()))
  162. return regex.Replace(result.ToString().Trim(), " ");
  163. type = type.BaseType;
  164. }
  165. while (type != null);
  166. return string.Empty;
  167. // TZ
  168. }
  169. protected static string GetDescription(MemberInfo info, string paramName)
  170. {
  171. if (info.Name == null)
  172. return string.Empty;
  173. string prefix = string.Empty;
  174. string postfix = info.Name.Replace(".ctor", "#ctor");
  175. switch (info.MemberType)
  176. {
  177. case MemberTypes.Constructor:
  178. case MemberTypes.Method:
  179. {
  180. prefix = "M";
  181. break;
  182. }
  183. case MemberTypes.Field:
  184. {
  185. prefix = "F";
  186. break;
  187. }
  188. case MemberTypes.Event:
  189. {
  190. prefix = "E";
  191. break;
  192. }
  193. case MemberTypes.TypeInfo:
  194. case MemberTypes.NestedType:
  195. {
  196. prefix = "T";
  197. postfix = string.Empty;
  198. break;
  199. }
  200. case MemberTypes.Property:
  201. {
  202. prefix = "P";
  203. break;
  204. }
  205. default:
  206. {
  207. return string.Empty;
  208. }
  209. }
  210. if (info is MethodInfo)
  211. {
  212. ParameterInfo[] paramsInfo = ((MethodInfo)info).GetParameters();
  213. if (paramsInfo.Length > 0)
  214. {
  215. string pars = string.Empty;
  216. for (int i = 0; i < paramsInfo.Length; i++)
  217. {
  218. ParameterInfo pInfo = paramsInfo[i];
  219. string s = pInfo.ParameterType.ToString().Replace('&', '@');
  220. pars = pars == string.Empty ? s : pars + "," + s;
  221. }
  222. postfix += "(" + pars + ")";
  223. }
  224. }
  225. object result = null;
  226. Type type = (info is Type) ? (Type)info : info.ReflectedType;
  227. if (type != null)
  228. result = GetDescription(prefix, type, postfix, paramName);
  229. if ((result == null) && (info.DeclaringType != null))
  230. result = GetDescription(prefix, info.DeclaringType, postfix, paramName);
  231. if (result == null)
  232. {
  233. while (type != null)
  234. {
  235. type = type.BaseType;
  236. if (type != null)
  237. {
  238. result = GetDescription(prefix, info.DeclaringType, postfix, paramName);
  239. if (result != null)
  240. break;
  241. }
  242. }
  243. }
  244. return (result != null) ? result.ToString() : string.Empty;
  245. }
  246. public static string GetDescription(ParameterInfo pinfo)
  247. {
  248. return (enabled && pinfo.Member != null) ? GetDescription(pinfo.Member, ".param:" + pinfo.Name) : string.Empty;
  249. }
  250. public static string GetDescription(MemberInfo info)
  251. {
  252. return enabled ? GetDescription(info, string.Empty) : string.Empty;
  253. }
  254. public static string SystemAssemblyFolder {
  255. get {
  256. if (systemAssemblyFolder == string.Empty)
  257. {
  258. foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
  259. {
  260. AssemblyName name = assembly.GetName();
  261. if ((name != null) && string.Compare(name.Name, "mscorlib", true) == 0)
  262. systemAssemblyFolder = string.Format("{0}\\", Path.GetDirectoryName(assembly.Location));
  263. }
  264. }
  265. return systemAssemblyFolder;
  266. }
  267. }
  268. }
  269. }