ExtensionManager.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System.Reflection;
  30. using System.Collections;
  31. using System.Web.Services.Configuration;
  32. using System.Xml.Serialization;
  33. using System.Xml;
  34. namespace System.Web.Services.Description
  35. {
  36. internal abstract class ExtensionManager
  37. {
  38. static Hashtable extensionsByName;
  39. static Hashtable extensionsByType;
  40. static ArrayList maps = new ArrayList ();
  41. static ArrayList extensions = new ArrayList ();
  42. static ExtensionManager ()
  43. {
  44. extensionsByName = new Hashtable ();
  45. extensionsByType = new Hashtable ();
  46. RegisterExtensionType (typeof (HttpAddressBinding));
  47. RegisterExtensionType (typeof (HttpBinding));
  48. RegisterExtensionType (typeof (HttpOperationBinding));
  49. RegisterExtensionType (typeof (HttpUrlEncodedBinding));
  50. RegisterExtensionType (typeof (HttpUrlReplacementBinding));
  51. RegisterExtensionType (typeof (MimeContentBinding));
  52. RegisterExtensionType (typeof (MimeMultipartRelatedBinding));
  53. RegisterExtensionType (typeof (MimeTextBinding));
  54. RegisterExtensionType (typeof (MimeXmlBinding));
  55. RegisterExtensionType (typeof (SoapAddressBinding));
  56. RegisterExtensionType (typeof (SoapBinding));
  57. RegisterExtensionType (typeof (SoapBodyBinding));
  58. RegisterExtensionType (typeof (SoapFaultBinding));
  59. RegisterExtensionType (typeof (SoapHeaderBinding));
  60. // RegisterExtensionType (typeof (SoapHeaderFaultBinding));
  61. RegisterExtensionType (typeof (SoapOperationBinding));
  62. foreach (Type type in WSConfig.Instance.FormatExtensionTypes)
  63. RegisterExtensionType (type);
  64. CreateExtensionSerializers ();
  65. }
  66. static void RegisterExtensionType (Type type)
  67. {
  68. ExtensionInfo ext = new ExtensionInfo();
  69. ext.Type = type;
  70. object[] ats = type.GetCustomAttributes (typeof(XmlFormatExtensionPrefixAttribute), true);
  71. foreach (XmlFormatExtensionPrefixAttribute at in ats)
  72. ext.NamespaceDeclarations.Add (new XmlQualifiedName (at.Prefix, at.Namespace));
  73. ats = type.GetCustomAttributes (typeof(XmlFormatExtensionAttribute), true);
  74. if (ats.Length > 0)
  75. {
  76. XmlFormatExtensionAttribute at = (XmlFormatExtensionAttribute)ats[0];
  77. ext.ElementName = at.ElementName;
  78. if (at.Namespace != null) ext.Namespace = at.Namespace;
  79. }
  80. XmlRootAttribute root = new XmlRootAttribute ();
  81. root.ElementName = ext.ElementName;
  82. if (ext.Namespace != null) root.Namespace = ext.Namespace;
  83. XmlReflectionImporter ri = new XmlReflectionImporter ();
  84. XmlTypeMapping map = ri.ImportTypeMapping (type, root);
  85. if (ext.ElementName == null) throw new InvalidOperationException ("XmlFormatExtensionAttribute must be applied to type " + type);
  86. extensionsByName.Add (ext.Namespace + " " + ext.ElementName, ext);
  87. extensionsByType.Add (type, ext);
  88. maps.Add (map);
  89. extensions.Add (ext);
  90. }
  91. static void CreateExtensionSerializers ()
  92. {
  93. XmlSerializer[] sers = XmlSerializer.FromMappings ((XmlMapping[]) maps.ToArray (typeof(XmlMapping)));
  94. for (int n=0; n<sers.Length; n++)
  95. ((ExtensionInfo)extensions[n]).Serializer = sers[n];
  96. maps = null;
  97. extensions = null;
  98. }
  99. public static ExtensionInfo GetFormatExtensionInfo (string elementName, string namesp)
  100. {
  101. return (ExtensionInfo) extensionsByName [namesp + " " + elementName];
  102. }
  103. public static ExtensionInfo GetFormatExtensionInfo (Type extType)
  104. {
  105. return (ExtensionInfo) extensionsByType [extType];
  106. }
  107. public static ICollection GetFormatExtensions ()
  108. {
  109. return extensionsByName.Values;
  110. }
  111. public static ServiceDescriptionFormatExtensionCollection GetExtensionPoint (object ob)
  112. {
  113. Type type = ob.GetType ();
  114. object[] ats = type.GetCustomAttributes (typeof(XmlFormatExtensionPointAttribute), true);
  115. if (ats.Length == 0) return null;
  116. XmlFormatExtensionPointAttribute at = (XmlFormatExtensionPointAttribute)ats[0];
  117. PropertyInfo prop = type.GetProperty (at.MemberName);
  118. if (prop != null)
  119. return prop.GetValue (ob, null) as ServiceDescriptionFormatExtensionCollection;
  120. else {
  121. FieldInfo field = type.GetField (at.MemberName);
  122. if (field != null)
  123. return field.GetValue (ob) as ServiceDescriptionFormatExtensionCollection;
  124. else
  125. throw new InvalidOperationException ("XmlFormatExtensionPointAttribute: Member " + at.MemberName + " not found");
  126. }
  127. }
  128. public static ArrayList BuildExtensionImporters ()
  129. {
  130. return BuildExtensionList (WSConfig.Instance.ExtensionImporterTypes);
  131. }
  132. public static ArrayList BuildExtensionReflectors ()
  133. {
  134. return BuildExtensionList (WSConfig.Instance.ExtensionReflectorTypes);
  135. }
  136. public static ArrayList BuildExtensionList (ArrayList exts)
  137. {
  138. ArrayList extensionTypes = new ArrayList ();
  139. if (exts != null)
  140. {
  141. foreach (WSExtensionConfig econf in exts)
  142. {
  143. bool added = false;
  144. for (int n=0; n<extensionTypes.Count && !added; n++)
  145. {
  146. WSExtensionConfig cureconf = (WSExtensionConfig) extensionTypes [n];
  147. if ((econf.Group < cureconf.Group) || ((econf.Group == cureconf.Group) && (econf.Priority < cureconf.Priority))) {
  148. extensionTypes.Insert (n, econf);
  149. added = true;
  150. }
  151. }
  152. if (!added) extensionTypes.Add (econf);
  153. }
  154. }
  155. ArrayList extensions = new ArrayList (extensionTypes.Count);
  156. foreach (WSExtensionConfig econf in extensionTypes)
  157. extensions.Add (Activator.CreateInstance (econf.Type));
  158. return extensions;
  159. }
  160. }
  161. internal class ExtensionInfo
  162. {
  163. ArrayList _namespaceDeclarations;
  164. string _namespace;
  165. string _elementName;
  166. Type _type;
  167. XmlSerializer _serializer;
  168. public ArrayList NamespaceDeclarations
  169. {
  170. get {
  171. if (_namespaceDeclarations == null) _namespaceDeclarations = new ArrayList ();
  172. return _namespaceDeclarations;
  173. }
  174. }
  175. public string Namespace
  176. {
  177. get { return _namespace; }
  178. set { _namespace = value; }
  179. }
  180. public string ElementName
  181. {
  182. get { return _elementName; }
  183. set { _elementName = value; }
  184. }
  185. public Type Type
  186. {
  187. get { return _type; }
  188. set { _type = value; }
  189. }
  190. public XmlSerializer Serializer
  191. {
  192. get { return _serializer; }
  193. set { _serializer = value; }
  194. }
  195. }
  196. }