ExtensionManager.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. //
  2. // System.Web.Services.Description.ExtensionManager.cs
  3. //
  4. // Author:
  5. // Lluis Sanchez Gual ([email protected])
  6. //
  7. // Copyright (C) 2003 Ximian, Inc.
  8. //
  9. using System.Reflection;
  10. using System.Collections;
  11. using System.Web.Services.Configuration;
  12. using System.Xml.Serialization;
  13. namespace System.Web.Services.Description
  14. {
  15. internal abstract class ExtensionManager
  16. {
  17. static Hashtable extensionsByName;
  18. static Hashtable extensionsByType;
  19. static ExtensionManager ()
  20. {
  21. extensionsByName = new Hashtable ();
  22. extensionsByType = new Hashtable ();
  23. RegisterExtensionType (typeof (HttpAddressBinding));
  24. RegisterExtensionType (typeof (HttpBinding));
  25. RegisterExtensionType (typeof (HttpOperationBinding));
  26. RegisterExtensionType (typeof (HttpUrlEncodedBinding));
  27. RegisterExtensionType (typeof (HttpUrlReplacementBinding));
  28. RegisterExtensionType (typeof (MimeContentBinding));
  29. RegisterExtensionType (typeof (MimeMultipartRelatedBinding));
  30. RegisterExtensionType (typeof (MimeTextBinding));
  31. RegisterExtensionType (typeof (MimeXmlBinding));
  32. RegisterExtensionType (typeof (SoapAddressBinding));
  33. RegisterExtensionType (typeof (SoapBinding));
  34. RegisterExtensionType (typeof (SoapBodyBinding));
  35. RegisterExtensionType (typeof (SoapFaultBinding));
  36. RegisterExtensionType (typeof (SoapHeaderBinding));
  37. RegisterExtensionType (typeof (SoapHeaderFaultBinding));
  38. RegisterExtensionType (typeof (SoapOperationBinding));
  39. foreach (Type type in WSConfig.Instance.FormatExtensionTypes)
  40. RegisterExtensionType (type);
  41. }
  42. public static void RegisterExtensionType (Type type)
  43. {
  44. ExtensionInfo ext = new ExtensionInfo();
  45. ext.Type = type;
  46. object[] ats = type.GetCustomAttributes (typeof(XmlFormatExtensionPrefixAttribute), true);
  47. if (ats.Length > 0)
  48. {
  49. XmlFormatExtensionPrefixAttribute at = (XmlFormatExtensionPrefixAttribute)ats[0];
  50. ext.Prefix = at.Prefix;
  51. ext.Namespace = at.Namespace;
  52. }
  53. ats = type.GetCustomAttributes (typeof(XmlFormatExtensionAttribute), true);
  54. if (ats.Length > 0)
  55. {
  56. XmlFormatExtensionAttribute at = (XmlFormatExtensionAttribute)ats[0];
  57. ext.ElementName = at.ElementName;
  58. if (at.Namespace != null) ext.Namespace = at.Namespace;
  59. }
  60. XmlRootAttribute root = new XmlRootAttribute ();
  61. root.ElementName = ext.ElementName;
  62. if (ext.Namespace != null) root.Namespace = ext.Namespace;
  63. XmlReflectionImporter ri = new XmlReflectionImporter ();
  64. XmlTypeMapping map = ri.ImportTypeMapping (type, root);
  65. // TODO: use array method to create the serializers
  66. ext.Serializer = new XmlSerializer (map);
  67. if (ext.ElementName == null) throw new InvalidOperationException ("XmlFormatExtensionAttribute must be applied to type " + type);
  68. extensionsByName.Add (ext.Namespace + " " + ext.ElementName, ext);
  69. extensionsByType.Add (type, ext);
  70. }
  71. public static ExtensionInfo GetFormatExtensionInfo (string elementName, string namesp)
  72. {
  73. return (ExtensionInfo) extensionsByName [namesp + " " + elementName];
  74. }
  75. public static ExtensionInfo GetFormatExtensionInfo (Type extType)
  76. {
  77. return (ExtensionInfo) extensionsByType [extType];
  78. }
  79. public static ICollection GetFormatExtensions ()
  80. {
  81. return extensionsByName.Values;
  82. }
  83. public static ServiceDescriptionFormatExtensionCollection GetExtensionPoint (object ob)
  84. {
  85. Type type = ob.GetType ();
  86. object[] ats = type.GetCustomAttributes (typeof(XmlFormatExtensionPointAttribute), true);
  87. if (ats.Length == 0) return null;
  88. XmlFormatExtensionPointAttribute at = (XmlFormatExtensionPointAttribute)ats[0];
  89. PropertyInfo prop = type.GetProperty (at.MemberName);
  90. if (prop != null)
  91. return prop.GetValue (ob, null) as ServiceDescriptionFormatExtensionCollection;
  92. else {
  93. FieldInfo field = type.GetField (at.MemberName);
  94. if (field != null)
  95. return field.GetValue (ob) as ServiceDescriptionFormatExtensionCollection;
  96. else
  97. throw new InvalidOperationException ("XmlFormatExtensionPointAttribute: Member " + at.MemberName + " not found");
  98. }
  99. }
  100. public static ArrayList BuildExtensionImporters ()
  101. {
  102. return BuildExtensionList (WSConfig.Instance.ExtensionImporterTypes);
  103. }
  104. public static ArrayList BuildExtensionReflectors ()
  105. {
  106. return BuildExtensionList (WSConfig.Instance.ExtensionReflectorTypes);
  107. }
  108. public static ArrayList BuildExtensionList (ArrayList exts)
  109. {
  110. ArrayList extensionTypes = new ArrayList ();
  111. if (exts != null)
  112. {
  113. foreach (WSExtensionConfig econf in exts)
  114. {
  115. bool added = false;
  116. for (int n=0; n<extensionTypes.Count && !added; n++)
  117. {
  118. WSExtensionConfig cureconf = (WSExtensionConfig) extensionTypes [n];
  119. if ((econf.Group < cureconf.Group) || ((econf.Group == cureconf.Group) && (econf.Priority < cureconf.Priority))) {
  120. extensionTypes.Insert (n, econf);
  121. added = true;
  122. }
  123. }
  124. if (!added) extensionTypes.Add (econf);
  125. }
  126. }
  127. ArrayList extensions = new ArrayList (extensionTypes.Count);
  128. foreach (WSExtensionConfig econf in extensionTypes)
  129. extensions.Add (Activator.CreateInstance (econf.Type));
  130. return extensions;
  131. }
  132. }
  133. internal class ExtensionInfo
  134. {
  135. public string _prefix;
  136. public string _namespace;
  137. public string _elementName;
  138. public Type _type;
  139. public XmlSerializer _serializer;
  140. public string Prefix
  141. {
  142. get { return _prefix; }
  143. set { _prefix = value; }
  144. }
  145. public string Namespace
  146. {
  147. get { return _namespace; }
  148. set { _namespace = value; }
  149. }
  150. public string ElementName
  151. {
  152. get { return _elementName; }
  153. set { _elementName = value; }
  154. }
  155. public Type Type
  156. {
  157. get { return _type; }
  158. set { _type = value; }
  159. }
  160. public XmlSerializer Serializer
  161. {
  162. get { return _serializer; }
  163. set { _serializer = value; }
  164. }
  165. }
  166. }