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" %>
18 <%@ Import Namespace="System.Web.Services.Description" %>
19 <%@ Import Namespace="System" %>
20 <%@ Import Namespace="System.Net" %>
21 <%@ Import Namespace="System.Globalization" %>
22 <%@ Import Namespace="System.Resources" %>
23 <%@ Import Namespace="System.Diagnostics" %>
24 <%@ Import Namespace="System.CodeDom" %>
25 <%@ Import Namespace="System.CodeDom.Compiler" %>
26 <%@ Import Namespace="Microsoft.CSharp" %>
27 <%@ Import Namespace="Microsoft.VisualBasic" %>
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;
55 BasicProfileViolationCollection ProfileViolations;
57 void Page_Load(object sender, EventArgs e)
59 descriptions = (ServiceDescriptionCollection) Context.Items["wsdls"];
60 schemas = (XmlSchemas) Context.Items["schemas"];
62 ServiceDescription desc = descriptions [0];
63 if (schemas.Count == 0) schemas = desc.Types.Schemas;
65 Service service = desc.Services[0];
66 WebServiceName = service.Name;
67 if (desc.Bindings.Count == 0)
70 DefaultBinding = desc.Bindings[0].Name;
71 WebServiceDescription = service.Documentation;
72 if (WebServiceDescription == "" || WebServiceDescription == null)
73 WebServiceDescription = "Description has not been provided";
74 ServiceProtocols = FindServiceProtocols (null);
76 CurrentOperationName = Request.QueryString["op"];
77 CurrentOperationBinding = Request.QueryString["bnd"];
78 if (CurrentOperationName != null) BuildOperationInfo ();
80 PageName = HttpUtility.UrlEncode (Path.GetFileName(Request.Path), Encoding.UTF8);
82 ArrayList list = new ArrayList ();
83 foreach (ServiceDescription sd in descriptions) {
84 foreach (Binding bin in sd.Bindings)
85 if (bin.Extensions.Find (typeof(SoapBinding)) != null) list.Add (bin);
88 BindingsRepeater.DataSource = list;
91 ProfileViolations = new BasicProfileViolationCollection ();
92 WebServicesInteroperability.CheckConformance (WsiProfiles.BasicProfile1_1, descriptions, ProfileViolations);
95 void BuildOperationInfo ()
97 InParams = new ArrayList ();
98 OutParams = new ArrayList ();
100 Port port = FindPort (CurrentOperationBinding, null);
101 Binding binding = descriptions.GetBinding (port.Binding);
103 PortType portType = descriptions.GetPortType (binding.Type);
104 Operation oper = FindOperation (portType, CurrentOperationName);
106 OperationDocumentation = oper.Documentation;
107 if (OperationDocumentation == null || OperationDocumentation == "")
108 OperationDocumentation = "No additional remarks";
110 foreach (OperationMessage opm in oper.Messages)
112 if (opm is OperationInput)
113 BuildParameters (InParams, opm);
114 else if (opm is OperationOutput)
115 BuildParameters (OutParams, opm);
118 // Protocols supported by the operation
119 CurrentOperationProtocols = "";
120 ArrayList prots = FindServiceProtocols (CurrentOperationName);
121 for (int n=0; n<prots.Count; n++) {
122 if (n != 0) CurrentOperationProtocols += ", ";
123 CurrentOperationProtocols += (string) prots[n];
126 CurrentOperationSupportsTest = prots.Contains ("HttpGet") || prots.Contains ("HttpPost");
129 OperationBinding obin = FindOperation (binding, CurrentOperationName);
131 CurrentOperationFormat = GetOperationFormat (obin);
133 InputParamsRepeater.DataSource = InParams;
134 InputFormParamsRepeater.DataSource = InParams;
135 OutputParamsRepeater.DataSource = OutParams;
138 void BuildParameters (ArrayList list, OperationMessage opm)
140 Message msg = descriptions.GetMessage (opm.Message);
141 if (msg.Parts.Count > 0 && msg.Parts[0].Name == "parameters")
143 MessagePart part = msg.Parts[0];
144 XmlSchemaComplexType ctype;
145 if (part.Element == XmlQualifiedName.Empty)
147 ctype = (XmlSchemaComplexType) schemas.Find (part.Type, typeof(XmlSchemaComplexType));
151 XmlSchemaElement elem = (XmlSchemaElement) schemas.Find (part.Element, typeof(XmlSchemaElement));
152 ctype = (XmlSchemaComplexType) elem.SchemaType;
154 XmlSchemaSequence seq = ctype.Particle as XmlSchemaSequence;
155 if (seq == null) return;
157 foreach (XmlSchemaObject ob in seq.Items)
159 Parameter p = new Parameter();
160 p.Description = "No additional remarks";
162 if (ob is XmlSchemaElement)
164 XmlSchemaElement selem = GetRefElement ((XmlSchemaElement)ob);
166 p.Type = selem.SchemaTypeName.Name;
178 foreach (MessagePart part in msg.Parts)
180 Parameter p = new Parameter ();
181 p.Description = "No additional remarks";
183 if (part.Element == XmlQualifiedName.Empty)
184 p.Type = part.Type.Name;
187 XmlSchemaElement elem = (XmlSchemaElement) schemas.Find (part.Element, typeof(XmlSchemaElement));
188 p.Type = elem.SchemaTypeName.Name;
195 string GetOperationFormat (OperationBinding obin)
198 SoapOperationBinding sob = obin.Extensions.Find (typeof(SoapOperationBinding)) as SoapOperationBinding;
200 format = sob.Style.ToString ();
201 SoapBodyBinding sbb = obin.Input.Extensions.Find (typeof(SoapBodyBinding)) as SoapBodyBinding;
203 format += " / " + sbb.Use;
208 XmlSchemaElement GetRefElement (XmlSchemaElement elem)
210 if (!elem.RefName.IsEmpty)
211 return (XmlSchemaElement) schemas.Find (elem.RefName, typeof(XmlSchemaElement));
216 ArrayList FindServiceProtocols(string operName)
218 ArrayList table = new ArrayList ();
219 Service service = descriptions[0].Services[0];
220 foreach (Port port in service.Ports)
223 Binding bin = descriptions.GetBinding (port.Binding);
224 if (bin.Extensions.Find (typeof(SoapBinding)) != null)
228 HttpBinding hb = (HttpBinding) bin.Extensions.Find (typeof(HttpBinding));
229 if (hb != null && hb.Verb == "POST") prot = "HttpPost";
230 else if (hb != null && hb.Verb == "GET") prot = "HttpGet";
233 if (prot != null && operName != null)
235 if (FindOperation (bin, operName) == null)
239 if (prot != null && !table.Contains (prot))
245 Port FindPort (string portName, string protocol)
247 Service service = descriptions[0].Services[0];
248 foreach (Port port in service.Ports)
250 if (portName == null)
252 Binding binding = descriptions.GetBinding (port.Binding);
253 if (GetProtocol (binding) == protocol) return port;
255 else if (port.Name == portName)
261 string GetProtocol (Binding binding)
263 if (binding.Extensions.Find (typeof(SoapBinding)) != null) return "Soap";
264 HttpBinding hb = (HttpBinding) binding.Extensions.Find (typeof(HttpBinding));
265 if (hb == null) return "";
266 if (hb.Verb == "POST") return "HttpPost";
267 if (hb.Verb == "GET") return "HttpGet";
272 Operation FindOperation (PortType portType, string name)
274 foreach (Operation oper in portType.Operations) {
275 if (oper.Messages.Input.Name != null) {
276 if (oper.Messages.Input.Name == name) return oper;
279 if (oper.Name == name) return oper;
285 OperationBinding FindOperation (Binding binding, string name)
287 foreach (OperationBinding oper in binding.Operations) {
288 if (oper.Input.Name != null) {
289 if (oper.Input.Name == name) return oper;
292 if (oper.Name == name) return oper;
298 string FormatBindingName (string name)
300 if (name == DefaultBinding) return "Methods";
301 else return "Methods for binding<br>" + name;
304 string GetOpName (object op)
306 OperationBinding ob = op as OperationBinding;
307 if (ob == null) return "";
308 if (ob.Input.Name != null) return ob.Input.Name;
314 get { return Request.QueryString ["ext"] == "testform"; }
317 class NoCheckCertificatePolicy : ICertificatePolicy {
318 public bool CheckValidationResult (ServicePoint a, X509Certificate b, WebRequest c, int d)
324 string GetTestResult ()
326 if (!HasFormResult) return null;
330 for (int n=0; n<Request.QueryString.Count; n++)
333 if (qs != "") qs += "&";
334 qs += Request.QueryString.GetKey(n) + "=" + Server.UrlEncode (Request.QueryString [n]);
336 if (Request.QueryString.GetKey(n) == "ext") fill = true;
339 string location = null;
340 ServiceDescription desc = descriptions [0];
341 Service service = desc.Services[0];
342 foreach (Port port in service.Ports)
343 if (port.Name == CurrentOperationBinding)
345 SoapAddressBinding sbi = (SoapAddressBinding) port.Extensions.Find (typeof(SoapAddressBinding));
347 location = sbi.Location;
350 if (location == null)
351 return "Could not locate web service";
355 string url = location + "/" + CurrentOperationName;
356 Uri uri = new Uri (url);
357 HttpWebRequest req = (HttpWebRequest) WebRequest.Create (url + "?" + qs);
358 if (url.StartsWith ("https:"))
359 ServicePointManager.CertificatePolicy = new NoCheckCertificatePolicy ();
360 HttpCookieCollection cookies = Request.Cookies;
361 int last = cookies.Count;
363 CookieContainer container = new CookieContainer ();
364 for (int i = 0; i < last; i++) {
365 HttpCookie hcookie = cookies [i];
366 Cookie cookie = new Cookie (hcookie.Name, hcookie.Value, hcookie.Path, hcookie.Domain);
367 container.Add (uri, cookie);
369 ((HttpWebRequest) req).CookieContainer = container;
371 WebResponse resp = req.GetResponse();
372 StreamReader sr = new StreamReader (resp.GetResponseStream());
373 string s = sr.ReadToEnd ();
375 return "<div class='code-xml'>" + ColorizeXml(WrapText(s,CodeTextColumns)) + "</div>";
379 string res = "<b style='color:red'>" + ex.Message + "</b>";
380 WebException wex = ex as WebException;
383 WebResponse resp = wex.Response;
385 StreamReader sr = new StreamReader (resp.GetResponseStream());
386 string s = sr.ReadToEnd ();
388 res += "<div class='code-xml'>" + ColorizeXml(WrapText(s,CodeTextColumns)) + "</div>";
395 string GenerateOperationMessages (string protocol, bool generateInput)
397 if (!IsOperationSupported (protocol)) return "";
400 if (protocol != "Soap") port = FindPort (null, protocol);
401 else port = FindPort (CurrentOperationBinding, null);
403 Binding binding = descriptions.GetBinding (port.Binding);
404 OperationBinding obin = FindOperation (binding, CurrentOperationName);
405 PortType portType = descriptions.GetPortType (binding.Type);
406 Operation oper = FindOperation (portType, CurrentOperationName);
408 HtmlSampleGenerator sg = new HtmlSampleGenerator (descriptions, schemas);
409 string txt = sg.GenerateMessage (port, obin, oper, protocol, generateInput);
410 if (protocol == "Soap") txt = WrapText (txt,CodeTextColumns);
411 txt = ColorizeXml (txt);
412 txt = txt.Replace ("@placeholder!","<span class='literal-placeholder'>");
413 txt = txt.Replace ("!placeholder@","</span>");
417 bool IsOperationSupported (string protocol)
419 if (CurrentPage != "op" || CurrentTab != "msg") return false;
420 if (protocol == "Soap") return true;
422 Port port = FindPort (null, protocol);
423 if (port == null) return false;
424 Binding binding = descriptions.GetBinding (port.Binding);
425 if (binding == null) return false;
426 return FindOperation (binding, CurrentOperationName) != null;
430 // Proxy code generation
433 string GetProxyCode ()
435 CodeNamespace codeNamespace = new CodeNamespace();
436 CodeCompileUnit codeUnit = new CodeCompileUnit();
438 codeUnit.Namespaces.Add (codeNamespace);
440 ServiceDescriptionImporter importer = new ServiceDescriptionImporter();
442 foreach (ServiceDescription sd in descriptions)
443 importer.AddServiceDescription(sd, null, null);
445 foreach (XmlSchema sc in schemas)
446 importer.Schemas.Add (sc);
448 importer.Import(codeNamespace, codeUnit);
450 string langId = Request.QueryString ["lang"];
451 if (langId == null || langId == "") langId = "cs";
452 CodeDomProvider provider = GetProvider (langId);
453 ICodeGenerator generator = provider.CreateGenerator();
454 CodeGeneratorOptions options = new CodeGeneratorOptions();
456 StringWriter sw = new StringWriter ();
457 generator.GenerateCodeFromCompileUnit(codeUnit, sw, options);
459 return Colorize (WrapText (sw.ToString (), CodeTextColumns), langId);
462 public string CurrentLanguage
465 string langId = Request.QueryString ["lang"];
466 if (langId == null || langId == "") langId = "cs";
471 public string CurrentProxytName
474 string lan = CurrentLanguage == "cs" ? "C#" : "Visual Basic";
475 return lan + " Client Proxy";
479 private CodeDomProvider GetProvider(string langId)
481 switch (langId.ToUpper())
483 case "CS": return new CSharpCodeProvider();
484 case "VB": return new VBCodeProvider();
485 default: return null;
490 // Document generation
493 string GenerateDocument ()
495 StringWriter sw = new StringWriter ();
497 if (CurrentDocType == "wsdl")
498 descriptions [CurrentDocInd].Write (sw);
499 else if (CurrentDocType == "schema")
500 schemas [CurrentDocInd].Write (sw);
502 return Colorize (WrapText (sw.ToString (), CodeTextColumns), "xml");
505 public string CurrentDocType
507 get { return Request.QueryString ["doctype"] != null ? Request.QueryString ["doctype"] : "wsdl"; }
510 public int CurrentDocInd
512 get { return Request.QueryString ["docind"] != null ? int.Parse (Request.QueryString ["docind"]) : 0; }
515 public string CurrentDocumentName
518 if (CurrentDocType == "wsdl")
519 return "WSDL document for namespace \"" + descriptions [CurrentDocInd].TargetNamespace + "\"";
521 return "Xml Schema for namespace \"" + schemas [CurrentDocInd].TargetNamespace + "\"";
529 bool firstTab = true;
530 ArrayList disabledTabs = new ArrayList ();
534 get { return Request.QueryString["tab"] != null ? Request.QueryString["tab"] : "main" ; }
539 get { return Request.QueryString["page"] != null ? Request.QueryString["page"] : "main" ; }
544 if (CurrentOperationName != null)
546 WriteTab ("main","Overview");
547 WriteTab ("test","Test Form");
548 WriteTab ("msg","Message Layout");
552 void WriteTab (string id, string label)
554 if (!firstTab) Response.Write(" | ");
557 string cname = CurrentTab == id ? "tabLabelOn" : "tabLabelOff";
558 Response.Write ("<a href='" + PageName + "?" + GetPageContext(null) + GetDataContext() + "tab=" + id + "' style='text-decoration:none'>");
559 Response.Write ("<span class='" + cname + "'>" + label + "</span>");
560 Response.Write ("</a>");
563 string GetTabContext (string pag, string tab)
565 if (tab == null) tab = CurrentTab;
566 if (pag == null) pag = CurrentPage;
567 if (pag != CurrentPage) tab = "main";
568 return "page=" + pag + "&tab=" + tab + "&";
571 string GetPageContext (string pag)
573 if (pag == null) pag = CurrentPage;
574 return "page=" + pag + "&";
587 static string keywords_cs =
588 "(\\babstract\\b|\\bevent\\b|\\bnew\\b|\\bstruct\\b|\\bas\\b|\\bexplicit\\b|\\bnull\\b|\\bswitch\\b|\\bbase\\b|\\bextern\\b|" +
589 "\\bobject\\b|\\bthis\\b|\\bbool\\b|\\bfalse\\b|\\boperator\\b|\\bthrow\\b|\\bbreak\\b|\\bfinally\\b|\\bout\\b|\\btrue\\b|" +
590 "\\bbyte\\b|\\bfixed\\b|\\boverride\\b|\\btry\\b|\\bcase\\b|\\bfloat\\b|\\bparams\\b|\\btypeof\\b|\\bcatch\\b|\\bfor\\b|" +
591 "\\bprivate\\b|\\buint\\b|\\bchar\\b|\\bforeach\\b|\\bprotected\\b|\\bulong\\b|\\bchecked\\b|\\bgoto\\b|\\bpublic\\b|" +
592 "\\bunchecked\\b|\\bclass\\b|\\bif\\b|\\breadonly\\b|\\bunsafe\\b|\\bconst\\b|\\bimplicit\\b|\\bref\\b|\\bushort\\b|" +
593 "\\bcontinue\\b|\\bin\\b|\\breturn\\b|\\busing\\b|\\bdecimal\\b|\\bint\\b|\\bsbyte\\b|\\bvirtual\\b|\\bdefault\\b|" +
594 "\\binterface\\b|\\bsealed\\b|\\bvolatile\\b|\\bdelegate\\b|\\binternal\\b|\\bshort\\b|\\bvoid\\b|\\bdo\\b|\\bis\\b|" +
595 "\\bsizeof\\b|\\bwhile\\b|\\bdouble\\b|\\block\\b|\\bstackalloc\\b|\\belse\\b|\\blong\\b|\\bstatic\\b|\\benum\\b|" +
596 "\\bnamespace\\b|\\bstring\\b)";
598 static string keywords_vb =
599 "(\\bAddHandler\\b|\\bAddressOf\\b|\\bAlias\\b|\\bAnd\\b|\\bAndAlso\\b|\\bAnsi\\b|\\bAs\\b|\\bAssembly\\b|" +
600 "\\bAuto\\b|\\bBoolean\\b|\\bByRef\\b|\\bByte\\b|\\bByVal\\b|\\bCall\\b|\\bCase\\b|\\bCatch\\b|" +
601 "\\bCBool\\b|\\bCByte\\b|\\bCChar\\b|\\bCDate\\b|\\bCDec\\b|\\bCDbl\\b|\\bChar\\b|\\bCInt\\b|" +
602 "\\bClass\\b|\\bCLng\\b|\\bCObj\\b|\\bConst\\b|\\bCShort\\b|\\bCSng\\b|\\bCStr\\b|\\bCType\\b|" +
603 "\\bDate\\b|\\bDecimal\\b|\\bDeclare\\b|\\bDefault\\b|\\bDelegate\\b|\\bDim\\b|\\bDirectCast\\b|\\bDo\\b|" +
604 "\\bDouble\\b|\\bEach\\b|\\bElse\\b|\\bElseIf\\b|\\bEnd\\b|\\bEnum\\b|\\bErase\\b|\\bError\\b|" +
605 "\\bEvent\\b|\\bExit\\b|\\bFalse\\b|\\bFinally\\b|\\bFor\\b|\\bFriend\\b|\\bFunction\\b|\\bGet\\b|" +
606 "\\bGetType\\b|\\bGoSub\\b|\\bGoTo\\b|\\bHandles\\b|\\bIf\\b|\\bImplements\\b|\\bImports\\b|\\bIn\\b|" +
607 "\\bInherits\\b|\\bInteger\\b|\\bInterface\\b|\\bIs\\b|\\bLet\\b|\\bLib\\b|\\bLike\\b|\\bLong\\b|" +
608 "\\bLoop\\b|\\bMe\\b|\\bMod\\b|\\bModule\\b|\\bMustInherit\\b|\\bMustOverride\\b|\\bMyBase\\b|\\bMyClass\\b|" +
609 "\\bNamespace\\b|\\bNew\\b|\\bNext\\b|\\bNot\\b|\\bNothing\\b|\\bNotInheritable\\b|\\bNotOverridable\\b|\\bObject\\b|" +
610 "\\bOn\\b|\\bOption\\b|\\bOptional\\b|\\bOr\\b|\\bOrElse\\b|\\bOverloads\\b|\\bOverridable\\b|\\bOverrides\\b|" +
611 "\\bParamArray\\b|\\bPreserve\\b|\\bPrivate\\b|\\bProperty\\b|\\bProtected\\b|\\bPublic\\b|\\bRaiseEvent\\b|\\bReadOnly\\b|" +
612 "\\bReDim\\b|\\bREM\\b|\\bRemoveHandler\\b|\\bResume\\b|\\bReturn\\b|\\bSelect\\b|\\bSet\\b|\\bShadows\\b|" +
613 "\\bShared\\b|\\bShort\\b|\\bSingle\\b|\\bStatic\\b|\\bStep\\b|\\bStop\\b|\\bString\\b|\\bStructure\\b|" +
614 "\\bSub\\b|\\bSyncLock\\b|\\bThen\\b|\\bThrow\\b|\\bTo\\b|\\bTrue\\b|\\bTry\\b|\\bTypeOf\\b|" +
615 "\\bUnicode\\b|\\bUntil\\b|\\bVariant\\b|\\bWhen\\b|\\bWhile\\b|\\bWith\\b|\\bWithEvents\\b|\\bWriteOnly\\b|\\bXor\\b)";
617 string Colorize (string text, string lang)
619 if (lang == "xml") return ColorizeXml (text);
620 else if (lang == "cs") return ColorizeCs (text);
621 else if (lang == "vb") return ColorizeVb (text);
625 string ColorizeXml (string text)
627 text = text.Replace (" ", " ");
628 Regex re = new Regex ("\r\n|\r|\n");
629 text = re.Replace (text, "_br_");
631 re = new Regex ("<\\s*(\\/?)\\s*([\\s\\S]*?)\\s*(\\/?)\\s*>");
632 text = re.Replace (text,"{blue:<$1}{maroon:$2}{blue:$3>}");
634 re = new Regex ("\\{(\\w*):([\\s\\S]*?)\\}");
635 text = re.Replace (text,"<span style='color:$1'>$2</span>");
637 re = new Regex ("\"(.*?)\"");
638 text = re.Replace (text,"\"<span style='color:purple'>$1</span>\"");
641 text = text.Replace ("\t", " ");
642 text = text.Replace ("_br_", "<br>");
646 string ColorizeCs (string text)
648 text = text.Replace (" ", " ");
650 text = text.Replace ("<", "<");
651 text = text.Replace (">", ">");
653 Regex re = new Regex ("\"((((?!\").)|\\\")*?)\"");
654 text = re.Replace (text,"<span style='color:purple'>\"$1\"</span>");
656 re = new Regex ("//(((.(?!\"</span>))|\"(((?!\").)*)\"</span>)*)(\r|\n|\r\n)");
657 text = re.Replace (text,"<span style='color:green'>//$1</span><br/>");
659 re = new Regex (keywords_cs);
660 text = re.Replace (text,"<span style='color:blue'>$1</span>");
662 text = text.Replace ("\t"," ");
663 text = text.Replace ("\n","<br/>");
668 string ColorizeVb (string text)
670 text = text.Replace (" ", " ");
672 /* Regex re = new Regex ("\"((((?!\").)|\\\")*?)\"");
673 text = re.Replace (text,"<span style='color:purple'>\"$1\"</span>");
675 re = new Regex ("'(((.(?!\"\\<\\/span\\>))|\"(((?!\").)*)\"\\<\\/span\\>)*)(\r|\n|\r\n)");
676 text = re.Replace (text,"<span style='color:green'>//$1</span><br/>");
678 re = new Regex (keywords_vb);
679 text = re.Replace (text,"<span style='color:blue'>$1</span>");
681 text = text.Replace ("\t"," ");
682 text = text.Replace ("\n","<br/>");
687 // Helper methods and classes
690 string GetDataContext ()
692 return "op=" + CurrentOperationName + "&bnd=" + CurrentOperationBinding + "&";
695 string GetOptionSel (string v1, string v2)
697 string op = "<option ";
698 if (v1 == v2) op += "selected ";
699 return op + "value='" + v1 + "'>";
702 string WrapText (string text, int maxChars)
704 text = text.Replace(" />","/>");
706 string linspace = null;
710 bool inquotes = false;
712 string sublineIndent = "";
713 System.Text.StringBuilder sb = new System.Text.StringBuilder ();
714 for (int n=0; n<text.Length; n++)
718 if (c=='\r' || c=='\n' || n==text.Length-1)
720 sb.Append (linspace + sublineIndent + text.Substring (linstart, n-linstart+1));
730 if (lastc==',' || lastc=='(')
732 if (!inquotes) breakpos = n;
735 if (lincount > maxChars && breakpos >= linstart)
737 if (linspace != null)
738 sb.Append (linspace + sublineIndent);
739 sb.Append (text.Substring (linstart, breakpos-linstart));
742 lincount = linspace.Length + sublineIndent.Length + (n-breakpos);
746 if (c==' ' || c=='\t')
753 inquotes = !inquotes;
756 if (linspace == null) {
757 linspace = text.Substring (linstart, n-linstart);
764 return sb.ToString ();
773 public string Name { get { return name; } set { name = value; } }
774 public string Type { get { return type; } set { type = value; } }
775 public string Description { get { return description; } set { description = value; } }
778 public class HtmlSampleGenerator: SampleGenerator
780 public HtmlSampleGenerator (ServiceDescriptionCollection services, XmlSchemas schemas)
781 : base (services, schemas)
785 protected override string GetLiteral (string s)
787 return "@placeholder!" + s + "!placeholder@";
792 public class SampleGenerator
794 protected ServiceDescriptionCollection descriptions;
795 protected XmlSchemas schemas;
796 XmlSchemaElement anyElement;
798 SoapBindingUse currentUse;
799 XmlDocument document = new XmlDocument ();
801 static readonly XmlQualifiedName anyType = new XmlQualifiedName ("anyType",XmlSchema.Namespace);
802 static readonly XmlQualifiedName arrayType = new XmlQualifiedName ("Array","http://schemas.xmlsoap.org/soap/encoding/");
803 static readonly XmlQualifiedName arrayTypeRefName = new XmlQualifiedName ("arrayType","http://schemas.xmlsoap.org/soap/encoding/");
804 const string SoapEnvelopeNamespace = "http://schemas.xmlsoap.org/soap/envelope/";
805 const string WsdlNamespace = "http://schemas.xmlsoap.org/wsdl/";
806 const string SoapEncodingNamespace = "http://schemas.xmlsoap.org/soap/encoding/";
810 public EncodedType (string ns, XmlSchemaElement elem) { Namespace = ns; Element = elem; }
811 public string Namespace;
812 public XmlSchemaElement Element;
815 public SampleGenerator (ServiceDescriptionCollection services, XmlSchemas schemas)
817 descriptions = services;
818 this.schemas = schemas;
819 queue = new ArrayList ();
822 public string GenerateMessage (Port port, OperationBinding obin, Operation oper, string protocol, bool generateInput)
824 OperationMessage msg = null;
825 foreach (OperationMessage opm in oper.Messages)
827 if (opm is OperationInput && generateInput) msg = opm;
828 else if (opm is OperationOutput && !generateInput) msg = opm;
830 if (msg == null) return null;
833 case "Soap": return GenerateHttpSoapMessage (port, obin, oper, msg);
834 case "HttpGet": return GenerateHttpGetMessage (port, obin, oper, msg);
835 case "HttpPost": return GenerateHttpPostMessage (port, obin, oper, msg);
837 return "Unknown protocol";
840 public string GenerateHttpSoapMessage (Port port, OperationBinding obin, Operation oper, OperationMessage msg)
844 if (msg is OperationInput)
846 SoapAddressBinding sab = port.Extensions.Find (typeof(SoapAddressBinding)) as SoapAddressBinding;
847 SoapOperationBinding sob = obin.Extensions.Find (typeof(SoapOperationBinding)) as SoapOperationBinding;
848 req += "POST " + new Uri (sab.Location).AbsolutePath + "\n";
849 req += "SOAPAction: " + sob.SoapAction + "\n";
850 req += "Content-Type: text/xml; charset=utf-8\n";
851 req += "Content-Length: " + GetLiteral ("string") + "\n";
852 req += "Host: " + GetLiteral ("string") + "\n\n";
856 req += "HTTP/1.0 200 OK\n";
857 req += "Content-Type: text/xml; charset=utf-8\n";
858 req += "Content-Length: " + GetLiteral ("string") + "\n\n";
861 req += GenerateSoapMessage (obin, oper, msg);
865 public string GenerateHttpGetMessage (Port port, OperationBinding obin, Operation oper, OperationMessage msg)
869 if (msg is OperationInput)
871 HttpAddressBinding sab = port.Extensions.Find (typeof(HttpAddressBinding)) as HttpAddressBinding;
872 HttpOperationBinding sob = obin.Extensions.Find (typeof(HttpOperationBinding)) as HttpOperationBinding;
873 string location = new Uri (sab.Location).AbsolutePath + sob.Location + "?" + BuildQueryString (msg);
874 req += "GET " + location + "\n";
875 req += "Host: " + GetLiteral ("string");
879 req += "HTTP/1.0 200 OK\n";
880 req += "Content-Type: text/xml; charset=utf-8\n";
881 req += "Content-Length: " + GetLiteral ("string") + "\n\n";
883 MimeXmlBinding mxb = (MimeXmlBinding) obin.Output.Extensions.Find (typeof(MimeXmlBinding)) as MimeXmlBinding;
884 if (mxb == null) return req;
886 Message message = descriptions.GetMessage (msg.Message);
887 XmlQualifiedName ename = null;
888 foreach (MessagePart part in message.Parts)
889 if (part.Name == mxb.Part) ename = part.Element;
891 if (ename == null) return req + GetLiteral("string");
893 StringWriter sw = new StringWriter ();
894 XmlTextWriter xtw = new XmlTextWriter (sw);
895 xtw.Formatting = Formatting.Indented;
896 currentUse = SoapBindingUse.Literal;
897 WriteRootElementSample (xtw, ename);
899 req += sw.ToString ();
905 public string GenerateHttpPostMessage (Port port, OperationBinding obin, Operation oper, OperationMessage msg)
909 if (msg is OperationInput)
911 HttpAddressBinding sab = port.Extensions.Find (typeof(HttpAddressBinding)) as HttpAddressBinding;
912 HttpOperationBinding sob = obin.Extensions.Find (typeof(HttpOperationBinding)) as HttpOperationBinding;
913 string location = new Uri (sab.Location).AbsolutePath + sob.Location;
914 req += "POST " + location + "\n";
915 req += "Content-Type: application/x-www-form-urlencoded\n";
916 req += "Content-Length: " + GetLiteral ("string") + "\n";
917 req += "Host: " + GetLiteral ("string") + "\n\n";
918 req += BuildQueryString (msg);
920 else return GenerateHttpGetMessage (port, obin, oper, msg);
925 string BuildQueryString (OperationMessage opm)
928 Message msg = descriptions.GetMessage (opm.Message);
929 foreach (MessagePart part in msg.Parts)
931 if (s.Length != 0) s += "&";
932 s += part.Name + "=" + GetLiteral (part.Type.Name);
937 public string GenerateSoapMessage (OperationBinding obin, Operation oper, OperationMessage msg)
939 SoapOperationBinding sob = obin.Extensions.Find (typeof(SoapOperationBinding)) as SoapOperationBinding;
940 SoapBindingStyle style = (sob != null) ? sob.Style : SoapBindingStyle.Document;
942 MessageBinding msgbin = (msg is OperationInput) ? (MessageBinding) obin.Input : (MessageBinding)obin.Output;
943 SoapBodyBinding sbb = msgbin.Extensions.Find (typeof(SoapBodyBinding)) as SoapBodyBinding;
944 SoapBindingUse bodyUse = (sbb != null) ? sbb.Use : SoapBindingUse.Literal;
946 StringWriter sw = new StringWriter ();
947 XmlTextWriter xtw = new XmlTextWriter (sw);
948 xtw.Formatting = Formatting.Indented;
950 xtw.WriteStartDocument ();
951 xtw.WriteStartElement ("soap", "Envelope", SoapEnvelopeNamespace);
952 xtw.WriteAttributeString ("xmlns", "xsi", null, XmlSchema.InstanceNamespace);
953 xtw.WriteAttributeString ("xmlns", "xsd", null, XmlSchema.Namespace);
955 if (bodyUse == SoapBindingUse.Encoded)
957 xtw.WriteAttributeString ("xmlns", "soapenc", null, SoapEncodingNamespace);
958 xtw.WriteAttributeString ("xmlns", "tns", null, msg.Message.Namespace);
963 bool writtenHeader = false;
964 foreach (object ob in msgbin.Extensions)
966 SoapHeaderBinding hb = ob as SoapHeaderBinding;
967 if (hb == null) continue;
969 if (!writtenHeader) {
970 xtw.WriteStartElement ("soap", "Header", SoapEnvelopeNamespace);
971 writtenHeader = true;
974 WriteHeader (xtw, hb);
978 xtw.WriteEndElement ();
981 xtw.WriteStartElement ("soap", "Body", SoapEnvelopeNamespace);
983 currentUse = bodyUse;
984 WriteBody (xtw, oper, msg, sbb, style);
986 xtw.WriteEndElement ();
987 xtw.WriteEndElement ();
989 return sw.ToString ();
992 void WriteHeader (XmlTextWriter xtw, SoapHeaderBinding header)
994 Message msg = descriptions.GetMessage (header.Message);
995 if (msg == null) throw new InvalidOperationException ("Message " + header.Message + " not found");
996 MessagePart part = msg.Parts [header.Part];
997 if (part == null) throw new InvalidOperationException ("Message part " + header.Part + " not found in message " + header.Message);
999 currentUse = header.Use;
1001 if (currentUse == SoapBindingUse.Literal)
1002 WriteRootElementSample (xtw, part.Element);
1004 WriteTypeSample (xtw, part.Type);
1007 void WriteBody (XmlTextWriter xtw, Operation oper, OperationMessage opm, SoapBodyBinding sbb, SoapBindingStyle style)
1009 Message msg = descriptions.GetMessage (opm.Message);
1010 if (msg.Parts.Count > 0 && msg.Parts[0].Name == "parameters")
1012 MessagePart part = msg.Parts[0];
1013 if (part.Element == XmlQualifiedName.Empty)
1014 WriteTypeSample (xtw, part.Type);
1016 WriteRootElementSample (xtw, part.Element);
1020 string elemName = oper.Name;
1022 if (opm is OperationOutput) elemName += "Response";
1024 if (style == SoapBindingStyle.Rpc) {
1025 xtw.WriteStartElement (elemName, sbb.Namespace);
1029 foreach (MessagePart part in msg.Parts)
1031 if (part.Element == XmlQualifiedName.Empty)
1033 XmlSchemaElement elem = new XmlSchemaElement ();
1034 elem.SchemaTypeName = part.Type;
1035 elem.Name = part.Name;
1036 WriteElementSample (xtw, ns, elem);
1039 WriteRootElementSample (xtw, part.Element);
1042 if (style == SoapBindingStyle.Rpc)
1043 xtw.WriteEndElement ();
1045 WriteQueuedTypeSamples (xtw);
1048 void WriteRootElementSample (XmlTextWriter xtw, XmlQualifiedName qname)
1050 XmlSchemaElement elem = (XmlSchemaElement) schemas.Find (qname, typeof(XmlSchemaElement));
1051 if (elem == null) throw new InvalidOperationException ("Element not found: " + qname);
1052 WriteElementSample (xtw, qname.Namespace, elem);
1055 void WriteElementSample (XmlTextWriter xtw, string ns, XmlSchemaElement elem)
1057 bool sharedAnnType = false;
1058 XmlQualifiedName root;
1060 if (!elem.RefName.IsEmpty) {
1061 XmlSchemaElement refElem = FindRefElement (elem);
1062 if (refElem == null) throw new InvalidOperationException ("Global element not found: " + elem.RefName);
1063 root = elem.RefName;
1065 sharedAnnType = true;
1068 root = new XmlQualifiedName (elem.Name, ns);
1070 if (!elem.SchemaTypeName.IsEmpty)
1072 XmlSchemaComplexType st = FindComplexTyype (elem.SchemaTypeName);
1074 WriteComplexTypeSample (xtw, st, root);
1077 xtw.WriteStartElement (root.Name, root.Namespace);
1078 if (currentUse == SoapBindingUse.Encoded)
1079 xtw.WriteAttributeString ("type", XmlSchema.InstanceNamespace, GetQualifiedNameString (xtw, elem.SchemaTypeName));
1080 xtw.WriteString (GetLiteral (FindBuiltInType (elem.SchemaTypeName)));
1081 xtw.WriteEndElement ();
1084 else if (elem.SchemaType == null)
1086 xtw.WriteStartElement ("any");
1087 xtw.WriteEndElement ();
1090 WriteComplexTypeSample (xtw, (XmlSchemaComplexType) elem.SchemaType, root);
1093 void WriteTypeSample (XmlTextWriter xtw, XmlQualifiedName qname)
1095 XmlSchemaComplexType ctype = FindComplexTyype (qname);
1096 if (ctype != null) {
1097 WriteComplexTypeSample (xtw, ctype, qname);
1101 XmlSchemaSimpleType stype = (XmlSchemaSimpleType) schemas.Find (qname, typeof(XmlSchemaSimpleType));
1102 if (stype != null) {
1103 WriteSimpleTypeSample (xtw, stype);
1107 xtw.WriteString (GetLiteral (FindBuiltInType (qname)));
1108 throw new InvalidOperationException ("Type not found: " + qname);
1111 void WriteComplexTypeSample (XmlTextWriter xtw, XmlSchemaComplexType stype, XmlQualifiedName rootName)
1113 WriteComplexTypeSample (xtw, stype, rootName, -1);
1116 void WriteComplexTypeSample (XmlTextWriter xtw, XmlSchemaComplexType stype, XmlQualifiedName rootName, int id)
1118 string ns = rootName.Namespace;
1120 if (rootName.Name.IndexOf ("[]") != -1) rootName = arrayType;
1122 if (currentUse == SoapBindingUse.Encoded) {
1123 string pref = xtw.LookupPrefix (rootName.Namespace);
1124 if (pref == null) pref = "q1";
1125 xtw.WriteStartElement (pref, rootName.Name, rootName.Namespace);
1129 xtw.WriteStartElement (rootName.Name, rootName.Namespace);
1133 xtw.WriteAttributeString ("id", "id" + id);
1134 if (rootName != arrayType)
1135 xtw.WriteAttributeString ("type", XmlSchema.InstanceNamespace, GetQualifiedNameString (xtw, rootName));
1138 WriteComplexTypeAttributes (xtw, stype);
1139 WriteComplexTypeElements (xtw, ns, stype);
1141 xtw.WriteEndElement ();
1144 void WriteComplexTypeAttributes (XmlTextWriter xtw, XmlSchemaComplexType stype)
1146 WriteAttributes (xtw, stype.Attributes, stype.AnyAttribute);
1149 void WriteComplexTypeElements (XmlTextWriter xtw, string ns, XmlSchemaComplexType stype)
1151 if (stype.Particle != null)
1152 WriteParticleComplexContent (xtw, ns, stype.Particle);
1155 if (stype.ContentModel is XmlSchemaSimpleContent)
1156 WriteSimpleContent (xtw, (XmlSchemaSimpleContent)stype.ContentModel);
1157 else if (stype.ContentModel is XmlSchemaComplexContent)
1158 WriteComplexContent (xtw, ns, (XmlSchemaComplexContent)stype.ContentModel);
1162 void WriteAttributes (XmlTextWriter xtw, XmlSchemaObjectCollection atts, XmlSchemaAnyAttribute anyat)
1164 foreach (XmlSchemaObject at in atts)
1166 if (at is XmlSchemaAttribute)
1169 XmlSchemaAttribute attr = (XmlSchemaAttribute)at;
1170 XmlSchemaAttribute refAttr = attr;
1172 // refAttr.Form; TODO
1174 if (!attr.RefName.IsEmpty) {
1175 refAttr = FindRefAttribute (attr.RefName);
1176 if (refAttr == null) throw new InvalidOperationException ("Global attribute not found: " + attr.RefName);
1180 if (!refAttr.SchemaTypeName.IsEmpty) val = FindBuiltInType (refAttr.SchemaTypeName);
1181 else val = FindBuiltInType ((XmlSchemaSimpleType) refAttr.SchemaType);
1183 xtw.WriteAttributeString (refAttr.Name, val);
1185 else if (at is XmlSchemaAttributeGroupRef)
1187 XmlSchemaAttributeGroupRef gref = (XmlSchemaAttributeGroupRef)at;
1188 XmlSchemaAttributeGroup grp = (XmlSchemaAttributeGroup) schemas.Find (gref.RefName, typeof(XmlSchemaAttributeGroup));
1189 WriteAttributes (xtw, grp.Attributes, grp.AnyAttribute);
1194 xtw.WriteAttributeString ("custom-attribute","value");
1197 void WriteParticleComplexContent (XmlTextWriter xtw, string ns, XmlSchemaParticle particle)
1199 WriteParticleContent (xtw, ns, particle, false);
1202 void WriteParticleContent (XmlTextWriter xtw, string ns, XmlSchemaParticle particle, bool multiValue)
1204 if (particle is XmlSchemaGroupRef)
1205 particle = GetRefGroupParticle ((XmlSchemaGroupRef)particle);
1207 if (particle.MaxOccurs > 1) multiValue = true;
1209 if (particle is XmlSchemaSequence) {
1210 WriteSequenceContent (xtw, ns, ((XmlSchemaSequence)particle).Items, multiValue);
1212 else if (particle is XmlSchemaChoice) {
1213 if (((XmlSchemaChoice)particle).Items.Count == 1)
1214 WriteSequenceContent (xtw, ns, ((XmlSchemaChoice)particle).Items, multiValue);
1216 WriteChoiceContent (xtw, ns, (XmlSchemaChoice)particle, multiValue);
1218 else if (particle is XmlSchemaAll) {
1219 WriteSequenceContent (xtw, ns, ((XmlSchemaAll)particle).Items, multiValue);
1223 void WriteSequenceContent (XmlTextWriter xtw, string ns, XmlSchemaObjectCollection items, bool multiValue)
1225 foreach (XmlSchemaObject item in items)
1226 WriteContentItem (xtw, ns, item, multiValue);
1229 void WriteContentItem (XmlTextWriter xtw, string ns, XmlSchemaObject item, bool multiValue)
1231 if (item is XmlSchemaGroupRef)
1232 item = GetRefGroupParticle ((XmlSchemaGroupRef)item);
1234 if (item is XmlSchemaElement)
1236 XmlSchemaElement elem = (XmlSchemaElement) item;
1237 XmlSchemaElement refElem;
1238 if (!elem.RefName.IsEmpty) refElem = FindRefElement (elem);
1239 else refElem = elem;
1241 int num = (elem.MaxOccurs == 1 && !multiValue) ? 1 : 2;
1242 for (int n=0; n<num; n++)
1244 if (currentUse == SoapBindingUse.Literal)
1245 WriteElementSample (xtw, ns, refElem);
1247 WriteRefTypeSample (xtw, ns, refElem);
1250 else if (item is XmlSchemaAny)
1252 xtw.WriteString (GetLiteral ("xml"));
1254 else if (item is XmlSchemaParticle) {
1255 WriteParticleContent (xtw, ns, (XmlSchemaParticle)item, multiValue);
1259 void WriteChoiceContent (XmlTextWriter xtw, string ns, XmlSchemaChoice choice, bool multiValue)
1261 foreach (XmlSchemaObject item in choice.Items)
1262 WriteContentItem (xtw, ns, item, multiValue);
1265 void WriteSimpleContent (XmlTextWriter xtw, XmlSchemaSimpleContent content)
1267 XmlSchemaSimpleContentExtension ext = content.Content as XmlSchemaSimpleContentExtension;
1269 WriteAttributes (xtw, ext.Attributes, ext.AnyAttribute);
1271 XmlQualifiedName qname = GetContentBaseType (content.Content);
1272 xtw.WriteString (GetLiteral (FindBuiltInType (qname)));
1275 string FindBuiltInType (XmlQualifiedName qname)
1277 if (qname.Namespace == XmlSchema.Namespace)
1280 XmlSchemaComplexType ct = FindComplexTyype (qname);
1283 XmlSchemaSimpleContent sc = ct.ContentModel as XmlSchemaSimpleContent;
1284 if (sc == null) throw new InvalidOperationException ("Invalid schema");
1285 return FindBuiltInType (GetContentBaseType (sc.Content));
1288 XmlSchemaSimpleType st = (XmlSchemaSimpleType) schemas.Find (qname, typeof(XmlSchemaSimpleType));
1290 return FindBuiltInType (st);
1292 throw new InvalidOperationException ("Definition of type " + qname + " not found");
1295 string FindBuiltInType (XmlSchemaSimpleType st)
1297 if (st.Content is XmlSchemaSimpleTypeRestriction) {
1298 return FindBuiltInType (GetContentBaseType (st.Content));
1300 else if (st.Content is XmlSchemaSimpleTypeList) {
1301 string s = FindBuiltInType (GetContentBaseType (st.Content));
1302 return s + " " + s + " ...";
1304 else if (st.Content is XmlSchemaSimpleTypeUnion)
1306 //Check if all types of the union are equal. If not, then will use anyType.
1307 XmlSchemaSimpleTypeUnion uni = (XmlSchemaSimpleTypeUnion) st.Content;
1308 string utype = null;
1310 // Anonymous types are unique
1311 if (uni.BaseTypes.Count != 0 && uni.MemberTypes.Length != 0)
1314 foreach (XmlQualifiedName mt in uni.MemberTypes)
1316 string qn = FindBuiltInType (mt);
1317 if (utype != null && qn != utype) return "string";
1327 XmlQualifiedName GetContentBaseType (XmlSchemaObject ob)
1329 if (ob is XmlSchemaSimpleContentExtension)
1330 return ((XmlSchemaSimpleContentExtension)ob).BaseTypeName;
1331 else if (ob is XmlSchemaSimpleContentRestriction)
1332 return ((XmlSchemaSimpleContentRestriction)ob).BaseTypeName;
1333 else if (ob is XmlSchemaSimpleTypeRestriction)
1334 return ((XmlSchemaSimpleTypeRestriction)ob).BaseTypeName;
1335 else if (ob is XmlSchemaSimpleTypeList)
1336 return ((XmlSchemaSimpleTypeList)ob).ItemTypeName;
1341 void WriteComplexContent (XmlTextWriter xtw, string ns, XmlSchemaComplexContent content)
1343 XmlQualifiedName qname;
1345 XmlSchemaComplexContentExtension ext = content.Content as XmlSchemaComplexContentExtension;
1346 if (ext != null) qname = ext.BaseTypeName;
1348 XmlSchemaComplexContentRestriction rest = (XmlSchemaComplexContentRestriction)content.Content;
1349 qname = rest.BaseTypeName;
1350 if (qname == arrayType) {
1351 ParseArrayType (rest, out qname);
1352 XmlSchemaElement elem = new XmlSchemaElement ();
1354 elem.SchemaTypeName = qname;
1356 xtw.WriteAttributeString ("arrayType", SoapEncodingNamespace, qname.Name + "[2]");
1357 WriteContentItem (xtw, ns, elem, true);
1362 // Add base map members to this map
1363 XmlSchemaComplexType ctype = FindComplexTyype (qname);
1364 WriteComplexTypeAttributes (xtw, ctype);
1367 // Add the members of this map
1368 WriteAttributes (xtw, ext.Attributes, ext.AnyAttribute);
1369 if (ext.Particle != null)
1370 WriteParticleComplexContent (xtw, ns, ext.Particle);
1373 WriteComplexTypeElements (xtw, ns, ctype);
1376 void ParseArrayType (XmlSchemaComplexContentRestriction rest, out XmlQualifiedName qtype)
1378 XmlSchemaAttribute arrayTypeAt = FindArrayAttribute (rest.Attributes);
1379 XmlAttribute[] uatts = arrayTypeAt.UnhandledAttributes;
1380 if (uatts == null || uatts.Length == 0) throw new InvalidOperationException ("arrayType attribute not specified in array declaration");
1382 XmlAttribute xat = null;
1383 foreach (XmlAttribute at in uatts)
1384 if (at.LocalName == "arrayType" && at.NamespaceURI == WsdlNamespace)
1385 { xat = at; break; }
1388 throw new InvalidOperationException ("arrayType attribute not specified in array declaration");
1390 string arrayType = xat.Value;
1392 int i = arrayType.LastIndexOf (":");
1393 if (i == -1) ns = "";
1394 else ns = arrayType.Substring (0,i);
1396 int j = arrayType.IndexOf ("[", i+1);
1397 if (j == -1) throw new InvalidOperationException ("Cannot parse WSDL array type: " + arrayType);
1398 type = arrayType.Substring (i+1);
1399 type = type.Substring (0, type.Length-2);
1401 qtype = new XmlQualifiedName (type, ns);
1404 XmlSchemaAttribute FindArrayAttribute (XmlSchemaObjectCollection atts)
1406 foreach (object ob in atts)
1408 XmlSchemaAttribute att = ob as XmlSchemaAttribute;
1409 if (att != null && att.RefName == arrayTypeRefName) return att;
1411 XmlSchemaAttributeGroupRef gref = ob as XmlSchemaAttributeGroupRef;
1414 XmlSchemaAttributeGroup grp = (XmlSchemaAttributeGroup) schemas.Find (gref.RefName, typeof(XmlSchemaAttributeGroup));
1415 att = FindArrayAttribute (grp.Attributes);
1416 if (att != null) return att;
1422 void WriteSimpleTypeSample (XmlTextWriter xtw, XmlSchemaSimpleType stype)
1424 xtw.WriteString (GetLiteral (FindBuiltInType (stype)));
1427 XmlSchemaParticle GetRefGroupParticle (XmlSchemaGroupRef refGroup)
1429 XmlSchemaGroup grp = (XmlSchemaGroup) schemas.Find (refGroup.RefName, typeof (XmlSchemaGroup));
1430 return grp.Particle;
1433 XmlSchemaElement FindRefElement (XmlSchemaElement elem)
1435 if (elem.RefName.Namespace == XmlSchema.Namespace)
1437 if (anyElement != null) return anyElement;
1438 anyElement = new XmlSchemaElement ();
1439 anyElement.Name = "any";
1440 anyElement.SchemaTypeName = anyType;
1443 return (XmlSchemaElement) schemas.Find (elem.RefName, typeof(XmlSchemaElement));
1446 XmlSchemaAttribute FindRefAttribute (XmlQualifiedName refName)
1448 if (refName.Namespace == XmlSchema.Namespace)
1450 XmlSchemaAttribute at = new XmlSchemaAttribute ();
1451 at.Name = refName.Name;
1452 at.SchemaTypeName = new XmlQualifiedName ("string",XmlSchema.Namespace);
1455 return (XmlSchemaAttribute) schemas.Find (refName, typeof(XmlSchemaAttribute));
1458 void WriteRefTypeSample (XmlTextWriter xtw, string ns, XmlSchemaElement elem)
1460 if (elem.SchemaTypeName.Namespace == XmlSchema.Namespace || schemas.Find (elem.SchemaTypeName, typeof(XmlSchemaSimpleType)) != null)
1461 WriteElementSample (xtw, ns, elem);
1464 xtw.WriteStartElement (elem.Name, ns);
1465 xtw.WriteAttributeString ("href", "#id" + (queue.Count+1));
1466 xtw.WriteEndElement ();
1467 queue.Add (new EncodedType (ns, elem));
1471 void WriteQueuedTypeSamples (XmlTextWriter xtw)
1473 for (int n=0; n<queue.Count; n++)
1475 EncodedType ec = (EncodedType) queue[n];
1476 XmlSchemaComplexType st = FindComplexTyype (ec.Element.SchemaTypeName);
1477 WriteComplexTypeSample (xtw, st, ec.Element.SchemaTypeName, n+1);
1481 XmlSchemaComplexType FindComplexTyype (XmlQualifiedName qname)
1483 if (qname.Name.IndexOf ("[]") != -1)
1485 XmlSchemaComplexType stype = new XmlSchemaComplexType ();
1486 stype.ContentModel = new XmlSchemaComplexContent ();
1488 XmlSchemaComplexContentRestriction res = new XmlSchemaComplexContentRestriction ();
1489 stype.ContentModel.Content = res;
1490 res.BaseTypeName = arrayType;
1492 XmlSchemaAttribute att = new XmlSchemaAttribute ();
1493 att.RefName = arrayTypeRefName;
1494 res.Attributes.Add (att);
1496 XmlAttribute xat = document.CreateAttribute ("arrayType", WsdlNamespace);
1497 xat.Value = qname.Namespace + ":" + qname.Name;
1498 att.UnhandledAttributes = new XmlAttribute[] {xat};
1502 return (XmlSchemaComplexType) schemas.Find (qname, typeof(XmlSchemaComplexType));
1505 string GetQualifiedNameString (XmlTextWriter xtw, XmlQualifiedName qname)
1507 string pref = xtw.LookupPrefix (qname.Namespace);
1508 if (pref != null) return pref + ":" + qname.Name;
1510 xtw.WriteAttributeString ("xmlns", "q1", null, qname.Namespace);
1511 return "q1:" + qname.Name;
1514 protected virtual string GetLiteral (string s)
1519 void GetOperationFormat (OperationBinding obin, out SoapBindingStyle style, out SoapBindingUse use)
1521 style = SoapBindingStyle.Document;
1522 use = SoapBindingUse.Literal;
1523 SoapOperationBinding sob = obin.Extensions.Find (typeof(SoapOperationBinding)) as SoapOperationBinding;
1526 SoapBodyBinding sbb = obin.Input.Extensions.Find (typeof(SoapBodyBinding)) as SoapBodyBinding;
1540 <link rel="alternate" type="text/xml" href="<%=Request.FilePath%>?disco"/>
1542 <title><%=WebServiceName%> Web Service</title>
1543 <style type="text/css">
1544 BODY { font-family: Arial; margin-left: 20px; margin-top: 20px; font-size: x-small}
1545 TABLE { font-size: x-small }
1546 .title { color:dimgray; font-family: Arial; font-size:20pt; font-weight:900}
1547 .operationTitle { color:dimgray; font-family: Arial; font-size:15pt; font-weight:900}
1548 .method { font-size: x-small }
1549 .bindingLabel { font-size: x-small; font-weight:bold; color:darkgray; line-height:8pt; display:block; margin-bottom:3px }
1550 .label { font-size: small; font-weight:bold; color:darkgray }
1551 .paramTable { font-size: x-small }
1552 .paramTable TR { background-color: gainsboro }
1553 .paramFormTable { font-size: x-small; padding: 10px; background-color: gainsboro }
1554 .paramFormTable TR { background-color: gainsboro }
1555 .paramInput { border: solid 1px gray }
1556 .button {border: solid 1px gray }
1557 .smallSeparator { height:3px; overflow:hidden }
1558 .panel { background-color:whitesmoke; border: solid 1px silver; border-top: solid 1px silver }
1559 .codePanel { background-color: white; font-size:x-small; padding:7px; border:solid 1px silver}
1560 .code-xml { font-size:10pt; font-family:courier }
1561 .code-cs { font-size:10pt; font-family:courier }
1562 .code-vb { font-size:10pt; font-family:courier }
1563 .tabLabelOn { font-weight:bold }
1564 .tabLabelOff {color: darkgray }
1565 .literal-placeholder {color: darkblue; font-weight:bold}
1566 A:link { color: black; }
1567 A:visited { color: black; }
1568 A:active { color: black; }
1569 A:hover { color: blue }
1573 function clearForm ()
1575 document.getElementById("testFormResult").style.display="none";
1582 <div class="title" style="margin-left:20px">
1583 <span class="label">Web Service</span><br>
1588 **********************************************************
1592 <table border="0" width="100%" cellpadding="15px" cellspacing="15px">
1593 <tr valign="top"><td width="150px" class="panel">
1594 <div style="width:150px"></div>
1595 <a class="method" href='<%=PageName%>'>Overview</a><br>
1596 <div class="smallSeparator"></div>
1597 <a class="method" href='<%=PageName + "?" + GetPageContext("wsdl")%>'>Service Description</a>
1598 <div class="smallSeparator"></div>
1599 <a class="method" href='<%=PageName + "?" + GetPageContext("proxy")%>'>Client proxy</a>
1601 <asp:repeater id="BindingsRepeater" runat=server>
1602 <itemtemplate name="itemtemplate">
1603 <span class="bindingLabel"><%#FormatBindingName(DataBinder.Eval(Container.DataItem, "Name").ToString())%></span>
1604 <asp:repeater id="OperationsRepeater" runat=server datasource='<%# ((Binding)Container.DataItem).Operations %>'>
1606 <a class="method" href="<%=PageName%>?<%=GetTabContext("op",null)%>op=<%#GetOpName(Container.DataItem)%>&bnd=<%#DataBinder.Eval(Container.DataItem, "Binding.Name")%>"><%#GetOpName(Container.DataItem)%></a>
1607 <div class="smallSeparator"></div>
1614 </td><td class="panel">
1616 <% if (CurrentPage == "main") {%>
1619 **********************************************************
1620 Web service overview
1623 <p class="label">Web Service Overview</p>
1624 <%=WebServiceDescription%>
1626 <% if (ProfileViolations.Count > 0) { %>
1627 <p class="label">Basic Profile Conformance</p>
1628 This web service does not conform to WS-I Basic Profile v1.0
1630 Response.Write ("<ul>");
1631 foreach (BasicProfileViolation vio in ProfileViolations) {
1632 Response.Write ("<li><b>" + vio.NormativeStatement + "</b>: " + vio.Details);
1633 Response.Write ("<ul>");
1634 foreach (string ele in vio.Elements)
1635 Response.Write ("<li>" + ele + "</li>");
1636 Response.Write ("</ul>");
1637 Response.Write ("</li>");
1639 Response.Write ("</ul>");
1642 <%} if (DefaultBinding == null) {%>
1643 This service does not contain any public web method.
1644 <%} else if (CurrentPage == "op") {%>
1647 **********************************************************
1648 Operation description
1651 <span class="operationTitle"><%=CurrentOperationName%></span>
1656 <% if (CurrentTab == "main") { %>
1657 <span class="label">Input Parameters</span>
1658 <div class="smallSeparator"></div>
1659 <% if (InParams.Count == 0) { %>
1660 No input parameters<br>
1662 <table class="paramTable" cellspacing="1" cellpadding="5">
1663 <asp:repeater id="InputParamsRepeater" runat=server>
1666 <td width="150"><%#DataBinder.Eval(Container.DataItem, "Name")%></td>
1667 <td width="150"><%#DataBinder.Eval(Container.DataItem, "Type")%></td>
1675 <% if (OutParams.Count > 0) { %>
1676 <span class="label">Output Parameters</span>
1677 <div class="smallSeparator"></div>
1678 <table class="paramTable" cellspacing="1" cellpadding="5">
1679 <asp:repeater id="OutputParamsRepeater" runat=server>
1682 <td width="150"><%#DataBinder.Eval(Container.DataItem, "Name")%></td>
1683 <td width="150"><%#DataBinder.Eval(Container.DataItem, "Type")%></td>
1691 <span class="label">Remarks</span>
1692 <div class="smallSeparator"></div>
1693 <%=OperationDocumentation%>
1695 <span class="label">Technical information</span>
1696 <div class="smallSeparator"></div>
1697 Format: <%=CurrentOperationFormat%>
1698 <br>Supported protocols: <%=CurrentOperationProtocols%>
1702 **********************************************************
1703 Operation description - Test form
1706 <% if (CurrentTab == "test") {
1707 if (CurrentOperationSupportsTest) {%>
1708 Enter values for the parameters and click the 'Invoke' button to test this method:<br><br>
1709 <form action="<%=PageName%>" method="GET">
1710 <input type="hidden" name="page" value="<%=CurrentPage%>">
1711 <input type="hidden" name="tab" value="<%=CurrentTab%>">
1712 <input type="hidden" name="op" value="<%=CurrentOperationName%>">
1713 <input type="hidden" name="bnd" value="<%=CurrentOperationBinding%>">
1714 <input type="hidden" name="ext" value="testform">
1715 <table class="paramFormTable" cellspacing="0" cellpadding="3">
1716 <asp:repeater id="InputFormParamsRepeater" runat=server>
1719 <td><%#DataBinder.Eval(Container.DataItem, "Name")%>: </td>
1720 <td width="150"><input class="paramInput" type="text" size="20" name="<%#DataBinder.Eval(Container.DataItem, "Name")%>"></td>
1724 <tr><td></td><td><input class="button" type="submit" value="Invoke"> <input class="button" type="button" onclick="clearForm()" value="Clear"></td></tr>
1727 <div id="testFormResult" style="display:<%= (HasFormResult?"block":"none") %>">
1728 The web service returned the following result:<br/><br/>
1729 <div class="codePanel"><%=GetTestResult()%></div>
1732 The test form is not available for this operation because it has parameters with a complex structure.
1737 **********************************************************
1738 Operation description - Message Layout
1741 <% if (CurrentTab == "msg") { %>
1743 The following are sample SOAP requests and responses for each protocol supported by this method:
1746 <% if (IsOperationSupported ("Soap")) { %>
1747 <span class="label">Soap</span>
1749 <div class="codePanel"><div class="code-xml"><%=GenerateOperationMessages ("Soap", true)%></div></div>
1751 <div class="codePanel"><div class="code-xml"><%=GenerateOperationMessages ("Soap", false)%></div></div>
1754 <% if (IsOperationSupported ("HttpGet")) { %>
1755 <span class="label">HTTP Get</span>
1757 <div class="codePanel"><div class="code-xml"><%=GenerateOperationMessages ("HttpGet", true)%></div></div>
1759 <div class="codePanel"><div class="code-xml"><%=GenerateOperationMessages ("HttpGet", false)%></div></div>
1762 <% if (IsOperationSupported ("HttpPost")) { %>
1763 <span class="label">HTTP Post</span>
1765 <div class="codePanel"><div class="code-xml"><%=GenerateOperationMessages ("HttpPost", true)%></div></div>
1767 <div class="codePanel"><div class="code-xml"><%=GenerateOperationMessages ("HttpPost", false)%></div></div>
1772 <%} else if (CurrentPage == "proxy") {%>
1774 **********************************************************
1777 <form action="<%=PageName%>" name="langForm" method="GET">
1778 Select the language for which you want to generate a proxy
1779 <input type="hidden" name="page" value="<%=CurrentPage%>">
1780 <SELECT name="lang" onchange="langForm.submit()">
1781 <%=GetOptionSel("cs",CurrentLanguage)%>C#</option>
1782 <%=GetOptionSel("vb",CurrentLanguage)%>Visual Basic</option>
1787 <span class="label"><%=CurrentProxytName%></span>
1788 <a href="<%=PageName + "?code=" + CurrentLanguage%>">Download</a>
1790 <div class="codePanel">
1791 <div class="code-<%=CurrentLanguage%>"><%=GetProxyCode ()%></div>
1793 <%} else if (CurrentPage == "wsdl") {%>
1795 **********************************************************
1798 <% if (descriptions.Count > 1 || schemas.Count > 1) {%>
1799 The description of this web service is composed by several documents. Click on the document you want to see:
1803 for (int n=0; n<descriptions.Count; n++)
1804 Response.Write ("<li><a href='" + PageName + "?" + GetPageContext(null) + "doctype=wsdl&docind=" + n + "'>WSDL document " + descriptions[n].TargetNamespace + "</a></li>");
1805 for (int n=0; n<schemas.Count; n++)
1806 Response.Write ("<li><a href='" + PageName + "?" + GetPageContext(null) + "doctype=schema&docind=" + n + "'>Xml Schema " + schemas[n].TargetNamespace + "</a></li>");
1813 <span class="label"><%=CurrentDocumentName%></span>
1814 <a href="<%=PageName + "?" + CurrentDocType + "=" + CurrentDocInd %>">Download</a>
1816 <div class="codePanel">
1817 <div class="code-xml"><%=GenerateDocument ()%></div>
1822 <br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
1824 <td width="20px"></td>