2004-08-27 Martin Baulig <martin@ximian.com>
[mono.git] / mcs / tools / wsdl / SampleGenerator.cs
1 // 
2 // SampleGenerator.cs
3 //
4 // Author:
5 //   Lluis Sanchez Gual (lluis@ximian.com)
6 //
7 // Copyright (C) 2004 Novel Inc.
8 //
9
10 using System;
11 using System.IO;
12 using System.Text;
13 using System.Collections;
14 using System.Web.Services.Description;
15 using System.Xml;
16 using System.Xml.Schema;
17 using System.Xml.Serialization;
18
19 namespace Mono.WebServices
20 {
21         public class ConsoleSampleGenerator: SampleGenerator
22         {
23                 public ConsoleSampleGenerator (ServiceDescriptionCollection services, XmlSchemas schemas)
24                 : base (services, schemas)
25                 {
26                 }
27                 
28                 public static void Generate (ArrayList services, ArrayList schemas, string binOper, string protocol)
29                 {
30                         ServiceDescriptionCollection descCol = new ServiceDescriptionCollection ();
31                         foreach (ServiceDescription sd in services)
32                                 descCol.Add (sd);
33                                 
34                         XmlSchemas schemaCol;
35
36                         if (schemas.Count > 0) {
37                                 schemaCol = new XmlSchemas ();
38                                 foreach (XmlSchema sc in schemas)
39                                         schemaCol.Add (sc);
40                         }
41                         else
42                                 schemaCol = descCol[0].Types.Schemas;
43                                 
44                         string oper, bin = null; 
45                         
46                         int i = binOper.IndexOf ('/');
47                         if (i != -1) {
48                                 oper = binOper.Substring (i+1);
49                                 bin = binOper.Substring (0,i);
50                         }
51                         else
52                                 oper = binOper;
53                         
54                         ConsoleSampleGenerator sg = new ConsoleSampleGenerator (descCol, schemaCol);
55                         
56                         string req, resp;
57                         sg.GenerateMessages (oper, bin, protocol, out req, out resp);
58                         
59                         Console.WriteLine ();
60                         Console.WriteLine ("Sample request message:");
61                         Console.WriteLine ();
62                         Console.WriteLine (req);
63                         Console.WriteLine ();
64                         Console.WriteLine ("Sample response message:");
65                         Console.WriteLine ();
66                         Console.WriteLine (resp);
67                 }
68                 
69                 public void GenerateMessages (string operation, string bindingName, string protocol, out string req, out string resp)
70                 {
71                         Port port = FindPort (bindingName, protocol);
72                         Binding binding = descriptions.GetBinding (port.Binding);
73                         if (binding == null) throw new InvalidOperationException ("Binding " + bindingName + " not found");
74                         
75                         PortType portType = descriptions.GetPortType (binding.Type);
76                         Operation oper = FindOperation (portType, operation);
77                         if (oper == null) throw new InvalidOperationException ("Operation " + operation + " not found");
78                         OperationBinding obin = FindOperation (binding, operation);
79                         
80                         req = GenerateMessage (port, obin, oper, protocol, true);
81                         resp = GenerateMessage (port, obin, oper, protocol, false);
82                 }
83                 
84                 Port FindPort (string portName, string protocol)
85                 {
86                         Service service = descriptions[0].Services[0];
87                         foreach (Port port in service.Ports)
88                         {
89                                 if (portName == null)
90                                 {
91                                         Binding binding = descriptions.GetBinding (port.Binding);
92                                         if (GetProtocol (binding) == protocol) return port;
93                                 }
94                                 else if (port.Name == portName)
95                                         return port;
96                         }
97                         return null;
98                 }
99                 
100                 string GetProtocol (Binding binding)
101                 {
102                         if (binding.Extensions.Find (typeof(SoapBinding)) != null) return "Soap";
103                         HttpBinding hb = (HttpBinding) binding.Extensions.Find (typeof(HttpBinding));
104                         if (hb == null) return "";
105                         if (hb.Verb == "POST") return "HttpPost";
106                         if (hb.Verb == "GET") return "HttpGet";
107                         return "";
108                 }
109                 
110                 Operation FindOperation (PortType portType, string name)
111                 {
112                         foreach (Operation oper in portType.Operations) {
113                                 if (oper.Messages.Input.Name != null) {
114                                         if (oper.Messages.Input.Name == name) return oper;
115                                 }
116                                 else
117                                         if (oper.Name == name) return oper;
118                         }
119                                 
120                         return null;
121                 }
122                 
123                 OperationBinding FindOperation (Binding binding, string name)
124                 {
125                         foreach (OperationBinding oper in binding.Operations) {
126                                 if (oper.Input.Name != null) {
127                                         if (oper.Input.Name == name) return oper;
128                                 }
129                                 else 
130                                         if (oper.Name == name) return oper;
131                         }
132                                 
133                         return null;
134                 }
135         }
136         
137         //
138         // Sample generator class       
139         //
140         
141         public class SampleGenerator
142         {
143                 protected ServiceDescriptionCollection descriptions;
144                 protected XmlSchemas schemas;
145                 XmlSchemaElement anyElement;
146                 ArrayList queue;
147                 SoapBindingUse currentUse;
148                 XmlDocument document = new XmlDocument ();
149                 
150                 static readonly XmlQualifiedName anyType = new XmlQualifiedName ("anyType",XmlSchema.Namespace);
151                 static readonly XmlQualifiedName arrayType = new XmlQualifiedName ("Array","http://schemas.xmlsoap.org/soap/encoding/");
152                 static readonly XmlQualifiedName arrayTypeRefName = new XmlQualifiedName ("arrayType","http://schemas.xmlsoap.org/soap/encoding/");
153                 const string SoapEnvelopeNamespace = "http://schemas.xmlsoap.org/soap/envelope/";
154                 const string WsdlNamespace = "http://schemas.xmlsoap.org/wsdl/";
155                 const string SoapEncodingNamespace = "http://schemas.xmlsoap.org/soap/encoding/";
156                 
157                 class EncodedType
158                 {
159                         public EncodedType (string ns, XmlSchemaElement elem) { Namespace = ns; Element = elem; }
160                         public string Namespace;
161                         public XmlSchemaElement Element;
162                 }
163
164                 public SampleGenerator (ServiceDescriptionCollection services, XmlSchemas schemas)
165                 {
166                         descriptions = services;
167                         this.schemas = schemas;
168                         queue = new ArrayList ();
169                 }
170                 
171                 public string GenerateMessage (Port port, OperationBinding obin, Operation oper, string protocol, bool generateInput)
172                 {
173                         OperationMessage msg = null;
174                         foreach (OperationMessage opm in oper.Messages)
175                         {
176                                 if (opm is OperationInput && generateInput) msg = opm;
177                                 else if (opm is OperationOutput && !generateInput) msg = opm;
178                         }
179                         if (msg == null) return null;
180                         
181                         switch (protocol) {
182                                 case "Soap": return GenerateHttpSoapMessage (port, obin, oper, msg);
183                                 case "HttpGet": return GenerateHttpGetMessage (port, obin, oper, msg);
184                                 case "HttpPost": return GenerateHttpPostMessage (port, obin, oper, msg);
185                         }
186                         return "Unknown protocol";
187                 }
188                 
189                 public string GenerateHttpSoapMessage (Port port, OperationBinding obin, Operation oper, OperationMessage msg)
190                 {
191                         string req = "";
192                         
193                         if (msg is OperationInput)
194                         {
195                                 SoapAddressBinding sab = port.Extensions.Find (typeof(SoapAddressBinding)) as SoapAddressBinding;
196                                 SoapOperationBinding sob = obin.Extensions.Find (typeof(SoapOperationBinding)) as SoapOperationBinding;
197                                 req += "POST " + new Uri (sab.Location).AbsolutePath + "\n";
198                                 req += "SOAPAction: " + sob.SoapAction + "\n";
199                                 req += "Content-Type: text/xml; charset=utf-8\n";
200                                 req += "Content-Length: " + GetLiteral ("string") + "\n";
201                                 req += "Host: " + GetLiteral ("string") + "\n\n";
202                         }
203                         else
204                         {
205                                 req += "HTTP/1.0 200 OK\n";
206                                 req += "Content-Type: text/xml; charset=utf-8\n";
207                                 req += "Content-Length: " + GetLiteral ("string") + "\n\n";
208                         }
209                         
210                         req += GenerateSoapMessage (obin, oper, msg);
211                         return req;
212                 }
213                 
214                 public string GenerateHttpGetMessage (Port port, OperationBinding obin, Operation oper, OperationMessage msg)
215                 {
216                         string req = "";
217                         
218                         if (msg is OperationInput)
219                         {
220                                 HttpAddressBinding sab = port.Extensions.Find (typeof(HttpAddressBinding)) as HttpAddressBinding;
221                                 HttpOperationBinding sob = obin.Extensions.Find (typeof(HttpOperationBinding)) as HttpOperationBinding;
222                                 string location = new Uri (sab.Location).AbsolutePath + sob.Location + "?" + BuildQueryString (msg);
223                                 req += "GET " + location + "\n";
224                                 req += "Host: " + GetLiteral ("string");
225                         }
226                         else
227                         {
228                                 req += "HTTP/1.0 200 OK\n";
229                                 req += "Content-Type: text/xml; charset=utf-8\n";
230                                 req += "Content-Length: " + GetLiteral ("string") + "\n\n";
231                         
232                                 MimeXmlBinding mxb = (MimeXmlBinding) obin.Output.Extensions.Find (typeof(MimeXmlBinding)) as MimeXmlBinding;
233                                 if (mxb == null) return req;
234                                 
235                                 Message message = descriptions.GetMessage (msg.Message);
236                                 XmlQualifiedName ename = null;
237                                 foreach (MessagePart part in message.Parts)
238                                         if (part.Name == mxb.Part) ename = part.Element;
239                                         
240                                 if (ename == null) return req + GetLiteral("string");
241                                 
242                                 StringWriter sw = new StringWriter ();
243                                 XmlTextWriter xtw = new XmlTextWriter (sw);
244                                 xtw.Formatting = Formatting.Indented;
245                                 currentUse = SoapBindingUse.Literal;
246                                 WriteRootElementSample (xtw, ename);
247                                 xtw.Close ();
248                                 req += sw.ToString ();
249                         }
250                         
251                         return req;
252                 }
253                 
254                 public string GenerateHttpPostMessage (Port port, OperationBinding obin, Operation oper, OperationMessage msg)
255                 {
256                         string req = "";
257                         
258                         if (msg is OperationInput)
259                         {
260                                 HttpAddressBinding sab = port.Extensions.Find (typeof(HttpAddressBinding)) as HttpAddressBinding;
261                                 HttpOperationBinding sob = obin.Extensions.Find (typeof(HttpOperationBinding)) as HttpOperationBinding;
262                                 string location = new Uri (sab.Location).AbsolutePath + sob.Location;
263                                 req += "POST " + location + "\n";
264                                 req += "Content-Type: application/x-www-form-urlencoded\n";
265                                 req += "Content-Length: " + GetLiteral ("string") + "\n";
266                                 req += "Host: " + GetLiteral ("string") + "\n\n";
267                                 req += BuildQueryString (msg);
268                         }
269                         else return GenerateHttpGetMessage (port, obin, oper, msg);
270                         
271                         return req;
272                 }
273                 
274                 string BuildQueryString (OperationMessage opm)
275                 {
276                         string s = "";
277                         Message msg = descriptions.GetMessage (opm.Message);
278                         foreach (MessagePart part in msg.Parts)
279                         {
280                                 if (s.Length != 0) s += "&";
281                                 s += part.Name + "=" + GetLiteral (part.Type.Name);
282                         }
283                         return s;
284                 }
285                 
286                 public string GenerateSoapMessage (OperationBinding obin, Operation oper, OperationMessage msg)
287                 {
288                         SoapOperationBinding sob = obin.Extensions.Find (typeof(SoapOperationBinding)) as SoapOperationBinding;
289                         SoapBindingStyle style = (sob != null) ? sob.Style : SoapBindingStyle.Document;
290                         
291                         MessageBinding msgbin = (msg is OperationInput) ? (MessageBinding) obin.Input : (MessageBinding)obin.Output;
292                         SoapBodyBinding sbb = msgbin.Extensions.Find (typeof(SoapBodyBinding)) as SoapBodyBinding;
293                         SoapBindingUse bodyUse = (sbb != null) ? sbb.Use : SoapBindingUse.Literal;
294                         
295                         StringWriter sw = new StringWriter ();
296                         XmlTextWriter xtw = new XmlTextWriter (sw);
297                         xtw.Formatting = Formatting.Indented;
298                         
299                         xtw.WriteStartDocument ();
300                         xtw.WriteStartElement ("soap", "Envelope", SoapEnvelopeNamespace);
301                         xtw.WriteAttributeString ("xmlns", "xsi", null, XmlSchema.InstanceNamespace);
302                         xtw.WriteAttributeString ("xmlns", "xsd", null, XmlSchema.Namespace);
303                         
304                         if (bodyUse == SoapBindingUse.Encoded) 
305                         {
306                                 xtw.WriteAttributeString ("xmlns", "soapenc", null, SoapEncodingNamespace);
307                                 xtw.WriteAttributeString ("xmlns", "tns", null, msg.Message.Namespace);
308                         }
309
310                         // Serialize headers
311                         foreach (object ob in msgbin.Extensions)
312                         {
313                                 SoapHeaderBinding hb = ob as SoapHeaderBinding;
314                                 if (hb == null) continue;
315                                 
316                                 xtw.WriteStartElement ("soap", "Header", SoapEnvelopeNamespace);
317                                 WriteHeader (xtw, hb);
318                                 xtw.WriteEndElement ();
319                         }
320
321                         // Serialize body
322                         xtw.WriteStartElement ("soap", "Body", SoapEnvelopeNamespace);
323                         
324                         currentUse = bodyUse;
325                         WriteBody (xtw, oper, msg, sbb, style);
326                         
327                         xtw.WriteEndElement ();
328                         xtw.WriteEndElement ();
329                         xtw.Close ();
330                         return sw.ToString ();
331                 }
332                 
333                 void WriteHeader (XmlTextWriter xtw, SoapHeaderBinding header)
334                 {
335                         Message msg = descriptions.GetMessage (header.Message);
336                         if (msg == null) throw new InvalidOperationException ("Message " + header.Message + " not found");
337                         MessagePart part = msg.Parts [header.Part];
338                         if (part == null) throw new InvalidOperationException ("Message part " + header.Part + " not found in message " + header.Message);
339
340                         currentUse = header.Use;
341                         
342                         if (currentUse == SoapBindingUse.Literal)
343                                 WriteRootElementSample (xtw, part.Element);
344                         else
345                                 WriteTypeSample (xtw, part.Type);
346                 }
347                 
348                 void WriteBody (XmlTextWriter xtw, Operation oper, OperationMessage opm, SoapBodyBinding sbb, SoapBindingStyle style)
349                 {
350                         Message msg = descriptions.GetMessage (opm.Message);
351                         if (msg.Parts.Count > 0 && msg.Parts[0].Name == "parameters")
352                         {
353                                 MessagePart part = msg.Parts[0];
354                                 if (part.Element == XmlQualifiedName.Empty)
355                                         WriteTypeSample (xtw, part.Type);
356                                 else
357                                         WriteRootElementSample (xtw, part.Element);
358                         }
359                         else
360                         {
361                                 string elemName = oper.Name;
362                                 string ns = "";
363                                 if (opm is OperationOutput) elemName += "Response";
364                                 
365                                 if (style == SoapBindingStyle.Rpc) {
366                                         xtw.WriteStartElement (elemName, sbb.Namespace);
367                                         ns = sbb.Namespace;
368                                 }
369                                         
370                                 foreach (MessagePart part in msg.Parts)
371                                 {
372                                         if (part.Element == XmlQualifiedName.Empty)
373                                         {
374                                                 XmlSchemaElement elem = new XmlSchemaElement ();
375                                                 elem.SchemaTypeName = part.Type;
376                                                 elem.Name = part.Name;
377                                                 WriteElementSample (xtw, ns, elem);
378                                         }
379                                         else
380                                                 WriteRootElementSample (xtw, part.Element);
381                                 }
382                                 
383                                 if (style == SoapBindingStyle.Rpc)
384                                         xtw.WriteEndElement ();
385                         }
386                         WriteQueuedTypeSamples (xtw);
387                 }
388                 
389                 void WriteRootElementSample (XmlTextWriter xtw, XmlQualifiedName qname)
390                 {
391                         XmlSchemaElement elem = (XmlSchemaElement) schemas.Find (qname, typeof(XmlSchemaElement));
392                         if (elem == null) throw new InvalidOperationException ("Element not found: " + qname);
393                         WriteElementSample (xtw, qname.Namespace, elem);
394                 }
395                 
396                 void WriteElementSample (XmlTextWriter xtw, string ns, XmlSchemaElement elem)
397                 {
398                         bool sharedAnnType = false;
399                         XmlQualifiedName root;
400                         
401                         if (!elem.RefName.IsEmpty) {
402                                 XmlSchemaElement refElem = FindRefElement (elem);
403                                 if (refElem == null) throw new InvalidOperationException ("Global element not found: " + elem.RefName);
404                                 root = elem.RefName;
405                                 elem = refElem;
406                                 sharedAnnType = true;
407                         }
408                         else
409                                 root = new XmlQualifiedName (elem.Name, ns);
410                         
411                         if (!elem.SchemaTypeName.IsEmpty)
412                         {
413                                 XmlSchemaComplexType st = FindComplexTyype (elem.SchemaTypeName);
414                                 if (st != null) 
415                                         WriteComplexTypeSample (xtw, st, root);
416                                 else
417                                 {
418                                         xtw.WriteStartElement (root.Name, root.Namespace);
419                                         if (currentUse == SoapBindingUse.Encoded) 
420                                                 xtw.WriteAttributeString ("type", XmlSchema.InstanceNamespace, GetQualifiedNameString (xtw, elem.SchemaTypeName));
421                                         xtw.WriteString (GetLiteral (FindBuiltInType (elem.SchemaTypeName)));
422                                         xtw.WriteEndElement ();
423                                 }
424                         }
425                         else if (elem.SchemaType == null)
426                         {
427                                 xtw.WriteStartElement ("any");
428                                 xtw.WriteEndElement ();
429                         }
430                         else
431                                 WriteComplexTypeSample (xtw, (XmlSchemaComplexType) elem.SchemaType, root);
432                 }
433                 
434                 void WriteTypeSample (XmlTextWriter xtw, XmlQualifiedName qname)
435                 {
436                         XmlSchemaComplexType ctype = FindComplexTyype (qname);
437                         if (ctype != null) {
438                                 WriteComplexTypeSample (xtw, ctype, qname);
439                                 return;
440                         }
441                         
442                         XmlSchemaSimpleType stype = (XmlSchemaSimpleType) schemas.Find (qname, typeof(XmlSchemaSimpleType));
443                         if (stype != null) {
444                                 WriteSimpleTypeSample (xtw, stype);
445                                 return;
446                         }
447                         
448                         xtw.WriteString (GetLiteral (FindBuiltInType (qname)));
449                         throw new InvalidOperationException ("Type not found: " + qname);
450                 }
451                 
452                 void WriteComplexTypeSample (XmlTextWriter xtw, XmlSchemaComplexType stype, XmlQualifiedName rootName)
453                 {
454                         WriteComplexTypeSample (xtw, stype, rootName, -1);
455                 }
456                 
457                 void WriteComplexTypeSample (XmlTextWriter xtw, XmlSchemaComplexType stype, XmlQualifiedName rootName, int id)
458                 {
459                         string ns = rootName.Namespace;
460                         
461                         if (rootName.Name.IndexOf ("[]") != -1) rootName = arrayType;
462                         
463                         if (currentUse == SoapBindingUse.Encoded) {
464                                 string pref = xtw.LookupPrefix (rootName.Namespace);
465                                 if (pref == null) pref = "q1";
466                                 xtw.WriteStartElement (pref, rootName.Name, rootName.Namespace);
467                                 ns = "";
468                         }
469                         else
470                                 xtw.WriteStartElement (rootName.Name, rootName.Namespace);
471                         
472                         if (id != -1)
473                         {
474                                 xtw.WriteAttributeString ("id", "id" + id);
475                                 if (rootName != arrayType)
476                                         xtw.WriteAttributeString ("type", XmlSchema.InstanceNamespace, GetQualifiedNameString (xtw, rootName));
477                         }
478                         
479                         WriteComplexTypeAttributes (xtw, stype);
480                         WriteComplexTypeElements (xtw, ns, stype);
481                         
482                         xtw.WriteEndElement ();
483                 }
484                 
485                 void WriteComplexTypeAttributes (XmlTextWriter xtw, XmlSchemaComplexType stype)
486                 {
487                         WriteAttributes (xtw, stype.Attributes, stype.AnyAttribute);
488                 }
489                 
490                 void WriteComplexTypeElements (XmlTextWriter xtw, string ns, XmlSchemaComplexType stype)
491                 {
492                         if (stype.Particle != null)
493                                 WriteParticleComplexContent (xtw, ns, stype.Particle);
494                         else
495                         {
496                                 if (stype.ContentModel is XmlSchemaSimpleContent)
497                                         WriteSimpleContent (xtw, (XmlSchemaSimpleContent)stype.ContentModel);
498                                 else if (stype.ContentModel is XmlSchemaComplexContent)
499                                         WriteComplexContent (xtw, ns, (XmlSchemaComplexContent)stype.ContentModel);
500                         }
501                 }
502
503                 void WriteAttributes (XmlTextWriter xtw, XmlSchemaObjectCollection atts, XmlSchemaAnyAttribute anyat)
504                 {
505                         foreach (XmlSchemaObject at in atts)
506                         {
507                                 if (at is XmlSchemaAttribute)
508                                 {
509                                         string ns;
510                                         XmlSchemaAttribute attr = (XmlSchemaAttribute)at;
511                                         XmlSchemaAttribute refAttr = attr;
512                                         
513                                         // refAttr.Form; TODO
514                                         
515                                         if (!attr.RefName.IsEmpty) {
516                                                 refAttr = FindRefAttribute (attr.RefName);
517                                                 if (refAttr == null) throw new InvalidOperationException ("Global attribute not found: " + attr.RefName);
518                                         }
519                                         
520                                         string val;
521                                         if (!refAttr.SchemaTypeName.IsEmpty) val = FindBuiltInType (refAttr.SchemaTypeName);
522                                         else val = FindBuiltInType ((XmlSchemaSimpleType) refAttr.SchemaType);
523                                         
524                                         xtw.WriteAttributeString (refAttr.Name, val);
525                                 }
526                                 else if (at is XmlSchemaAttributeGroupRef)
527                                 {
528                                         XmlSchemaAttributeGroupRef gref = (XmlSchemaAttributeGroupRef)at;
529                                         XmlSchemaAttributeGroup grp = (XmlSchemaAttributeGroup) schemas.Find (gref.RefName, typeof(XmlSchemaAttributeGroup));
530                                         WriteAttributes (xtw, grp.Attributes, grp.AnyAttribute);
531                                 }
532                         }
533                         
534                         if (anyat != null)
535                                 xtw.WriteAttributeString ("custom-attribute","value");
536                 }
537                 
538                 void WriteParticleComplexContent (XmlTextWriter xtw, string ns, XmlSchemaParticle particle)
539                 {
540                         WriteParticleContent (xtw, ns, particle, false);
541                 }
542                 
543                 void WriteParticleContent (XmlTextWriter xtw, string ns, XmlSchemaParticle particle, bool multiValue)
544                 {
545                         if (particle is XmlSchemaGroupRef)
546                                 particle = GetRefGroupParticle ((XmlSchemaGroupRef)particle);
547
548                         if (particle.MaxOccurs > 1) multiValue = true;
549                         
550                         if (particle is XmlSchemaSequence) {
551                                 WriteSequenceContent (xtw, ns, ((XmlSchemaSequence)particle).Items, multiValue);
552                         }
553                         else if (particle is XmlSchemaChoice) {
554                                 if (((XmlSchemaChoice)particle).Items.Count == 1)
555                                         WriteSequenceContent (xtw, ns, ((XmlSchemaChoice)particle).Items, multiValue);
556                                 else
557                                         WriteChoiceContent (xtw, ns, (XmlSchemaChoice)particle, multiValue);
558                         }
559                         else if (particle is XmlSchemaAll) {
560                                 WriteSequenceContent (xtw, ns, ((XmlSchemaAll)particle).Items, multiValue);
561                         }
562                 }
563
564                 void WriteSequenceContent (XmlTextWriter xtw, string ns, XmlSchemaObjectCollection items, bool multiValue)
565                 {
566                         foreach (XmlSchemaObject item in items)
567                                 WriteContentItem (xtw, ns, item, multiValue);
568                 }
569                 
570                 void WriteContentItem (XmlTextWriter xtw, string ns, XmlSchemaObject item, bool multiValue)
571                 {
572                         if (item is XmlSchemaGroupRef)
573                                 item = GetRefGroupParticle ((XmlSchemaGroupRef)item);
574                                         
575                         if (item is XmlSchemaElement)
576                         {
577                                 XmlSchemaElement elem = (XmlSchemaElement) item;
578                                 XmlSchemaElement refElem;
579                                 if (!elem.RefName.IsEmpty) refElem = FindRefElement (elem);
580                                 else refElem = elem;
581
582                                 int num = (elem.MaxOccurs == 1 && !multiValue) ? 1 : 2;
583                                 for (int n=0; n<num; n++)
584                                 {
585                                         if (currentUse == SoapBindingUse.Literal)
586                                                 WriteElementSample (xtw, ns, refElem);
587                                         else
588                                                 WriteRefTypeSample (xtw, ns, refElem);
589                                 }
590                         }
591                         else if (item is XmlSchemaAny)
592                         {
593                                 xtw.WriteString (GetLiteral ("xml"));
594                         }
595                         else if (item is XmlSchemaParticle) {
596                                 WriteParticleContent (xtw, ns, (XmlSchemaParticle)item, multiValue);
597                         }
598                 }
599                 
600                 void WriteChoiceContent (XmlTextWriter xtw, string ns, XmlSchemaChoice choice, bool multiValue)
601                 {
602                         foreach (XmlSchemaObject item in choice.Items)
603                                 WriteContentItem (xtw, ns, item, multiValue);
604                 }
605
606                 void WriteSimpleContent (XmlTextWriter xtw, XmlSchemaSimpleContent content)
607                 {
608                         XmlSchemaSimpleContentExtension ext = content.Content as XmlSchemaSimpleContentExtension;
609                         if (ext != null)
610                                 WriteAttributes (xtw, ext.Attributes, ext.AnyAttribute);
611                                 
612                         XmlQualifiedName qname = GetContentBaseType (content.Content);
613                         xtw.WriteString (GetLiteral (FindBuiltInType (qname)));
614                 }
615
616                 string FindBuiltInType (XmlQualifiedName qname)
617                 {
618                         if (qname.Namespace == XmlSchema.Namespace)
619                                 return qname.Name;
620
621                         XmlSchemaComplexType ct = FindComplexTyype (qname);
622                         if (ct != null)
623                         {
624                                 XmlSchemaSimpleContent sc = ct.ContentModel as XmlSchemaSimpleContent;
625                                 if (sc == null) throw new InvalidOperationException ("Invalid schema");
626                                 return FindBuiltInType (GetContentBaseType (sc.Content));
627                         }
628                         
629                         XmlSchemaSimpleType st = (XmlSchemaSimpleType) schemas.Find (qname, typeof(XmlSchemaSimpleType));
630                         if (st != null)
631                                 return FindBuiltInType (st);
632
633                         throw new InvalidOperationException ("Definition of type " + qname + " not found");
634                 }
635
636                 string FindBuiltInType (XmlSchemaSimpleType st)
637                 {
638                         if (st.Content is XmlSchemaSimpleTypeRestriction) {
639                                 return FindBuiltInType (GetContentBaseType (st.Content));
640                         }
641                         else if (st.Content is XmlSchemaSimpleTypeList) {
642                                 string s = FindBuiltInType (GetContentBaseType (st.Content));
643                                 return s + " " + s + " ...";
644                         }
645                         else if (st.Content is XmlSchemaSimpleTypeUnion)
646                         {
647                                 // Check if all types of the union are equal. If not, then will use anyType.
648                                 XmlSchemaSimpleTypeUnion uni = (XmlSchemaSimpleTypeUnion) st.Content;
649                                 string utype = null;
650
651                                 // Anonymous types are unique
652                                 if (uni.BaseTypes.Count != 0 && uni.MemberTypes.Length != 0)
653                                         return "string";
654
655                                 foreach (XmlQualifiedName mt in uni.MemberTypes)
656                                 {
657                                         string qn = FindBuiltInType (mt);
658                                         if (utype != null && qn != utype) return "string";
659                                         else utype = qn;
660                                 }
661                                 return utype;
662                         }
663                         else
664                                 return "string";
665                 }
666                 
667
668                 XmlQualifiedName GetContentBaseType (XmlSchemaObject ob)
669                 {
670                         if (ob is XmlSchemaSimpleContentExtension)
671                                 return ((XmlSchemaSimpleContentExtension)ob).BaseTypeName;
672                         else if (ob is XmlSchemaSimpleContentRestriction)
673                                 return ((XmlSchemaSimpleContentRestriction)ob).BaseTypeName;
674                         else if (ob is XmlSchemaSimpleTypeRestriction)
675                                 return ((XmlSchemaSimpleTypeRestriction)ob).BaseTypeName;
676                         else if (ob is XmlSchemaSimpleTypeList)
677                                 return ((XmlSchemaSimpleTypeList)ob).ItemTypeName;
678                         else
679                                 return null;
680                 }
681
682                 void WriteComplexContent (XmlTextWriter xtw, string ns, XmlSchemaComplexContent content)
683                 {
684                         XmlQualifiedName qname;
685
686                         XmlSchemaComplexContentExtension ext = content.Content as XmlSchemaComplexContentExtension;
687                         if (ext != null) qname = ext.BaseTypeName;
688                         else {
689                                 XmlSchemaComplexContentRestriction rest = (XmlSchemaComplexContentRestriction)content.Content;
690                                 qname = rest.BaseTypeName;
691                                 if (qname == arrayType) {
692                                         ParseArrayType (rest, out qname);
693                                         XmlSchemaElement elem = new XmlSchemaElement ();
694                                         elem.Name = "Item";
695                                         elem.SchemaTypeName = qname;
696                                         
697                                         xtw.WriteAttributeString ("arrayType", SoapEncodingNamespace, qname.Name + "[2]");
698                                         WriteContentItem (xtw, ns, elem, true);
699                                         return;
700                                 }
701                         }
702                         
703                         // Add base map members to this map
704                         XmlSchemaComplexType ctype = FindComplexTyype (qname);
705                         WriteComplexTypeAttributes (xtw, ctype);
706                         
707                         if (ext != null) {
708                                 // Add the members of this map
709                                 WriteAttributes (xtw, ext.Attributes, ext.AnyAttribute);
710                                 if (ext.Particle != null)
711                                         WriteParticleComplexContent (xtw, ns, ext.Particle);
712                         }
713                         
714                         WriteComplexTypeElements (xtw, ns, ctype);
715                 }
716                 
717                 void ParseArrayType (XmlSchemaComplexContentRestriction rest, out XmlQualifiedName qtype)
718                 {
719                         XmlSchemaAttribute arrayTypeAt = FindArrayAttribute (rest.Attributes);
720                         XmlAttribute[] uatts = arrayTypeAt.UnhandledAttributes;
721                         if (uatts == null || uatts.Length == 0) throw new InvalidOperationException ("arrayType attribute not specified in array declaration");
722                         
723                         XmlAttribute xat = null;
724                         foreach (XmlAttribute at in uatts)
725                                 if (at.LocalName == "arrayType" && at.NamespaceURI == WsdlNamespace)
726                                         { xat = at; break; }
727                         
728                         if (xat == null) 
729                                 throw new InvalidOperationException ("arrayType attribute not specified in array declaration");
730                         
731                         string arrayType = xat.Value;
732                         string type, ns;
733                         int i = arrayType.LastIndexOf (":");
734                         if (i == -1) ns = "";
735                         else ns = arrayType.Substring (0,i);
736                         
737                         int j = arrayType.IndexOf ("[", i+1);
738                         if (j == -1) throw new InvalidOperationException ("Cannot parse WSDL array type: " + arrayType);
739                         type = arrayType.Substring (i+1);
740                         type = type.Substring (0, type.Length-2);
741                         
742                         qtype = new XmlQualifiedName (type, ns);
743                 }
744                 
745                 XmlSchemaAttribute FindArrayAttribute (XmlSchemaObjectCollection atts)
746                 {
747                         foreach (object ob in atts)
748                         {
749                                 XmlSchemaAttribute att = ob as XmlSchemaAttribute;
750                                 if (att != null && att.RefName == arrayTypeRefName) return att;
751                                 
752                                 XmlSchemaAttributeGroupRef gref = ob as XmlSchemaAttributeGroupRef;
753                                 if (gref != null)
754                                 {
755                                         XmlSchemaAttributeGroup grp = (XmlSchemaAttributeGroup) schemas.Find (gref.RefName, typeof(XmlSchemaAttributeGroup));
756                                         att = FindArrayAttribute (grp.Attributes);
757                                         if (att != null) return att;
758                                 }
759                         }
760                         return null;
761                 }
762                 
763                 void WriteSimpleTypeSample (XmlTextWriter xtw, XmlSchemaSimpleType stype)
764                 {
765                         xtw.WriteString (GetLiteral (FindBuiltInType (stype)));
766                 }
767                 
768                 XmlSchemaParticle GetRefGroupParticle (XmlSchemaGroupRef refGroup)
769                 {
770                         XmlSchemaGroup grp = (XmlSchemaGroup) schemas.Find (refGroup.RefName, typeof (XmlSchemaGroup));
771                         return grp.Particle;
772                 }
773
774                 XmlSchemaElement FindRefElement (XmlSchemaElement elem)
775                 {
776                         if (elem.RefName.Namespace == XmlSchema.Namespace)
777                         {
778                                 if (anyElement != null) return anyElement;
779                                 anyElement = new XmlSchemaElement ();
780                                 anyElement.Name = "any";
781                                 anyElement.SchemaTypeName = anyType;
782                                 return anyElement;
783                         }
784                         return (XmlSchemaElement) schemas.Find (elem.RefName, typeof(XmlSchemaElement));
785                 }
786                 
787                 XmlSchemaAttribute FindRefAttribute (XmlQualifiedName refName)
788                 {
789                         if (refName.Namespace == XmlSchema.Namespace)
790                         {
791                                 XmlSchemaAttribute at = new XmlSchemaAttribute ();
792                                 at.Name = refName.Name;
793                                 at.SchemaTypeName = new XmlQualifiedName ("string",XmlSchema.Namespace);
794                                 return at;
795                         }
796                         return (XmlSchemaAttribute) schemas.Find (refName, typeof(XmlSchemaAttribute));
797                 }
798                 
799                 void WriteRefTypeSample (XmlTextWriter xtw, string ns, XmlSchemaElement elem)
800                 {
801                         if (elem.SchemaTypeName.Namespace == XmlSchema.Namespace || schemas.Find (elem.SchemaTypeName, typeof(XmlSchemaSimpleType)) != null)
802                                 WriteElementSample (xtw, ns, elem);
803                         else
804                         {
805                                 xtw.WriteStartElement (elem.Name, ns);
806                                 xtw.WriteAttributeString ("href", "#id" + (queue.Count+1));
807                                 xtw.WriteEndElement ();
808                                 queue.Add (new EncodedType (ns, elem));
809                         }
810                 }
811                 
812                 void WriteQueuedTypeSamples (XmlTextWriter xtw)
813                 {
814                         for (int n=0; n<queue.Count; n++)
815                         {
816                                 EncodedType ec = (EncodedType) queue[n];
817                                 XmlSchemaComplexType st = FindComplexTyype (ec.Element.SchemaTypeName);
818                                 WriteComplexTypeSample (xtw, st, ec.Element.SchemaTypeName, n+1);
819                         }
820                 }
821                 
822                 XmlSchemaComplexType FindComplexTyype (XmlQualifiedName qname)
823                 {
824                         if (qname.Name.IndexOf ("[]") != -1)
825                         {
826                                 XmlSchemaComplexType stype = new XmlSchemaComplexType ();
827                                 stype.ContentModel = new XmlSchemaComplexContent ();
828                                 
829                                 XmlSchemaComplexContentRestriction res = new XmlSchemaComplexContentRestriction ();
830                                 stype.ContentModel.Content = res;
831                                 res.BaseTypeName = arrayType;
832                                 
833                                 XmlSchemaAttribute att = new XmlSchemaAttribute ();
834                                 att.RefName = arrayTypeRefName;
835                                 res.Attributes.Add (att);
836                                 
837                                 XmlAttribute xat = document.CreateAttribute ("arrayType", WsdlNamespace);
838                                 xat.Value = qname.Namespace + ":" + qname.Name;
839                                 att.UnhandledAttributes = new XmlAttribute[] {xat};
840                                 return stype;
841                         }
842                                 
843                         return (XmlSchemaComplexType) schemas.Find (qname, typeof(XmlSchemaComplexType));
844                 }
845                 
846                 string GetQualifiedNameString (XmlTextWriter xtw, XmlQualifiedName qname)
847                 {
848                         string pref = xtw.LookupPrefix (qname.Namespace);
849                         if (pref != null) return pref + ":" + qname.Name;
850                         
851                         xtw.WriteAttributeString ("xmlns", "q1", null, qname.Namespace);
852                         return "q1:" + qname.Name;
853                 }
854                                 
855                 protected virtual string GetLiteral (string s)
856                 {
857                         return s;
858                 }
859
860                 void GetOperationFormat (OperationBinding obin, out SoapBindingStyle style, out SoapBindingUse use)
861                 {
862                         style = SoapBindingStyle.Document;
863                         use = SoapBindingUse.Literal;
864                         SoapOperationBinding sob = obin.Extensions.Find (typeof(SoapOperationBinding)) as SoapOperationBinding;
865                         if (sob != null) {
866                                 style = sob.Style;
867                                 SoapBodyBinding sbb = obin.Input.Extensions.Find (typeof(SoapBodyBinding)) as SoapBodyBinding;
868                                 if (sbb != null)
869                                         use = sbb.Use;
870                         }
871                 }
872         }
873         
874         
875         
876 }
877