2 // System.Web.Services.Description.ExtensionManager.cs
5 // Lluis Sanchez Gual (lluis@ximian.com)
7 // Copyright (C) 2003 Ximian, Inc.
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using System.Reflection;
32 using System.Collections;
33 using System.Web.Services.Configuration;
34 using System.Xml.Serialization;
37 namespace System.Web.Services.Description
39 internal abstract class ExtensionManager
41 static Hashtable extensionsByName;
42 static Hashtable extensionsByType;
43 static ArrayList maps = new ArrayList ();
44 static ArrayList extensions = new ArrayList ();
46 static ExtensionManager ()
48 extensionsByName = new Hashtable ();
49 extensionsByType = new Hashtable ();
51 RegisterExtensionType (typeof (HttpAddressBinding));
52 RegisterExtensionType (typeof (HttpBinding));
53 RegisterExtensionType (typeof (HttpOperationBinding));
54 RegisterExtensionType (typeof (HttpUrlEncodedBinding));
55 RegisterExtensionType (typeof (HttpUrlReplacementBinding));
56 RegisterExtensionType (typeof (MimeContentBinding));
57 RegisterExtensionType (typeof (MimeMultipartRelatedBinding));
58 RegisterExtensionType (typeof (MimeTextBinding));
59 RegisterExtensionType (typeof (MimeXmlBinding));
60 RegisterExtensionType (typeof (SoapAddressBinding));
61 RegisterExtensionType (typeof (SoapBinding));
62 RegisterExtensionType (typeof (SoapBodyBinding));
63 RegisterExtensionType (typeof (SoapFaultBinding));
64 RegisterExtensionType (typeof (SoapHeaderBinding));
65 // RegisterExtensionType (typeof (SoapHeaderFaultBinding));
66 RegisterExtensionType (typeof (SoapOperationBinding));
68 RegisterExtensionType (typeof (Soap12AddressBinding));
69 RegisterExtensionType (typeof (Soap12Binding));
70 RegisterExtensionType (typeof (Soap12BodyBinding));
71 RegisterExtensionType (typeof (Soap12FaultBinding));
72 RegisterExtensionType (typeof (Soap12HeaderBinding));
73 RegisterExtensionType (typeof (Soap12OperationBinding));
75 foreach (TypeElement el in WebServicesSection.Current.ServiceDescriptionFormatExtensionTypes)
76 RegisterExtensionType (el.Type);
78 foreach (Type type in WSConfig.Instance.FormatExtensionTypes)
79 RegisterExtensionType (type);
82 CreateExtensionSerializers ();
85 static void RegisterExtensionType (Type type)
87 ExtensionInfo ext = new ExtensionInfo();
90 object[] ats = type.GetCustomAttributes (typeof(XmlFormatExtensionPrefixAttribute), true);
92 foreach (XmlFormatExtensionPrefixAttribute at in ats)
93 ext.NamespaceDeclarations.Add (new XmlQualifiedName (at.Prefix, at.Namespace));
95 ats = type.GetCustomAttributes (typeof(XmlFormatExtensionAttribute), true);
98 XmlFormatExtensionAttribute at = (XmlFormatExtensionAttribute)ats[0];
99 ext.ElementName = at.ElementName;
100 if (at.Namespace != null) ext.Namespace = at.Namespace;
103 XmlRootAttribute root = new XmlRootAttribute ();
104 root.ElementName = ext.ElementName;
105 if (ext.Namespace != null) root.Namespace = ext.Namespace;
107 XmlReflectionImporter ri = new XmlReflectionImporter ();
108 XmlTypeMapping map = ri.ImportTypeMapping (type, root);
110 if (ext.ElementName == null) throw new InvalidOperationException ("XmlFormatExtensionAttribute must be applied to type " + type);
111 extensionsByName.Add (ext.Namespace + " " + ext.ElementName, ext);
112 extensionsByType.Add (type, ext);
115 extensions.Add (ext);
118 static void CreateExtensionSerializers ()
120 XmlSerializer[] sers = XmlSerializer.FromMappings ((XmlMapping[]) maps.ToArray (typeof(XmlMapping)));
121 for (int n=0; n<sers.Length; n++)
122 ((ExtensionInfo)extensions[n]).Serializer = sers[n];
128 public static ExtensionInfo GetFormatExtensionInfo (string elementName, string namesp)
130 return (ExtensionInfo) extensionsByName [namesp + " " + elementName];
133 public static ExtensionInfo GetFormatExtensionInfo (Type extType)
135 return (ExtensionInfo) extensionsByType [extType];
138 public static ICollection GetFormatExtensions ()
140 return extensionsByName.Values;
143 public static ServiceDescriptionFormatExtensionCollection GetExtensionPoint (object ob)
145 Type type = ob.GetType ();
146 object[] ats = type.GetCustomAttributes (typeof(XmlFormatExtensionPointAttribute), true);
147 if (ats.Length == 0) return null;
149 XmlFormatExtensionPointAttribute at = (XmlFormatExtensionPointAttribute)ats[0];
151 PropertyInfo prop = type.GetProperty (at.MemberName);
153 return prop.GetValue (ob, null) as ServiceDescriptionFormatExtensionCollection;
155 FieldInfo field = type.GetField (at.MemberName);
157 return field.GetValue (ob) as ServiceDescriptionFormatExtensionCollection;
159 throw new InvalidOperationException ("XmlFormatExtensionPointAttribute: Member " + at.MemberName + " not found");
163 public static ArrayList BuildExtensionImporters ()
166 return BuildExtensionList (WebServicesSection.Current.SoapExtensionImporterTypes);
168 return BuildExtensionList (WSConfig.Instance.ExtensionImporterTypes);
172 public static ArrayList BuildExtensionReflectors ()
175 return BuildExtensionList (WebServicesSection.Current.SoapExtensionReflectorTypes);
177 return BuildExtensionList (WSConfig.Instance.ExtensionReflectorTypes);
182 public static ArrayList BuildExtensionList (TypeElementCollection exts)
184 public static ArrayList BuildExtensionList (ArrayList exts)
187 ArrayList extensionTypes = new ArrayList ();
192 foreach (TypeElement econf in exts)
194 extensionTypes.Add (econf);
197 foreach (WSExtensionConfig econf in exts)
200 for (int n=0; n<extensionTypes.Count && !added; n++)
202 WSExtensionConfig cureconf = (WSExtensionConfig) extensionTypes [n];
204 if ((econf.Group < cureconf.Group) || ((econf.Group == cureconf.Group) && (econf.Priority < cureconf.Priority))) {
205 extensionTypes.Insert (n, econf);
209 if (!added) extensionTypes.Add (econf);
214 ArrayList extensions = new ArrayList (extensionTypes.Count);
216 foreach (TypeElement econf in extensionTypes)
218 foreach (WSExtensionConfig econf in extensionTypes)
220 extensions.Add (Activator.CreateInstance (econf.Type));
226 internal class ExtensionInfo
228 ArrayList _namespaceDeclarations;
232 XmlSerializer _serializer;
234 public ArrayList NamespaceDeclarations
237 if (_namespaceDeclarations == null) _namespaceDeclarations = new ArrayList ();
238 return _namespaceDeclarations;
242 public string Namespace
244 get { return _namespace; }
245 set { _namespace = value; }
248 public string ElementName
250 get { return _elementName; }
251 set { _elementName = value; }
256 get { return _type; }
257 set { _type = value; }
260 public XmlSerializer Serializer
262 get { return _serializer; }
263 set { _serializer = value; }