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