| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348 |
- //
- // System.Web.Services.Description.ServiceDescription.cs
- //
- // Author:
- // Tim Coleman ([email protected])
- // Lluis Sanchez Gual ([email protected])
- //
- // Copyright (C) Tim Coleman, 2002
- //
- using System.IO;
- using System.Collections;
- using System.Reflection;
- using System.Web.Services;
- using System.Web.Services.Configuration;
- using System.Xml;
- using System.Xml.Schema;
- using System.Xml.Serialization;
- namespace System.Web.Services.Description {
- [XmlFormatExtensionPoint ("Extensions")]
- [XmlRoot ("definitions", Namespace = "http://schemas.xmlsoap.org/wsdl/")]
- public sealed class ServiceDescription : DocumentableItem {
- #region Fields
- public const string Namespace = "http://schemas.xmlsoap.org/wsdl/";
- BindingCollection bindings;
- ServiceDescriptionFormatExtensionCollection extensions;
- ImportCollection imports;
- MessageCollection messages;
- string name;
- PortTypeCollection portTypes;
- string retrievalUrl;
- ServiceDescriptionCollection serviceDescriptions;
- ServiceCollection services;
- string targetNamespace;
- Types types;
- static ServiceDescriptionSerializer serializer;
- #endregion // Fields
- #region Constructors
- static ServiceDescription ()
- {
- serializer = new ServiceDescriptionSerializer ();
- }
- [MonoTODO ("Move namespaces to subtype, use ServiceDescriptionSerializer")]
- public ServiceDescription ()
- {
- bindings = new BindingCollection (this);
- extensions = new ServiceDescriptionFormatExtensionCollection (this);
- imports = new ImportCollection (this);
- messages = new MessageCollection (this);
- name = String.Empty;
- portTypes = new PortTypeCollection (this);
- serviceDescriptions = null;
- services = new ServiceCollection (this);
- targetNamespace = String.Empty;
- types = null;
- }
-
- #endregion // Constructors
- #region Properties
- [XmlElement ("import")]
- public ImportCollection Imports {
- get { return imports; }
- }
- [XmlElement ("types")]
- public Types Types {
- get { return types; }
- set { types = value; }
- }
- [XmlElement ("message")]
- public MessageCollection Messages {
- get { return messages; }
- }
- [XmlElement ("portType")]
- public PortTypeCollection PortTypes {
- get { return portTypes; }
- }
-
- [XmlElement ("binding")]
- public BindingCollection Bindings {
- get { return bindings; }
- }
- [XmlIgnore]
- public ServiceDescriptionFormatExtensionCollection Extensions {
- get { return extensions; }
- }
- [XmlAttribute ("name", DataType = "NMTOKEN")]
- public string Name {
- get { return name; }
- set { name = value; }
- }
- [XmlIgnore]
- public string RetrievalUrl {
- get { return retrievalUrl; }
- set { retrievalUrl = value; }
- }
-
- [XmlIgnore]
- public static XmlSerializer Serializer {
- get { return serializer; }
- }
- [XmlIgnore]
- public ServiceDescriptionCollection ServiceDescriptions {
- get {
- if (serviceDescriptions == null)
- throw new NullReferenceException ();
- return serviceDescriptions;
- }
- }
- [XmlElement ("service")]
- public ServiceCollection Services {
- get { return services; }
- }
- [XmlAttribute ("targetNamespace")]
- public string TargetNamespace {
- get { return targetNamespace; }
- set { targetNamespace = value; }
- }
- #endregion // Properties
- #region Methods
- public static bool CanRead (XmlReader reader)
- {
- return serializer.CanDeserialize (reader);
- }
- public static ServiceDescription Read (Stream stream)
- {
- return (ServiceDescription) serializer.Deserialize (stream);
- }
- public static ServiceDescription Read (string fileName)
- {
- return Read (new FileStream (fileName, FileMode.Open));
- }
- public static ServiceDescription Read (TextReader textReader)
- {
- return (ServiceDescription) serializer.Deserialize (textReader);
- }
- public static ServiceDescription Read (XmlReader reader)
- {
- return (ServiceDescription) serializer.Deserialize (reader);
- }
- public void Write (Stream stream)
- {
- serializer.Serialize (stream, this, GetNamespaceList ());
- }
- public void Write (string fileName)
- {
- Write (new FileStream (fileName, FileMode.Create));
- }
- public void Write (TextWriter writer)
- {
- serializer.Serialize (writer, this, GetNamespaceList ());
- }
- public void Write (XmlWriter writer)
- {
- serializer.Serialize (writer, this, GetNamespaceList ());
- }
- internal void SetParent (ServiceDescriptionCollection serviceDescriptions)
- {
- this.serviceDescriptions = serviceDescriptions;
- }
-
- XmlSerializerNamespaces GetNamespaceList ()
- {
- XmlSerializerNamespaces ns;
- ns = new XmlSerializerNamespaces ();
- ns.Add ("soap", SoapBinding.Namespace);
- ns.Add ("s", XmlSchema.Namespace);
- ns.Add ("http", HttpBinding.Namespace);
- ns.Add ("mime", MimeContentBinding.Namespace);
- ns.Add ("tm", MimeTextBinding.Namespace);
- ns.Add ("s0", TargetNamespace);
- return ns;
- }
- #endregion
- internal class ServiceDescriptionSerializer : XmlSerializer
- {
- static XmlTypeMapping _typeMap;
- static Type[] _builtinExtensionTypes = new Type[] {
- typeof (HttpAddressBinding),
- typeof (HttpBinding),
- typeof (HttpOperationBinding),
- typeof (HttpUrlEncodedBinding),
- typeof (HttpUrlReplacementBinding),
- typeof (MimeContentBinding),
- typeof (MimeMultipartRelatedBinding),
- typeof (MimePart),
- typeof (MimeTextBinding),
- typeof (MimeXmlBinding),
- typeof (SoapAddressBinding),
- typeof (SoapBinding),
- typeof (SoapBodyBinding),
- typeof (SoapFaultBinding),
- typeof (SoapHeaderBinding),
- typeof (SoapHeaderFaultBinding),
- typeof (SoapOperationBinding)
- };
-
- protected override void Serialize (object o, XmlSerializationWriter writer)
- {
- ServiceDescriptionWriter xsWriter = writer as ServiceDescriptionWriter;
- xsWriter.WriteObject (o);
- }
-
- protected override object Deserialize (XmlSerializationReader reader)
- {
- ServiceDescriptionReader xsReader = reader as ServiceDescriptionReader;
- return xsReader.ReadObject ();
- }
-
- protected override XmlSerializationWriter CreateWriter ()
- {
- return new ServiceDescriptionWriter (GetTypeMapping());
- }
-
- protected override XmlSerializationReader CreateReader ()
- {
- return new ServiceDescriptionReader (GetTypeMapping());
- }
-
- XmlTypeMapping GetTypeMapping ()
- {
- if (_typeMap == null) {
- XmlReflectionImporter ri = new XmlReflectionImporter (ServiceDescription.Namespace);
- foreach (Type t in _builtinExtensionTypes) ri.IncludeType (t);
- _typeMap = ri.ImportTypeMapping (typeof (ServiceDescription));
- }
- return _typeMap;
- }
- }
-
- internal class ServiceDescriptionWriter : XmlSerializationWriterInterpreter
- {
- public ServiceDescriptionWriter (XmlMapping typeMap)
- : base (typeMap)
- {
- }
- protected override void WriteObjectElementElements (XmlTypeMapping typeMap, object ob)
- {
- Type type = ob.GetType ();
- object[] ats = type.GetCustomAttributes (typeof(XmlFormatExtensionPointAttribute), true);
- if (ats.Length > 0)
- {
- XmlFormatExtensionPointAttribute at = (XmlFormatExtensionPointAttribute)ats[0];
- IEnumerable extensions = null;
-
- PropertyInfo prop = type.GetProperty (at.MemberName);
- if (prop != null)
- extensions = (IEnumerable) prop.GetValue (ob, null);
- else {
- FieldInfo field = type.GetField (at.MemberName);
- if (field != null)
- extensions = (IEnumerable) field.GetValue (ob);
- else
- throw new InvalidOperationException ("XmlFormatExtensionPointAttribute: Member " + at.MemberName + " not found");
- }
-
- if (extensions != null)
- {
- foreach (ServiceDescriptionFormatExtension ext in extensions)
- WriteExtension (ext);
- }
- }
-
- base.WriteObjectElementElements (typeMap, ob);
- }
-
- void WriteExtension (ServiceDescriptionFormatExtension ext)
- {
- string prefix = null;
- string ns = null;
- string name = null;
-
- Type type = ext.GetType ();
-
- object[] ats = type.GetCustomAttributes (typeof(XmlFormatExtensionPrefixAttribute), true);
- if (ats.Length > 0)
- {
- XmlFormatExtensionPrefixAttribute at = (XmlFormatExtensionPrefixAttribute)ats[0];
- prefix = at.Prefix;
- ns = at.Namespace;
- }
-
- ats = type.GetCustomAttributes (typeof(XmlFormatExtensionAttribute), true);
- if (ats.Length > 0)
- {
- XmlFormatExtensionAttribute at = (XmlFormatExtensionAttribute)ats[0];
- name = at.ElementName;
- if (at.Namespace != null) ns = at.Namespace;
- }
-
- if (name == null) throw new InvalidOperationException ("XmlFormatExtensionAttribute must be applied to type " + type);
-
- if (prefix == null || prefix == "") prefix = Writer.LookupPrefix (ns);
-
- if (prefix != null && prefix != "")
- Writer.WriteStartElement (prefix, name, ns);
- else
- WriteStartElement (name, ns, false);
- WriteObjectElement (GetTypeMap (type), ext, name, ns);
-
- WriteEndElement ();
- }
- }
- internal class ServiceDescriptionReader : XmlSerializationReaderInterpreter
- {
- public ServiceDescriptionReader (XmlMapping typeMap)
- : base (typeMap)
- {
- }
- }
- }
- }
|