23 |
* Authors: |
* Authors: |
24 |
* Nicolai P Guba |
* Nicolai P Guba |
25 |
*/ |
*/ |
|
using System.Text; |
|
26 |
using System.Reflection; |
using System.Reflection; |
27 |
|
using DotGNU.XmlRpc; |
28 |
|
|
29 |
namespace DotGNU.DGEE.Protocols.XmlRpc |
namespace DotGNU.DGEE.Protocols.XmlRpc |
30 |
{ |
{ |
31 |
public class XmlRpcReplyHandler : IWebServiceReplyHandler |
public class XmlRpcReplyHandler : IWebServiceReplyHandler |
32 |
{ |
{ |
|
private StringBuilder buf = new StringBuilder(); |
|
|
|
|
|
// XML Rpc Parameter Types |
|
|
public enum XmlRpcParamType |
|
|
{ |
|
|
tInvalid, |
|
|
tInt32, |
|
|
tBoolean, |
|
|
tString, |
|
|
tDouble, |
|
|
tDateTime, |
|
|
tBase64, |
|
|
tStruct, |
|
|
tHashtable, |
|
|
tArray, |
|
|
tMultiDimArray, |
|
|
tVoid |
|
|
} |
|
|
|
|
33 |
public String GetReply( String assemblyName, String className, String methodName ) { |
public String GetReply( String assemblyName, String className, String methodName ) { |
34 |
// Load the assembly |
|
35 |
/* |
string reply; |
36 |
Assembly assembly = Assembly.Load( assemblyName ); |
XmlRpcWriter writer = new XmlRpcWriter(); |
|
Type[] types = assembly.GetTypes(); |
|
37 |
|
|
38 |
foreach( Type theType in types ) { |
XmlRpcStruct h = new XmlRpcStruct(); |
|
Console.WriteLine( "Type is {0}", theType ); |
|
|
|
|
|
Console.WriteLine( "Single Type is {0}", theType ); |
|
|
MemberInfo[] mbrInfoArray = theType.GetMembers(); |
|
|
foreach( MemberInfo info in mbrInfoArray) { |
|
|
Console.WriteLine( "{0} is a {1}", info, info.MemberType ); |
|
|
} |
|
|
} |
|
|
*/ |
|
|
|
|
|
// Write the Header |
|
|
buf.Append( "<?xml version=\"1.0\" encoding=\"us-ascii\"?>\n" ); |
|
|
buf.Append( "<methodResponse>\n" ); |
|
|
buf.Append( " <params>\n" ); |
|
|
buf.Append( " <param>\n" ); |
|
|
|
|
|
// Do the Nitty Gritty bits |
|
|
//SerializeObject( w.Dll ); |
|
|
|
|
|
// Write the Footer |
|
|
buf.Append( " </param>\n" ); |
|
|
buf.Append( " </params>\n" ); |
|
|
buf.Append( "</methodResponse>\n" ); |
|
39 |
|
|
40 |
return buf.ToString(); |
int i = 1; |
41 |
} |
double d = 2.0; |
42 |
|
bool b = false; |
43 |
|
|
44 |
// Determine the parameter type from its arguments |
h["one"] = i; |
45 |
internal static XmlRpcParamType GetParamType( Type t ) |
h["two"] = d; |
46 |
{ |
h["three"] = b; |
47 |
XmlRpcParamType ret; |
|
48 |
|
XmlRpcStruct h0 = new XmlRpcStruct(); |
49 |
if( t == typeof(Int32) ) { |
h0["nested one"] = 11; |
50 |
ret = XmlRpcParamType.tInt32; |
h0["nested two"] = 22; |
51 |
} |
|
52 |
else if( t == typeof(Boolean) ) { |
h["A structure"] = h0; |
|
ret = XmlRpcParamType.tBoolean; |
|
|
} |
|
|
else if( t == typeof(String) ) { |
|
|
ret = XmlRpcParamType.tString; |
|
|
} |
|
|
else if( t == typeof(Double) ) { |
|
|
ret = XmlRpcParamType.tDouble; |
|
|
} |
|
|
else if( t == typeof(DateTime) ) { |
|
|
ret = XmlRpcParamType.tDateTime; |
|
|
} |
|
|
else if( t == typeof(byte[]) ) { |
|
|
ret = XmlRpcParamType.tBase64; |
|
|
} |
|
|
else if( t == typeof(Array) ) { |
|
|
ret = XmlRpcParamType.tArray; |
|
|
} |
|
|
///else if (t == typeof(Struct)) { |
|
|
// FIXME |
|
|
//ret = XmlRpcParamType.tStruct; |
|
|
//} |
|
|
else if (t == typeof(void)) { |
|
|
ret = XmlRpcParamType.tVoid; |
|
|
} |
|
|
else if (t.IsValueType && !t.IsPrimitive) |
|
|
{ |
|
|
MemberInfo[] mis = t.GetMembers(); |
|
|
|
|
|
foreach( MemberInfo mi in mis ) { |
|
|
|
|
|
if( mi.MemberType == MemberTypes.Field ) { |
|
|
FieldInfo fi = (FieldInfo)mi; |
|
|
|
|
|
if( GetParamType( fi.FieldType ) == XmlRpcParamType.tInvalid ) { |
|
|
return XmlRpcParamType.tInvalid; |
|
|
} |
|
|
} |
|
|
} |
|
|
ret = XmlRpcParamType.tStruct; |
|
|
} |
|
|
else { |
|
|
ret = XmlRpcParamType.tInvalid; |
|
|
} |
|
|
|
|
|
// Check for the datatypes in the array |
|
|
if (ret == XmlRpcParamType.tArray) { |
|
|
// TODO check types of array elements if not Object[] |
|
|
} |
|
|
return ret; |
|
|
} |
|
53 |
|
|
54 |
/// <summary> |
reply = writer.Parse( h ); |
55 |
/// Processes the Object into valid Xml-Rpc |
return reply; |
|
/// </summary> |
|
|
/// <param name="obj"> |
|
|
/// The Dll to process |
|
|
/// </param> |
|
|
public void SerializeObject( Object obj ) |
|
|
{ |
|
|
try { |
|
|
XmlRpcParamType dtype = GetParamType( obj.GetType() ); |
|
|
buf.Append( " <value>\n" ); |
|
|
|
|
|
switch( dtype ) { |
|
|
case XmlRpcParamType.tString: |
|
|
buf.Append( " <string>" ); |
|
|
buf.Append( obj.ToString() ); |
|
|
buf.Append( " </string>" ); |
|
|
break; |
|
|
|
|
|
case XmlRpcParamType.tInt32: |
|
|
buf.Append( " <i4>" ); |
|
|
buf.Append( ((Int32)obj).ToString() ); |
|
|
buf.Append( " </i4>" ); |
|
|
break; |
|
|
|
|
|
case XmlRpcParamType.tDouble: |
|
|
buf.Append( " <double>" ); |
|
|
buf.Append( ((double)obj).ToString() ); |
|
|
buf.Append( " </double>" ); |
|
|
break; |
|
|
|
|
|
case XmlRpcParamType.tBoolean: |
|
|
buf.Append( " <boolean>" ); |
|
|
buf.Append( ((bool)obj) ? "1" : "0" ); |
|
|
buf.Append( " </boolean>" ); |
|
|
break; |
|
|
|
|
|
case XmlRpcParamType.tBase64: // TODO Not implemented |
|
|
buf.Append( " <!-- <base64>" ); |
|
|
buf.Append( " Base 64 Serialisation not implemented" ); |
|
|
buf.Append( " </base64> -->" ); |
|
|
break; |
|
|
|
|
|
case XmlRpcParamType.tDateTime: |
|
|
String dt = ((DateTime)obj).ToString("yyyyMMddTHH:mm:ss"); |
|
|
buf.Append( " <dateTime.iso8601>" ); |
|
|
buf.Append( dt ); |
|
|
buf.Append( " </dateTime.iso8601>" ); |
|
|
break; |
|
|
|
|
|
case XmlRpcParamType.tArray: |
|
|
buf.Append( " <array>\n <data>\n " ); |
|
|
foreach ( Object ar_obj in (Array)obj ) |
|
|
{ |
|
|
SerializeObject( ar_obj ); |
|
|
} |
|
|
buf.Append( " </data>\n</array>" ); |
|
|
break; |
|
|
|
|
|
case XmlRpcParamType.tStruct: |
|
|
buf.Append( " <struct>\n" ); |
|
|
MemberInfo []miarr = obj.GetType().GetMembers(); |
|
|
|
|
|
foreach( MemberInfo mi in miarr ) { |
|
|
if( mi.MemberType == MemberTypes.Field ) { |
|
|
FieldInfo f = (FieldInfo)mi; |
|
|
buf.Append( " <member>\n" ); |
|
|
buf.Append( " <name>" ); |
|
|
buf.Append( f.Name ); |
|
|
buf.Append( "</name>\n" ); |
|
|
SerializeObject( f.GetValue(obj) ); |
|
|
buf.Append( " </member>\n" ); |
|
|
} |
|
|
} |
|
|
buf.Append( " </struct>\n" ); |
|
|
break; |
|
|
} |
|
|
buf.Append( " </value>\n" ); |
|
|
} |
|
|
catch(System.NullReferenceException) { |
|
|
// throw new XmlRpcNullReferenceException( |
|
|
// "Cannot serialize null reference"); |
|
|
} |
|
56 |
} |
} |
57 |
} |
} |
58 |
} |
} |