1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
|
//
// System.Web.Services.Description.ExtensionManager.cs
//
// Author:
// Lluis Sanchez Gual (lluis@ximian.com)
//
// Copyright (C) 2003 Ximian, Inc.
//
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System.Reflection;
using System.Collections;
using System.Web.Services.Configuration;
using System.Xml.Serialization;
using System.Xml;
namespace System.Web.Services.Description
{
internal abstract class ExtensionManager
{
static Hashtable extensionsByName;
static Hashtable extensionsByType;
static ArrayList maps = new ArrayList ();
static ArrayList extensions = new ArrayList ();
static ExtensionManager ()
{
extensionsByName = new Hashtable ();
extensionsByType = new Hashtable ();
RegisterExtensionType (typeof (HttpAddressBinding));
RegisterExtensionType (typeof (HttpBinding));
RegisterExtensionType (typeof (HttpOperationBinding));
RegisterExtensionType (typeof (HttpUrlEncodedBinding));
RegisterExtensionType (typeof (HttpUrlReplacementBinding));
RegisterExtensionType (typeof (MimeContentBinding));
RegisterExtensionType (typeof (MimeMultipartRelatedBinding));
RegisterExtensionType (typeof (MimeTextBinding));
RegisterExtensionType (typeof (MimeXmlBinding));
RegisterExtensionType (typeof (SoapAddressBinding));
RegisterExtensionType (typeof (SoapBinding));
RegisterExtensionType (typeof (SoapBodyBinding));
RegisterExtensionType (typeof (SoapFaultBinding));
RegisterExtensionType (typeof (SoapHeaderBinding));
// RegisterExtensionType (typeof (SoapHeaderFaultBinding));
RegisterExtensionType (typeof (SoapOperationBinding));
RegisterExtensionType (typeof (Soap12AddressBinding));
RegisterExtensionType (typeof (Soap12Binding));
RegisterExtensionType (typeof (Soap12BodyBinding));
RegisterExtensionType (typeof (Soap12FaultBinding));
RegisterExtensionType (typeof (Soap12HeaderBinding));
RegisterExtensionType (typeof (Soap12OperationBinding));
#if !MOBILE && !XAMMAC_4_5
/*
* Currently, the mobile profile has not support for
* System.Configuration, so there are no external modules
* defined
*/
foreach (TypeElement el in WebServicesSection.Current.ServiceDescriptionFormatExtensionTypes)
RegisterExtensionType (el.Type);
#endif
CreateExtensionSerializers ();
}
static void RegisterExtensionType (Type type)
{
ExtensionInfo ext = new ExtensionInfo();
ext.Type = type;
object[] ats = type.GetCustomAttributes (typeof(XmlFormatExtensionPrefixAttribute), true);
foreach (XmlFormatExtensionPrefixAttribute at in ats)
ext.NamespaceDeclarations.Add (new XmlQualifiedName (at.Prefix, at.Namespace));
ats = type.GetCustomAttributes (typeof(XmlFormatExtensionAttribute), true);
if (ats.Length > 0)
{
XmlFormatExtensionAttribute at = (XmlFormatExtensionAttribute)ats[0];
ext.ElementName = at.ElementName;
if (at.Namespace != null) ext.Namespace = at.Namespace;
}
XmlRootAttribute root = new XmlRootAttribute ();
root.ElementName = ext.ElementName;
if (ext.Namespace != null) root.Namespace = ext.Namespace;
XmlReflectionImporter ri = new XmlReflectionImporter ();
XmlTypeMapping map = ri.ImportTypeMapping (type, root);
if (ext.ElementName == null) throw new InvalidOperationException ("XmlFormatExtensionAttribute must be applied to type " + type);
extensionsByName.Add (ext.Namespace + " " + ext.ElementName, ext);
extensionsByType.Add (type, ext);
maps.Add (map);
extensions.Add (ext);
}
static void CreateExtensionSerializers ()
{
XmlSerializer[] sers = XmlSerializer.FromMappings ((XmlMapping[]) maps.ToArray (typeof(XmlMapping)));
for (int n=0; n<sers.Length; n++)
((ExtensionInfo)extensions[n]).Serializer = sers[n];
maps = null;
extensions = null;
}
public static ExtensionInfo GetFormatExtensionInfo (string elementName, string namesp)
{
return (ExtensionInfo) extensionsByName [namesp + " " + elementName];
}
public static ExtensionInfo GetFormatExtensionInfo (Type extType)
{
return (ExtensionInfo) extensionsByType [extType];
}
public static ICollection GetFormatExtensions ()
{
return extensionsByName.Values;
}
public static ServiceDescriptionFormatExtensionCollection GetExtensionPoint (object ob)
{
Type type = ob.GetType ();
object[] ats = type.GetCustomAttributes (typeof(XmlFormatExtensionPointAttribute), true);
if (ats.Length == 0) return null;
XmlFormatExtensionPointAttribute at = (XmlFormatExtensionPointAttribute)ats[0];
PropertyInfo prop = type.GetProperty (at.MemberName);
if (prop != null)
return prop.GetValue (ob, null) as ServiceDescriptionFormatExtensionCollection;
else {
FieldInfo field = type.GetField (at.MemberName);
if (field != null)
return field.GetValue (ob) as ServiceDescriptionFormatExtensionCollection;
else
throw new InvalidOperationException ("XmlFormatExtensionPointAttribute: Member " + at.MemberName + " not found");
}
}
/*
* The mobile profile lacks support for configuration
*/
#if MOBILE || XAMMAC_4_5
public static ArrayList BuildExtensionImporters ()
{
return new ArrayList (0);
}
public static ArrayList BuildExtensionReflectors ()
{
return new ArrayList (0);
}
#else
public static ArrayList BuildExtensionImporters ()
{
return BuildExtensionList (WebServicesSection.Current.SoapExtensionImporterTypes);
}
public static ArrayList BuildExtensionReflectors ()
{
return BuildExtensionList (WebServicesSection.Current.SoapExtensionReflectorTypes);
}
public static ArrayList BuildExtensionList (TypeElementCollection exts)
{
ArrayList extensionTypes = new ArrayList ();
if (exts != null)
{
foreach (TypeElement econf in exts)
{
extensionTypes.Add (econf);
}
}
ArrayList extensions = new ArrayList (extensionTypes.Count);
foreach (TypeElement econf in extensionTypes)
extensions.Add (Activator.CreateInstance (econf.Type));
return extensions;
}
#endif
}
internal class ExtensionInfo
{
ArrayList _namespaceDeclarations;
string _namespace;
string _elementName;
Type _type;
XmlSerializer _serializer;
public ArrayList NamespaceDeclarations
{
get {
if (_namespaceDeclarations == null) _namespaceDeclarations = new ArrayList ();
return _namespaceDeclarations;
}
}
public string Namespace
{
get { return _namespace; }
set { _namespace = value; }
}
public string ElementName
{
get { return _elementName; }
set { _elementName = value; }
}
public Type Type
{
get { return _type; }
set { _type = value; }
}
public XmlSerializer Serializer
{
get { return _serializer; }
set { _serializer = value; }
}
}
}
|