File: WsdlExporter.cs

package info (click to toggle)
mono 6.12.0.199%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 1,296,836 kB
  • sloc: cs: 11,181,803; xml: 2,850,076; ansic: 699,709; cpp: 123,344; perl: 59,361; javascript: 30,841; asm: 21,853; makefile: 20,405; sh: 15,009; python: 4,839; pascal: 925; sql: 859; sed: 16; php: 1
file content (726 lines) | stat: -rw-r--r-- 22,703 bytes parent folder | download | duplicates (8)
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
//
// WsdlExporter.cs
//
// Author:
//	Atsushi Enomoto <atsushi@ximian.com>
//	Ankit Jain <JAnkit@novell.com>
//	Martin Baulig <martin.baulig@xamarin.com>
//
// Copyright (C) 2005 Novell, Inc.  http://www.novell.com
// Copyright (c) 2012 Xamarin Inc. (http://www.xamarin.com)
//
// 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;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.Web.Services.Description;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;
using System.Runtime.Serialization;

using SMBinding = System.ServiceModel.Channels.Binding;
using SMMessage = System.ServiceModel.Channels.Message;

using WSServiceDescription = System.Web.Services.Description.ServiceDescription;
using WSBinding = System.Web.Services.Description.Binding;
using WSMessage = System.Web.Services.Description.Message;
using QName = System.Xml.XmlQualifiedName;

namespace System.ServiceModel.Description
{
	[MonoTODO]
	public class WsdlExporter : MetadataExporter
	{
		class ContractExportMap
		{
			public ContractExportMap (QName qname, ContractDescription contract, List<IWsdlExportExtension> results)
			{
				QName = qname;
				Contract = contract;
				Results = results;
			}

			public QName QName { get; private set; }
			public ContractDescription Contract { get; private set; }
			public List<IWsdlExportExtension> Results { get; private set; }
		}

		class EndpointExportMap
		{
			public EndpointExportMap (string name, ServiceEndpoint endpoint)
			{
				Name = name;
				Endpoint = endpoint;
			}

			public string Name { get; private set; }
			public ServiceEndpoint Endpoint { get; private set; }
		}

		MetadataSet metadata;
		ServiceDescriptionCollection wsdl_colln;
		XsdDataContractExporter xsd_exporter;
		Dictionary<ContractDescription, ContractExportMap> exported_contracts;
		List<EndpointExportMap> exported_endpoints;

		public override MetadataSet GetGeneratedMetadata ()
		{
			if (metadata != null)
				return metadata;

			metadata = new MetadataSet ();
			foreach (WSServiceDescription sd in GeneratedWsdlDocuments)
				metadata.MetadataSections.Add (
					MetadataSection.CreateFromServiceDescription (sd));

			foreach (XmlSchema xs in GeneratedXmlSchemas.Schemas ())
				if (xs.TargetNamespace != XmlSchema.Namespace)
					metadata.MetadataSections.Add (
						MetadataSection.CreateFromSchema (xs));
				
			return metadata;
		}

		public override void ExportContract (ContractDescription contract)
		{
			ExportContractInternal (contract);
		}

		ContractExportMap ExportContractInternal (ContractDescription contract)
		{
			if (ExportedContracts.ContainsKey (contract))
				return ExportedContracts [contract];

			QName qname = new QName (contract.Name, contract.Namespace);
			if (ExportedContracts.Any (m => m.Value.QName == qname))
				throw new ArgumentException (String.Format (
					"A ContractDescription with Namespace : {0} and Name : {1} has already been exported.",
					contract.Namespace, contract.Name));

			WSServiceDescription sd = GetServiceDescription (contract.Namespace);

			List<IWsdlExportExtension> extensions = new List<IWsdlExportExtension> ();
			foreach (IWsdlExportExtension extn in contract.Behaviors.FindAll<IWsdlExportExtension> ())
				extensions.Add (extn);

			XmlDocument xdoc = new XmlDocument ();

			PortType ws_port = new PortType ();
			ws_port.Name = contract.Name;

			foreach (OperationDescription sm_op in contract.Operations) {
				Operation ws_op = new Operation ();
				ws_op.Name = sm_op.Name;

				foreach (MessageDescription sm_md in sm_op.Messages) {
					//OperationMessage
					OperationMessage ws_opmsg;
					WSMessage ws_msg = new WSMessage ();
					MessagePart ws_msgpart;
					if (sm_md.Direction == MessageDirection.Input) {
						ws_opmsg = new OperationInput ();
						ws_msg.Name = String.Concat (ws_port.Name, "_", ws_op.Name, "_", "InputMessage");
						ws_msgpart = ExportMessageBodyDescription (sm_md.Body, ws_op.Name, sd.TargetNamespace);
					} else {
						ws_opmsg = new OperationOutput ();
						ws_msg.Name = String.Concat (ws_port.Name, "_", ws_op.Name, "_", "OutputMessage");
						ws_msgpart = ExportMessageBodyDescription (sm_md.Body, ws_op.Name + "Response", sd.TargetNamespace);
					}
					ws_msg.Parts.Add (ws_msgpart);	

					/* FIXME: Faults */

					//Action
					XmlAttribute attr = xdoc.CreateAttribute ("wsaw", "Action", "http://www.w3.org/2006/05/addressing/wsdl");
					attr.Value = sm_md.Action;
					ws_opmsg.ExtensibleAttributes = new XmlAttribute [] { attr };
					
					//FIXME: Set .Input & .Output

					ws_opmsg.Message = new QName (ws_msg.Name, sd.TargetNamespace);
					ws_op.Messages.Add (ws_opmsg);
					sd.Messages.Add (ws_msg);
				}

				ws_port.Operations.Add (ws_op);

				foreach (IWsdlExportExtension extn in sm_op.Behaviors.FindAll<IWsdlExportExtension> ())
					extensions.Add (extn);
			}

			//Add Imports for <types
			XmlSchema xs_import = new XmlSchema ();
			xs_import.TargetNamespace = String.Concat (
					contract.Namespace, 
					contract.Namespace.EndsWith ("/") ? "" : "/",
					"Imports");
			foreach (XmlSchema schema in GeneratedXmlSchemas.Schemas ()) {
				XmlSchemaImport imp = new XmlSchemaImport ();
				imp.Namespace = schema.TargetNamespace;
				xs_import.Includes.Add (imp);
			}
			sd.Types.Schemas.Add (xs_import);

			sd.PortTypes.Add (ws_port);
			var map = new ContractExportMap (qname, contract, extensions);
			ExportedContracts.Add (contract, map);

			WsdlContractConversionContext context = new WsdlContractConversionContext (contract, ws_port);
			foreach (IWsdlExportExtension extn in extensions)
				extn.ExportContract (this, context);

			return map;
		}

		public override void ExportEndpoint (ServiceEndpoint endpoint)
		{
			ExportEndpoint_Internal (endpoint);
		}

		EndpointExportMap ExportEndpoint_Internal (ServiceEndpoint endpoint)
		{
			var map = ExportedEndpoints.FirstOrDefault (m => m.Endpoint == endpoint);
			if (map != null)
				return map;

			int index = 0;
			var baseName = String.Concat (endpoint.Binding.Name, "_", endpoint.Contract.Name);
			var name = baseName;
			while (ExportedEndpoints.Exists (m => m.Name == name))
				name = String.Concat (baseName, (++index).ToString ());

			map = new EndpointExportMap (name, endpoint);
			ExportedEndpoints.Add (map);

			var contract = ExportContractInternal (endpoint.Contract);

			//FIXME: Namespace
			WSServiceDescription sd = GetServiceDescription ("http://tempuri.org/");
			if (sd.TargetNamespace != endpoint.Contract.Namespace) {
				sd.Namespaces.Add ("i0", endpoint.Contract.Namespace);

				//Import
				Import import = new Import ();
				import.Namespace = endpoint.Contract.Namespace;

				sd.Imports.Add (import);
			}
			
			if (endpoint.Binding == null)
				throw new ArgumentException (String.Format (
					"Binding for ServiceEndpoint named '{0}' is null",
					endpoint.Name));

			var extensions = new List<IWsdlExportExtension> ();
			var extensionTypes = new Dictionary<Type, IWsdlExportExtension> ();
			if (contract.Results != null) {
				foreach (var extension in contract.Results) {
					var type = extension.GetType ();
					if (extensionTypes.ContainsKey (type))
						continue;
					extensionTypes.Add (type, extension);
					extensions.Add (extension);
				}
			}

			var bindingElements = endpoint.Binding.CreateBindingElements ();
			foreach (var element in bindingElements) {
				var extension = element as IWsdlExportExtension;
				if (extension == null)
					continue;
				var type = extension.GetType ();
				if (extensionTypes.ContainsKey (type))
					continue;
				extensionTypes.Add (type, extension);
				extensions.Add (extension);
			}

			//ExportBinding
			WSBinding ws_binding = new WSBinding ();
			
			//<binding name = .. 
			ws_binding.Name = name;

			//<binding type = ..
			ws_binding.Type = new QName (endpoint.Contract.Name, endpoint.Contract.Namespace);
			sd.Bindings.Add (ws_binding);

			//	<operation
			foreach (OperationDescription sm_op in endpoint.Contract.Operations) {
				var op_binding = CreateOperationBinding (endpoint, sm_op);
				ws_binding.Operations.Add (op_binding);
			}

			//Add <service
			Port ws_port = ExportService (sd, ws_binding, endpoint.Address);

			//Call IWsdlExportExtension.ExportEndpoint
			WsdlContractConversionContext contract_context = new WsdlContractConversionContext (
				endpoint.Contract, sd.PortTypes [endpoint.Contract.Name]);
			WsdlEndpointConversionContext endpoint_context = new WsdlEndpointConversionContext (
				contract_context, endpoint, ws_port, ws_binding);

			foreach (var extension in extensions) {
				try {
					extension.ExportEndpoint (this, endpoint_context);
				} catch (Exception ex) {
					var error = AddError (
						"Failed to export endpoint '{0}': wsdl exporter '{1}' " +
						"threw an exception: {2}", endpoint.Name, extension.GetType (), ex);
					throw new MetadataExportException (error, ex);
				}
			}

			try {
				ExportPolicy (endpoint, ws_binding);
			} catch (MetadataExportException) {
				throw;
			} catch (Exception ex) {
				var error = AddError (
					"Failed to export endpoint '{0}': unhandled exception " +
					"while exporting policy: {1}", endpoint.Name, ex);
				throw new MetadataExportException (error, ex);
			}

			return map;
		}

		OperationBinding CreateOperationBinding (ServiceEndpoint endpoint, OperationDescription sm_op)
		{
			OperationBinding op_binding = new OperationBinding ();
			op_binding.Name = sm_op.Name;
			
			foreach (MessageDescription sm_md in sm_op.Messages) {
				if (sm_md.Direction == MessageDirection.Input) {
					//<input
					CreateInputBinding (endpoint, op_binding, sm_md);
				} else {
					//<output
					CreateOutputBinding (endpoint, op_binding, sm_md);
				}
			}

			return op_binding;
		}

		void CreateInputBinding (ServiceEndpoint endpoint, OperationBinding op_binding,
		                         MessageDescription sm_md)
		{
			var in_binding = new InputBinding ();
			op_binding.Input = in_binding;

			var message_version = endpoint.Binding.MessageVersion ?? MessageVersion.None;
			if (message_version == MessageVersion.None)
				return;

			SoapBodyBinding soap_body_binding;
			SoapOperationBinding soap_operation_binding;
			if (message_version.Envelope == EnvelopeVersion.Soap11) {
				soap_body_binding = new SoapBodyBinding ();
				soap_operation_binding = new SoapOperationBinding ();
			} else if (message_version.Envelope == EnvelopeVersion.Soap12) {
				soap_body_binding = new Soap12BodyBinding ();
				soap_operation_binding = new Soap12OperationBinding ();
			} else {
				throw new InvalidOperationException ();
			}

			soap_body_binding.Use = SoapBindingUse.Literal;
			in_binding.Extensions.Add (soap_body_binding);
				
			//Set Action
			//<operation > <soap:operation soapAction .. >
			soap_operation_binding.SoapAction = sm_md.Action;
			soap_operation_binding.Style = SoapBindingStyle.Document;
			op_binding.Extensions.Add (soap_operation_binding);
		}

		void CreateOutputBinding (ServiceEndpoint endpoint, OperationBinding op_binding,
		                          MessageDescription sm_md)
		{
			var out_binding = new OutputBinding ();
			op_binding.Output = out_binding;

			var message_version = endpoint.Binding.MessageVersion ?? MessageVersion.None;
			if (message_version == MessageVersion.None)
				return;

			SoapBodyBinding soap_body_binding;
			if (message_version.Envelope == EnvelopeVersion.Soap11) {
				soap_body_binding = new SoapBodyBinding ();
			} else if (message_version.Envelope == EnvelopeVersion.Soap12) {
				soap_body_binding = new Soap12BodyBinding ();
			} else {
				throw new InvalidOperationException ();
			}

			soap_body_binding.Use = SoapBindingUse.Literal;
			out_binding.Extensions.Add (soap_body_binding);
		}
		
		Port ExportService (WSServiceDescription sd, WSBinding ws_binding, EndpointAddress address)
		{
			if (address == null)
				return null;

			Service ws_svc = GetService (sd, "service");
			sd.Name = "service";

			Port ws_port = new Port ();
			ws_port.Name = ws_binding.Name;
			ws_port.Binding = new QName (ws_binding.Name, sd.TargetNamespace);

			ws_svc.Ports.Add (ws_port);

			return ws_port;
		}

		Service GetService (WSServiceDescription sd, string name)
		{
			Service svc = sd.Services [name];
			if (svc != null)
				return svc;

			svc = new Service ();
			svc.Name = name;
			sd.Services.Add (svc);

			return svc;
		}

		WSServiceDescription GetServiceDescription (string ns)
		{
			foreach (WSServiceDescription sd in GeneratedWsdlDocuments) {
				if (sd.TargetNamespace == ns)
					return sd;
			}

			WSServiceDescription ret = new WSServiceDescription ();
			ret.TargetNamespace = ns;
			ret.Namespaces = GetNamespaces (ns);
			GeneratedWsdlDocuments.Add (ret);

			metadata = null;

			return ret;
		}

		public ServiceDescriptionCollection GeneratedWsdlDocuments {
			get {
				if (wsdl_colln == null)
					wsdl_colln = new ServiceDescriptionCollection ();
				return wsdl_colln;
			}
		}

		public XmlSchemaSet GeneratedXmlSchemas {
			get { return XsdExporter.Schemas; }
		}

		public void ExportEndpoints (
			IEnumerable<ServiceEndpoint> endpoints,
			XmlQualifiedName wsdlServiceQName)
		{
			if (endpoints == null)
				throw new ArgumentNullException ("endpoints");
			if (wsdlServiceQName == null)
				throw new ArgumentNullException ("wsdlServiceQName");

			foreach (ServiceEndpoint ep in endpoints) {
				if (ep.Contract.Name == ServiceMetadataBehavior.MexContractName)
					continue;

				ExportEndpoint (ep);
			}
		}

		XsdDataContractExporter XsdExporter {
			get {
				if (xsd_exporter == null)
					xsd_exporter = new XsdDataContractExporter ();
				return xsd_exporter;
			}
		}

		Dictionary<ContractDescription, ContractExportMap> ExportedContracts {
			get {
				if (exported_contracts == null)
					exported_contracts = new Dictionary<ContractDescription, ContractExportMap> ();
				return exported_contracts;
			}
		}
		
		List<EndpointExportMap> ExportedEndpoints {
			get {
				if (exported_endpoints == null)
					exported_endpoints = new List<EndpointExportMap> ();
				return exported_endpoints;
			}
		}
		
		XmlSerializerNamespaces GetNamespaces (string target_namespace)
		{
			XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces ();

			namespaces.Add ("soap", "http://schemas.xmlsoap.org/wsdl/soap/");
			namespaces.Add ("wsu", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");
			namespaces.Add ("soapenc", "http://schemas.xmlsoap.org/soap/encoding/");
			namespaces.Add ("tns", target_namespace);
			namespaces.Add ("wsa", "http://schemas.xmlsoap.org/ws/2004/08/addressing");
			namespaces.Add ("wsp", "http://schemas.xmlsoap.org/ws/2004/09/policy");
			namespaces.Add ("wsap", "http://schemas.xmlsoap.org/ws/2004/08/addressing/policy");
			namespaces.Add ("msc", "http://schemas.microsoft.com/ws/2005/12/wsdl/contract");
			namespaces.Add ("wsaw", "http://www.w3.org/2006/05/addressing/wsdl");
			namespaces.Add ("soap12", "http://schemas.xmlsoap.org/wsdl/soap12/");
			namespaces.Add ("wsa10", "http://www.w3.org/2005/08/addressing");
			namespaces.Add ("wsdl", "http://schemas.xmlsoap.org/wsdl/");

			return namespaces;
		}

		MessagePart ExportMessageBodyDescription (MessageBodyDescription msgbody, string name, string ns)
		{
			MessagePart msgpart = new MessagePart ();
			string part_name = IsTypeMessage (msgbody);

			if (part_name != null) {
				msgpart.Name = part_name;
				msgpart.Type = ExportTypeMessage (); //FIXME: Cache this
			} else {
				msgpart.Name = "parameters";
				msgpart.Element = ExportParameters (msgbody, name, ns);
			}
			return msgpart;
		}

		/* Sets the @name if the param or return type is SMMessage */
		string IsTypeMessage (MessageBodyDescription msgbody)
		{
			MessagePartDescription part = null;

			if (msgbody.Parts.Count == 0)
				part = msgbody.ReturnValue;
			else if (msgbody.Parts.Count == 1)
				part = msgbody.Parts [0];

			if (part != null && (part.Type.FullName == typeof (SMMessage).FullName))
				return part.Name;

			return null;
		}

		QName ExportParameters (MessageBodyDescription msgbody, string name, string ns)
		{
			XmlSchema xs = GetSchema (ns);
			//FIXME: Extract to a HasElement method ?
			foreach (XmlSchemaObject o in xs.Items) {
				XmlSchemaElement e = o as XmlSchemaElement;
				if (e == null)
					continue;

				if (e.Name == name)
					throw new InvalidOperationException (String.Format (
						"Message element named '{0}:{1}' has already been exported.",
						ns, name));
			}
				
			//Create the element for "parameters"
			XmlSchemaElement schema_element = new XmlSchemaElement ();
			schema_element.Name = name;

			XmlSchemaComplexType complex_type = new XmlSchemaComplexType ();
			//Generate Sequence representing the message/parameters
			//FIXME: MessageContractAttribute
		
			XmlSchemaSequence sequence = new XmlSchemaSequence ();
			XmlSchemaElement element = null;

			if (msgbody.ReturnValue == null) {
				//parameters
				foreach (MessagePartDescription part in msgbody.Parts) {
					if (part.Type == null)
						//FIXME: Eg. when WsdlImporter is used to import a wsdl
						throw new NotImplementedException ();
					
					element = GetSchemaElementForPart (part, xs);
					sequence.Items.Add (element);
				}
			} else {
				//ReturnValue
				if (msgbody.ReturnValue.Type != typeof (void)) {
					element = GetSchemaElementForPart (msgbody.ReturnValue, xs);
					sequence.Items.Add (element);
				}
			}

			complex_type.Particle = sequence;
			schema_element.SchemaType = complex_type;

			xs.Items.Add (schema_element);
			GeneratedXmlSchemas.Reprocess (xs);

			return new QName (schema_element.Name, xs.TargetNamespace);
		}

		//Exports <xs:type for SMMessage
		//FIXME: complex type for this can be made static
		QName ExportTypeMessage ()
		{
			XmlSchema xs = GetSchema ("http://schemas.microsoft.com/Message");
			QName qname = new QName ("MessageBody", xs.TargetNamespace);

			foreach (XmlSchemaObject o in xs.Items) {
				XmlSchemaComplexType ct = o as XmlSchemaComplexType;
				if (ct == null)
					continue;

				if (ct.Name == "MessageBody")
					//Already exported
					return qname;
			}

			XmlSchemaComplexType complex_type = new XmlSchemaComplexType ();
			complex_type.Name = "MessageBody";
			XmlSchemaSequence sequence = new XmlSchemaSequence ();

			XmlSchemaAny any = new XmlSchemaAny ();
			any.MinOccurs = 0;
			any.MaxOccursString = "unbounded";
			any.Namespace = "##any";

			sequence.Items.Add (any);
			complex_type.Particle = sequence;

			xs.Items.Add (complex_type);
			GeneratedXmlSchemas.Reprocess (xs);
			
			return qname;
		}

		XmlSchemaElement GetSchemaElementForPart (MessagePartDescription part, XmlSchema schema)
		{
			XmlSchemaElement element = new XmlSchemaElement ();

			element.Name = part.Name;
			XsdExporter.Export (part.Type);
			element.SchemaTypeName = XsdExporter.GetSchemaTypeName (part.Type);
			AddImport (schema, element.SchemaTypeName.Namespace);

			//FIXME: nillable, minOccurs
			if (XsdExporter.GetSchemaType (part.Type) is XmlSchemaComplexType ||
				part.Type == typeof (string))
				element.IsNillable = true;
			element.MinOccurs = 0;

			return element;
		}

		//FIXME: Replace with a dictionary ?
		void AddImport (XmlSchema schema, string ns)
		{
			if (ns == XmlSchema.Namespace || schema.TargetNamespace == ns)
				return;

			foreach (XmlSchemaObject o in schema.Includes) {
				XmlSchemaImport import = o as XmlSchemaImport;
				if (import == null)
					continue;
				if (import.Namespace == ns)
					return;
			}

			if (ns == string.Empty)
				return;

			XmlSchemaImport imp = new XmlSchemaImport ();
			imp.Namespace = ns;
			schema.Includes.Add (imp);
		}

		XmlSchema GetSchema (string ns)
		{
			ICollection colln = GeneratedXmlSchemas.Schemas (ns);
			if (colln.Count > 0) { 
				if (colln.Count > 1)
					throw new Exception ("More than 1 schema found for ns = " + ns);
				//FIXME: HORRIBLE!
				foreach (object o in colln)
					return (o as XmlSchema);
			}

			XmlSchema schema = new XmlSchema ();
			schema.TargetNamespace = ns;
			schema.ElementFormDefault = XmlSchemaForm.Qualified;
			GeneratedXmlSchemas.Add (schema);

			return schema;
		}

		PolicyConversionContext ExportPolicy (ServiceEndpoint endpoint, WSBinding binding)
		{
			var context = new CustomPolicyConversionContext (endpoint);

			var elements = endpoint.Binding.CreateBindingElements ();
			foreach (var element in elements) {
				var exporter = element as IPolicyExportExtension;
				if (exporter == null)
					continue;

				try {
					exporter.ExportPolicy (this, context);
				} catch (Exception ex) {
					var error = AddError (
						"Failed to export endpoint '{0}': policy exporter " +
						"'{1}' threw an exception: {2}", endpoint.Name,
						element.GetType (), ex);
					throw new MetadataExportException (error, ex);
				}
			}

			var assertions = context.GetBindingAssertions ();
			if (assertions.Count == 0)
				return context;

			var doc = new XmlDocument ();
			var policy = doc.CreateElement ("wsp", "Policy", PolicyImportHelper.PolicyNS);
			doc.AppendChild (policy);

			var exactlyOne = doc.CreateElement ("wsp", "ExactlyOne", PolicyImportHelper.PolicyNS);
			var all = doc.CreateElement ("wsp", "All", PolicyImportHelper.PolicyNS);

			policy.AppendChild (exactlyOne);
			exactlyOne.AppendChild (all);

			foreach (var assertion in assertions) {
				var imported = doc.ImportNode (assertion, true);
				all.AppendChild (imported);
			}

			binding.Extensions.Add (policy);
			return context;
		}

	}
}