forked from igorshmukler/XMLRPC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
XmlRpcServiceInfo.cs
executable file
·450 lines (425 loc) · 13.5 KB
/
XmlRpcServiceInfo.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
/*
XML-RPC.NET library
Copyright (c) 2001-2006, Charles Cook <[email protected]>
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of the Software,
and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
namespace CookComputing.XmlRpc
{
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Text.RegularExpressions;
public enum XmlRpcType
{
tInvalid,
tInt32,
tInt64,
tBoolean,
tString,
tDouble,
tDateTime,
tBase64,
tStruct,
tHashtable,
tArray,
tMultiDimArray,
tVoid
}
public class XmlRpcServiceInfo
{
public static XmlRpcServiceInfo CreateServiceInfo(Type type)
{
XmlRpcServiceInfo svcInfo = new XmlRpcServiceInfo();
// extract service info
XmlRpcServiceAttribute svcAttr = (XmlRpcServiceAttribute)
Attribute.GetCustomAttribute(type, typeof(XmlRpcServiceAttribute));
if (svcAttr != null && svcAttr.Description != "")
svcInfo.doc = svcAttr.Description;
if (svcAttr != null && svcAttr.Name != "")
svcInfo.Name = svcAttr.Name;
else
svcInfo.Name = type.Name;
// extract method info
Hashtable methods = new Hashtable();
foreach (Type itf in type.GetInterfaces())
{
XmlRpcServiceAttribute itfAttr = (XmlRpcServiceAttribute)
Attribute.GetCustomAttribute(itf, typeof(XmlRpcServiceAttribute));
if (itfAttr != null)
svcInfo.doc = itfAttr.Description;
#if (!COMPACT_FRAMEWORK)
InterfaceMapping imap = type.GetInterfaceMap(itf);
foreach (MethodInfo mi in imap.InterfaceMethods)
{
ExtractMethodInfo(methods, mi, itf);
}
#else
foreach (MethodInfo mi in itf.GetMethods())
{
ExtractMethodInfo(methods, mi, itf);
}
#endif
}
foreach (MethodInfo mi in type.GetMethods())
{
ArrayList mthds = new ArrayList();
mthds.Add(mi);
MethodInfo curMi = mi;
while (true)
{
MethodInfo baseMi = curMi.GetBaseDefinition();
if (baseMi.DeclaringType == curMi.DeclaringType)
break;
mthds.Insert(0, baseMi);
curMi = baseMi;
}
foreach (MethodInfo mthd in mthds)
{
ExtractMethodInfo(methods, mthd, type);
}
}
svcInfo.methodInfos = new XmlRpcMethodInfo[methods.Count];
methods.Values.CopyTo(svcInfo.methodInfos, 0);
Array.Sort(svcInfo.methodInfos);
return svcInfo;
}
static void ExtractMethodInfo(Hashtable methods, MethodInfo mi, Type type)
{
XmlRpcMethodAttribute attr = (XmlRpcMethodAttribute)
Attribute.GetCustomAttribute(mi,
typeof(XmlRpcMethodAttribute));
if (attr == null)
return;
XmlRpcMethodInfo mthdInfo = new XmlRpcMethodInfo();
mthdInfo.MethodInfo = mi;
mthdInfo.XmlRpcName = GetXmlRpcMethodName(mi);
mthdInfo.MiName = mi.Name;
mthdInfo.Doc = attr.Description;
mthdInfo.IsHidden = attr.IntrospectionMethod | attr.Hidden;
// extract parameters information
ArrayList parmList = new ArrayList();
ParameterInfo[] parms = mi.GetParameters();
foreach (ParameterInfo parm in parms)
{
XmlRpcParameterInfo parmInfo = new XmlRpcParameterInfo();
parmInfo.Name = parm.Name;
parmInfo.Type = parm.ParameterType;
parmInfo.XmlRpcType = GetXmlRpcTypeString(parm.ParameterType);
// retrieve optional attributed info
parmInfo.Doc = "";
XmlRpcParameterAttribute pattr = (XmlRpcParameterAttribute)
Attribute.GetCustomAttribute(parm,
typeof(XmlRpcParameterAttribute));
if (pattr != null)
{
parmInfo.Doc = pattr.Description;
parmInfo.XmlRpcName = pattr.Name;
}
parmInfo.IsParams = Attribute.IsDefined(parm,
typeof(ParamArrayAttribute));
parmList.Add(parmInfo);
}
mthdInfo.Parameters = (XmlRpcParameterInfo[])
parmList.ToArray(typeof(XmlRpcParameterInfo));
// extract return type information
mthdInfo.ReturnType = mi.ReturnType;
mthdInfo.ReturnXmlRpcType = GetXmlRpcTypeString(mi.ReturnType);
object[] orattrs = mi.ReturnTypeCustomAttributes.GetCustomAttributes(
typeof(XmlRpcReturnValueAttribute), false);
if (orattrs.Length > 0)
{
mthdInfo.ReturnDoc = ((XmlRpcReturnValueAttribute)orattrs[0]).Description;
}
if (methods[mthdInfo.XmlRpcName] != null)
{
throw new XmlRpcDupXmlRpcMethodNames(String.Format("Method "
+ "{0} in type {1} has duplicate XmlRpc method name {2}",
mi.Name, type.Name, mthdInfo.XmlRpcName));
}
else
methods.Add(mthdInfo.XmlRpcName, mthdInfo);
}
public MethodInfo GetMethodInfo(string xmlRpcMethodName)
{
foreach (XmlRpcMethodInfo xmi in methodInfos)
{
if (xmlRpcMethodName == xmi.XmlRpcName)
{
return xmi.MethodInfo;
}
}
return null;
}
static bool IsVisibleXmlRpcMethod(MethodInfo mi)
{
bool ret = false;
Attribute attr = Attribute.GetCustomAttribute(mi,
typeof(XmlRpcMethodAttribute));
if (attr != null)
{
XmlRpcMethodAttribute mattr = (XmlRpcMethodAttribute)attr;
ret = !(mattr.Hidden || mattr.IntrospectionMethod == true);
}
return ret;
}
public static string GetXmlRpcMethodName(MethodInfo mi)
{
XmlRpcMethodAttribute attr = (XmlRpcMethodAttribute)
Attribute.GetCustomAttribute(mi,
typeof(XmlRpcMethodAttribute));
if (attr != null
&& attr.Method != null
&& attr.Method != "")
{
return attr.Method;
}
else
{
return mi.Name;
}
}
public string GetMethodName(string XmlRpcMethodName)
{
foreach (XmlRpcMethodInfo methodInfo in methodInfos)
{
if (methodInfo.XmlRpcName == XmlRpcMethodName)
return methodInfo.MiName;
}
return null;
}
public String Doc
{
get { return doc; }
set { doc = value; }
}
public String Name
{
get { return name; }
set { name = value; }
}
public XmlRpcMethodInfo[] Methods
{
get { return methodInfos; }
}
public XmlRpcMethodInfo GetMethod(
String methodName)
{
foreach (XmlRpcMethodInfo mthdInfo in methodInfos)
{
if (mthdInfo.XmlRpcName == methodName)
return mthdInfo;
}
return null;
}
private XmlRpcServiceInfo()
{
}
public static XmlRpcType GetXmlRpcType(Type t)
{
return GetXmlRpcType(t, new Stack());
}
private static XmlRpcType GetXmlRpcType(Type t, Stack typeStack)
{
XmlRpcType ret;
if (t == typeof(Int32))
ret = XmlRpcType.tInt32;
else if (t == typeof(XmlRpcInt))
ret = XmlRpcType.tInt32;
else if (t == typeof(Int64))
ret = XmlRpcType.tInt64;
else if (t == typeof(Boolean))
ret = XmlRpcType.tBoolean;
else if (t == typeof(XmlRpcBoolean))
ret = XmlRpcType.tBoolean;
else if (t == typeof(String))
ret = XmlRpcType.tString;
else if (t == typeof(Double))
ret = XmlRpcType.tDouble;
else if (t == typeof(XmlRpcDouble))
ret = XmlRpcType.tDouble;
else if (t == typeof(DateTime))
ret = XmlRpcType.tDateTime;
else if (t == typeof(XmlRpcDateTime))
ret = XmlRpcType.tDateTime;
else if (t == typeof(byte[]))
ret = XmlRpcType.tBase64;
else if (t == typeof(XmlRpcStruct))
{
ret = XmlRpcType.tHashtable;
}
else if (t == typeof(Array))
ret = XmlRpcType.tArray;
else if (t.IsArray)
{
#if (!COMPACT_FRAMEWORK)
Type elemType = t.GetElementType();
if (elemType != typeof(Object)
&& GetXmlRpcType(elemType, typeStack) == XmlRpcType.tInvalid)
{
ret = XmlRpcType.tInvalid;
}
else
{
if (t.GetArrayRank() == 1) // single dim array
ret = XmlRpcType.tArray;
else
ret = XmlRpcType.tMultiDimArray;
}
#else
//!! check types of array elements if not Object[]
Type elemType = null;
string[] checkSingleDim = Regex.Split(t.FullName, "\\[\\]$");
if (checkSingleDim.Length > 1) // single dim array
{
elemType = Type.GetType(checkSingleDim[0]);
ret = XmlRpcType.tArray;
}
else
{
string[] checkMultiDim = Regex.Split(t.FullName, "\\[,[,]*\\]$");
if (checkMultiDim.Length > 1)
{
elemType = Type.GetType(checkMultiDim[0]);
ret = XmlRpcType.tMultiDimArray;
}
else
ret = XmlRpcType.tInvalid;
}
if (elemType != null)
{
if (elemType != typeof(Object)
&& GetXmlRpcType(elemType, typeStack) == XmlRpcType.tInvalid)
{
ret = XmlRpcType.tInvalid;
}
}
#endif
}
#if !FX1_0
else if (t == typeof(int?))
ret = XmlRpcType.tInt32;
else if (t == typeof(long?))
ret = XmlRpcType.tInt64;
else if (t == typeof(Boolean?))
ret = XmlRpcType.tBoolean;
else if (t == typeof(Double?))
ret = XmlRpcType.tDouble;
else if (t == typeof(DateTime?))
ret = XmlRpcType.tDateTime;
#endif
else if (t == typeof(void))
{
ret = XmlRpcType.tVoid;
}
else if ((t.IsValueType && !t.IsPrimitive && !t.IsEnum)
|| t.IsClass)
{
// if type is struct or class its only valid for XML-RPC mapping if all
// its members have a valid mapping or are of type object which
// maps to any XML-RPC type
MemberInfo[] mis = t.GetMembers();
foreach (MemberInfo mi in mis)
{
if (mi.MemberType == MemberTypes.Field)
{
FieldInfo fi = (FieldInfo)mi;
if (typeStack.Contains(fi.FieldType))
continue;
try
{
typeStack.Push(fi.FieldType);
if ((fi.FieldType != typeof(Object)
&& GetXmlRpcType(fi.FieldType, typeStack) == XmlRpcType.tInvalid))
{
return XmlRpcType.tInvalid;
}
}
finally
{
typeStack.Pop();
}
}
else if (mi.MemberType == MemberTypes.Property)
{
PropertyInfo pi = (PropertyInfo)mi;
if (typeStack.Contains(pi.PropertyType))
continue;
try
{
typeStack.Push(pi.PropertyType);
if ((pi.PropertyType != typeof(Object)
&& GetXmlRpcType(pi.PropertyType, typeStack) == XmlRpcType.tInvalid))
{
return XmlRpcType.tInvalid;
}
}
finally
{
typeStack.Pop();
}
}
}
ret = XmlRpcType.tStruct;
}
else
ret = XmlRpcType.tInvalid;
return ret;
}
static public string GetXmlRpcTypeString(Type t)
{
XmlRpcType rpcType = GetXmlRpcType(t);
return GetXmlRpcTypeString(rpcType);
}
static public string GetXmlRpcTypeString(XmlRpcType t)
{
string ret = null;
if (t == XmlRpcType.tInt32)
ret = "integer";
else if (t == XmlRpcType.tInt64)
ret = "i8";
else if (t == XmlRpcType.tBoolean)
ret = "boolean";
else if (t == XmlRpcType.tString)
ret = "string";
else if (t == XmlRpcType.tDouble)
ret = "double";
else if (t == XmlRpcType.tDateTime)
ret = "dateTime";
else if (t == XmlRpcType.tBase64)
ret = "base64";
else if (t == XmlRpcType.tStruct)
ret = "struct";
else if (t == XmlRpcType.tHashtable)
ret = "struct";
else if (t == XmlRpcType.tArray)
ret = "array";
else if (t == XmlRpcType.tMultiDimArray)
ret = "array";
else if (t == XmlRpcType.tVoid)
ret = "void";
else
ret = null;
return ret;
}
XmlRpcMethodInfo[] methodInfos;
String doc;
string name;
}
}