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