NetRepository.Mono.cs 11 KB

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