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));
78 * Currently, the mobile profile has not support for
79 * System.Configuration, so there are no external modules
83 foreach (TypeElement el in WebServicesSection.Current.ServiceDescriptionFormatExtensionTypes)
84 RegisterExtensionType (el.Type);
86 foreach (Type type in WSConfig.Instance.FormatExtensionTypes)
87 RegisterExtensionType (type);
90 CreateExtensionSerializers ();
93 static void RegisterExtensionType (Type type)
95 ExtensionInfo ext = new ExtensionInfo();
98 object[] ats = type.GetCustomAttributes (typeof(XmlFormatExtensionPrefixAttribute), true);
100 foreach (XmlFormatExtensionPrefixAttribute at in ats)
101 ext.NamespaceDeclarations.Add (new XmlQualifiedName (at.Prefix, at.Namespace));
103 ats = type.GetCustomAttributes (typeof(XmlFormatExtensionAttribute), true);
106 XmlFormatExtensionAttribute at = (XmlFormatExtensionAttribute)ats[0];
107 ext.ElementName = at.ElementName;
108 if (at.Namespace != null) ext.Namespace = at.Namespace;
111 XmlRootAttribute root = new XmlRootAttribute ();
112 root.ElementName = ext.ElementName;
113 if (ext.Namespace != null) root.Namespace = ext.Namespace;
115 XmlReflectionImporter ri = new XmlReflectionImporter ();
116 XmlTypeMapping map = ri.ImportTypeMapping (type, root);
118 if (ext.ElementName == null) throw new InvalidOperationException ("XmlFormatExtensionAttribute must be applied to type " + type);
119 extensionsByName.Add (ext.Namespace + " " + ext.ElementName, ext);
120 extensionsByType.Add (type, ext);
123 extensions.Add (ext);
126 static void CreateExtensionSerializers ()
128 XmlSerializer[] sers = XmlSerializer.FromMappings ((XmlMapping[]) maps.ToArray (typeof(XmlMapping)));
129 for (int n=0; n<sers.Length; n++)
130 ((ExtensionInfo)extensions[n]).Serializer = sers[n];
136 public static ExtensionInfo GetFormatExtensionInfo (string elementName, string namesp)
138 return (ExtensionInfo) extensionsByName [namesp + " " + elementName];
141 public static ExtensionInfo GetFormatExtensionInfo (Type extType)
143 return (ExtensionInfo) extensionsByType [extType];
146 public static ICollection GetFormatExtensions ()
148 return extensionsByName.Values;
151 public static ServiceDescriptionFormatExtensionCollection GetExtensionPoint (object ob)
153 Type type = ob.GetType ();
154 object[] ats = type.GetCustomAttributes (typeof(XmlFormatExtensionPointAttribute), true);
155 if (ats.Length == 0) return null;
157 XmlFormatExtensionPointAttribute at = (XmlFormatExtensionPointAttribute)ats[0];
159 PropertyInfo prop = type.GetProperty (at.MemberName);
161 return prop.GetValue (ob, null) as ServiceDescriptionFormatExtensionCollection;
163 FieldInfo field = type.GetField (at.MemberName);
165 return field.GetValue (ob) as ServiceDescriptionFormatExtensionCollection;
167 throw new InvalidOperationException ("XmlFormatExtensionPointAttribute: Member " + at.MemberName + " not found");
172 * The mobile profile lacks support for configuration
175 public static ArrayList BuildExtensionImporters ()
177 return new ArrayList (0);
180 public static ArrayList BuildExtensionReflectors ()
182 return new ArrayList (0);
186 public static ArrayList BuildExtensionImporters ()
189 return BuildExtensionList (WebServicesSection.Current.SoapExtensionImporterTypes);
191 return BuildExtensionList (WSConfig.Instance.ExtensionImporterTypes);
195 public static ArrayList BuildExtensionReflectors ()
198 return BuildExtensionList (WebServicesSection.Current.SoapExtensionReflectorTypes);
200 return BuildExtensionList (WSConfig.Instance.ExtensionReflectorTypes);
205 public static ArrayList BuildExtensionList (TypeElementCollection exts)
207 public static ArrayList BuildExtensionList (ArrayList exts)
210 ArrayList extensionTypes = new ArrayList ();
215 foreach (TypeElement econf in exts)
217 extensionTypes.Add (econf);
220 foreach (WSExtensionConfig econf in exts)
223 for (int n=0; n<extensionTypes.Count && !added; n++)
225 WSExtensionConfig cureconf = (WSExtensionConfig) extensionTypes [n];
227 if ((econf.Group < cureconf.Group) || ((econf.Group == cureconf.Group) && (econf.Priority < cureconf.Priority))) {
228 extensionTypes.Insert (n, econf);
232 if (!added) extensionTypes.Add (econf);
237 ArrayList extensions = new ArrayList (extensionTypes.Count);
239 foreach (TypeElement econf in extensionTypes)
241 foreach (WSExtensionConfig econf in extensionTypes)
243 extensions.Add (Activator.CreateInstance (econf.Type));
250 internal class ExtensionInfo
252 ArrayList _namespaceDeclarations;
256 XmlSerializer _serializer;
258 public ArrayList NamespaceDeclarations
261 if (_namespaceDeclarations == null) _namespaceDeclarations = new ArrayList ();
262 return _namespaceDeclarations;
266 public string Namespace
268 get { return _namespace; }
269 set { _namespace = value; }
272 public string ElementName
274 get { return _elementName; }
275 set { _elementName = value; }
280 get { return _type; }
281 set { _type = value; }
284 public XmlSerializer Serializer
286 get { return _serializer; }
287 set { _serializer = value; }