| 1 |
// Copyright 2006 Alp Toker <[email protected]> |
| 2 |
// This software is made available under the MIT License |
| 3 |
// See COPYING for details |
| 4 |
|
| 5 |
using System; |
| 6 |
using System.Collections.Generic; |
| 7 |
using System.IO; |
| 8 |
using System.Xml; |
| 9 |
using System.Text; |
| 10 |
using System.Reflection; |
| 11 |
|
| 12 |
namespace NDesk.DBus |
| 13 |
{ |
| 14 |
//TODO: complete this class |
| 15 |
class Introspector |
| 16 |
{ |
| 17 |
const string NAMESPACE = "http://www.freedesktop.org/standards/dbus"; |
| 18 |
const string PUBLIC_IDENTIFIER = "-//freedesktop//DTD D-BUS Object Introspection 1.0//EN"; |
| 19 |
const string SYSTEM_IDENTIFIER = "http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd"; |
| 20 |
|
| 21 |
public StringBuilder sb; |
| 22 |
public string xml; |
| 23 |
public ObjectPath root_path = ObjectPath.Root; |
| 24 |
|
| 25 |
protected XmlWriter writer; |
| 26 |
|
| 27 |
public Introspector () |
| 28 |
{ |
| 29 |
XmlWriterSettings settings = new XmlWriterSettings (); |
| 30 |
settings.Indent = true; |
| 31 |
settings.IndentChars = (" "); |
| 32 |
settings.OmitXmlDeclaration = true; |
| 33 |
|
| 34 |
sb = new StringBuilder (); |
| 35 |
|
| 36 |
writer = XmlWriter.Create (sb, settings); |
| 37 |
} |
| 38 |
|
| 39 |
static string GetProductDescription () |
| 40 |
{ |
| 41 |
String version; |
| 42 |
|
| 43 |
Assembly assembly = Assembly.GetExecutingAssembly (); |
| 44 |
AssemblyName aname = assembly.GetName (); |
| 45 |
|
| 46 |
AssemblyInformationalVersionAttribute iversion = Attribute.GetCustomAttribute (assembly, typeof (AssemblyInformationalVersionAttribute)) as AssemblyInformationalVersionAttribute; |
| 47 |
|
| 48 |
if (iversion != null) |
| 49 |
version = iversion.InformationalVersion; |
| 50 |
else |
| 51 |
version = aname.Version.ToString (); |
| 52 |
|
| 53 |
return aname.Name + " " + version; |
| 54 |
} |
| 55 |
|
| 56 |
public void WriteStart () |
| 57 |
{ |
| 58 |
writer.WriteDocType ("node", PUBLIC_IDENTIFIER, SYSTEM_IDENTIFIER, null); |
| 59 |
|
| 60 |
writer.WriteComment (" " + GetProductDescription () + " "); |
| 61 |
|
| 62 |
//the root node element |
| 63 |
writer.WriteStartElement ("node"); |
| 64 |
} |
| 65 |
|
| 66 |
public void WriteNode (string name) |
| 67 |
{ |
| 68 |
writer.WriteStartElement ("node"); |
| 69 |
writer.WriteAttributeString ("name", name); |
| 70 |
writer.WriteEndElement (); |
| 71 |
} |
| 72 |
|
| 73 |
public void WriteEnd () |
| 74 |
{ |
| 75 |
/* |
| 76 |
WriteEnum (typeof (org.freedesktop.DBus.NameFlag)); |
| 77 |
WriteEnum (typeof (org.freedesktop.DBus.NameReply)); |
| 78 |
WriteEnum (typeof (org.freedesktop.DBus.ReleaseNameReply)); |
| 79 |
WriteEnum (typeof (org.freedesktop.DBus.StartReply)); |
| 80 |
WriteInterface (typeof (org.freedesktop.DBus.IBus)); |
| 81 |
*/ |
| 82 |
|
| 83 |
writer.WriteEndElement (); |
| 84 |
|
| 85 |
writer.Flush (); |
| 86 |
xml = sb.ToString (); |
| 87 |
} |
| 88 |
|
| 89 |
//public void WriteNode () |
| 90 |
public void WriteType (Type target_type) |
| 91 |
{ |
| 92 |
//writer.WriteStartElement ("node"); |
| 93 |
|
| 94 |
//TODO: non-well-known introspection has paths as well, which we don't do yet. read the spec again |
| 95 |
//hackishly just remove the root '/' to make the path relative for now |
| 96 |
//writer.WriteAttributeString ("name", target_path.Value.Substring (1)); |
| 97 |
//writer.WriteAttributeString ("name", "test"); |
| 98 |
|
| 99 |
//reflect our own interface manually |
| 100 |
WriteInterface (typeof (org.freedesktop.DBus.Introspectable)); |
| 101 |
|
| 102 |
//reflect the target interface |
| 103 |
if (target_type != null) { |
| 104 |
WriteInterface (target_type); |
| 105 |
|
| 106 |
foreach (Type ifType in target_type.GetInterfaces ()) |
| 107 |
WriteInterface (ifType); |
| 108 |
} |
| 109 |
|
| 110 |
//TODO: review recursion of interfaces and inheritance hierarchy |
| 111 |
|
| 112 |
//writer.WriteEndElement (); |
| 113 |
} |
| 114 |
|
| 115 |
public void WriteArg (ParameterInfo pi) |
| 116 |
{ |
| 117 |
WriteArg (pi.ParameterType, Mapper.GetArgumentName (pi), pi.IsOut, false); |
| 118 |
} |
| 119 |
|
| 120 |
public void WriteArgReverse (ParameterInfo pi) |
| 121 |
{ |
| 122 |
WriteArg (pi.ParameterType, Mapper.GetArgumentName (pi), pi.IsOut, true); |
| 123 |
} |
| 124 |
|
| 125 |
//TODO: clean up and get rid of reverse (or argIsOut) parm |
| 126 |
public void WriteArg (Type argType, string argName, bool argIsOut, bool reverse) |
| 127 |
{ |
| 128 |
argType = argIsOut ? argType.GetElementType () : argType; |
| 129 |
if (argType == typeof (void)) |
| 130 |
return; |
| 131 |
|
| 132 |
writer.WriteStartElement ("arg"); |
| 133 |
|
| 134 |
if (!String.IsNullOrEmpty (argName)) |
| 135 |
writer.WriteAttributeString ("name", argName); |
| 136 |
|
| 137 |
//we can't rely on the default direction (qt-dbus requires a direction at time of writing), so we use a boolean to reverse the parameter direction and make it explicit |
| 138 |
|
| 139 |
if (argIsOut) |
| 140 |
writer.WriteAttributeString ("direction", !reverse ? "out" : "in"); |
| 141 |
else |
| 142 |
writer.WriteAttributeString ("direction", !reverse ? "in" : "out"); |
| 143 |
|
| 144 |
Signature sig = Signature.GetSig (argType); |
| 145 |
|
| 146 |
//TODO: avoid writing null (DType.Invalid) to the XML stream |
| 147 |
writer.WriteAttributeString ("type", sig.Value); |
| 148 |
|
| 149 |
//annotations aren't valid in an arg element, so this is disabled |
| 150 |
//if (argType.IsEnum) |
| 151 |
// WriteAnnotation ("org.ndesk.DBus.Enum", Mapper.GetInterfaceName (argType)); |
| 152 |
|
| 153 |
writer.WriteEndElement (); |
| 154 |
} |
| 155 |
|
| 156 |
public void WriteMethod (MethodInfo mi) |
| 157 |
{ |
| 158 |
writer.WriteStartElement ("method"); |
| 159 |
writer.WriteAttributeString ("name", mi.Name); |
| 160 |
|
| 161 |
foreach (ParameterInfo pi in mi.GetParameters ()) |
| 162 |
WriteArg (pi); |
| 163 |
|
| 164 |
//Mono <= 1.1.13 doesn't support MethodInfo.ReturnParameter, so avoid it |
| 165 |
//WriteArgReverse (mi.ReturnParameter); |
| 166 |
WriteArg (mi.ReturnType, Mapper.GetArgumentName (mi.ReturnTypeCustomAttributes, "ret"), false, true); |
| 167 |
|
| 168 |
WriteAnnotations (mi); |
| 169 |
|
| 170 |
writer.WriteEndElement (); |
| 171 |
} |
| 172 |
|
| 173 |
public void WriteProperty (PropertyInfo pri) |
| 174 |
{ |
| 175 |
//expose properties as dbus properties |
| 176 |
writer.WriteStartElement ("property"); |
| 177 |
writer.WriteAttributeString ("name", pri.Name); |
| 178 |
writer.WriteAttributeString ("type", Signature.GetSig (pri.PropertyType).Value); |
| 179 |
string access = (pri.CanRead ? "read" : String.Empty) + (pri.CanWrite ? "write" : String.Empty); |
| 180 |
writer.WriteAttributeString ("access", access); |
| 181 |
WriteAnnotations (pri); |
| 182 |
writer.WriteEndElement (); |
| 183 |
|
| 184 |
//expose properties as methods also |
| 185 |
//it may not be worth doing this in the long run |
| 186 |
/* |
| 187 |
if (pri.CanRead) { |
| 188 |
writer.WriteStartElement ("method"); |
| 189 |
writer.WriteAttributeString ("name", "Get" + pri.Name); |
| 190 |
WriteArgReverse (pri.GetGetMethod ().ReturnParameter); |
| 191 |
writer.WriteEndElement (); |
| 192 |
} |
| 193 |
|
| 194 |
if (pri.CanWrite) { |
| 195 |
writer.WriteStartElement ("method"); |
| 196 |
writer.WriteAttributeString ("name", "Set" + pri.Name); |
| 197 |
foreach (ParameterInfo pi in pri.GetSetMethod ().GetParameters ()) |
| 198 |
WriteArg (pi); |
| 199 |
writer.WriteEndElement (); |
| 200 |
} |
| 201 |
*/ |
| 202 |
} |
| 203 |
|
| 204 |
public void WriteSignal (EventInfo ei) |
| 205 |
{ |
| 206 |
writer.WriteStartElement ("signal"); |
| 207 |
writer.WriteAttributeString ("name", ei.Name); |
| 208 |
|
| 209 |
foreach (ParameterInfo pi in ei.EventHandlerType.GetMethod ("Invoke").GetParameters ()) |
| 210 |
WriteArgReverse (pi); |
| 211 |
|
| 212 |
WriteAnnotations (ei); |
| 213 |
|
| 214 |
//no need to consider the delegate return value as dbus doesn't support it |
| 215 |
writer.WriteEndElement (); |
| 216 |
} |
| 217 |
|
| 218 |
const BindingFlags relevantBindingFlags = BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly; |
| 219 |
|
| 220 |
public void WriteInterface (Type type) |
| 221 |
{ |
| 222 |
if (type == null) |
| 223 |
return; |
| 224 |
|
| 225 |
//TODO: this is unreliable, fix it |
| 226 |
if (!Mapper.IsPublic (type)) |
| 227 |
return; |
| 228 |
|
| 229 |
writer.WriteStartElement ("interface"); |
| 230 |
|
| 231 |
writer.WriteAttributeString ("name", Mapper.GetInterfaceName (type)); |
| 232 |
|
| 233 |
/* |
| 234 |
foreach (MemberInfo mbi in type.GetMembers (relevantBindingFlags)) { |
| 235 |
switch (mbi.MemberType) { |
| 236 |
case MemberTypes.Method: |
| 237 |
if (!((MethodInfo)mbi).IsSpecialName) |
| 238 |
WriteMethod ((MethodInfo)mbi); |
| 239 |
break; |
| 240 |
case MemberTypes.Event: |
| 241 |
WriteSignal ((EventInfo)mbi); |
| 242 |
break; |
| 243 |
case MemberTypes.Property: |
| 244 |
WriteProperty ((PropertyInfo)mbi); |
| 245 |
break; |
| 246 |
default: |
| 247 |
Console.Error.WriteLine ("Warning: Unhandled MemberType '{0}' encountered while introspecting {1}", mbi.MemberType, type.FullName); |
| 248 |
break; |
| 249 |
} |
| 250 |
} |
| 251 |
*/ |
| 252 |
|
| 253 |
foreach (MethodInfo mi in type.GetMethods (relevantBindingFlags)) |
| 254 |
if (!mi.IsSpecialName) |
| 255 |
WriteMethod (mi); |
| 256 |
|
| 257 |
foreach (EventInfo ei in type.GetEvents (relevantBindingFlags)) |
| 258 |
WriteSignal (ei); |
| 259 |
|
| 260 |
foreach (PropertyInfo pri in type.GetProperties (relevantBindingFlags)) |
| 261 |
WriteProperty (pri); |
| 262 |
|
| 263 |
//TODO: indexers |
| 264 |
|
| 265 |
//TODO: attributes as annotations? |
| 266 |
|
| 267 |
writer.WriteEndElement (); |
| 268 |
|
| 269 |
//this recursion seems somewhat inelegant |
| 270 |
WriteInterface (type.BaseType); |
| 271 |
} |
| 272 |
|
| 273 |
public void WriteAnnotations (ICustomAttributeProvider attrProvider) |
| 274 |
{ |
| 275 |
if (Mapper.IsDeprecated (attrProvider)) |
| 276 |
WriteAnnotation ("org.freedesktop.DBus.Deprecated", "true"); |
| 277 |
} |
| 278 |
|
| 279 |
public void WriteAnnotation (string name, string value) |
| 280 |
{ |
| 281 |
writer.WriteStartElement ("annotation"); |
| 282 |
|
| 283 |
writer.WriteAttributeString ("name", name); |
| 284 |
writer.WriteAttributeString ("value", value); |
| 285 |
|
| 286 |
writer.WriteEndElement (); |
| 287 |
} |
| 288 |
|
| 289 |
//this is not in the spec, and is not finalized |
| 290 |
public void WriteEnum (Type type) |
| 291 |
{ |
| 292 |
writer.WriteStartElement ("enum"); |
| 293 |
writer.WriteAttributeString ("name", Mapper.GetInterfaceName (type)); |
| 294 |
writer.WriteAttributeString ("type", Signature.GetSig (type.GetElementType ()).Value); |
| 295 |
writer.WriteAttributeString ("flags", (type.IsDefined (typeof (FlagsAttribute), false)) ? "true" : "false"); |
| 296 |
|
| 297 |
string[] names = Enum.GetNames (type); |
| 298 |
|
| 299 |
int i = 0; |
| 300 |
foreach (Enum val in Enum.GetValues (type)) { |
| 301 |
writer.WriteStartElement ("element"); |
| 302 |
writer.WriteAttributeString ("name", names[i++]); |
| 303 |
writer.WriteAttributeString ("value", val.ToString ("d")); |
| 304 |
writer.WriteEndElement (); |
| 305 |
} |
| 306 |
|
| 307 |
writer.WriteEndElement (); |
| 308 |
} |
| 309 |
} |
| 310 |
} |