3 // DefaultWsdlHelpGenerator.aspx:
6 // Lluis Sanchez Gual (lluis@ximian.com)
8 // (C) 2003 Ximian, Inc. http://www.ximian.com
12 <%@ Import Namespace="System.Collections" %>
13 <%@ Import Namespace="System.IO" %>
14 <%@ Import Namespace="System.Xml.Serialization" %>
15 <%@ Import Namespace="System.Xml" %>
16 <%@ Import Namespace="System.Xml.Schema" %>
17 <%@ Import Namespace="System.Web.Services.Description" %>
18 <%@ Import Namespace="System" %>
19 <%@ Import Namespace="System.Net" %>
20 <%@ Import Namespace="System.Globalization" %>
21 <%@ Import Namespace="System.Resources" %>
22 <%@ Import Namespace="System.Diagnostics" %>
23 <%@ Import Namespace="System.CodeDom" %>
24 <%@ Import Namespace="System.CodeDom.Compiler" %>
25 <%@ Import Namespace="Microsoft.CSharp" %>
26 <%@ Import Namespace="Microsoft.VisualBasic" %>
27 <%@ Import Namespace="System.Text" %>
28 <%@ Import Namespace="System.Text.RegularExpressions" %>
29 <%@ Import Namespace="System.Security.Cryptography.X509Certificates" %>
30 <%@ Assembly name="System.Web.Services" %>
31 <%@ Page debug="true" %>
34 <script language="C#" runat="server">
36 ServiceDescriptionCollection descriptions;
39 string WebServiceName;
40 string WebServiceDescription;
43 string DefaultBinding;
44 ArrayList ServiceProtocols;
46 string CurrentOperationName;
47 string CurrentOperationBinding;
48 string OperationDocumentation;
49 string CurrentOperationFormat;
50 bool CurrentOperationSupportsTest;
53 string CurrentOperationProtocols;
54 int CodeTextColumns = 95;
56 void Page_Load(object sender, EventArgs e)
58 descriptions = (ServiceDescriptionCollection) Context.Items["wsdls"];
59 schemas = (XmlSchemas) Context.Items["schemas"];
61 ServiceDescription desc = descriptions [0];
62 if (schemas.Count == 0) schemas = desc.Types.Schemas;
64 Service service = desc.Services[0];
65 WebServiceName = service.Name;
66 if (desc.Bindings.Count == 0)
69 DefaultBinding = desc.Bindings[0].Name;
70 WebServiceDescription = service.Documentation;
71 ServiceProtocols = FindServiceProtocols (null);
73 CurrentOperationName = Request.QueryString["op"];
74 CurrentOperationBinding = Request.QueryString["bnd"];
75 if (CurrentOperationName != null) BuildOperationInfo ();
77 PageName = HttpUtility.UrlEncode (Path.GetFileName(Request.Path), Encoding.UTF8);
79 ArrayList list = new ArrayList ();
80 foreach (ServiceDescription sd in descriptions) {
81 foreach (Binding bin in sd.Bindings)
82 if (bin.Extensions.Find (typeof(SoapBinding)) != null) list.Add (bin);
85 BindingsRepeater.DataSource = list;
89 void BuildOperationInfo ()
91 InParams = new ArrayList ();
92 OutParams = new ArrayList ();
94 Port port = FindPort (CurrentOperationBinding, null);
95 Binding binding = descriptions.GetBinding (port.Binding);
97 PortType portType = descriptions.GetPortType (binding.Type);
98 Operation oper = FindOperation (portType, CurrentOperationName);
100 OperationDocumentation = oper.Documentation;
101 if (OperationDocumentation == null || OperationDocumentation == "")
102 OperationDocumentation = "No additional remarks";
104 foreach (OperationMessage opm in oper.Messages)
106 if (opm is OperationInput)
107 BuildParameters (InParams, opm);
108 else if (opm is OperationOutput)
109 BuildParameters (OutParams, opm);
112 // Protocols supported by the operation
113 CurrentOperationProtocols = "";
114 ArrayList prots = FindServiceProtocols (CurrentOperationName);
115 for (int n=0; n<prots.Count; n++) {
116 if (n != 0) CurrentOperationProtocols += ", ";
117 CurrentOperationProtocols += (string) prots[n];
120 CurrentOperationSupportsTest = prots.Contains ("HttpGet") || prots.Contains ("HttpPost");
123 OperationBinding obin = FindOperation (binding, CurrentOperationName);
125 CurrentOperationFormat = GetOperationFormat (obin);
127 InputParamsRepeater.DataSource = InParams;
128 InputFormParamsRepeater.DataSource = InParams;
129 OutputParamsRepeater.DataSource = OutParams;
132 void BuildParameters (ArrayList list, OperationMessage opm)
134 Message msg = descriptions.GetMessage (opm.Message);
135 if (msg.Parts.Count > 0 && msg.Parts[0].Name == "parameters")
137 MessagePart part = msg.Parts[0];
138 XmlSchemaComplexType ctype;
139 if (part.Element == XmlQualifiedName.Empty)
141 ctype = (XmlSchemaComplexType) schemas.Find (part.Type, typeof(XmlSchemaComplexType));
145 XmlSchemaElement elem = (XmlSchemaElement) schemas.Find (part.Element, typeof(XmlSchemaElement));
146 ctype = (XmlSchemaComplexType) elem.SchemaType;
148 XmlSchemaSequence seq = ctype.Particle as XmlSchemaSequence;
149 if (seq == null) return;
151 foreach (XmlSchemaObject ob in seq.Items)
153 Parameter p = new Parameter();
154 p.Description = "No additional remarks";
156 if (ob is XmlSchemaElement)
158 XmlSchemaElement selem = GetRefElement ((XmlSchemaElement)ob);
160 p.Type = selem.SchemaTypeName.Name;
172 foreach (MessagePart part in msg.Parts)
174 Parameter p = new Parameter ();
175 p.Description = "No additional remarks";
177 if (part.Element == XmlQualifiedName.Empty)
178 p.Type = part.Type.Name;
181 XmlSchemaElement elem = (XmlSchemaElement) schemas.Find (part.Element, typeof(XmlSchemaElement));
182 p.Type = elem.SchemaTypeName.Name;
189 string GetOperationFormat (OperationBinding obin)
192 SoapOperationBinding sob = obin.Extensions.Find (typeof(SoapOperationBinding)) as SoapOperationBinding;
194 format = sob.Style.ToString ();
195 SoapBodyBinding sbb = obin.Input.Extensions.Find (typeof(SoapBodyBinding)) as SoapBodyBinding;
197 format += " / " + sbb.Use;
202 XmlSchemaElement GetRefElement (XmlSchemaElement elem)
204 if (!elem.RefName.IsEmpty)
205 return (XmlSchemaElement) schemas.Find (elem.RefName, typeof(XmlSchemaElement));
210 ArrayList FindServiceProtocols(string operName)
212 ArrayList table = new ArrayList ();
213 Service service = descriptions[0].Services[0];
214 foreach (Port port in service.Ports)
217 Binding bin = descriptions.GetBinding (port.Binding);
218 if (bin.Extensions.Find (typeof(SoapBinding)) != null)
222 HttpBinding hb = (HttpBinding) bin.Extensions.Find (typeof(HttpBinding));
223 if (hb != null && hb.Verb == "POST") prot = "HttpPost";
224 else if (hb != null && hb.Verb == "GET") prot = "HttpGet";
227 if (prot != null && operName != null)
229 if (FindOperation (bin, operName) == null)
233 if (prot != null && !table.Contains (prot))
239 Port FindPort (string portName, string protocol)
241 Service service = descriptions[0].Services[0];
242 foreach (Port port in service.Ports)
244 if (portName == null)
246 Binding binding = descriptions.GetBinding (port.Binding);
247 if (GetProtocol (binding) == protocol) return port;
249 else if (port.Name == portName)
255 string GetProtocol (Binding binding)
257 if (binding.Extensions.Find (typeof(SoapBinding)) != null) return "Soap";
258 HttpBinding hb = (HttpBinding) binding.Extensions.Find (typeof(HttpBinding));
259 if (hb == null) return "";
260 if (hb.Verb == "POST") return "HttpPost";
261 if (hb.Verb == "GET") return "HttpGet";
266 Operation FindOperation (PortType portType, string name)
268 foreach (Operation oper in portType.Operations) {
269 if (oper.Messages.Input.Name != null) {
270 if (oper.Messages.Input.Name == name) return oper;
273 if (oper.Name == name) return oper;
279 OperationBinding FindOperation (Binding binding, string name)
281 foreach (OperationBinding oper in binding.Operations) {
282 if (oper.Input.Name != null) {
283 if (oper.Input.Name == name) return oper;
286 if (oper.Name == name) return oper;
292 string FormatBindingName (string name)
294 if (name == DefaultBinding) return "Methods";
295 else return "Methods for binding<br>" + name;
298 string GetOpName (object op)
300 OperationBinding ob = op as OperationBinding;
301 if (ob == null) return "";
302 if (ob.Input.Name != null) return ob.Input.Name;
308 get { return Request.QueryString ["ext"] == "testform"; }
311 class NoCheckCertificatePolicy : ICertificatePolicy {
312 public bool CheckValidationResult (ServicePoint a, X509Certificate b, WebRequest c, int d)
318 string GetTestResult ()
320 if (!HasFormResult) return null;
324 for (int n=0; n<Request.QueryString.Count; n++)
327 if (qs != "") qs += "&";
328 qs += Request.QueryString.GetKey(n) + "=" + Server.UrlEncode (Request.QueryString [n]);
330 if (Request.QueryString.GetKey(n) == "ext") fill = true;
333 string location = null;
334 ServiceDescription desc = descriptions [0];
335 Service service = desc.Services[0];
336 foreach (Port port in service.Ports)
337 if (port.Name == CurrentOperationBinding)
339 SoapAddressBinding sbi = (SoapAddressBinding) port.Extensions.Find (typeof(SoapAddressBinding));
341 location = sbi.Location;
344 if (location == null)
345 return "Could not locate web service";
349 string url = location + "/" + CurrentOperationName;
350 Uri uri = new Uri (url);
352 if (CurrentOperationProtocols.IndexOf ("HttpGet") < 0) {
353 req = WebRequest.Create (url);
355 if (qs != null && qs.Length > 0) {
356 req.ContentType = "application/x-www-form-urlencoded";
357 byte [] postBuffer = Encoding.UTF8.GetBytes (qs);
358 req.ContentLength = postBuffer.Length;
359 using (Stream requestStream = req.GetRequestStream ())
360 requestStream.Write (postBuffer, 0, postBuffer.Length);
364 req = WebRequest.Create (url + "?" + qs);
365 if (url.StartsWith ("https:"))
366 ServicePointManager.CertificatePolicy = new NoCheckCertificatePolicy ();
367 HttpCookieCollection cookies = Request.Cookies;
368 int last = cookies.Count;
370 CookieContainer container = new CookieContainer ();
371 for (int i = 0; i < last; i++) {
372 HttpCookie hcookie = cookies [i];
373 Cookie cookie = new Cookie (hcookie.Name, hcookie.Value, hcookie.Path, hcookie.Domain);
374 container.Add (uri, cookie);
376 ((HttpWebRequest) req).CookieContainer = container;
378 WebResponse resp = req.GetResponse();
379 StreamReader sr = new StreamReader (resp.GetResponseStream());
380 string s = sr.ReadToEnd ();
382 return "<div class='code-xml'>" + ColorizeXml(WrapText(s,CodeTextColumns)) + "</div>";
386 string res = "<b style='color:red'>" + ex.Message + "</b>";
387 WebException wex = ex as WebException;
390 WebResponse resp = wex.Response;
392 StreamReader sr = new StreamReader (resp.GetResponseStream());
393 string s = sr.ReadToEnd ();
395 res += "<div class='code-xml'>" + ColorizeXml(WrapText(s,CodeTextColumns)) + "</div>";
402 string GenerateOperationMessages (string protocol, bool generateInput)
404 if (!IsOperationSupported (protocol)) return "";
407 if (protocol != "Soap") port = FindPort (null, protocol);
408 else port = FindPort (CurrentOperationBinding, null);
410 Binding binding = descriptions.GetBinding (port.Binding);
411 OperationBinding obin = FindOperation (binding, CurrentOperationName);
412 PortType portType = descriptions.GetPortType (binding.Type);
413 Operation oper = FindOperation (portType, CurrentOperationName);
415 HtmlSampleGenerator sg = new HtmlSampleGenerator (descriptions, schemas);
416 string txt = sg.GenerateMessage (port, obin, oper, protocol, generateInput);
417 if (protocol == "Soap") txt = WrapText (txt,CodeTextColumns);
418 txt = ColorizeXml (txt);
419 txt = txt.Replace ("@placeholder!","<span class='literal-placeholder'>");
420 txt = txt.Replace ("!placeholder@","</span>");
424 bool IsOperationSupported (string protocol)
426 if (CurrentPage != "op" || CurrentTab != "msg") return false;
427 if (protocol == "Soap") return true;
429 Port port = FindPort (null, protocol);
430 if (port == null) return false;
431 Binding binding = descriptions.GetBinding (port.Binding);
432 if (binding == null) return false;
433 return FindOperation (binding, CurrentOperationName) != null;
437 // Proxy code generation
440 string GetProxyCode ()
442 CodeNamespace codeNamespace = new CodeNamespace();
443 CodeCompileUnit codeUnit = new CodeCompileUnit();
445 codeUnit.Namespaces.Add (codeNamespace);
447 ServiceDescriptionImporter importer = new ServiceDescriptionImporter();
449 foreach (ServiceDescription sd in descriptions)
450 importer.AddServiceDescription(sd, null, null);
452 foreach (XmlSchema sc in schemas)
453 importer.Schemas.Add (sc);
455 importer.Import(codeNamespace, codeUnit);
457 string langId = Request.QueryString ["lang"];
458 if (langId == null || langId == "") langId = "cs";
459 CodeDomProvider provider = GetProvider (langId);
460 ICodeGenerator generator = provider.CreateGenerator();
461 CodeGeneratorOptions options = new CodeGeneratorOptions();
463 StringWriter sw = new StringWriter ();
464 generator.GenerateCodeFromCompileUnit(codeUnit, sw, options);
466 return Colorize (WrapText (sw.ToString (), CodeTextColumns), langId);
469 public string CurrentLanguage
472 string langId = Request.QueryString ["lang"];
473 if (langId == null || langId == "") langId = "cs";
478 public string CurrentProxytName
481 string lan = CurrentLanguage == "cs" ? "C#" : "Visual Basic";
482 return lan + " Client Proxy";
486 private CodeDomProvider GetProvider(string langId)
488 switch (langId.ToUpper())
490 case "CS": return new CSharpCodeProvider();
491 case "VB": return new VBCodeProvider();
492 default: return null;
497 // Document generation
500 string GenerateDocument ()
502 StringWriter sw = new StringWriter ();
504 if (CurrentDocType == "wsdl")
505 descriptions [CurrentDocInd].Write (sw);
506 else if (CurrentDocType == "schema")
507 schemas [CurrentDocInd].Write (sw);
509 return Colorize (WrapText (sw.ToString (), CodeTextColumns), "xml");
512 public string CurrentDocType
514 get { return Request.QueryString ["doctype"] != null ? Request.QueryString ["doctype"] : "wsdl"; }
517 public int CurrentDocInd
519 get { return Request.QueryString ["docind"] != null ? int.Parse (Request.QueryString ["docind"]) : 0; }
522 public string CurrentDocumentName
525 if (CurrentDocType == "wsdl")
526 return "WSDL document for namespace \"" + descriptions [CurrentDocInd].TargetNamespace + "\"";
528 return "Xml Schema for namespace \"" + schemas [CurrentDocInd].TargetNamespace + "\"";
536 bool firstTab = true;
537 ArrayList disabledTabs = new ArrayList ();
541 get { return Request.QueryString["tab"] != null ? Request.QueryString["tab"] : "main" ; }
546 get { return Request.QueryString["page"] != null ? Request.QueryString["page"] : "main" ; }
551 if (CurrentOperationName != null)
553 WriteTab ("main","Overview");
554 WriteTab ("test","Test Form");
555 WriteTab ("msg","Message Layout");
559 void WriteTab (string id, string label)
561 if (!firstTab) Response.Write(" | ");
564 string cname = CurrentTab == id ? "tabLabelOn" : "tabLabelOff";
565 Response.Write ("<a href='" + PageName + "?" + GetPageContext(null) + GetDataContext() + "tab=" + id + "' style='text-decoration:none'>");
566 Response.Write ("<span class='" + cname + "'>" + label + "</span>");
567 Response.Write ("</a>");
570 string GetTabContext (string pag, string tab)
572 if (tab == null) tab = CurrentTab;
573 if (pag == null) pag = CurrentPage;
574 if (pag != CurrentPage) tab = "main";
575 return "page=" + pag + "&tab=" + tab + "&";
578 string GetPageContext (string pag)
580 if (pag == null) pag = CurrentPage;
581 return "page=" + pag + "&";
594 static string keywords_cs =
595 "(\\babstract\\b|\\bevent\\b|\\bnew\\b|\\bstruct\\b|\\bas\\b|\\bexplicit\\b|\\bnull\\b|\\bswitch\\b|\\bbase\\b|\\bextern\\b|" +
596 "\\bobject\\b|\\bthis\\b|\\bbool\\b|\\bfalse\\b|\\boperator\\b|\\bthrow\\b|\\bbreak\\b|\\bfinally\\b|\\bout\\b|\\btrue\\b|" +
597 "\\bbyte\\b|\\bfixed\\b|\\boverride\\b|\\btry\\b|\\bcase\\b|\\bfloat\\b|\\bparams\\b|\\btypeof\\b|\\bcatch\\b|\\bfor\\b|" +
598 "\\bprivate\\b|\\buint\\b|\\bchar\\b|\\bforeach\\b|\\bprotected\\b|\\bulong\\b|\\bchecked\\b|\\bgoto\\b|\\bpublic\\b|" +
599 "\\bunchecked\\b|\\bclass\\b|\\bif\\b|\\breadonly\\b|\\bunsafe\\b|\\bconst\\b|\\bimplicit\\b|\\bref\\b|\\bushort\\b|" +
600 "\\bcontinue\\b|\\bin\\b|\\breturn\\b|\\busing\\b|\\bdecimal\\b|\\bint\\b|\\bsbyte\\b|\\bvirtual\\b|\\bdefault\\b|" +
601 "\\binterface\\b|\\bsealed\\b|\\bvolatile\\b|\\bdelegate\\b|\\binternal\\b|\\bshort\\b|\\bvoid\\b|\\bdo\\b|\\bis\\b|" +
602 "\\bsizeof\\b|\\bwhile\\b|\\bdouble\\b|\\block\\b|\\bstackalloc\\b|\\belse\\b|\\blong\\b|\\bstatic\\b|\\benum\\b|" +
603 "\\bnamespace\\b|\\bstring\\b)";
605 static string keywords_vb =
606 "(\\bAddHandler\\b|\\bAddressOf\\b|\\bAlias\\b|\\bAnd\\b|\\bAndAlso\\b|\\bAnsi\\b|\\bAs\\b|\\bAssembly\\b|" +
607 "\\bAuto\\b|\\bBoolean\\b|\\bByRef\\b|\\bByte\\b|\\bByVal\\b|\\bCall\\b|\\bCase\\b|\\bCatch\\b|" +
608 "\\bCBool\\b|\\bCByte\\b|\\bCChar\\b|\\bCDate\\b|\\bCDec\\b|\\bCDbl\\b|\\bChar\\b|\\bCInt\\b|" +
609 "\\bClass\\b|\\bCLng\\b|\\bCObj\\b|\\bConst\\b|\\bCShort\\b|\\bCSng\\b|\\bCStr\\b|\\bCType\\b|" +
610 "\\bDate\\b|\\bDecimal\\b|\\bDeclare\\b|\\bDefault\\b|\\bDelegate\\b|\\bDim\\b|\\bDirectCast\\b|\\bDo\\b|" +
611 "\\bDouble\\b|\\bEach\\b|\\bElse\\b|\\bElseIf\\b|\\bEnd\\b|\\bEnum\\b|\\bErase\\b|\\bError\\b|" +
612 "\\bEvent\\b|\\bExit\\b|\\bFalse\\b|\\bFinally\\b|\\bFor\\b|\\bFriend\\b|\\bFunction\\b|\\bGet\\b|" +
613 "\\bGetType\\b|\\bGoSub\\b|\\bGoTo\\b|\\bHandles\\b|\\bIf\\b|\\bImplements\\b|\\bImports\\b|\\bIn\\b|" +
614 "\\bInherits\\b|\\bInteger\\b|\\bInterface\\b|\\bIs\\b|\\bLet\\b|\\bLib\\b|\\bLike\\b|\\bLong\\b|" +
615 "\\bLoop\\b|\\bMe\\b|\\bMod\\b|\\bModule\\b|\\bMustInherit\\b|\\bMustOverride\\b|\\bMyBase\\b|\\bMyClass\\b|" +
616 "\\bNamespace\\b|\\bNew\\b|\\bNext\\b|\\bNot\\b|\\bNothing\\b|\\bNotInheritable\\b|\\bNotOverridable\\b|\\bObject\\b|" +
617 "\\bOn\\b|\\bOption\\b|\\bOptional\\b|\\bOr\\b|\\bOrElse\\b|\\bOverloads\\b|\\bOverridable\\b|\\bOverrides\\b|" +
618 "\\bParamArray\\b|\\bPreserve\\b|\\bPrivate\\b|\\bProperty\\b|\\bProtected\\b|\\bPublic\\b|\\bRaiseEvent\\b|\\bReadOnly\\b|" +
619 "\\bReDim\\b|\\bREM\\b|\\bRemoveHandler\\b|\\bResume\\b|\\bReturn\\b|\\bSelect\\b|\\bSet\\b|\\bShadows\\b|" +
620 "\\bShared\\b|\\bShort\\b|\\bSingle\\b|\\bStatic\\b|\\bStep\\b|\\bStop\\b|\\bString\\b|\\bStructure\\b|" +
621 "\\bSub\\b|\\bSyncLock\\b|\\bThen\\b|\\bThrow\\b|\\bTo\\b|\\bTrue\\b|\\bTry\\b|\\bTypeOf\\b|" +
622 "\\bUnicode\\b|\\bUntil\\b|\\bVariant\\b|\\bWhen\\b|\\bWhile\\b|\\bWith\\b|\\bWithEvents\\b|\\bWriteOnly\\b|\\bXor\\b)";
624 string Colorize (string text, string lang)
626 if (lang == "xml") return ColorizeXml (text);
627 else if (lang == "cs") return ColorizeCs (text);
628 else if (lang == "vb") return ColorizeVb (text);
632 string ColorizeXml (string text)
634 text = text.Replace (" ", " ");
635 Regex re = new Regex ("\r\n|\r|\n");
636 text = re.Replace (text, "_br_");
638 re = new Regex ("<\\s*(\\/?)\\s*([\\s\\S]*?)\\s*(\\/?)\\s*>");
639 text = re.Replace (text,"{blue:<$1}{maroon:$2}{blue:$3>}");
641 re = new Regex ("\\{(\\w*):([\\s\\S]*?)\\}");
642 text = re.Replace (text,"<span style='color:$1'>$2</span>");
644 re = new Regex ("\"(.*?)\"");
645 text = re.Replace (text,"\"<span style='color:purple'>$1</span>\"");
648 text = text.Replace ("\t", " ");
649 text = text.Replace ("_br_", "<br>");
653 string ColorizeCs (string text)
655 text = text.Replace (" ", " ");
657 text = text.Replace ("<", "<");
658 text = text.Replace (">", ">");
660 Regex re = new Regex ("\"((((?!\").)|\\\")*?)\"");
661 text = re.Replace (text,"<span style='color:purple'>\"$1\"</span>");
663 re = new Regex ("//(((.(?!\"</span>))|\"(((?!\").)*)\"</span>)*)(\r|\n|\r\n)");
664 text = re.Replace (text,"<span style='color:green'>//$1</span><br/>");
666 re = new Regex (keywords_cs);
667 text = re.Replace (text,"<span style='color:blue'>$1</span>");
669 text = text.Replace ("\t"," ");
670 text = text.Replace ("\n","<br/>");
675 string ColorizeVb (string text)
677 text = text.Replace (" ", " ");
679 /* Regex re = new Regex ("\"((((?!\").)|\\\")*?)\"");
680 text = re.Replace (text,"<span style='color:purple'>\"$1\"</span>");
682 re = new Regex ("'(((.(?!\"\\<\\/span\\>))|\"(((?!\").)*)\"\\<\\/span\\>)*)(\r|\n|\r\n)");
683 text = re.Replace (text,"<span style='color:green'>//$1</span><br/>");
685 re = new Regex (keywords_vb);
686 text = re.Replace (text,"<span style='color:blue'>$1</span>");
688 text = text.Replace ("\t"," ");
689 text = text.Replace ("\n","<br/>");
694 // Helper methods and classes
697 string GetDataContext ()
699 return "op=" + CurrentOperationName + "&bnd=" + CurrentOperationBinding + "&";
702 string GetOptionSel (string v1, string v2)
704 string op = "<option ";
705 if (v1 == v2) op += "selected ";
706 return op + "value='" + v1 + "'>";
709 string WrapText (string text, int maxChars)
711 text = text.Replace(" />","/>");
713 string linspace = null;
717 bool inquotes = false;
719 string sublineIndent = "";
720 System.Text.StringBuilder sb = new System.Text.StringBuilder ();
721 for (int n=0; n<text.Length; n++)
725 if (c=='\r' || c=='\n' || n==text.Length-1)
727 sb.Append (linspace + sublineIndent + text.Substring (linstart, n-linstart+1));
737 if (lastc==',' || lastc=='(')
739 if (!inquotes) breakpos = n;
742 if (lincount > maxChars && breakpos >= linstart)
744 if (linspace != null)
745 sb.Append (linspace + sublineIndent);
746 sb.Append (text.Substring (linstart, breakpos-linstart));
749 lincount = linspace.Length + sublineIndent.Length + (n-breakpos);
753 if (c==' ' || c=='\t')
760 inquotes = !inquotes;
763 if (linspace == null) {
764 linspace = text.Substring (linstart, n-linstart);
771 return sb.ToString ();
780 public string Name { get { return name; } set { name = value; } }
781 public string Type { get { return type; } set { type = value; } }
782 public string Description { get { return description; } set { description = value; } }
785 public class HtmlSampleGenerator: SampleGenerator
787 public HtmlSampleGenerator (ServiceDescriptionCollection services, XmlSchemas schemas)
788 : base (services, schemas)
792 protected override string GetLiteral (string s)
794 return "@placeholder!" + s + "!placeholder@";
799 public class SampleGenerator
801 protected ServiceDescriptionCollection descriptions;
802 protected XmlSchemas schemas;
803 XmlSchemaElement anyElement;
805 SoapBindingUse currentUse;
806 XmlDocument document = new XmlDocument ();
808 static readonly XmlQualifiedName anyType = new XmlQualifiedName ("anyType",XmlSchema.Namespace);
809 static readonly XmlQualifiedName arrayType = new XmlQualifiedName ("Array","http://schemas.xmlsoap.org/soap/encoding/");
810 static readonly XmlQualifiedName arrayTypeRefName = new XmlQualifiedName ("arrayType","http://schemas.xmlsoap.org/soap/encoding/");
811 const string SoapEnvelopeNamespace = "http://schemas.xmlsoap.org/soap/envelope/";
812 const string WsdlNamespace = "http://schemas.xmlsoap.org/wsdl/";
813 const string SoapEncodingNamespace = "http://schemas.xmlsoap.org/soap/encoding/";
817 public EncodedType (string ns, XmlSchemaElement elem) { Namespace = ns; Element = elem; }
818 public string Namespace;
819 public XmlSchemaElement Element;
822 public SampleGenerator (ServiceDescriptionCollection services, XmlSchemas schemas)
824 descriptions = services;
825 this.schemas = schemas;
826 queue = new ArrayList ();
829 public string GenerateMessage (Port port, OperationBinding obin, Operation oper, string protocol, bool generateInput)
831 OperationMessage msg = null;
832 foreach (OperationMessage opm in oper.Messages)
834 if (opm is OperationInput && generateInput) msg = opm;
835 else if (opm is OperationOutput && !generateInput) msg = opm;
837 if (msg == null) return null;
840 case "Soap": return GenerateHttpSoapMessage (port, obin, oper, msg);
841 case "HttpGet": return GenerateHttpGetMessage (port, obin, oper, msg);
842 case "HttpPost": return GenerateHttpPostMessage (port, obin, oper, msg);
844 return "Unknown protocol";
847 public string GenerateHttpSoapMessage (Port port, OperationBinding obin, Operation oper, OperationMessage msg)
851 if (msg is OperationInput)
853 SoapAddressBinding sab = port.Extensions.Find (typeof(SoapAddressBinding)) as SoapAddressBinding;
854 SoapOperationBinding sob = obin.Extensions.Find (typeof(SoapOperationBinding)) as SoapOperationBinding;
855 req += "POST " + new Uri (sab.Location).AbsolutePath + "\n";
856 req += "SOAPAction: " + sob.SoapAction + "\n";
857 req += "Content-Type: text/xml; charset=utf-8\n";
858 req += "Content-Length: " + GetLiteral ("string") + "\n";
859 req += "Host: " + GetLiteral ("string") + "\n\n";
863 req += "HTTP/1.0 200 OK\n";
864 req += "Content-Type: text/xml; charset=utf-8\n";
865 req += "Content-Length: " + GetLiteral ("string") + "\n\n";
868 req += GenerateSoapMessage (obin, oper, msg);
872 public string GenerateHttpGetMessage (Port port, OperationBinding obin, Operation oper, OperationMessage msg)
876 if (msg is OperationInput)
878 HttpAddressBinding sab = port.Extensions.Find (typeof(HttpAddressBinding)) as HttpAddressBinding;
879 HttpOperationBinding sob = obin.Extensions.Find (typeof(HttpOperationBinding)) as HttpOperationBinding;
880 string location = new Uri (sab.Location).AbsolutePath + sob.Location + "?" + BuildQueryString (msg);
881 req += "GET " + location + "\n";
882 req += "Host: " + GetLiteral ("string");
886 req += "HTTP/1.0 200 OK\n";
887 req += "Content-Type: text/xml; charset=utf-8\n";
888 req += "Content-Length: " + GetLiteral ("string") + "\n\n";
890 MimeXmlBinding mxb = (MimeXmlBinding) obin.Output.Extensions.Find (typeof(MimeXmlBinding)) as MimeXmlBinding;
891 if (mxb == null) return req;
893 Message message = descriptions.GetMessage (msg.Message);
894 XmlQualifiedName ename = null;
895 foreach (MessagePart part in message.Parts)
896 if (part.Name == mxb.Part) ename = part.Element;
898 if (ename == null) return req + GetLiteral("string");
900 StringWriter sw = new StringWriter ();
901 XmlTextWriter xtw = new XmlTextWriter (sw);
902 xtw.Formatting = Formatting.Indented;
903 currentUse = SoapBindingUse.Literal;
904 WriteRootElementSample (xtw, ename);
906 req += sw.ToString ();
912 public string GenerateHttpPostMessage (Port port, OperationBinding obin, Operation oper, OperationMessage msg)
916 if (msg is OperationInput)
918 HttpAddressBinding sab = port.Extensions.Find (typeof(HttpAddressBinding)) as HttpAddressBinding;
919 HttpOperationBinding sob = obin.Extensions.Find (typeof(HttpOperationBinding)) as HttpOperationBinding;
920 string location = new Uri (sab.Location).AbsolutePath + sob.Location;
921 req += "POST " + location + "\n";
922 req += "Content-Type: application/x-www-form-urlencoded\n";
923 req += "Content-Length: " + GetLiteral ("string") + "\n";
924 req += "Host: " + GetLiteral ("string") + "\n\n";
925 req += BuildQueryString (msg);
927 else return GenerateHttpGetMessage (port, obin, oper, msg);
932 string BuildQueryString (OperationMessage opm)
935 Message msg = descriptions.GetMessage (opm.Message);
936 foreach (MessagePart part in msg.Parts)
938 if (s.Length != 0) s += "&";
939 s += part.Name + "=" + GetLiteral (part.Type.Name);
944 public string GenerateSoapMessage (OperationBinding obin, Operation oper, OperationMessage msg)
946 SoapOperationBinding sob = obin.Extensions.Find (typeof(SoapOperationBinding)) as SoapOperationBinding;
947 SoapBindingStyle style = (sob != null) ? sob.Style : SoapBindingStyle.Document;
949 MessageBinding msgbin = (msg is OperationInput) ? (MessageBinding) obin.Input : (MessageBinding)obin.Output;
950 SoapBodyBinding sbb = msgbin.Extensions.Find (typeof(SoapBodyBinding)) as SoapBodyBinding;
951 SoapBindingUse bodyUse = (sbb != null) ? sbb.Use : SoapBindingUse.Literal;
953 StringWriter sw = new StringWriter ();
954 XmlTextWriter xtw = new XmlTextWriter (sw);
955 xtw.Formatting = Formatting.Indented;
957 xtw.WriteStartDocument ();
958 xtw.WriteStartElement ("soap", "Envelope", SoapEnvelopeNamespace);
959 xtw.WriteAttributeString ("xmlns", "xsi", null, XmlSchema.InstanceNamespace);
960 xtw.WriteAttributeString ("xmlns", "xsd", null, XmlSchema.Namespace);
962 if (bodyUse == SoapBindingUse.Encoded)
964 xtw.WriteAttributeString ("xmlns", "soapenc", null, SoapEncodingNamespace);
965 xtw.WriteAttributeString ("xmlns", "tns", null, msg.Message.Namespace);
970 bool writtenHeader = false;
971 foreach (object ob in msgbin.Extensions)
973 SoapHeaderBinding hb = ob as SoapHeaderBinding;
974 if (hb == null) continue;
976 if (!writtenHeader) {
977 xtw.WriteStartElement ("soap", "Header", SoapEnvelopeNamespace);
978 writtenHeader = true;
981 WriteHeader (xtw, hb);
985 xtw.WriteEndElement ();
988 xtw.WriteStartElement ("soap", "Body", SoapEnvelopeNamespace);
990 currentUse = bodyUse;
991 WriteBody (xtw, oper, msg, sbb, style);
993 xtw.WriteEndElement ();
994 xtw.WriteEndElement ();
996 return sw.ToString ();
999 void WriteHeader (XmlTextWriter xtw, SoapHeaderBinding header)
1001 Message msg = descriptions.GetMessage (header.Message);
1002 if (msg == null) throw new InvalidOperationException ("Message " + header.Message + " not found");
1003 MessagePart part = msg.Parts [header.Part];
1004 if (part == null) throw new InvalidOperationException ("Message part " + header.Part + " not found in message " + header.Message);
1006 currentUse = header.Use;
1008 if (currentUse == SoapBindingUse.Literal)
1009 WriteRootElementSample (xtw, part.Element);
1011 WriteTypeSample (xtw, part.Type);
1014 void WriteBody (XmlTextWriter xtw, Operation oper, OperationMessage opm, SoapBodyBinding sbb, SoapBindingStyle style)
1016 Message msg = descriptions.GetMessage (opm.Message);
1017 if (msg.Parts.Count > 0 && msg.Parts[0].Name == "parameters")
1019 MessagePart part = msg.Parts[0];
1020 if (part.Element == XmlQualifiedName.Empty)
1021 WriteTypeSample (xtw, part.Type);
1023 WriteRootElementSample (xtw, part.Element);
1027 string elemName = oper.Name;
1029 if (opm is OperationOutput) elemName += "Response";
1031 if (style == SoapBindingStyle.Rpc) {
1032 xtw.WriteStartElement (elemName, sbb.Namespace);
1036 foreach (MessagePart part in msg.Parts)
1038 if (part.Element == XmlQualifiedName.Empty)
1040 XmlSchemaElement elem = new XmlSchemaElement ();
1041 elem.SchemaTypeName = part.Type;
1042 elem.Name = part.Name;
1043 WriteElementSample (xtw, ns, elem);
1046 WriteRootElementSample (xtw, part.Element);
1049 if (style == SoapBindingStyle.Rpc)
1050 xtw.WriteEndElement ();
1052 WriteQueuedTypeSamples (xtw);
1055 void WriteRootElementSample (XmlTextWriter xtw, XmlQualifiedName qname)
1057 XmlSchemaElement elem = (XmlSchemaElement) schemas.Find (qname, typeof(XmlSchemaElement));
1058 if (elem == null) throw new InvalidOperationException ("Element not found: " + qname);
1059 WriteElementSample (xtw, qname.Namespace, elem);
1062 void WriteElementSample (XmlTextWriter xtw, string ns, XmlSchemaElement elem)
1064 bool sharedAnnType = false;
1065 XmlQualifiedName root;
1067 if (!elem.RefName.IsEmpty) {
1068 XmlSchemaElement refElem = FindRefElement (elem);
1069 if (refElem == null) throw new InvalidOperationException ("Global element not found: " + elem.RefName);
1070 root = elem.RefName;
1072 sharedAnnType = true;
1075 root = new XmlQualifiedName (elem.Name, ns);
1077 if (!elem.SchemaTypeName.IsEmpty)
1079 XmlSchemaComplexType st = FindComplexTyype (elem.SchemaTypeName);
1081 WriteComplexTypeSample (xtw, st, root);
1084 xtw.WriteStartElement (root.Name, root.Namespace);
1085 if (currentUse == SoapBindingUse.Encoded)
1086 xtw.WriteAttributeString ("type", XmlSchema.InstanceNamespace, GetQualifiedNameString (xtw, elem.SchemaTypeName));
1087 xtw.WriteString (GetLiteral (FindBuiltInType (elem.SchemaTypeName)));
1088 xtw.WriteEndElement ();
1091 else if (elem.SchemaType == null)
1093 xtw.WriteStartElement ("any");
1094 xtw.WriteEndElement ();
1097 WriteComplexTypeSample (xtw, (XmlSchemaComplexType) elem.SchemaType, root);
1100 void WriteTypeSample (XmlTextWriter xtw, XmlQualifiedName qname)
1102 XmlSchemaComplexType ctype = FindComplexTyype (qname);
1103 if (ctype != null) {
1104 WriteComplexTypeSample (xtw, ctype, qname);
1108 XmlSchemaSimpleType stype = (XmlSchemaSimpleType) schemas.Find (qname, typeof(XmlSchemaSimpleType));
1109 if (stype != null) {
1110 WriteSimpleTypeSample (xtw, stype);
1114 xtw.WriteString (GetLiteral (FindBuiltInType (qname)));
1115 throw new InvalidOperationException ("Type not found: " + qname);
1118 void WriteComplexTypeSample (XmlTextWriter xtw, XmlSchemaComplexType stype, XmlQualifiedName rootName)
1120 WriteComplexTypeSample (xtw, stype, rootName, -1);
1123 void WriteComplexTypeSample (XmlTextWriter xtw, XmlSchemaComplexType stype, XmlQualifiedName rootName, int id)
1125 string ns = rootName.Namespace;
1127 if (rootName.Name.IndexOf ("[]") != -1) rootName = arrayType;
1129 if (currentUse == SoapBindingUse.Encoded) {
1130 string pref = xtw.LookupPrefix (rootName.Namespace);
1131 if (pref == null) pref = "q1";
1132 xtw.WriteStartElement (pref, rootName.Name, rootName.Namespace);
1136 xtw.WriteStartElement (rootName.Name, rootName.Namespace);
1140 xtw.WriteAttributeString ("id", "id" + id);
1141 if (rootName != arrayType)
1142 xtw.WriteAttributeString ("type", XmlSchema.InstanceNamespace, GetQualifiedNameString (xtw, rootName));
1145 WriteComplexTypeAttributes (xtw, stype);
1146 WriteComplexTypeElements (xtw, ns, stype);
1148 xtw.WriteEndElement ();
1151 void WriteComplexTypeAttributes (XmlTextWriter xtw, XmlSchemaComplexType stype)
1153 WriteAttributes (xtw, stype.Attributes, stype.AnyAttribute);
1156 void WriteComplexTypeElements (XmlTextWriter xtw, string ns, XmlSchemaComplexType stype)
1158 if (stype.Particle != null)
1159 WriteParticleComplexContent (xtw, ns, stype.Particle);
1162 if (stype.ContentModel is XmlSchemaSimpleContent)
1163 WriteSimpleContent (xtw, (XmlSchemaSimpleContent)stype.ContentModel);
1164 else if (stype.ContentModel is XmlSchemaComplexContent)
1165 WriteComplexContent (xtw, ns, (XmlSchemaComplexContent)stype.ContentModel);
1169 void WriteAttributes (XmlTextWriter xtw, XmlSchemaObjectCollection atts, XmlSchemaAnyAttribute anyat)
1171 foreach (XmlSchemaObject at in atts)
1173 if (at is XmlSchemaAttribute)
1176 XmlSchemaAttribute attr = (XmlSchemaAttribute)at;
1177 XmlSchemaAttribute refAttr = attr;
1179 // refAttr.Form; TODO
1181 if (!attr.RefName.IsEmpty) {
1182 refAttr = FindRefAttribute (attr.RefName);
1183 if (refAttr == null) throw new InvalidOperationException ("Global attribute not found: " + attr.RefName);
1187 if (!refAttr.SchemaTypeName.IsEmpty) val = FindBuiltInType (refAttr.SchemaTypeName);
1188 else val = FindBuiltInType ((XmlSchemaSimpleType) refAttr.SchemaType);
1190 xtw.WriteAttributeString (refAttr.Name, val);
1192 else if (at is XmlSchemaAttributeGroupRef)
1194 XmlSchemaAttributeGroupRef gref = (XmlSchemaAttributeGroupRef)at;
1195 XmlSchemaAttributeGroup grp = (XmlSchemaAttributeGroup) schemas.Find (gref.RefName, typeof(XmlSchemaAttributeGroup));
1196 WriteAttributes (xtw, grp.Attributes, grp.AnyAttribute);
1201 xtw.WriteAttributeString ("custom-attribute","value");
1204 void WriteParticleComplexContent (XmlTextWriter xtw, string ns, XmlSchemaParticle particle)
1206 WriteParticleContent (xtw, ns, particle, false);
1209 void WriteParticleContent (XmlTextWriter xtw, string ns, XmlSchemaParticle particle, bool multiValue)
1211 if (particle is XmlSchemaGroupRef)
1212 particle = GetRefGroupParticle ((XmlSchemaGroupRef)particle);
1214 if (particle.MaxOccurs > 1) multiValue = true;
1216 if (particle is XmlSchemaSequence) {
1217 WriteSequenceContent (xtw, ns, ((XmlSchemaSequence)particle).Items, multiValue);
1219 else if (particle is XmlSchemaChoice) {
1220 if (((XmlSchemaChoice)particle).Items.Count == 1)
1221 WriteSequenceContent (xtw, ns, ((XmlSchemaChoice)particle).Items, multiValue);
1223 WriteChoiceContent (xtw, ns, (XmlSchemaChoice)particle, multiValue);
1225 else if (particle is XmlSchemaAll) {
1226 WriteSequenceContent (xtw, ns, ((XmlSchemaAll)particle).Items, multiValue);
1230 void WriteSequenceContent (XmlTextWriter xtw, string ns, XmlSchemaObjectCollection items, bool multiValue)
1232 foreach (XmlSchemaObject item in items)
1233 WriteContentItem (xtw, ns, item, multiValue);
1236 void WriteContentItem (XmlTextWriter xtw, string ns, XmlSchemaObject item, bool multiValue)
1238 if (item is XmlSchemaGroupRef)
1239 item = GetRefGroupParticle ((XmlSchemaGroupRef)item);
1241 if (item is XmlSchemaElement)
1243 XmlSchemaElement elem = (XmlSchemaElement) item;
1244 XmlSchemaElement refElem;
1245 if (!elem.RefName.IsEmpty) refElem = FindRefElement (elem);
1246 else refElem = elem;
1248 int num = (elem.MaxOccurs == 1 && !multiValue) ? 1 : 2;
1249 for (int n=0; n<num; n++)
1251 if (currentUse == SoapBindingUse.Literal)
1252 WriteElementSample (xtw, ns, refElem);
1254 WriteRefTypeSample (xtw, ns, refElem);
1257 else if (item is XmlSchemaAny)
1259 xtw.WriteString (GetLiteral ("xml"));
1261 else if (item is XmlSchemaParticle) {
1262 WriteParticleContent (xtw, ns, (XmlSchemaParticle)item, multiValue);
1266 void WriteChoiceContent (XmlTextWriter xtw, string ns, XmlSchemaChoice choice, bool multiValue)
1268 foreach (XmlSchemaObject item in choice.Items)
1269 WriteContentItem (xtw, ns, item, multiValue);
1272 void WriteSimpleContent (XmlTextWriter xtw, XmlSchemaSimpleContent content)
1274 XmlSchemaSimpleContentExtension ext = content.Content as XmlSchemaSimpleContentExtension;
1276 WriteAttributes (xtw, ext.Attributes, ext.AnyAttribute);
1278 XmlQualifiedName qname = GetContentBaseType (content.Content);
1279 xtw.WriteString (GetLiteral (FindBuiltInType (qname)));
1282 string FindBuiltInType (XmlQualifiedName qname)
1284 if (qname.Namespace == XmlSchema.Namespace)
1287 XmlSchemaComplexType ct = FindComplexTyype (qname);
1290 XmlSchemaSimpleContent sc = ct.ContentModel as XmlSchemaSimpleContent;
1291 if (sc == null) throw new InvalidOperationException ("Invalid schema");
1292 return FindBuiltInType (GetContentBaseType (sc.Content));
1295 XmlSchemaSimpleType st = (XmlSchemaSimpleType) schemas.Find (qname, typeof(XmlSchemaSimpleType));
1297 return FindBuiltInType (st);
1299 throw new InvalidOperationException ("Definition of type " + qname + " not found");
1302 string FindBuiltInType (XmlSchemaSimpleType st)
1304 if (st.Content is XmlSchemaSimpleTypeRestriction) {
1305 return FindBuiltInType (GetContentBaseType (st.Content));
1307 else if (st.Content is XmlSchemaSimpleTypeList) {
1308 string s = FindBuiltInType (GetContentBaseType (st.Content));
1309 return s + " " + s + " ...";
1311 else if (st.Content is XmlSchemaSimpleTypeUnion)
1313 //Check if all types of the union are equal. If not, then will use anyType.
1314 XmlSchemaSimpleTypeUnion uni = (XmlSchemaSimpleTypeUnion) st.Content;
1315 string utype = null;
1317 // Anonymous types are unique
1318 if (uni.BaseTypes.Count != 0 && uni.MemberTypes.Length != 0)
1321 foreach (XmlQualifiedName mt in uni.MemberTypes)
1323 string qn = FindBuiltInType (mt);
1324 if (utype != null && qn != utype) return "string";
1334 XmlQualifiedName GetContentBaseType (XmlSchemaObject ob)
1336 if (ob is XmlSchemaSimpleContentExtension)
1337 return ((XmlSchemaSimpleContentExtension)ob).BaseTypeName;
1338 else if (ob is XmlSchemaSimpleContentRestriction)
1339 return ((XmlSchemaSimpleContentRestriction)ob).BaseTypeName;
1340 else if (ob is XmlSchemaSimpleTypeRestriction)
1341 return ((XmlSchemaSimpleTypeRestriction)ob).BaseTypeName;
1342 else if (ob is XmlSchemaSimpleTypeList)
1343 return ((XmlSchemaSimpleTypeList)ob).ItemTypeName;
1348 void WriteComplexContent (XmlTextWriter xtw, string ns, XmlSchemaComplexContent content)
1350 XmlQualifiedName qname;
1352 XmlSchemaComplexContentExtension ext = content.Content as XmlSchemaComplexContentExtension;
1353 if (ext != null) qname = ext.BaseTypeName;
1355 XmlSchemaComplexContentRestriction rest = (XmlSchemaComplexContentRestriction)content.Content;
1356 qname = rest.BaseTypeName;
1357 if (qname == arrayType) {
1358 ParseArrayType (rest, out qname);
1359 XmlSchemaElement elem = new XmlSchemaElement ();
1361 elem.SchemaTypeName = qname;
1363 xtw.WriteAttributeString ("arrayType", SoapEncodingNamespace, qname.Name + "[2]");
1364 WriteContentItem (xtw, ns, elem, true);
1369 // Add base map members to this map
1370 XmlSchemaComplexType ctype = FindComplexTyype (qname);
1371 WriteComplexTypeAttributes (xtw, ctype);
1374 // Add the members of this map
1375 WriteAttributes (xtw, ext.Attributes, ext.AnyAttribute);
1376 if (ext.Particle != null)
1377 WriteParticleComplexContent (xtw, ns, ext.Particle);
1380 WriteComplexTypeElements (xtw, ns, ctype);
1383 void ParseArrayType (XmlSchemaComplexContentRestriction rest, out XmlQualifiedName qtype)
1385 XmlSchemaAttribute arrayTypeAt = FindArrayAttribute (rest.Attributes);
1386 XmlAttribute[] uatts = arrayTypeAt.UnhandledAttributes;
1387 if (uatts == null || uatts.Length == 0) throw new InvalidOperationException ("arrayType attribute not specified in array declaration");
1389 XmlAttribute xat = null;
1390 foreach (XmlAttribute at in uatts)
1391 if (at.LocalName == "arrayType" && at.NamespaceURI == WsdlNamespace)
1392 { xat = at; break; }
1395 throw new InvalidOperationException ("arrayType attribute not specified in array declaration");
1397 string arrayType = xat.Value;
1399 int i = arrayType.LastIndexOf (":");
1400 if (i == -1) ns = "";
1401 else ns = arrayType.Substring (0,i);
1403 int j = arrayType.IndexOf ("[", i+1);
1404 if (j == -1) throw new InvalidOperationException ("Cannot parse WSDL array type: " + arrayType);
1405 type = arrayType.Substring (i+1);
1406 type = type.Substring (0, type.Length-2);
1408 qtype = new XmlQualifiedName (type, ns);
1411 XmlSchemaAttribute FindArrayAttribute (XmlSchemaObjectCollection atts)
1413 foreach (object ob in atts)
1415 XmlSchemaAttribute att = ob as XmlSchemaAttribute;
1416 if (att != null && att.RefName == arrayTypeRefName) return att;
1418 XmlSchemaAttributeGroupRef gref = ob as XmlSchemaAttributeGroupRef;
1421 XmlSchemaAttributeGroup grp = (XmlSchemaAttributeGroup) schemas.Find (gref.RefName, typeof(XmlSchemaAttributeGroup));
1422 att = FindArrayAttribute (grp.Attributes);
1423 if (att != null) return att;
1429 void WriteSimpleTypeSample (XmlTextWriter xtw, XmlSchemaSimpleType stype)
1431 xtw.WriteString (GetLiteral (FindBuiltInType (stype)));
1434 XmlSchemaParticle GetRefGroupParticle (XmlSchemaGroupRef refGroup)
1436 XmlSchemaGroup grp = (XmlSchemaGroup) schemas.Find (refGroup.RefName, typeof (XmlSchemaGroup));
1437 return grp.Particle;
1440 XmlSchemaElement FindRefElement (XmlSchemaElement elem)
1442 if (elem.RefName.Namespace == XmlSchema.Namespace)
1444 if (anyElement != null) return anyElement;
1445 anyElement = new XmlSchemaElement ();
1446 anyElement.Name = "any";
1447 anyElement.SchemaTypeName = anyType;
1450 return (XmlSchemaElement) schemas.Find (elem.RefName, typeof(XmlSchemaElement));
1453 XmlSchemaAttribute FindRefAttribute (XmlQualifiedName refName)
1455 if (refName.Namespace == XmlSchema.Namespace)
1457 XmlSchemaAttribute at = new XmlSchemaAttribute ();
1458 at.Name = refName.Name;
1459 at.SchemaTypeName = new XmlQualifiedName ("string",XmlSchema.Namespace);
1462 return (XmlSchemaAttribute) schemas.Find (refName, typeof(XmlSchemaAttribute));
1465 void WriteRefTypeSample (XmlTextWriter xtw, string ns, XmlSchemaElement elem)
1467 if (elem.SchemaTypeName.Namespace == XmlSchema.Namespace || schemas.Find (elem.SchemaTypeName, typeof(XmlSchemaSimpleType)) != null)
1468 WriteElementSample (xtw, ns, elem);
1471 xtw.WriteStartElement (elem.Name, ns);
1472 xtw.WriteAttributeString ("href", "#id" + (queue.Count+1));
1473 xtw.WriteEndElement ();
1474 queue.Add (new EncodedType (ns, elem));
1478 void WriteQueuedTypeSamples (XmlTextWriter xtw)
1480 for (int n=0; n<queue.Count; n++)
1482 EncodedType ec = (EncodedType) queue[n];
1483 XmlSchemaComplexType st = FindComplexTyype (ec.Element.SchemaTypeName);
1484 WriteComplexTypeSample (xtw, st, ec.Element.SchemaTypeName, n+1);
1488 XmlSchemaComplexType FindComplexTyype (XmlQualifiedName qname)
1490 if (qname.Name.IndexOf ("[]") != -1)
1492 XmlSchemaComplexType stype = new XmlSchemaComplexType ();
1493 stype.ContentModel = new XmlSchemaComplexContent ();
1495 XmlSchemaComplexContentRestriction res = new XmlSchemaComplexContentRestriction ();
1496 stype.ContentModel.Content = res;
1497 res.BaseTypeName = arrayType;
1499 XmlSchemaAttribute att = new XmlSchemaAttribute ();
1500 att.RefName = arrayTypeRefName;
1501 res.Attributes.Add (att);
1503 XmlAttribute xat = document.CreateAttribute ("arrayType", WsdlNamespace);
1504 xat.Value = qname.Namespace + ":" + qname.Name;
1505 att.UnhandledAttributes = new XmlAttribute[] {xat};
1509 return (XmlSchemaComplexType) schemas.Find (qname, typeof(XmlSchemaComplexType));
1512 string GetQualifiedNameString (XmlTextWriter xtw, XmlQualifiedName qname)
1514 string pref = xtw.LookupPrefix (qname.Namespace);
1515 if (pref != null) return pref + ":" + qname.Name;
1517 xtw.WriteAttributeString ("xmlns", "q1", null, qname.Namespace);
1518 return "q1:" + qname.Name;
1521 protected virtual string GetLiteral (string s)
1526 void GetOperationFormat (OperationBinding obin, out SoapBindingStyle style, out SoapBindingUse use)
1528 style = SoapBindingStyle.Document;
1529 use = SoapBindingUse.Literal;
1530 SoapOperationBinding sob = obin.Extensions.Find (typeof(SoapOperationBinding)) as SoapOperationBinding;
1533 SoapBodyBinding sbb = obin.Input.Extensions.Find (typeof(SoapBodyBinding)) as SoapBodyBinding;
1547 <link rel="alternate" type="text/xml" href="<%=Request.FilePath%>?disco"/>
1549 <title><%=WebServiceName%> Web Service</title>
1550 <style type="text/css">
1551 BODY { font-family: Arial; margin-left: 20px; margin-top: 20px; font-size: x-small}
1552 TABLE { font-size: x-small }
1553 .title { color:dimgray; font-family: Arial; font-size:20pt; font-weight:900}
1554 .operationTitle { color:dimgray; font-family: Arial; font-size:15pt; font-weight:900}
1555 .method { font-size: x-small }
1556 .bindingLabel { font-size: x-small; font-weight:bold; color:darkgray; line-height:8pt; display:block; margin-bottom:3px }
1557 .label { font-size: small; font-weight:bold; color:darkgray }
1558 .paramTable { font-size: x-small }
1559 .paramTable TR { background-color: gainsboro }
1560 .paramFormTable { font-size: x-small; padding: 10px; background-color: gainsboro }
1561 .paramFormTable TR { background-color: gainsboro }
1562 .paramInput { border: solid 1px gray }
1563 .button {border: solid 1px gray }
1564 .smallSeparator { height:3px; overflow:hidden }
1565 .panel { background-color:whitesmoke; border: solid 1px silver; border-top: solid 1px silver }
1566 .codePanel { background-color: white; font-size:x-small; padding:7px; border:solid 1px silver}
1567 .code-xml { font-size:10pt; font-family:courier }
1568 .code-cs { font-size:10pt; font-family:courier }
1569 .code-vb { font-size:10pt; font-family:courier }
1570 .tabLabelOn { font-weight:bold }
1571 .tabLabelOff {color: darkgray }
1572 .literal-placeholder {color: darkblue; font-weight:bold}
1573 A:link { color: black; }
1574 A:visited { color: black; }
1575 A:active { color: black; }
1576 A:hover { color: blue }
1580 function clearForm ()
1582 document.getElementById("testFormResult").style.display="none";
1589 <div class="title" style="margin-left:20px">
1590 <span class="label">Web Service</span><br>
1595 **********************************************************
1599 <table border="0" width="100%" cellpadding="15px" cellspacing="15px">
1600 <tr valign="top"><td width="150px" class="panel">
1601 <div style="width:150px"></div>
1602 <a class="method" href='<%=PageName%>'>Overview</a><br>
1603 <div class="smallSeparator"></div>
1604 <a class="method" href='<%=PageName + "?" + GetPageContext("wsdl")%>'>Service Description</a>
1605 <div class="smallSeparator"></div>
1606 <a class="method" href='<%=PageName + "?" + GetPageContext("proxy")%>'>Client proxy</a>
1608 <asp:repeater id="BindingsRepeater" runat=server>
1609 <itemtemplate name="itemtemplate">
1610 <span class="bindingLabel"><%#FormatBindingName(DataBinder.Eval(Container.DataItem, "Name").ToString())%></span>
1611 <asp:repeater id="OperationsRepeater" runat=server datasource='<%# ((Binding)Container.DataItem).Operations %>'>
1613 <a class="method" href="<%=PageName%>?<%=GetTabContext("op",null)%>op=<%#GetOpName(Container.DataItem)%>&bnd=<%#DataBinder.Eval(Container.DataItem, "Binding.Name")%>"><%#GetOpName(Container.DataItem)%></a>
1614 <div class="smallSeparator"></div>
1621 </td><td class="panel">
1623 <% if (CurrentPage == "main") {%>
1626 **********************************************************
1627 Web service overview
1630 <p class="label">Web Service Overview</p>
1631 <%=WebServiceDescription%>
1633 <%} if (DefaultBinding == null) {%>
1634 This service does not contain any public web method.
1635 <%} else if (CurrentPage == "op") {%>
1638 **********************************************************
1639 Operation description
1642 <span class="operationTitle"><%=CurrentOperationName%></span>
1647 <% if (CurrentTab == "main") { %>
1648 <span class="label">Input Parameters</span>
1649 <div class="smallSeparator"></div>
1650 <% if (InParams.Count == 0) { %>
1651 No input parameters<br>
1653 <table class="paramTable" cellspacing="1" cellpadding="5">
1654 <asp:repeater id="InputParamsRepeater" runat=server>
1657 <td width="150"><%#DataBinder.Eval(Container.DataItem, "Name")%></td>
1658 <td width="150"><%#DataBinder.Eval(Container.DataItem, "Type")%></td>
1666 <% if (OutParams.Count > 0) { %>
1667 <span class="label">Output Parameters</span>
1668 <div class="smallSeparator"></div>
1669 <table class="paramTable" cellspacing="1" cellpadding="5">
1670 <asp:repeater id="OutputParamsRepeater" runat=server>
1673 <td width="150"><%#DataBinder.Eval(Container.DataItem, "Name")%></td>
1674 <td width="150"><%#DataBinder.Eval(Container.DataItem, "Type")%></td>
1682 <span class="label">Remarks</span>
1683 <div class="smallSeparator"></div>
1684 <%=OperationDocumentation%>
1686 <span class="label">Technical information</span>
1687 <div class="smallSeparator"></div>
1688 Format: <%=CurrentOperationFormat%>
1689 <br>Supported protocols: <%=CurrentOperationProtocols%>
1693 **********************************************************
1694 Operation description - Test form
1697 <% if (CurrentTab == "test") {
1698 if (CurrentOperationSupportsTest) {%>
1699 Enter values for the parameters and click the 'Invoke' button to test this method:<br><br>
1700 <form action="<%=PageName%>" method="GET">
1701 <input type="hidden" name="page" value="<%=CurrentPage%>">
1702 <input type="hidden" name="tab" value="<%=CurrentTab%>">
1703 <input type="hidden" name="op" value="<%=CurrentOperationName%>">
1704 <input type="hidden" name="bnd" value="<%=CurrentOperationBinding%>">
1705 <input type="hidden" name="ext" value="testform">
1706 <table class="paramFormTable" cellspacing="0" cellpadding="3">
1707 <asp:repeater id="InputFormParamsRepeater" runat=server>
1710 <td><%#DataBinder.Eval(Container.DataItem, "Name")%>: </td>
1711 <td width="150"><input class="paramInput" type="text" size="20" name="<%#DataBinder.Eval(Container.DataItem, "Name")%>"></td>
1715 <tr><td></td><td><input class="button" type="submit" value="Invoke"> <input class="button" type="button" onclick="clearForm()" value="Clear"></td></tr>
1718 <div id="testFormResult" style="display:<%= (HasFormResult?"block":"none") %>">
1719 The web service returned the following result:<br/><br/>
1720 <div class="codePanel"><%=GetTestResult()%></div>
1723 The test form is not available for this operation because it has parameters with a complex structure.
1728 **********************************************************
1729 Operation description - Message Layout
1732 <% if (CurrentTab == "msg") { %>
1734 The following are sample SOAP requests and responses for each protocol supported by this method:
1737 <% if (IsOperationSupported ("Soap")) { %>
1738 <span class="label">Soap</span>
1740 <div class="codePanel"><div class="code-xml"><%=GenerateOperationMessages ("Soap", true)%></div></div>
1742 <div class="codePanel"><div class="code-xml"><%=GenerateOperationMessages ("Soap", false)%></div></div>
1745 <% if (IsOperationSupported ("HttpGet")) { %>
1746 <span class="label">HTTP Get</span>
1748 <div class="codePanel"><div class="code-xml"><%=GenerateOperationMessages ("HttpGet", true)%></div></div>
1750 <div class="codePanel"><div class="code-xml"><%=GenerateOperationMessages ("HttpGet", false)%></div></div>
1753 <% if (IsOperationSupported ("HttpPost")) { %>
1754 <span class="label">HTTP Post</span>
1756 <div class="codePanel"><div class="code-xml"><%=GenerateOperationMessages ("HttpPost", true)%></div></div>
1758 <div class="codePanel"><div class="code-xml"><%=GenerateOperationMessages ("HttpPost", false)%></div></div>
1763 <%} else if (CurrentPage == "proxy") {%>
1765 **********************************************************
1768 <form action="<%=PageName%>" name="langForm" method="GET">
1769 Select the language for which you want to generate a proxy
1770 <input type="hidden" name="page" value="<%=CurrentPage%>">
1771 <SELECT name="lang" onchange="langForm.submit()">
1772 <%=GetOptionSel("cs",CurrentLanguage)%>C#</option>
1773 <%=GetOptionSel("vb",CurrentLanguage)%>Visual Basic</option>
1778 <span class="label"><%=CurrentProxytName%></span>
1779 <a href="<%=PageName + "?code=" + CurrentLanguage%>">Download</a>
1781 <div class="codePanel">
1782 <div class="code-<%=CurrentLanguage%>"><%=GetProxyCode ()%></div>
1784 <%} else if (CurrentPage == "wsdl") {%>
1786 **********************************************************
1789 <% if (descriptions.Count > 1 || schemas.Count > 1) {%>
1790 The description of this web service is composed by several documents. Click on the document you want to see:
1794 for (int n=0; n<descriptions.Count; n++)
1795 Response.Write ("<li><a href='" + PageName + "?" + GetPageContext(null) + "doctype=wsdl&docind=" + n + "'>WSDL document " + descriptions[n].TargetNamespace + "</a></li>");
1796 for (int n=0; n<schemas.Count; n++)
1797 Response.Write ("<li><a href='" + PageName + "?" + GetPageContext(null) + "doctype=schema&docind=" + n + "'>Xml Schema " + schemas[n].TargetNamespace + "</a></li>");
1804 <span class="label"><%=CurrentDocumentName%></span>
1805 <a href="<%=PageName + "?" + CurrentDocType + "=" + CurrentDocInd %>">Download</a>
1807 <div class="codePanel">
1808 <div class="code-xml"><%=GenerateDocument ()%></div>
1813 <br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
1815 <td width="20px"></td>