Fixed handling of modifier keys in KeyEventArgs constructor (bug #6707)
[mono.git] / mcs / class / System.Web.Services / System.Web.Services.Description / ExtensionManager.cs
1 // 
2 // System.Web.Services.Description.ExtensionManager.cs
3 //
4 // Author:
5 //   Lluis Sanchez Gual (lluis@ximian.com)
6 //
7 // Copyright (C) 2003 Ximian, Inc.
8 //
9
10 //
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:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
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.
29 //
30
31 using System.Reflection;
32 using System.Collections;
33 using System.Web.Services.Configuration;
34 using System.Xml.Serialization;
35 using System.Xml;
36
37 namespace System.Web.Services.Description 
38 {
39         internal abstract class ExtensionManager 
40         {
41                 static Hashtable extensionsByName;
42                 static Hashtable extensionsByType;
43                 static ArrayList maps = new ArrayList ();
44                 static ArrayList extensions = new ArrayList ();
45
46                 static ExtensionManager ()
47                 {
48                         extensionsByName = new Hashtable ();
49                         extensionsByType = new Hashtable ();
50
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));
67 #if NET_2_0
68                         RegisterExtensionType (typeof (Soap12AddressBinding));
69                         RegisterExtensionType (typeof (Soap12Binding));
70                         RegisterExtensionType (typeof (Soap12BodyBinding));
71                         RegisterExtensionType (typeof (Soap12FaultBinding));
72                         RegisterExtensionType (typeof (Soap12HeaderBinding));
73                         RegisterExtensionType (typeof (Soap12OperationBinding));
74 #endif
75
76 #if !MOBILE
77                         /*
78                          * Currently, the mobile profile has not support for
79                          * System.Configuration, so there are no external modules
80                          * defined
81                          */
82 #if NET_2_0 
83                         foreach (TypeElement el in WebServicesSection.Current.ServiceDescriptionFormatExtensionTypes)
84                                 RegisterExtensionType (el.Type);
85 #else
86                         foreach (Type type in WSConfig.Instance.FormatExtensionTypes)
87                                 RegisterExtensionType (type);
88 #endif
89 #endif
90                         CreateExtensionSerializers ();
91                 }
92         
93                 static void RegisterExtensionType (Type type)
94                 {
95                         ExtensionInfo ext = new ExtensionInfo();
96                         ext.Type = type;
97                         
98                         object[] ats = type.GetCustomAttributes (typeof(XmlFormatExtensionPrefixAttribute), true);
99                         
100                         foreach (XmlFormatExtensionPrefixAttribute at in ats)
101                                 ext.NamespaceDeclarations.Add (new XmlQualifiedName (at.Prefix, at.Namespace));
102                         
103                         ats = type.GetCustomAttributes (typeof(XmlFormatExtensionAttribute), true);
104                         if (ats.Length > 0)
105                         {
106                                 XmlFormatExtensionAttribute at = (XmlFormatExtensionAttribute)ats[0];
107                                 ext.ElementName = at.ElementName;
108                                 if (at.Namespace != null) ext.Namespace = at.Namespace;
109                         }
110
111                         XmlRootAttribute root = new XmlRootAttribute ();
112                         root.ElementName = ext.ElementName;
113                         if (ext.Namespace != null) root.Namespace = ext.Namespace;
114
115                         XmlReflectionImporter ri = new XmlReflectionImporter ();
116                         XmlTypeMapping map = ri.ImportTypeMapping (type, root);
117                         
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);
121                         
122                         maps.Add (map);
123                         extensions.Add (ext);
124                 }
125                 
126                 static void CreateExtensionSerializers ()
127                 {
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];
131                         
132                         maps = null;
133                         extensions = null;
134                 }
135                 
136                 public static ExtensionInfo GetFormatExtensionInfo (string elementName, string namesp)
137                 {
138                         return (ExtensionInfo) extensionsByName [namesp + " " + elementName];
139                 }
140                 
141                 public static ExtensionInfo GetFormatExtensionInfo (Type extType)
142                 {
143                         return (ExtensionInfo) extensionsByType [extType];
144                 }
145                 
146                 public static ICollection GetFormatExtensions ()
147                 {
148                         return extensionsByName.Values;
149                 }
150
151                 public static ServiceDescriptionFormatExtensionCollection GetExtensionPoint (object ob)
152                 {
153                         Type type = ob.GetType ();
154                         object[] ats = type.GetCustomAttributes (typeof(XmlFormatExtensionPointAttribute), true);
155                         if (ats.Length == 0) return null;
156
157                         XmlFormatExtensionPointAttribute at = (XmlFormatExtensionPointAttribute)ats[0];
158                         
159                         PropertyInfo prop = type.GetProperty (at.MemberName);
160                         if (prop != null)
161                                 return prop.GetValue (ob, null) as ServiceDescriptionFormatExtensionCollection;
162                         else {
163                                 FieldInfo field = type.GetField (at.MemberName);
164                                 if (field != null)
165                                         return field.GetValue (ob) as ServiceDescriptionFormatExtensionCollection;
166                                 else
167                                         throw new InvalidOperationException ("XmlFormatExtensionPointAttribute: Member " + at.MemberName + " not found");
168                         }
169                 }
170
171                 /*
172                  * The mobile profile lacks support for configuration
173                  */
174 #if MOBILE
175                 public static ArrayList BuildExtensionImporters ()
176                 {
177                         return new ArrayList (0);
178                 }
179                 
180                 public static ArrayList BuildExtensionReflectors ()
181                 {
182                         return new ArrayList (0);
183                 }
184
185 #else
186                 public static ArrayList BuildExtensionImporters ()
187                 {
188 #if NET_2_0
189                         return BuildExtensionList (WebServicesSection.Current.SoapExtensionImporterTypes);
190 #else
191                         return BuildExtensionList (WSConfig.Instance.ExtensionImporterTypes);
192 #endif
193                 }
194                 
195                 public static ArrayList BuildExtensionReflectors ()
196                 {
197 #if NET_2_0
198                         return BuildExtensionList (WebServicesSection.Current.SoapExtensionReflectorTypes);
199 #else
200                         return BuildExtensionList (WSConfig.Instance.ExtensionReflectorTypes);
201 #endif
202                 }
203
204 #if NET_2_0
205                 public static ArrayList BuildExtensionList (TypeElementCollection exts)
206 #else
207                 public static ArrayList BuildExtensionList (ArrayList exts)
208 #endif
209                 {
210                         ArrayList extensionTypes = new ArrayList ();
211                         
212                         if (exts != null)
213                         {
214 #if NET_2_0 
215                                 foreach (TypeElement econf in exts)
216                                 {
217                                         extensionTypes.Add (econf);
218                                 }
219 #else
220                                 foreach (WSExtensionConfig econf in exts)
221                                 {
222                                         bool added = false;
223                                         for (int n=0; n<extensionTypes.Count && !added; n++)
224                                         {
225                                                 WSExtensionConfig cureconf = (WSExtensionConfig) extensionTypes [n];
226         
227                                                 if ((econf.Group < cureconf.Group) || ((econf.Group == cureconf.Group) && (econf.Priority < cureconf.Priority))) {
228                                                         extensionTypes.Insert (n, econf);
229                                                         added = true;
230                                                 }
231                                         }
232                                         if (!added) extensionTypes.Add (econf);
233                                 }
234 #endif
235                         }
236
237                         ArrayList extensions = new ArrayList (extensionTypes.Count);
238 #if NET_2_0
239                         foreach (TypeElement econf in extensionTypes)
240 #else
241                         foreach (WSExtensionConfig econf in extensionTypes)
242 #endif
243                                 extensions.Add (Activator.CreateInstance (econf.Type));
244                                 
245                         return extensions;
246                 }
247 #endif
248         }
249         
250         internal class ExtensionInfo
251         {
252                 ArrayList _namespaceDeclarations;
253                 string _namespace;
254                 string _elementName;
255                 Type _type;
256                 XmlSerializer _serializer;
257
258                 public ArrayList NamespaceDeclarations
259                 {
260                         get { 
261                                 if (_namespaceDeclarations == null) _namespaceDeclarations = new ArrayList ();
262                                 return _namespaceDeclarations; 
263                         }
264                 }
265                 
266                 public string Namespace
267                 {
268                         get { return _namespace; }
269                         set { _namespace = value; }
270                 }
271                 
272                 public string ElementName
273                 {
274                         get { return _elementName; }
275                         set { _elementName = value; }
276                 }
277                 
278                 public Type Type
279                 {
280                         get { return _type; }
281                         set { _type = value; }
282                 }
283                 
284                 public XmlSerializer Serializer
285                 {
286                         get { return _serializer; }
287                         set { _serializer = value; }
288                 }               
289         }
290 }