DataContractSerializerMessageContractImporter.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. //
  2. // DataContractSerializerMessageContractImporter.cs
  3. //
  4. // Author: Atsushi Enomoto ([email protected])
  5. // Ankit Jain ([email protected])
  6. //
  7. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.Collections.Generic;
  30. using System.Collections.ObjectModel;
  31. using System.ServiceModel;
  32. using System.Text;
  33. using System.Web.Services.Description;
  34. using System.Xml;
  35. using System.Xml.Schema;
  36. using System.Xml.Serialization;
  37. using QName = System.Xml.XmlQualifiedName;
  38. using WSDL = System.Web.Services.Description.ServiceDescription;
  39. namespace System.ServiceModel.Description
  40. {
  41. [MonoTODO]
  42. public class DataContractSerializerMessageContractImporter
  43. : IWsdlImportExtension
  44. {
  45. bool enabled = true;
  46. public bool Enabled {
  47. get { return enabled; }
  48. set { enabled = value; }
  49. }
  50. void IWsdlImportExtension.BeforeImport (
  51. ServiceDescriptionCollection wsdlDocuments,
  52. XmlSchemaSet xmlSchemas,
  53. ICollection<XmlElement> policy)
  54. {
  55. }
  56. void IWsdlImportExtension.ImportContract (WsdlImporter importer,
  57. WsdlContractConversionContext context)
  58. {
  59. if (!enabled)
  60. return;
  61. if (importer == null)
  62. throw new ArgumentNullException ("importer");
  63. if (context == null)
  64. throw new ArgumentNullException ("context");
  65. if (this.importer != null || this.context != null)
  66. throw new SystemException ("INTERNAL ERROR: unexpected recursion of ImportContract method call");
  67. this.importer = importer;
  68. this.context = context;
  69. try {
  70. DoImportContract ();
  71. } finally {
  72. this.importer = null;
  73. this.context = null;
  74. }
  75. }
  76. WsdlImporter importer;
  77. WsdlContractConversionContext context;
  78. XmlSchemaImporter schema_importer_;
  79. XmlSchemaImporter schema_importer {
  80. get {
  81. if (schema_importer_ != null)
  82. return schema_importer_;
  83. schema_importer_ = new XmlSchemaImporter (xml_schemas);
  84. return schema_importer_;
  85. }
  86. }
  87. XmlSchemas xml_schemas_;
  88. XmlSchemas xml_schemas {
  89. get {
  90. if (xml_schemas_ != null)
  91. return xml_schemas_;
  92. xml_schemas_ = new XmlSchemas ();
  93. foreach (WSDL wsdl in importer.WsdlDocuments)
  94. foreach (XmlSchema schema in wsdl.Types.Schemas)
  95. xml_schemas_.Add (schema);
  96. foreach (XmlSchema schema in importer.XmlSchemas.Schemas ())
  97. xml_schemas_.Add (schema);
  98. xml_schemas_.Compile (null, true);
  99. return xml_schemas_;
  100. }
  101. }
  102. void DoImportContract ()
  103. {
  104. PortType port_type = context.WsdlPortType;
  105. ContractDescription contract = context.Contract;
  106. int i, j;
  107. List<MessagePartDescription> parts = new List<MessagePartDescription> ();
  108. i = 0;
  109. foreach (Operation op in port_type.Operations) {
  110. OperationDescription opdescr = contract.Operations [i];
  111. j = 0;
  112. foreach (OperationMessage opmsg in op.Messages) {
  113. //SM.MessageDescription
  114. MessageDescription msgdescr = opdescr.Messages [j];
  115. //OpMsg's corresponding WSMessage
  116. Message msg = port_type.ServiceDescription.Messages [opmsg.Message.Name];
  117. msgdescr.Body.WrapperNamespace = port_type.ServiceDescription.TargetNamespace;
  118. if (opmsg is OperationOutput) {
  119. //ReturnValue
  120. msg = port_type.ServiceDescription.Messages [opmsg.Message.Name];
  121. resolveMessage (msg, msgdescr.Body, parts);
  122. if (parts.Count > 0) {
  123. msgdescr.Body.ReturnValue = parts [0];
  124. parts.Clear ();
  125. }
  126. continue;
  127. }
  128. /* OperationInput */
  129. /* Parts, MessagePartDescription */
  130. resolveMessage (msg, msgdescr.Body, parts);
  131. foreach (MessagePartDescription p in parts)
  132. msgdescr.Body.Parts.Add (p);
  133. parts.Clear ();
  134. j ++;
  135. }
  136. i ++;
  137. }
  138. }
  139. void resolveMessage (Message msg, MessageBodyDescription body, List<MessagePartDescription> parts)
  140. {
  141. foreach (MessagePart part in msg.Parts) {
  142. if (part.Name == "parameters") {
  143. if (!part.Element.IsEmpty) {
  144. body.WrapperName = part.Element.Name;
  145. resolveElement (part.Element, parts, body.WrapperNamespace);
  146. } else {
  147. body.WrapperName = part.Type.Name;
  148. resolveType (part.Type, parts, body.WrapperNamespace);
  149. }
  150. }
  151. //FIXME: non-parameters?
  152. }
  153. }
  154. void resolveElement (QName qname, List<MessagePartDescription> parts, string ns)
  155. {
  156. XmlSchemaElement element = (XmlSchemaElement) xml_schemas.Find (qname, typeof (XmlSchemaElement));
  157. if (element == null)
  158. //FIXME: What to do here?
  159. throw new Exception ("Could not resolve : " + qname.ToString ());
  160. resolveParticle (schema_importer, element, parts, ns, 2);
  161. }
  162. void resolveType (QName qname, List<MessagePartDescription> parts, string ns)
  163. {
  164. /*foreach (XmlSchema xs in importer.Schemas)
  165. if (xs.Types [qname] != null)
  166. return resolveParameters ((XmlSchemaElement) xs.Types [qname]., msgdescr, importer);
  167. //FIXME: What to do here?
  168. throw new Exception ("Could not resolve : " + qname.ToString ());*/
  169. throw new NotImplementedException ();
  170. }
  171. string GetCLRTypeName (QName qname)
  172. {
  173. if (qname.Namespace != "http://www.w3.org/2001/XMLSchema")
  174. return null;
  175. switch (qname.Name) {
  176. case "anyURI":
  177. return "System.String";
  178. case "boolean":
  179. return "System.Boolean";
  180. /*FIXME: case "base64Binary":
  181. case "dateTime":
  182. case "duration":*/
  183. case "QName":
  184. return "System.String";
  185. case "decimal":
  186. return "System.Decimal";
  187. case "double":
  188. return "System.Double";
  189. case "float":
  190. return "System.Double";
  191. case "byte":
  192. return "System.SByte";
  193. case "short":
  194. return "System.Int16";
  195. case "int":
  196. return "System.Int32";
  197. case "long":
  198. return "System.Int64";
  199. case "unsignedByte":
  200. return "System.Byte";
  201. case "unsignedShort":
  202. return "System.UInt16";
  203. case "unsignedInt":
  204. return "System.UInt32";
  205. case "unsignedLong":
  206. return "System.UInt64";
  207. case "string":
  208. return "System.String";
  209. /* FIXME:
  210. case "anyType":
  211. return true;
  212. default:
  213. return false;*/
  214. }
  215. return null;
  216. }
  217. void resolveParticle (XmlSchemaImporter schema_importer,
  218. XmlSchemaParticle particle,
  219. List<MessagePartDescription> parts,
  220. string ns,
  221. int depth)
  222. {
  223. if (particle is XmlSchemaGroupBase) {
  224. //sequence,
  225. //FIXME: others?
  226. if (depth <= 0)
  227. return;
  228. XmlSchemaGroupBase groupBase = particle as XmlSchemaGroupBase;
  229. foreach (XmlSchemaParticle item in groupBase.Items)
  230. resolveParticle (schema_importer, item, parts, ns, depth - 1);
  231. return;
  232. }
  233. XmlSchemaElement elem = particle as XmlSchemaElement;
  234. if (elem == null)
  235. return;
  236. MessagePartDescription msg_part = null;
  237. XmlSchemaComplexType ct = elem.ElementSchemaType as XmlSchemaComplexType;
  238. if (ct == null) {
  239. //Not a complex type
  240. XmlSchemaSimpleType simple = elem.ElementSchemaType as XmlSchemaSimpleType;
  241. msg_part = new MessagePartDescription (
  242. elem.Name, ns);
  243. if (elem.SchemaType != null)
  244. msg_part.XmlTypeMapping = schema_importer.ImportTypeMapping (elem.QualifiedName);
  245. else
  246. msg_part.XmlTypeMapping = schema_importer.ImportSchemaType (elem.SchemaTypeName);
  247. msg_part.TypeName = new QName (GetCLRTypeName (elem.SchemaTypeName), "");
  248. parts.Add (msg_part);
  249. return;
  250. }
  251. if (depth > 0) {
  252. resolveParticle (schema_importer, ct.ContentTypeParticle, parts, ns, depth - 1);
  253. return;
  254. }
  255. //depth <= 0
  256. msg_part = new MessagePartDescription (elem.Name, ns);
  257. if (elem.SchemaType != null)
  258. msg_part.XmlTypeMapping = schema_importer.ImportTypeMapping (elem.QualifiedName);
  259. else
  260. msg_part.XmlTypeMapping = schema_importer.ImportSchemaType (elem.SchemaTypeName);
  261. msg_part.TypeName = elem.SchemaTypeName;
  262. parts.Add (msg_part);
  263. }
  264. void IWsdlImportExtension.ImportEndpoint (WsdlImporter importer,
  265. WsdlEndpointConversionContext context)
  266. {
  267. }
  268. }
  269. }