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.Web.Services.Configuration" %>
20 <%@ Import Namespace="System" %>
21 <%@ Import Namespace="System.Net" %>
22 <%@ Import Namespace="System.Globalization" %>
23 <%@ Import Namespace="System.Resources" %>
24 <%@ Import Namespace="System.Diagnostics" %>
25 <%@ Import Namespace="System.CodeDom" %>
26 <%@ Import Namespace="System.CodeDom.Compiler" %>
27 <%@ Import Namespace="Microsoft.CSharp" %>
28 <%@ Import Namespace="Microsoft.VisualBasic" %>
29 <%@ Import Namespace="System.Text" %>
30 <%@ Import Namespace="System.Text.RegularExpressions" %>
31 <%@ Import Namespace="System.Security.Cryptography.X509Certificates" %>
32 <%@ Assembly name="System.Web.Services" %>
33 <%@ Page debug="true" %>
36 <script language="C#" runat="server">
38 ServiceDescriptionCollection descriptions;
41 string WebServiceName;
42 string WebServiceDescription;
45 string DefaultBinding;
46 ArrayList ServiceProtocols;
48 string CurrentOperationName;
49 string CurrentOperationBinding;
50 string OperationDocumentation;
51 string CurrentOperationFormat;
52 bool CurrentOperationSupportsTest;
55 string CurrentOperationProtocols;
56 int CodeTextColumns = 95;
57 BasicProfileViolationCollection ProfileViolations;
59 void Page_Load(object sender, EventArgs e)
61 descriptions = (ServiceDescriptionCollection) Context.Items["wsdls"];
62 schemas = (XmlSchemas) Context.Items["schemas"];
64 ServiceDescription desc = descriptions [0];
65 if (schemas.Count == 0) schemas = desc.Types.Schemas;
67 Service service = desc.Services[0];
68 WebServiceName = service.Name;
69 if (desc.Bindings.Count == 0)
72 DefaultBinding = desc.Bindings[0].Name;
73 WebServiceDescription = service.Documentation;
74 if (WebServiceDescription == "" || WebServiceDescription == null)
75 WebServiceDescription = "Description has not been provided";
76 ServiceProtocols = FindServiceProtocols (null);
78 CurrentOperationName = Request.QueryString["op"];
79 CurrentOperationBinding = Request.QueryString["bnd"];
80 if (CurrentOperationName != null) BuildOperationInfo ();
82 PageName = HttpUtility.UrlEncode (Path.GetFileName(Request.Path), Encoding.UTF8);
84 ArrayList list = new ArrayList ();
85 foreach (ServiceDescription sd in descriptions) {
86 foreach (Binding bin in sd.Bindings)
87 if (bin.Extensions.Find (typeof(SoapBinding)) != null) list.Add (bin);
90 BindingsRepeater.DataSource = list;
93 ProfileViolations = new BasicProfileViolationCollection ();
94 WebServicesInteroperability.CheckConformance (WsiProfiles.BasicProfile1_1, descriptions, ProfileViolations);
97 void BuildOperationInfo ()
99 InParams = new ArrayList ();
100 OutParams = new ArrayList ();
102 Port port = FindPort (CurrentOperationBinding, null);
103 Binding binding = descriptions.GetBinding (port.Binding);
105 PortType portType = descriptions.GetPortType (binding.Type);
106 Operation oper = FindOperation (portType, CurrentOperationName);
108 OperationDocumentation = oper.Documentation;
109 if (OperationDocumentation == null || OperationDocumentation == "")
110 OperationDocumentation = "No additional remarks";
112 foreach (OperationMessage opm in oper.Messages)
114 if (opm is OperationInput)
115 BuildParameters (InParams, opm);
116 else if (opm is OperationOutput)
117 BuildParameters (OutParams, opm);
120 // Protocols supported by the operation
121 CurrentOperationProtocols = "";
122 ArrayList prots = FindServiceProtocols (CurrentOperationName);
123 for (int n=0; n<prots.Count; n++) {
124 if (n != 0) CurrentOperationProtocols += ", ";
125 CurrentOperationProtocols += (string) prots[n];
128 WebServiceProtocols testProtocols = WebServiceProtocols.HttpGet | WebServiceProtocols.HttpPost;
129 if (Context.Request.IsLocal)
130 testProtocols |= WebServiceProtocols.HttpPostLocalhost;
131 CurrentOperationSupportsTest = (WebServicesSection.Current.EnabledProtocols & testProtocols) != 0;
134 OperationBinding obin = FindOperation (binding, CurrentOperationName);
136 CurrentOperationFormat = GetOperationFormat (obin);
138 InputParamsRepeater.DataSource = InParams;
139 InputFormParamsRepeater.DataSource = InParams;
140 OutputParamsRepeater.DataSource = OutParams;
143 void BuildParameters (ArrayList list, OperationMessage opm)
145 Message msg = descriptions.GetMessage (opm.Message);
146 if (msg.Parts.Count > 0 && msg.Parts[0].Name == "parameters")
148 MessagePart part = msg.Parts[0];
149 XmlSchemaComplexType ctype;
150 if (part.Element == XmlQualifiedName.Empty)
152 ctype = (XmlSchemaComplexType) schemas.Find (part.Type, typeof(XmlSchemaComplexType));
156 XmlSchemaElement elem = (XmlSchemaElement) schemas.Find (part.Element, typeof(XmlSchemaElement));
157 ctype = (XmlSchemaComplexType) elem.SchemaType;
159 XmlSchemaSequence seq = ctype.Particle as XmlSchemaSequence;
160 if (seq == null) return;
162 foreach (XmlSchemaObject ob in seq.Items)
164 Parameter p = new Parameter();
165 p.Description = "No additional remarks";
167 if (ob is XmlSchemaElement)
169 XmlSchemaElement selem = GetRefElement ((XmlSchemaElement)ob);
171 p.Type = selem.SchemaTypeName.Name;
183 foreach (MessagePart part in msg.Parts)
185 Parameter p = new Parameter ();
186 p.Description = "No additional remarks";
188 if (part.Element == XmlQualifiedName.Empty)
189 p.Type = part.Type.Name;
192 XmlSchemaElement elem = (XmlSchemaElement) schemas.Find (part.Element, typeof(XmlSchemaElement));
193 p.Type = elem.SchemaTypeName.Name;
200 string GetOperationFormat (OperationBinding obin)
203 SoapOperationBinding sob = obin.Extensions.Find (typeof(SoapOperationBinding)) as SoapOperationBinding;
205 format = sob.Style.ToString ();
206 SoapBodyBinding sbb = obin.Input.Extensions.Find (typeof(SoapBodyBinding)) as SoapBodyBinding;
208 format += " / " + sbb.Use;
213 XmlSchemaElement GetRefElement (XmlSchemaElement elem)
215 if (!elem.RefName.IsEmpty)
216 return (XmlSchemaElement) schemas.Find (elem.RefName, typeof(XmlSchemaElement));
221 ArrayList FindServiceProtocols(string operName)
223 ArrayList table = new ArrayList ();
224 Service service = descriptions[0].Services[0];
225 foreach (Port port in service.Ports)
228 Binding bin = descriptions.GetBinding (port.Binding);
229 if (bin.Extensions.Find (typeof(SoapBinding)) != null)
233 HttpBinding hb = (HttpBinding) bin.Extensions.Find (typeof(HttpBinding));
234 if (hb != null && hb.Verb == "POST") prot = "HttpPost";
235 else if (hb != null && hb.Verb == "GET") prot = "HttpGet";
238 if (prot != null && operName != null)
240 if (FindOperation (bin, operName) == null)
244 if (prot != null && !table.Contains (prot))
250 Port FindPort (string portName, string protocol)
252 Service service = descriptions[0].Services[0];
253 foreach (Port port in service.Ports)
255 if (portName == null)
257 Binding binding = descriptions.GetBinding (port.Binding);
258 if (GetProtocol (binding) == protocol) return port;
260 else if (port.Name == portName)
266 string GetProtocol (Binding binding)
268 if (binding.Extensions.Find (typeof(SoapBinding)) != null) return "Soap";
269 HttpBinding hb = (HttpBinding) binding.Extensions.Find (typeof(HttpBinding));
270 if (hb == null) return "";
271 if (hb.Verb == "POST") return "HttpPost";
272 if (hb.Verb == "GET") return "HttpGet";
277 Operation FindOperation (PortType portType, string name)
279 foreach (Operation oper in portType.Operations) {
280 if (oper.Messages.Input.Name != null) {
281 if (oper.Messages.Input.Name == name) return oper;
284 if (oper.Name == name) return oper;
290 OperationBinding FindOperation (Binding binding, string name)
292 foreach (OperationBinding oper in binding.Operations) {
293 if (oper.Input.Name != null) {
294 if (oper.Input.Name == name) return oper;
297 if (oper.Name == name) return oper;
303 string FormatBindingName (string name)
305 if (name == DefaultBinding) return "Methods";
306 else return "Methods for binding<br>" + name;
309 string GetOpName (object op)
311 OperationBinding ob = op as OperationBinding;
312 if (ob == null) return "";
313 if (ob.Input.Name != null) return ob.Input.Name;
319 get { return Request.QueryString ["ext"] == "testform"; }
322 class NoCheckCertificatePolicy : ICertificatePolicy {
323 public bool CheckValidationResult (ServicePoint a, X509Certificate b, WebRequest c, int d)
329 string GetTestResult ()
331 if (!HasFormResult) return null;
335 for (int n=0; n<Request.QueryString.Count; n++)
338 if (qs != "") qs += "&";
339 qs += Request.QueryString.GetKey(n) + "=" + Server.UrlEncode (Request.QueryString [n]);
341 if (Request.QueryString.GetKey(n) == "ext") fill = true;
344 string location = null;
345 ServiceDescription desc = descriptions [0];
346 Service service = desc.Services[0];
347 foreach (Port port in service.Ports)
348 if (port.Name == CurrentOperationBinding)
350 SoapAddressBinding sbi = (SoapAddressBinding) port.Extensions.Find (typeof(SoapAddressBinding));
352 location = sbi.Location;
355 if (location == null)
356 return "Could not locate web service";
360 string url = location + "/" + CurrentOperationName;
361 Uri uri = new Uri (url);
363 if (CurrentOperationProtocols.IndexOf ("HttpGet") < 0) {
364 req = WebRequest.Create (url);
366 if (!String.IsNullOrEmpty (qs)) {
367 req.ContentType = "application/x-www-form-urlencoded";
368 byte [] postBuffer = Encoding.UTF8.GetBytes (qs);
369 req.ContentLength = postBuffer.Length;
370 using (Stream requestStream = req.GetRequestStream())
371 requestStream.Write (postBuffer, 0, postBuffer.Length);
375 req = WebRequest.Create (url + "?" + qs);
376 if (url.StartsWith ("https:"))
377 ServicePointManager.CertificatePolicy = new NoCheckCertificatePolicy ();
378 HttpCookieCollection cookies = Request.Cookies;
379 int last = cookies.Count;
381 CookieContainer container = new CookieContainer ();
382 for (int i = 0; i < last; i++) {
383 HttpCookie hcookie = cookies [i];
384 Cookie cookie = new Cookie (hcookie.Name, hcookie.Value, hcookie.Path, hcookie.Domain);
385 container.Add (uri, cookie);
387 ((HttpWebRequest) req).CookieContainer = container;
389 WebResponse resp = req.GetResponse();
390 StreamReader sr = new StreamReader (resp.GetResponseStream());
391 string s = sr.ReadToEnd ();
393 return "<div class='code-xml'>" + ColorizeXml(WrapText(s,CodeTextColumns)) + "</div>";
397 string res = "<b style='color:red'>" + ex.Message + "</b>";
398 WebException wex = ex as WebException;
401 WebResponse resp = wex.Response;
403 StreamReader sr = new StreamReader (resp.GetResponseStream());
404 string s = sr.ReadToEnd ();
406 res += "<div class='code-xml'>" + ColorizeXml(WrapText(s,CodeTextColumns)) + "</div>";
413 string GenerateOperationMessages (string protocol, bool generateInput)
415 if (!IsOperationSupported (protocol)) return "";
418 if (protocol != "Soap") port = FindPort (null, protocol);
419 else port = FindPort (CurrentOperationBinding, null);
421 Binding binding = descriptions.GetBinding (port.Binding);
422 OperationBinding obin = FindOperation (binding, CurrentOperationName);
423 PortType portType = descriptions.GetPortType (binding.Type);
424 Operation oper = FindOperation (portType, CurrentOperationName);
426 HtmlSampleGenerator sg = new HtmlSampleGenerator (descriptions, schemas);
427 string txt = sg.GenerateMessage (port, obin, oper, protocol, generateInput);
428 if (protocol == "Soap") txt = WrapText (txt,CodeTextColumns);
429 txt = ColorizeXml (txt);
430 txt = txt.Replace ("@placeholder!","<span class='literal-placeholder'>");
431 txt = txt.Replace ("!placeholder@","</span>");
435 bool IsOperationSupported (string protocol)
437 if (CurrentPage != "op" || CurrentTab != "msg") return false;
438 if (protocol == "Soap") return true;
440 Port port = FindPort (null, protocol);
441 if (port == null) return false;
442 Binding binding = descriptions.GetBinding (port.Binding);
443 if (binding == null) return false;
444 return FindOperation (binding, CurrentOperationName) != null;
448 // Proxy code generation
451 string GetProxyCode ()
453 CodeNamespace codeNamespace = new CodeNamespace();
454 CodeCompileUnit codeUnit = new CodeCompileUnit();
456 codeUnit.Namespaces.Add (codeNamespace);
458 ServiceDescriptionImporter importer = new ServiceDescriptionImporter();
460 foreach (ServiceDescription sd in descriptions)
461 importer.AddServiceDescription(sd, null, null);
463 foreach (XmlSchema sc in schemas)
464 importer.Schemas.Add (sc);
466 importer.Import(codeNamespace, codeUnit);
468 string langId = Request.QueryString ["lang"];
469 if (langId == null || langId == "") langId = "cs";
470 CodeDomProvider provider = GetProvider (langId);
471 ICodeGenerator generator = provider.CreateGenerator();
472 CodeGeneratorOptions options = new CodeGeneratorOptions();
474 StringWriter sw = new StringWriter ();
475 generator.GenerateCodeFromCompileUnit(codeUnit, sw, options);
477 return Colorize (WrapText (sw.ToString (), CodeTextColumns), langId);
480 public string CurrentLanguage
483 string langId = Request.QueryString ["lang"];
484 if (langId == null || langId == "") langId = "cs";
489 public string CurrentProxytName
492 string lan = CurrentLanguage == "cs" ? "C#" : "Visual Basic";
493 return lan + " Client Proxy";
497 private CodeDomProvider GetProvider(string langId)
499 switch (langId.ToUpper())
501 case "CS": return new CSharpCodeProvider();
502 case "VB": return new VBCodeProvider();
503 default: return null;
508 // Document generation
511 string GenerateDocument ()
513 StringWriter sw = new StringWriter ();
515 if (CurrentDocType == "wsdl")
516 descriptions [CurrentDocInd].Write (sw);
517 else if (CurrentDocType == "schema")
518 schemas [CurrentDocInd].Write (sw);
520 return Colorize (WrapText (sw.ToString (), CodeTextColumns), "xml");
523 public string CurrentDocType
525 get { return Request.QueryString ["doctype"] != null ? Request.QueryString ["doctype"] : "wsdl"; }
528 public int CurrentDocInd
530 get { return Request.QueryString ["docind"] != null ? int.Parse (Request.QueryString ["docind"]) : 0; }
533 public string CurrentDocumentName
536 if (CurrentDocType == "wsdl")
537 return "WSDL document for namespace \"" + descriptions [CurrentDocInd].TargetNamespace + "\"";
539 return "Xml Schema for namespace \"" + schemas [CurrentDocInd].TargetNamespace + "\"";
547 bool firstTab = true;
548 ArrayList disabledTabs = new ArrayList ();
552 get { return Request.QueryString["tab"] != null ? Request.QueryString["tab"] : "main" ; }
557 get { return Request.QueryString["page"] != null ? Request.QueryString["page"] : "main" ; }
562 if (CurrentOperationName != null)
564 WriteTab ("main","Overview");
565 WriteTab ("test","Test Form");
566 WriteTab ("msg","Message Layout");
570 void WriteTab (string id, string label)
572 if (!firstTab) Response.Write(" | ");
575 string cname = CurrentTab == id ? "tabLabelOn" : "tabLabelOff";
576 Response.Write ("<a href='" + PageName + "?" + GetPageContext(null) + GetDataContext() + "tab=" + id + "' style='text-decoration:none'>");
577 Response.Write ("<span class='" + cname + "'>" + label + "</span>");
578 Response.Write ("</a>");
581 string GetTabContext (string pag, string tab)
583 if (tab == null) tab = CurrentTab;
584 if (pag == null) pag = CurrentPage;
585 if (pag != CurrentPage) tab = "main";
586 return "page=" + pag + "&tab=" + tab + "&";
589 string GetPageContext (string pag)
591 if (pag == null) pag = CurrentPage;
592 return "page=" + pag + "&";
605 static string keywords_cs =
606 "(\\babstract\\b|\\bevent\\b|\\bnew\\b|\\bstruct\\b|\\bas\\b|\\bexplicit\\b|\\bnull\\b|\\bswitch\\b|\\bbase\\b|\\bextern\\b|" +
607 "\\bobject\\b|\\bthis\\b|\\bbool\\b|\\bfalse\\b|\\boperator\\b|\\bthrow\\b|\\bbreak\\b|\\bfinally\\b|\\bout\\b|\\btrue\\b|" +
608 "\\bbyte\\b|\\bfixed\\b|\\boverride\\b|\\btry\\b|\\bcase\\b|\\bfloat\\b|\\bparams\\b|\\btypeof\\b|\\bcatch\\b|\\bfor\\b|" +
609 "\\bprivate\\b|\\buint\\b|\\bchar\\b|\\bforeach\\b|\\bprotected\\b|\\bulong\\b|\\bchecked\\b|\\bgoto\\b|\\bpublic\\b|" +
610 "\\bunchecked\\b|\\bclass\\b|\\bif\\b|\\breadonly\\b|\\bunsafe\\b|\\bconst\\b|\\bimplicit\\b|\\bref\\b|\\bushort\\b|" +
611 "\\bcontinue\\b|\\bin\\b|\\breturn\\b|\\busing\\b|\\bdecimal\\b|\\bint\\b|\\bsbyte\\b|\\bvirtual\\b|\\bdefault\\b|" +
612 "\\binterface\\b|\\bsealed\\b|\\bvolatile\\b|\\bdelegate\\b|\\binternal\\b|\\bshort\\b|\\bvoid\\b|\\bdo\\b|\\bis\\b|" +
613 "\\bsizeof\\b|\\bwhile\\b|\\bdouble\\b|\\block\\b|\\bstackalloc\\b|\\belse\\b|\\blong\\b|\\bstatic\\b|\\benum\\b|" +
614 "\\bnamespace\\b|\\bstring\\b)";
616 static string keywords_vb =
617 "(\\bAddHandler\\b|\\bAddressOf\\b|\\bAlias\\b|\\bAnd\\b|\\bAndAlso\\b|\\bAnsi\\b|\\bAs\\b|\\bAssembly\\b|" +
618 "\\bAuto\\b|\\bBoolean\\b|\\bByRef\\b|\\bByte\\b|\\bByVal\\b|\\bCall\\b|\\bCase\\b|\\bCatch\\b|" +
619 "\\bCBool\\b|\\bCByte\\b|\\bCChar\\b|\\bCDate\\b|\\bCDec\\b|\\bCDbl\\b|\\bChar\\b|\\bCInt\\b|" +
620 "\\bClass\\b|\\bCLng\\b|\\bCObj\\b|\\bConst\\b|\\bCShort\\b|\\bCSng\\b|\\bCStr\\b|\\bCType\\b|" +
621 "\\bDate\\b|\\bDecimal\\b|\\bDeclare\\b|\\bDefault\\b|\\bDelegate\\b|\\bDim\\b|\\bDirectCast\\b|\\bDo\\b|" +
622 "\\bDouble\\b|\\bEach\\b|\\bElse\\b|\\bElseIf\\b|\\bEnd\\b|\\bEnum\\b|\\bErase\\b|\\bError\\b|" +
623 "\\bEvent\\b|\\bExit\\b|\\bFalse\\b|\\bFinally\\b|\\bFor\\b|\\bFriend\\b|\\bFunction\\b|\\bGet\\b|" +
624 "\\bGetType\\b|\\bGoSub\\b|\\bGoTo\\b|\\bHandles\\b|\\bIf\\b|\\bImplements\\b|\\bImports\\b|\\bIn\\b|" +
625 "\\bInherits\\b|\\bInteger\\b|\\bInterface\\b|\\bIs\\b|\\bLet\\b|\\bLib\\b|\\bLike\\b|\\bLong\\b|" +
626 "\\bLoop\\b|\\bMe\\b|\\bMod\\b|\\bModule\\b|\\bMustInherit\\b|\\bMustOverride\\b|\\bMyBase\\b|\\bMyClass\\b|" +
627 "\\bNamespace\\b|\\bNew\\b|\\bNext\\b|\\bNot\\b|\\bNothing\\b|\\bNotInheritable\\b|\\bNotOverridable\\b|\\bObject\\b|" +
628 "\\bOn\\b|\\bOption\\b|\\bOptional\\b|\\bOr\\b|\\bOrElse\\b|\\bOverloads\\b|\\bOverridable\\b|\\bOverrides\\b|" +
629 "\\bParamArray\\b|\\bPreserve\\b|\\bPrivate\\b|\\bProperty\\b|\\bProtected\\b|\\bPublic\\b|\\bRaiseEvent\\b|\\bReadOnly\\b|" +
630 "\\bReDim\\b|\\bREM\\b|\\bRemoveHandler\\b|\\bResume\\b|\\bReturn\\b|\\bSelect\\b|\\bSet\\b|\\bShadows\\b|" +
631 "\\bShared\\b|\\bShort\\b|\\bSingle\\b|\\bStatic\\b|\\bStep\\b|\\bStop\\b|\\bString\\b|\\bStructure\\b|" +
632 "\\bSub\\b|\\bSyncLock\\b|\\bThen\\b|\\bThrow\\b|\\bTo\\b|\\bTrue\\b|\\bTry\\b|\\bTypeOf\\b|" +
633 "\\bUnicode\\b|\\bUntil\\b|\\bVariant\\b|\\bWhen\\b|\\bWhile\\b|\\bWith\\b|\\bWithEvents\\b|\\bWriteOnly\\b|\\bXor\\b)";
635 string Colorize (string text, string lang)
637 if (lang == "xml") return ColorizeXml (text);
638 else if (lang == "cs") return ColorizeCs (text);
639 else if (lang == "vb") return ColorizeVb (text);
643 string ColorizeXml (string text)
645 text = text.Replace (" ", " ");
646 Regex re = new Regex ("\r\n|\r|\n");
647 text = re.Replace (text, "_br_");
649 re = new Regex ("<\\s*(\\/?)\\s*([\\s\\S]*?)\\s*(\\/?)\\s*>");
650 text = re.Replace (text,"{blue:<$1}{maroon:$2}{blue:$3>}");
652 re = new Regex ("\\{(\\w*):([\\s\\S]*?)\\}");
653 text = re.Replace (text,"<span style='color:$1'>$2</span>");
655 re = new Regex ("\"(.*?)\"");
656 text = re.Replace (text,"\"<span style='color:purple'>$1</span>\"");
659 text = text.Replace ("\t", " ");
660 text = text.Replace ("_br_", "<br>");
664 string ColorizeCs (string text)
666 text = text.Replace (" ", " ");
668 text = text.Replace ("<", "<");
669 text = text.Replace (">", ">");
671 Regex re = new Regex ("\"((((?!\").)|\\\")*?)\"");
672 text = re.Replace (text,"<span style='color:purple'>\"$1\"</span>");
674 re = new Regex ("//(((.(?!\"</span>))|\"(((?!\").)*)\"</span>)*)(\r|\n|\r\n)");
675 text = re.Replace (text,"<span style='color:green'>//$1</span><br/>");
677 re = new Regex (keywords_cs);
678 text = re.Replace (text,"<span style='color:blue'>$1</span>");
680 text = text.Replace ("\t"," ");
681 text = text.Replace ("\n","<br/>");
686 string ColorizeVb (string text)
688 text = text.Replace (" ", " ");
690 /* Regex re = new Regex ("\"((((?!\").)|\\\")*?)\"");
691 text = re.Replace (text,"<span style='color:purple'>\"$1\"</span>");
693 re = new Regex ("'(((.(?!\"\\<\\/span\\>))|\"(((?!\").)*)\"\\<\\/span\\>)*)(\r|\n|\r\n)");
694 text = re.Replace (text,"<span style='color:green'>//$1</span><br/>");
696 re = new Regex (keywords_vb);
697 text = re.Replace (text,"<span style='color:blue'>$1</span>");
699 text = text.Replace ("\t"," ");
700 text = text.Replace ("\n","<br/>");
705 // Helper methods and classes
708 string GetDataContext ()
710 return "op=" + CurrentOperationName + "&bnd=" + CurrentOperationBinding + "&";
713 string GetOptionSel (string v1, string v2)
715 string op = "<option ";
716 if (v1 == v2) op += "selected ";
717 return op + "value='" + v1 + "'>";
720 string WrapText (string text, int maxChars)
722 text = text.Replace(" />","/>");
724 string linspace = null;
728 bool inquotes = false;
730 string sublineIndent = "";
731 System.Text.StringBuilder sb = new System.Text.StringBuilder ();
732 for (int n=0; n<text.Length; n++)
736 if (c=='\r' || c=='\n' || n==text.Length-1)
738 sb.Append (linspace + sublineIndent + text.Substring (linstart, n-linstart+1));
748 if (lastc==',' || lastc=='(')
750 if (!inquotes) breakpos = n;
753 if (lincount > maxChars && breakpos >= linstart)
755 if (linspace != null)
756 sb.Append (linspace + sublineIndent);
757 sb.Append (text.Substring (linstart, breakpos-linstart));
760 lincount = linspace.Length + sublineIndent.Length + (n-breakpos);
764 if (c==' ' || c=='\t')
771 inquotes = !inquotes;
774 if (linspace == null) {
775 linspace = text.Substring (linstart, n-linstart);
782 return sb.ToString ();
791 public string Name { get { return name; } set { name = value; } }
792 public string Type { get { return type; } set { type = value; } }
793 public string Description { get { return description; } set { description = value; } }
796 public class HtmlSampleGenerator: SampleGenerator
798 public HtmlSampleGenerator (ServiceDescriptionCollection services, XmlSchemas schemas)
799 : base (services, schemas)
803 protected override string GetLiteral (string s)
805 return "@placeholder!" + s + "!placeholder@";
810 public class SampleGenerator
812 protected ServiceDescriptionCollection descriptions;
813 protected XmlSchemas schemas;
814 XmlSchemaElement anyElement;
816 SoapBindingUse currentUse;
817 XmlDocument document = new XmlDocument ();
819 static readonly XmlQualifiedName anyType = new XmlQualifiedName ("anyType",XmlSchema.Namespace);
820 static readonly XmlQualifiedName arrayType = new XmlQualifiedName ("Array","http://schemas.xmlsoap.org/soap/encoding/");
821 static readonly XmlQualifiedName arrayTypeRefName = new XmlQualifiedName ("arrayType","http://schemas.xmlsoap.org/soap/encoding/");
822 const string SoapEnvelopeNamespace = "http://schemas.xmlsoap.org/soap/envelope/";
823 const string WsdlNamespace = "http://schemas.xmlsoap.org/wsdl/";
824 const string SoapEncodingNamespace = "http://schemas.xmlsoap.org/soap/encoding/";
828 public EncodedType (string ns, XmlSchemaElement elem) { Namespace = ns; Element = elem; }
829 public string Namespace;
830 public XmlSchemaElement Element;
833 public SampleGenerator (ServiceDescriptionCollection services, XmlSchemas schemas)
835 descriptions = services;
836 this.schemas = schemas;
837 queue = new ArrayList ();
840 public string GenerateMessage (Port port, OperationBinding obin, Operation oper, string protocol, bool generateInput)
842 OperationMessage msg = null;
843 foreach (OperationMessage opm in oper.Messages)
845 if (opm is OperationInput && generateInput) msg = opm;
846 else if (opm is OperationOutput && !generateInput) msg = opm;
848 if (msg == null) return null;
851 case "Soap": return GenerateHttpSoapMessage (port, obin, oper, msg);
852 case "HttpGet": return GenerateHttpGetMessage (port, obin, oper, msg);
853 case "HttpPost": return GenerateHttpPostMessage (port, obin, oper, msg);
855 return "Unknown protocol";
858 public string GenerateHttpSoapMessage (Port port, OperationBinding obin, Operation oper, OperationMessage msg)
862 if (msg is OperationInput)
864 SoapAddressBinding sab = port.Extensions.Find (typeof(SoapAddressBinding)) as SoapAddressBinding;
865 SoapOperationBinding sob = obin.Extensions.Find (typeof(SoapOperationBinding)) as SoapOperationBinding;
866 req += "POST " + new Uri (sab.Location).AbsolutePath + "\n";
867 req += "SOAPAction: " + sob.SoapAction + "\n";
868 req += "Content-Type: text/xml; charset=utf-8\n";
869 req += "Content-Length: " + GetLiteral ("string") + "\n";
870 req += "Host: " + GetLiteral ("string") + "\n\n";
874 req += "HTTP/1.0 200 OK\n";
875 req += "Content-Type: text/xml; charset=utf-8\n";
876 req += "Content-Length: " + GetLiteral ("string") + "\n\n";
879 req += GenerateSoapMessage (obin, oper, msg);
883 public string GenerateHttpGetMessage (Port port, OperationBinding obin, Operation oper, OperationMessage msg)
887 if (msg is OperationInput)
889 HttpAddressBinding sab = port.Extensions.Find (typeof(HttpAddressBinding)) as HttpAddressBinding;
890 HttpOperationBinding sob = obin.Extensions.Find (typeof(HttpOperationBinding)) as HttpOperationBinding;
891 string location = new Uri (sab.Location).AbsolutePath + sob.Location + "?" + BuildQueryString (msg);
892 req += "GET " + location + "\n";
893 req += "Host: " + GetLiteral ("string");
897 req += "HTTP/1.0 200 OK\n";
898 req += "Content-Type: text/xml; charset=utf-8\n";
899 req += "Content-Length: " + GetLiteral ("string") + "\n\n";
901 MimeXmlBinding mxb = (MimeXmlBinding) obin.Output.Extensions.Find (typeof(MimeXmlBinding)) as MimeXmlBinding;
902 if (mxb == null) return req;
904 Message message = descriptions.GetMessage (msg.Message);
905 XmlQualifiedName ename = null;
906 foreach (MessagePart part in message.Parts)
907 if (part.Name == mxb.Part) ename = part.Element;
909 if (ename == null) return req + GetLiteral("string");
911 StringWriter sw = new StringWriter ();
912 XmlTextWriter xtw = new XmlTextWriter (sw);
913 xtw.Formatting = Formatting.Indented;
914 currentUse = SoapBindingUse.Literal;
915 WriteRootElementSample (xtw, ename);
917 req += sw.ToString ();
923 public string GenerateHttpPostMessage (Port port, OperationBinding obin, Operation oper, OperationMessage msg)
927 if (msg is OperationInput)
929 HttpAddressBinding sab = port.Extensions.Find (typeof(HttpAddressBinding)) as HttpAddressBinding;
930 HttpOperationBinding sob = obin.Extensions.Find (typeof(HttpOperationBinding)) as HttpOperationBinding;
931 string location = new Uri (sab.Location).AbsolutePath + sob.Location;
932 req += "POST " + location + "\n";
933 req += "Content-Type: application/x-www-form-urlencoded\n";
934 req += "Content-Length: " + GetLiteral ("string") + "\n";
935 req += "Host: " + GetLiteral ("string") + "\n\n";
936 req += BuildQueryString (msg);
938 else return GenerateHttpGetMessage (port, obin, oper, msg);
943 string BuildQueryString (OperationMessage opm)
946 Message msg = descriptions.GetMessage (opm.Message);
947 foreach (MessagePart part in msg.Parts)
949 if (s.Length != 0) s += "&";
950 s += part.Name + "=" + GetLiteral (part.Type.Name);
955 public string GenerateSoapMessage (OperationBinding obin, Operation oper, OperationMessage msg)
957 SoapOperationBinding sob = obin.Extensions.Find (typeof(SoapOperationBinding)) as SoapOperationBinding;
958 SoapBindingStyle style = (sob != null) ? sob.Style : SoapBindingStyle.Document;
960 MessageBinding msgbin = (msg is OperationInput) ? (MessageBinding) obin.Input : (MessageBinding)obin.Output;
961 SoapBodyBinding sbb = msgbin.Extensions.Find (typeof(SoapBodyBinding)) as SoapBodyBinding;
962 SoapBindingUse bodyUse = (sbb != null) ? sbb.Use : SoapBindingUse.Literal;
964 StringWriter sw = new StringWriter ();
965 XmlTextWriter xtw = new XmlTextWriter (sw);
966 xtw.Formatting = Formatting.Indented;
968 xtw.WriteStartDocument ();
969 xtw.WriteStartElement ("soap", "Envelope", SoapEnvelopeNamespace);
970 xtw.WriteAttributeString ("xmlns", "xsi", null, XmlSchema.InstanceNamespace);
971 xtw.WriteAttributeString ("xmlns", "xsd", null, XmlSchema.Namespace);
973 if (bodyUse == SoapBindingUse.Encoded)
975 xtw.WriteAttributeString ("xmlns", "soapenc", null, SoapEncodingNamespace);
976 xtw.WriteAttributeString ("xmlns", "tns", null, msg.Message.Namespace);
981 bool writtenHeader = false;
982 foreach (object ob in msgbin.Extensions)
984 SoapHeaderBinding hb = ob as SoapHeaderBinding;
985 if (hb == null) continue;
987 if (!writtenHeader) {
988 xtw.WriteStartElement ("soap", "Header", SoapEnvelopeNamespace);
989 writtenHeader = true;
992 WriteHeader (xtw, hb);
996 xtw.WriteEndElement ();
999 xtw.WriteStartElement ("soap", "Body", SoapEnvelopeNamespace);
1001 currentUse = bodyUse;
1002 WriteBody (xtw, oper, msg, sbb, style);
1004 xtw.WriteEndElement ();
1005 xtw.WriteEndElement ();
1007 return sw.ToString ();
1010 void WriteHeader (XmlTextWriter xtw, SoapHeaderBinding header)
1012 Message msg = descriptions.GetMessage (header.Message);
1013 if (msg == null) throw new InvalidOperationException ("Message " + header.Message + " not found");
1014 MessagePart part = msg.Parts [header.Part];
1015 if (part == null) throw new InvalidOperationException ("Message part " + header.Part + " not found in message " + header.Message);
1017 currentUse = header.Use;
1019 if (currentUse == SoapBindingUse.Literal)
1020 WriteRootElementSample (xtw, part.Element);
1022 WriteTypeSample (xtw, part.Type);
1025 void WriteBody (XmlTextWriter xtw, Operation oper, OperationMessage opm, SoapBodyBinding sbb, SoapBindingStyle style)
1027 Message msg = descriptions.GetMessage (opm.Message);
1028 if (msg.Parts.Count > 0 && msg.Parts[0].Name == "parameters")
1030 MessagePart part = msg.Parts[0];
1031 if (part.Element == XmlQualifiedName.Empty)
1032 WriteTypeSample (xtw, part.Type);
1034 WriteRootElementSample (xtw, part.Element);
1038 string elemName = oper.Name;
1040 if (opm is OperationOutput) elemName += "Response";
1042 if (style == SoapBindingStyle.Rpc) {
1043 xtw.WriteStartElement (elemName, sbb.Namespace);
1047 foreach (MessagePart part in msg.Parts)
1049 if (part.Element == XmlQualifiedName.Empty)
1051 XmlSchemaElement elem = new XmlSchemaElement ();
1052 elem.SchemaTypeName = part.Type;
1053 elem.Name = part.Name;
1054 WriteElementSample (xtw, ns, elem);
1057 WriteRootElementSample (xtw, part.Element);
1060 if (style == SoapBindingStyle.Rpc)
1061 xtw.WriteEndElement ();
1063 WriteQueuedTypeSamples (xtw);
1066 void WriteRootElementSample (XmlTextWriter xtw, XmlQualifiedName qname)
1068 XmlSchemaElement elem = (XmlSchemaElement) schemas.Find (qname, typeof(XmlSchemaElement));
1069 if (elem == null) throw new InvalidOperationException ("Element not found: " + qname);
1070 WriteElementSample (xtw, qname.Namespace, elem);
1073 void WriteElementSample (XmlTextWriter xtw, string ns, XmlSchemaElement elem)
1075 bool sharedAnnType = false;
1076 XmlQualifiedName root;
1078 if (!elem.RefName.IsEmpty) {
1079 XmlSchemaElement refElem = FindRefElement (elem);
1080 if (refElem == null) throw new InvalidOperationException ("Global element not found: " + elem.RefName);
1081 root = elem.RefName;
1083 sharedAnnType = true;
1086 root = new XmlQualifiedName (elem.Name, ns);
1088 if (!elem.SchemaTypeName.IsEmpty)
1090 XmlSchemaComplexType st = FindComplexTyype (elem.SchemaTypeName);
1092 WriteComplexTypeSample (xtw, st, root);
1095 xtw.WriteStartElement (root.Name, root.Namespace);
1096 if (currentUse == SoapBindingUse.Encoded)
1097 xtw.WriteAttributeString ("type", XmlSchema.InstanceNamespace, GetQualifiedNameString (xtw, elem.SchemaTypeName));
1098 xtw.WriteString (GetLiteral (FindBuiltInType (elem.SchemaTypeName)));
1099 xtw.WriteEndElement ();
1102 else if (elem.SchemaType == null)
1104 xtw.WriteStartElement ("any");
1105 xtw.WriteEndElement ();
1108 WriteComplexTypeSample (xtw, (XmlSchemaComplexType) elem.SchemaType, root);
1111 void WriteTypeSample (XmlTextWriter xtw, XmlQualifiedName qname)
1113 XmlSchemaComplexType ctype = FindComplexTyype (qname);
1114 if (ctype != null) {
1115 WriteComplexTypeSample (xtw, ctype, qname);
1119 XmlSchemaSimpleType stype = (XmlSchemaSimpleType) schemas.Find (qname, typeof(XmlSchemaSimpleType));
1120 if (stype != null) {
1121 WriteSimpleTypeSample (xtw, stype);
1125 xtw.WriteString (GetLiteral (FindBuiltInType (qname)));
1126 throw new InvalidOperationException ("Type not found: " + qname);
1129 void WriteComplexTypeSample (XmlTextWriter xtw, XmlSchemaComplexType stype, XmlQualifiedName rootName)
1131 WriteComplexTypeSample (xtw, stype, rootName, -1);
1134 void WriteComplexTypeSample (XmlTextWriter xtw, XmlSchemaComplexType stype, XmlQualifiedName rootName, int id)
1136 string ns = rootName.Namespace;
1138 if (rootName.Name.IndexOf ("[]") != -1) rootName = arrayType;
1140 if (currentUse == SoapBindingUse.Encoded) {
1141 string pref = xtw.LookupPrefix (rootName.Namespace);
1142 if (pref == null) pref = "q1";
1143 xtw.WriteStartElement (pref, rootName.Name, rootName.Namespace);
1147 xtw.WriteStartElement (rootName.Name, rootName.Namespace);
1151 xtw.WriteAttributeString ("id", "id" + id);
1152 if (rootName != arrayType)
1153 xtw.WriteAttributeString ("type", XmlSchema.InstanceNamespace, GetQualifiedNameString (xtw, rootName));
1156 WriteComplexTypeAttributes (xtw, stype);
1157 WriteComplexTypeElements (xtw, ns, stype);
1159 xtw.WriteEndElement ();
1162 void WriteComplexTypeAttributes (XmlTextWriter xtw, XmlSchemaComplexType stype)
1164 WriteAttributes (xtw, stype.Attributes, stype.AnyAttribute);
1167 void WriteComplexTypeElements (XmlTextWriter xtw, string ns, XmlSchemaComplexType stype)
1169 if (stype.Particle != null)
1170 WriteParticleComplexContent (xtw, ns, stype.Particle);
1173 if (stype.ContentModel is XmlSchemaSimpleContent)
1174 WriteSimpleContent (xtw, (XmlSchemaSimpleContent)stype.ContentModel);
1175 else if (stype.ContentModel is XmlSchemaComplexContent)
1176 WriteComplexContent (xtw, ns, (XmlSchemaComplexContent)stype.ContentModel);
1180 void WriteAttributes (XmlTextWriter xtw, XmlSchemaObjectCollection atts, XmlSchemaAnyAttribute anyat)
1182 foreach (XmlSchemaObject at in atts)
1184 if (at is XmlSchemaAttribute)
1187 XmlSchemaAttribute attr = (XmlSchemaAttribute)at;
1188 XmlSchemaAttribute refAttr = attr;
1190 // refAttr.Form; TODO
1192 if (!attr.RefName.IsEmpty) {
1193 refAttr = FindRefAttribute (attr.RefName);
1194 if (refAttr == null) throw new InvalidOperationException ("Global attribute not found: " + attr.RefName);
1198 if (!refAttr.SchemaTypeName.IsEmpty) val = FindBuiltInType (refAttr.SchemaTypeName);
1199 else val = FindBuiltInType ((XmlSchemaSimpleType) refAttr.SchemaType);
1201 xtw.WriteAttributeString (refAttr.Name, val);
1203 else if (at is XmlSchemaAttributeGroupRef)
1205 XmlSchemaAttributeGroupRef gref = (XmlSchemaAttributeGroupRef)at;
1206 XmlSchemaAttributeGroup grp = (XmlSchemaAttributeGroup) schemas.Find (gref.RefName, typeof(XmlSchemaAttributeGroup));
1207 WriteAttributes (xtw, grp.Attributes, grp.AnyAttribute);
1212 xtw.WriteAttributeString ("custom-attribute","value");
1215 void WriteParticleComplexContent (XmlTextWriter xtw, string ns, XmlSchemaParticle particle)
1217 WriteParticleContent (xtw, ns, particle, false);
1220 void WriteParticleContent (XmlTextWriter xtw, string ns, XmlSchemaParticle particle, bool multiValue)
1222 if (particle is XmlSchemaGroupRef)
1223 particle = GetRefGroupParticle ((XmlSchemaGroupRef)particle);
1225 if (particle.MaxOccurs > 1) multiValue = true;
1227 if (particle is XmlSchemaSequence) {
1228 WriteSequenceContent (xtw, ns, ((XmlSchemaSequence)particle).Items, multiValue);
1230 else if (particle is XmlSchemaChoice) {
1231 if (((XmlSchemaChoice)particle).Items.Count == 1)
1232 WriteSequenceContent (xtw, ns, ((XmlSchemaChoice)particle).Items, multiValue);
1234 WriteChoiceContent (xtw, ns, (XmlSchemaChoice)particle, multiValue);
1236 else if (particle is XmlSchemaAll) {
1237 WriteSequenceContent (xtw, ns, ((XmlSchemaAll)particle).Items, multiValue);
1241 void WriteSequenceContent (XmlTextWriter xtw, string ns, XmlSchemaObjectCollection items, bool multiValue)
1243 foreach (XmlSchemaObject item in items)
1244 WriteContentItem (xtw, ns, item, multiValue);
1247 void WriteContentItem (XmlTextWriter xtw, string ns, XmlSchemaObject item, bool multiValue)
1249 if (item is XmlSchemaGroupRef)
1250 item = GetRefGroupParticle ((XmlSchemaGroupRef)item);
1252 if (item is XmlSchemaElement)
1254 XmlSchemaElement elem = (XmlSchemaElement) item;
1255 XmlSchemaElement refElem;
1256 if (!elem.RefName.IsEmpty) refElem = FindRefElement (elem);
1257 else refElem = elem;
1259 int num = (elem.MaxOccurs == 1 && !multiValue) ? 1 : 2;
1260 for (int n=0; n<num; n++)
1262 if (currentUse == SoapBindingUse.Literal)
1263 WriteElementSample (xtw, ns, refElem);
1265 WriteRefTypeSample (xtw, ns, refElem);
1268 else if (item is XmlSchemaAny)
1270 xtw.WriteString (GetLiteral ("xml"));
1272 else if (item is XmlSchemaParticle) {
1273 WriteParticleContent (xtw, ns, (XmlSchemaParticle)item, multiValue);
1277 void WriteChoiceContent (XmlTextWriter xtw, string ns, XmlSchemaChoice choice, bool multiValue)
1279 foreach (XmlSchemaObject item in choice.Items)
1280 WriteContentItem (xtw, ns, item, multiValue);
1283 void WriteSimpleContent (XmlTextWriter xtw, XmlSchemaSimpleContent content)
1285 XmlSchemaSimpleContentExtension ext = content.Content as XmlSchemaSimpleContentExtension;
1287 WriteAttributes (xtw, ext.Attributes, ext.AnyAttribute);
1289 XmlQualifiedName qname = GetContentBaseType (content.Content);
1290 xtw.WriteString (GetLiteral (FindBuiltInType (qname)));
1293 string FindBuiltInType (XmlQualifiedName qname)
1295 if (qname.Namespace == XmlSchema.Namespace)
1298 XmlSchemaComplexType ct = FindComplexTyype (qname);
1301 XmlSchemaSimpleContent sc = ct.ContentModel as XmlSchemaSimpleContent;
1302 if (sc == null) throw new InvalidOperationException ("Invalid schema");
1303 return FindBuiltInType (GetContentBaseType (sc.Content));
1306 XmlSchemaSimpleType st = (XmlSchemaSimpleType) schemas.Find (qname, typeof(XmlSchemaSimpleType));
1308 return FindBuiltInType (st);
1310 throw new InvalidOperationException ("Definition of type " + qname + " not found");
1313 string FindBuiltInType (XmlSchemaSimpleType st)
1315 if (st.Content is XmlSchemaSimpleTypeRestriction) {
1316 return FindBuiltInType (GetContentBaseType (st.Content));
1318 else if (st.Content is XmlSchemaSimpleTypeList) {
1319 string s = FindBuiltInType (GetContentBaseType (st.Content));
1320 return s + " " + s + " ...";
1322 else if (st.Content is XmlSchemaSimpleTypeUnion)
1324 //Check if all types of the union are equal. If not, then will use anyType.
1325 XmlSchemaSimpleTypeUnion uni = (XmlSchemaSimpleTypeUnion) st.Content;
1326 string utype = null;
1328 // Anonymous types are unique
1329 if (uni.BaseTypes.Count != 0 && uni.MemberTypes.Length != 0)
1332 foreach (XmlQualifiedName mt in uni.MemberTypes)
1334 string qn = FindBuiltInType (mt);
1335 if (utype != null && qn != utype) return "string";
1345 XmlQualifiedName GetContentBaseType (XmlSchemaObject ob)
1347 if (ob is XmlSchemaSimpleContentExtension)
1348 return ((XmlSchemaSimpleContentExtension)ob).BaseTypeName;
1349 else if (ob is XmlSchemaSimpleContentRestriction)
1350 return ((XmlSchemaSimpleContentRestriction)ob).BaseTypeName;
1351 else if (ob is XmlSchemaSimpleTypeRestriction)
1352 return ((XmlSchemaSimpleTypeRestriction)ob).BaseTypeName;
1353 else if (ob is XmlSchemaSimpleTypeList)
1354 return ((XmlSchemaSimpleTypeList)ob).ItemTypeName;
1359 void WriteComplexContent (XmlTextWriter xtw, string ns, XmlSchemaComplexContent content)
1361 XmlQualifiedName qname;
1363 XmlSchemaComplexContentExtension ext = content.Content as XmlSchemaComplexContentExtension;
1364 if (ext != null) qname = ext.BaseTypeName;
1366 XmlSchemaComplexContentRestriction rest = (XmlSchemaComplexContentRestriction)content.Content;
1367 qname = rest.BaseTypeName;
1368 if (qname == arrayType) {
1369 ParseArrayType (rest, out qname);
1370 XmlSchemaElement elem = new XmlSchemaElement ();
1372 elem.SchemaTypeName = qname;
1374 xtw.WriteAttributeString ("arrayType", SoapEncodingNamespace, qname.Name + "[2]");
1375 WriteContentItem (xtw, ns, elem, true);
1380 // Add base map members to this map
1381 XmlSchemaComplexType ctype = FindComplexTyype (qname);
1382 WriteComplexTypeAttributes (xtw, ctype);
1385 // Add the members of this map
1386 WriteAttributes (xtw, ext.Attributes, ext.AnyAttribute);
1387 if (ext.Particle != null)
1388 WriteParticleComplexContent (xtw, ns, ext.Particle);
1391 WriteComplexTypeElements (xtw, ns, ctype);
1394 void ParseArrayType (XmlSchemaComplexContentRestriction rest, out XmlQualifiedName qtype)
1396 XmlSchemaAttribute arrayTypeAt = FindArrayAttribute (rest.Attributes);
1397 XmlAttribute[] uatts = arrayTypeAt.UnhandledAttributes;
1398 if (uatts == null || uatts.Length == 0) throw new InvalidOperationException ("arrayType attribute not specified in array declaration");
1400 XmlAttribute xat = null;
1401 foreach (XmlAttribute at in uatts)
1402 if (at.LocalName == "arrayType" && at.NamespaceURI == WsdlNamespace)
1403 { xat = at; break; }
1406 throw new InvalidOperationException ("arrayType attribute not specified in array declaration");
1408 string arrayType = xat.Value;
1410 int i = arrayType.LastIndexOf (":");
1411 if (i == -1) ns = "";
1412 else ns = arrayType.Substring (0,i);
1414 int j = arrayType.IndexOf ("[", i+1);
1415 if (j == -1) throw new InvalidOperationException ("Cannot parse WSDL array type: " + arrayType);
1416 type = arrayType.Substring (i+1);
1417 type = type.Substring (0, type.Length-2);
1419 qtype = new XmlQualifiedName (type, ns);
1422 XmlSchemaAttribute FindArrayAttribute (XmlSchemaObjectCollection atts)
1424 foreach (object ob in atts)
1426 XmlSchemaAttribute att = ob as XmlSchemaAttribute;
1427 if (att != null && att.RefName == arrayTypeRefName) return att;
1429 XmlSchemaAttributeGroupRef gref = ob as XmlSchemaAttributeGroupRef;
1432 XmlSchemaAttributeGroup grp = (XmlSchemaAttributeGroup) schemas.Find (gref.RefName, typeof(XmlSchemaAttributeGroup));
1433 att = FindArrayAttribute (grp.Attributes);
1434 if (att != null) return att;
1440 void WriteSimpleTypeSample (XmlTextWriter xtw, XmlSchemaSimpleType stype)
1442 xtw.WriteString (GetLiteral (FindBuiltInType (stype)));
1445 XmlSchemaParticle GetRefGroupParticle (XmlSchemaGroupRef refGroup)
1447 XmlSchemaGroup grp = (XmlSchemaGroup) schemas.Find (refGroup.RefName, typeof (XmlSchemaGroup));
1448 return grp.Particle;
1451 XmlSchemaElement FindRefElement (XmlSchemaElement elem)
1453 if (elem.RefName.Namespace == XmlSchema.Namespace)
1455 if (anyElement != null) return anyElement;
1456 anyElement = new XmlSchemaElement ();
1457 anyElement.Name = "any";
1458 anyElement.SchemaTypeName = anyType;
1461 return (XmlSchemaElement) schemas.Find (elem.RefName, typeof(XmlSchemaElement));
1464 XmlSchemaAttribute FindRefAttribute (XmlQualifiedName refName)
1466 if (refName.Namespace == XmlSchema.Namespace)
1468 XmlSchemaAttribute at = new XmlSchemaAttribute ();
1469 at.Name = refName.Name;
1470 at.SchemaTypeName = new XmlQualifiedName ("string",XmlSchema.Namespace);
1473 return (XmlSchemaAttribute) schemas.Find (refName, typeof(XmlSchemaAttribute));
1476 void WriteRefTypeSample (XmlTextWriter xtw, string ns, XmlSchemaElement elem)
1478 if (elem.SchemaTypeName.Namespace == XmlSchema.Namespace || schemas.Find (elem.SchemaTypeName, typeof(XmlSchemaSimpleType)) != null)
1479 WriteElementSample (xtw, ns, elem);
1482 xtw.WriteStartElement (elem.Name, ns);
1483 xtw.WriteAttributeString ("href", "#id" + (queue.Count+1));
1484 xtw.WriteEndElement ();
1485 queue.Add (new EncodedType (ns, elem));
1489 void WriteQueuedTypeSamples (XmlTextWriter xtw)
1491 for (int n=0; n<queue.Count; n++)
1493 EncodedType ec = (EncodedType) queue[n];
1494 XmlSchemaComplexType st = FindComplexTyype (ec.Element.SchemaTypeName);
1495 WriteComplexTypeSample (xtw, st, ec.Element.SchemaTypeName, n+1);
1499 XmlSchemaComplexType FindComplexTyype (XmlQualifiedName qname)
1501 if (qname.Name.IndexOf ("[]") != -1)
1503 XmlSchemaComplexType stype = new XmlSchemaComplexType ();
1504 stype.ContentModel = new XmlSchemaComplexContent ();
1506 XmlSchemaComplexContentRestriction res = new XmlSchemaComplexContentRestriction ();
1507 stype.ContentModel.Content = res;
1508 res.BaseTypeName = arrayType;
1510 XmlSchemaAttribute att = new XmlSchemaAttribute ();
1511 att.RefName = arrayTypeRefName;
1512 res.Attributes.Add (att);
1514 XmlAttribute xat = document.CreateAttribute ("arrayType", WsdlNamespace);
1515 xat.Value = qname.Namespace + ":" + qname.Name;
1516 att.UnhandledAttributes = new XmlAttribute[] {xat};
1520 return (XmlSchemaComplexType) schemas.Find (qname, typeof(XmlSchemaComplexType));
1523 string GetQualifiedNameString (XmlTextWriter xtw, XmlQualifiedName qname)
1525 string pref = xtw.LookupPrefix (qname.Namespace);
1526 if (pref != null) return pref + ":" + qname.Name;
1528 xtw.WriteAttributeString ("xmlns", "q1", null, qname.Namespace);
1529 return "q1:" + qname.Name;
1532 protected virtual string GetLiteral (string s)
1537 void GetOperationFormat (OperationBinding obin, out SoapBindingStyle style, out SoapBindingUse use)
1539 style = SoapBindingStyle.Document;
1540 use = SoapBindingUse.Literal;
1541 SoapOperationBinding sob = obin.Extensions.Find (typeof(SoapOperationBinding)) as SoapOperationBinding;
1544 SoapBodyBinding sbb = obin.Input.Extensions.Find (typeof(SoapBodyBinding)) as SoapBodyBinding;
1558 <link rel="alternate" type="text/xml" href="<%=Request.FilePath%>?disco"/>
1560 <title><%=WebServiceName%> Web Service</title>
1561 <style type="text/css">
1562 BODY { font-family: Arial; margin-left: 20px; margin-top: 20px; font-size: x-small}
1563 TABLE { font-size: x-small }
1564 .title { color:dimgray; font-family: Arial; font-size:20pt; font-weight:900}
1565 .operationTitle { color:dimgray; font-family: Arial; font-size:15pt; font-weight:900}
1566 .method { font-size: x-small }
1567 .bindingLabel { font-size: x-small; font-weight:bold; color:darkgray; line-height:8pt; display:block; margin-bottom:3px }
1568 .label { font-size: small; font-weight:bold; color:darkgray }
1569 .paramTable { font-size: x-small }
1570 .paramTable TR { background-color: gainsboro }
1571 .paramFormTable { font-size: x-small; padding: 10px; background-color: gainsboro }
1572 .paramFormTable TR { background-color: gainsboro }
1573 .paramInput { border: solid 1px gray }
1574 .button {border: solid 1px gray }
1575 .smallSeparator { height:3px; overflow:hidden }
1576 .panel { background-color:whitesmoke; border: solid 1px silver; border-top: solid 1px silver }
1577 .codePanel { background-color: white; font-size:x-small; padding:7px; border:solid 1px silver}
1578 .code-xml { font-size:10pt; font-family:courier }
1579 .code-cs { font-size:10pt; font-family:courier }
1580 .code-vb { font-size:10pt; font-family:courier }
1581 .tabLabelOn { font-weight:bold }
1582 .tabLabelOff {color: darkgray }
1583 .literal-placeholder {color: darkblue; font-weight:bold}
1584 A:link { color: black; }
1585 A:visited { color: black; }
1586 A:active { color: black; }
1587 A:hover { color: blue }
1591 function clearForm ()
1593 document.getElementById("testFormResult").style.display="none";
1600 <div class="title" style="margin-left:20px">
1601 <span class="label">Web Service</span><br>
1606 **********************************************************
1610 <table border="0" width="100%" cellpadding="15px" cellspacing="15px">
1611 <tr valign="top"><td width="150px" class="panel">
1612 <div style="width:150px"></div>
1613 <a class="method" href='<%=PageName%>'>Overview</a><br>
1614 <div class="smallSeparator"></div>
1615 <a class="method" href='<%=PageName + "?" + GetPageContext("wsdl")%>'>Service Description</a>
1616 <div class="smallSeparator"></div>
1617 <a class="method" href='<%=PageName + "?" + GetPageContext("proxy")%>'>Client proxy</a>
1619 <asp:repeater id="BindingsRepeater" runat=server>
1620 <itemtemplate name="itemtemplate">
1621 <span class="bindingLabel"><%#FormatBindingName(DataBinder.Eval(Container.DataItem, "Name").ToString())%></span>
1622 <asp:repeater id="OperationsRepeater" runat=server datasource='<%# ((Binding)Container.DataItem).Operations %>'>
1624 <a class="method" href="<%=PageName%>?<%=GetTabContext("op",null)%>op=<%#GetOpName(Container.DataItem)%>&bnd=<%#DataBinder.Eval(Container.DataItem, "Binding.Name")%>"><%#GetOpName(Container.DataItem)%></a>
1625 <div class="smallSeparator"></div>
1632 </td><td class="panel">
1634 <% if (CurrentPage == "main") {%>
1637 **********************************************************
1638 Web service overview
1641 <p class="label">Web Service Overview</p>
1642 <%=WebServiceDescription%>
1644 <% if (ProfileViolations.Count > 0) { %>
1645 <p class="label">Basic Profile Conformance</p>
1646 This web service does not conform to WS-I Basic Profile v1.1
1648 Response.Write ("<ul>");
1649 foreach (BasicProfileViolation vio in ProfileViolations) {
1650 Response.Write ("<li><b>" + vio.NormativeStatement + "</b>: " + vio.Details);
1651 Response.Write ("<ul>");
1652 foreach (string ele in vio.Elements)
1653 Response.Write ("<li>" + ele + "</li>");
1654 Response.Write ("</ul>");
1655 Response.Write ("</li>");
1657 Response.Write ("</ul>");
1660 <%} if (DefaultBinding == null) {%>
1661 This service does not contain any public web method.
1662 <%} else if (CurrentPage == "op") {%>
1665 **********************************************************
1666 Operation description
1669 <span class="operationTitle"><%=CurrentOperationName%></span>
1674 <% if (CurrentTab == "main") { %>
1675 <span class="label">Input Parameters</span>
1676 <div class="smallSeparator"></div>
1677 <% if (InParams.Count == 0) { %>
1678 No input parameters<br>
1680 <table class="paramTable" cellspacing="1" cellpadding="5">
1681 <asp:repeater id="InputParamsRepeater" runat=server>
1684 <td width="150"><%#DataBinder.Eval(Container.DataItem, "Name")%></td>
1685 <td width="150"><%#DataBinder.Eval(Container.DataItem, "Type")%></td>
1693 <% if (OutParams.Count > 0) { %>
1694 <span class="label">Output Parameters</span>
1695 <div class="smallSeparator"></div>
1696 <table class="paramTable" cellspacing="1" cellpadding="5">
1697 <asp:repeater id="OutputParamsRepeater" runat=server>
1700 <td width="150"><%#DataBinder.Eval(Container.DataItem, "Name")%></td>
1701 <td width="150"><%#DataBinder.Eval(Container.DataItem, "Type")%></td>
1709 <span class="label">Remarks</span>
1710 <div class="smallSeparator"></div>
1711 <%=OperationDocumentation%>
1713 <span class="label">Technical information</span>
1714 <div class="smallSeparator"></div>
1715 Format: <%=CurrentOperationFormat%>
1716 <br>Supported protocols: <%=CurrentOperationProtocols%>
1720 **********************************************************
1721 Operation description - Test form
1724 <% if (CurrentTab == "test") {
1725 if (CurrentOperationSupportsTest) {%>
1726 Enter values for the parameters and click the 'Invoke' button to test this method:<br><br>
1727 <form action="<%=PageName%>" method="GET">
1728 <input type="hidden" name="page" value="<%=CurrentPage%>">
1729 <input type="hidden" name="tab" value="<%=CurrentTab%>">
1730 <input type="hidden" name="op" value="<%=CurrentOperationName%>">
1731 <input type="hidden" name="bnd" value="<%=CurrentOperationBinding%>">
1732 <input type="hidden" name="ext" value="testform">
1733 <table class="paramFormTable" cellspacing="0" cellpadding="3">
1734 <asp:repeater id="InputFormParamsRepeater" runat=server>
1737 <td><%#DataBinder.Eval(Container.DataItem, "Name")%>: </td>
1738 <td width="150"><input class="paramInput" type="text" size="20" name="<%#DataBinder.Eval(Container.DataItem, "Name")%>"></td>
1742 <tr><td></td><td><input class="button" type="submit" value="Invoke"> <input class="button" type="button" onclick="clearForm()" value="Clear"></td></tr>
1745 <div id="testFormResult" style="display:<%= (HasFormResult?"block":"none") %>">
1746 The web service returned the following result:<br/><br/>
1747 <div class="codePanel"><%=GetTestResult()%></div>
1750 The test form is not available for this operation because it has parameters with a complex structure.
1755 **********************************************************
1756 Operation description - Message Layout
1759 <% if (CurrentTab == "msg") { %>
1761 The following are sample SOAP requests and responses for each protocol supported by this method:
1764 <% if (IsOperationSupported ("Soap")) { %>
1765 <span class="label">Soap</span>
1767 <div class="codePanel"><div class="code-xml"><%=GenerateOperationMessages ("Soap", true)%></div></div>
1769 <div class="codePanel"><div class="code-xml"><%=GenerateOperationMessages ("Soap", false)%></div></div>
1772 <% if (IsOperationSupported ("HttpGet")) { %>
1773 <span class="label">HTTP Get</span>
1775 <div class="codePanel"><div class="code-xml"><%=GenerateOperationMessages ("HttpGet", true)%></div></div>
1777 <div class="codePanel"><div class="code-xml"><%=GenerateOperationMessages ("HttpGet", false)%></div></div>
1780 <% if (IsOperationSupported ("HttpPost")) { %>
1781 <span class="label">HTTP Post</span>
1783 <div class="codePanel"><div class="code-xml"><%=GenerateOperationMessages ("HttpPost", true)%></div></div>
1785 <div class="codePanel"><div class="code-xml"><%=GenerateOperationMessages ("HttpPost", false)%></div></div>
1790 <%} else if (CurrentPage == "proxy") {%>
1792 **********************************************************
1795 <form action="<%=PageName%>" name="langForm" method="GET">
1796 Select the language for which you want to generate a proxy
1797 <input type="hidden" name="page" value="<%=CurrentPage%>">
1798 <SELECT name="lang" onchange="langForm.submit()">
1799 <%=GetOptionSel("cs",CurrentLanguage)%>C#</option>
1800 <%=GetOptionSel("vb",CurrentLanguage)%>Visual Basic</option>
1805 <span class="label"><%=CurrentProxytName%></span>
1806 <a href="<%=PageName + "?code=" + CurrentLanguage%>">Download</a>
1808 <div class="codePanel">
1809 <div class="code-<%=CurrentLanguage%>"><%=GetProxyCode ()%></div>
1811 <%} else if (CurrentPage == "wsdl") {%>
1813 **********************************************************
1816 <% if (descriptions.Count > 1 || schemas.Count > 1) {%>
1817 The description of this web service is composed by several documents. Click on the document you want to see:
1821 for (int n=0; n<descriptions.Count; n++)
1822 Response.Write ("<li><a href='" + PageName + "?" + GetPageContext(null) + "doctype=wsdl&docind=" + n + "'>WSDL document " + descriptions[n].TargetNamespace + "</a></li>");
1823 for (int n=0; n<schemas.Count; n++)
1824 Response.Write ("<li><a href='" + PageName + "?" + GetPageContext(null) + "doctype=schema&docind=" + n + "'>Xml Schema " + schemas[n].TargetNamespace + "</a></li>");
1831 <span class="label"><%=CurrentDocumentName%></span>
1832 <a href="<%=PageName + "?" + CurrentDocType + "=" + CurrentDocInd %>">Download</a>
1834 <div class="codePanel">
1835 <div class="code-xml"><%=GenerateDocument ()%></div>
1840 <br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
1842 <td width="20px"></td>