-
Notifications
You must be signed in to change notification settings - Fork 0
/
UnityInteractionGenerator.aspx
1901 lines (1622 loc) · 59.2 KB
/
UnityInteractionGenerator.aspx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<%--
//
// DefaultWsdlHelpGenerator.aspx:
//
// Author:
// Lluis Sanchez Gual ([email protected])
//
// (C) 2003 Ximian, Inc. http://www.ximian.com
//
--%>
<%@ Import Namespace="System.Collections" %>
<%@ Import Namespace="System.Collections.Generic" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Xml.Serialization" %>
<%@ Import Namespace="System.Xml" %>
<%@ Import Namespace="System.Xml.Schema" %>
<%@ Import Namespace="System.Web.Services" %>
<%@ Import Namespace="System.Web.Services.Description" %>
<%@ Import Namespace="System.Web.Services.Configuration" %>
<%@ Import Namespace="System.Web.Configuration" %>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Net" %>
<%@ Import Namespace="System.Globalization" %>
<%@ Import Namespace="System.Resources" %>
<%@ Import Namespace="System.Diagnostics" %>
<%@ Import Namespace="System.CodeDom" %>
<%@ Import Namespace="System.CodeDom.Compiler" %>
<%@ Import Namespace="Microsoft.CSharp" %>
<%@ Import Namespace="Microsoft.VisualBasic" %>
<%@ Import Namespace="System.Text" %>
<%@ Import Namespace="System.Text.RegularExpressions" %>
<%@ Import Namespace="System.Security.Cryptography.X509Certificates" %>
<%@ Assembly name="System.Web.Services" %>
<%@ Page debug="true" %>
<html>
<script language="C#" runat="server">
ServiceDescriptionCollection descriptions;
XmlSchemas schemas;
string WebServiceName;
string WebServiceDescription;
string PageName;
string DefaultBinding;
ArrayList ServiceProtocols;
string CurrentOperationName;
string CurrentOperationBinding;
string OperationDocumentation;
string CurrentOperationFormat;
bool CurrentOperationSupportsTest;
ArrayList InParams;
ArrayList OutParams;
string CurrentOperationProtocols;
int CodeTextColumns = 95;
BasicProfileViolationCollection ProfileViolations;
void Page_Load(object sender, EventArgs e)
{
descriptions = (ServiceDescriptionCollection) Context.Items["wsdls"];
schemas = (XmlSchemas) Context.Items["schemas"];
ServiceDescription desc = descriptions [0];
if (schemas.Count == 0) schemas = desc.Types.Schemas;
Service service = desc.Services[0];
WebServiceName = service.Name;
if (desc.Bindings.Count == 0)
return;
DefaultBinding = desc.Bindings[0].Name;
WebServiceDescription = service.Documentation;
if (WebServiceDescription == "" || WebServiceDescription == null)
WebServiceDescription = "Description has not been provided";
ServiceProtocols = FindServiceProtocols (null);
CurrentOperationName = Request.QueryString["op"];
CurrentOperationBinding = Request.QueryString["bnd"];
if (CurrentOperationName != null) BuildOperationInfo ();
PageName = HttpUtility.UrlEncode (Path.GetFileName(Request.Path), Encoding.UTF8);
ArrayList list = new ArrayList ();
foreach (ServiceDescription sd in descriptions) {
foreach (Binding bin in sd.Bindings)
if (bin.Extensions.Find (typeof(SoapBinding)) != null) list.Add (bin);
}
BindingsRepeater.DataSource = list;
Page.DataBind();
ProfileViolations = new BasicProfileViolationCollection ();
foreach (WsiProfilesElement claims in ((WebServicesSection) WebConfigurationManager.GetSection("system.web/webServices")).ConformanceWarnings)
if (claims.Name != WsiProfiles.None)
WebServicesInteroperability.CheckConformance (claims.Name, descriptions, ProfileViolations);
}
void BuildOperationInfo ()
{
InParams = new ArrayList ();
OutParams = new ArrayList ();
Port port = FindPort (CurrentOperationBinding, null);
Binding binding = descriptions.GetBinding (port.Binding);
PortType portType = descriptions.GetPortType (binding.Type);
Operation oper = FindOperation (portType, CurrentOperationName);
OperationDocumentation = oper.Documentation;
if (OperationDocumentation == null || OperationDocumentation == "")
OperationDocumentation = "No additional remarks";
foreach (OperationMessage opm in oper.Messages)
{
if (opm is OperationInput)
BuildParameters (InParams, opm);
else if (opm is OperationOutput)
BuildParameters (OutParams, opm);
}
// Protocols supported by the operation
CurrentOperationProtocols = "";
WebServiceProtocols testProtocols = 0;
ArrayList prots = FindServiceProtocols (CurrentOperationName);
for (int n=0; n<prots.Count; n++) {
string prot = (string) prots [n];
if (n != 0) CurrentOperationProtocols += ", ";
CurrentOperationProtocols += prot;
if (prot == "HttpGet")
testProtocols |= WebServiceProtocols.HttpGet;
else if (prot == "HttpPost") {
testProtocols |= WebServiceProtocols.HttpPost;
if (Context.Request.IsLocal)
testProtocols |= WebServiceProtocols.HttpPostLocalhost;
}
}
CurrentOperationSupportsTest = (WebServicesSection.Current.EnabledProtocols & testProtocols) != 0;
// Operation format
OperationBinding obin = FindOperation (binding, CurrentOperationName);
if (obin != null)
CurrentOperationFormat = GetOperationFormat (obin);
InputParamsRepeater.DataSource = InParams;
InputFormParamsRepeater.DataSource = InParams;
OutputParamsRepeater.DataSource = OutParams;
}
void BuildParameters (ArrayList list, OperationMessage opm)
{
Message msg = descriptions.GetMessage (opm.Message);
if (msg.Parts.Count > 0 && msg.Parts[0].Name == "parameters")
{
MessagePart part = msg.Parts[0];
XmlSchemaComplexType ctype;
if (part.Element == XmlQualifiedName.Empty)
{
ctype = (XmlSchemaComplexType) schemas.Find (part.Type, typeof(XmlSchemaComplexType));
}
else
{
XmlSchemaElement elem = (XmlSchemaElement) schemas.Find (part.Element, typeof(XmlSchemaElement));
ctype = (XmlSchemaComplexType) elem.SchemaType;
}
XmlSchemaSequence seq = ctype.Particle as XmlSchemaSequence;
if (seq == null) return;
foreach (XmlSchemaObject ob in seq.Items)
{
Parameter p = new Parameter();
p.Description = "No additional remarks";
if (ob is XmlSchemaElement)
{
XmlSchemaElement selem = GetRefElement ((XmlSchemaElement)ob);
p.Name = selem.Name;
p.Type = selem.SchemaTypeName.Name;
}
else
{
p.Name = "Unknown";
p.Type = "Unknown";
}
list.Add (p);
}
}
else
{
foreach (MessagePart part in msg.Parts)
{
Parameter p = new Parameter ();
p.Description = "No additional remarks";
p.Name = part.Name;
if (part.Element == XmlQualifiedName.Empty)
p.Type = part.Type.Name;
else
{
XmlSchemaElement elem = (XmlSchemaElement) schemas.Find (part.Element, typeof(XmlSchemaElement));
p.Type = elem.SchemaTypeName.Name;
}
list.Add (p);
}
}
}
string GetOperationFormat (OperationBinding obin)
{
string format = "";
SoapOperationBinding sob = obin.Extensions.Find (typeof(SoapOperationBinding)) as SoapOperationBinding;
if (sob != null) {
format = sob.Style.ToString ();
SoapBodyBinding sbb = obin.Input.Extensions.Find (typeof(SoapBodyBinding)) as SoapBodyBinding;
if (sbb != null)
format += " / " + sbb.Use;
}
return format;
}
XmlSchemaElement GetRefElement (XmlSchemaElement elem)
{
if (!elem.RefName.IsEmpty)
return (XmlSchemaElement) schemas.Find (elem.RefName, typeof(XmlSchemaElement));
else
return elem;
}
ArrayList FindServiceProtocols(string operName)
{
ArrayList table = new ArrayList ();
Service service = descriptions[0].Services[0];
foreach (Port port in service.Ports)
{
string prot = null;
Binding bin = descriptions.GetBinding (port.Binding);
if (bin.Extensions.Find (typeof(SoapBinding)) != null)
prot = "Soap";
else
{
HttpBinding hb = (HttpBinding) bin.Extensions.Find (typeof(HttpBinding));
if (hb != null && hb.Verb == "POST") prot = "HttpPost";
else if (hb != null && hb.Verb == "GET") prot = "HttpGet";
}
if (prot != null && operName != null)
{
if (FindOperation (bin, operName) == null)
prot = null;
}
if (prot != null && !table.Contains (prot))
table.Add (prot);
}
return table;
}
Port FindPort (string portName, string protocol)
{
Service service = descriptions[0].Services[0];
foreach (Port port in service.Ports)
{
if (portName == null)
{
Binding binding = descriptions.GetBinding (port.Binding);
if (GetProtocol (binding) == protocol) return port;
}
else if (port.Name == portName)
return port;
}
return null;
}
string GetProtocol (Binding binding)
{
if (binding.Extensions.Find (typeof(SoapBinding)) != null) return "Soap";
HttpBinding hb = (HttpBinding) binding.Extensions.Find (typeof(HttpBinding));
if (hb == null) return "";
if (hb.Verb == "POST") return "HttpPost";
if (hb.Verb == "GET") return "HttpGet";
return "";
}
Operation FindOperation (PortType portType, string name)
{
foreach (Operation oper in portType.Operations) {
if (oper.Messages.Input.Name != null) {
if (oper.Messages.Input.Name == name) return oper;
}
else
if (oper.Name == name) return oper;
}
return null;
}
OperationBinding FindOperation (Binding binding, string name)
{
foreach (OperationBinding oper in binding.Operations) {
if (oper.Input.Name != null) {
if (oper.Input.Name == name) return oper;
}
else
if (oper.Name == name) return oper;
}
return null;
}
string FormatBindingName (string name)
{
if (name == DefaultBinding) return "Methods";
else return "Methods for binding<br>" + name;
}
string GetOpName (object op)
{
OperationBinding ob = op as OperationBinding;
if (ob == null) return "";
if (ob.Input.Name != null) return ob.Input.Name;
else return ob.Name;
}
bool HasFormResult
{
get { return Request.QueryString ["ext"] == "testform"; }
}
class NoCheckCertificatePolicy : ICertificatePolicy {
public bool CheckValidationResult (ServicePoint a, X509Certificate b, WebRequest c, int d)
{
return true;
}
}
string GetOrPost ()
{
return (CurrentOperationProtocols.IndexOf ("HttpGet") >= 0) ? "GET" : "POST";
}
string GetQS ()
{
bool fill = false;
string qs = "";
NameValueCollection query_string = Request.QueryString;
for (int n = 0; n < query_string.Count; n++) {
if (fill) {
if (qs != "") qs += "&";
qs += query_string.GetKey(n) + "=" + Server.UrlEncode (query_string [n]);
}
if (query_string.GetKey(n) == "ext") fill = true;
}
return qs;
}
string GetTestResultUrl ()
{
if (!HasFormResult) return "";
string location = null;
ServiceDescription desc = descriptions [0];
Service service = desc.Services[0];
foreach (Port port in service.Ports)
if (port.Name == CurrentOperationBinding)
{
SoapAddressBinding sbi = (SoapAddressBinding) port.Extensions.Find (typeof(SoapAddressBinding));
if (sbi != null)
location = sbi.Location;
}
if (location == null)
return "Could not locate web service";
return location + "/" + CurrentOperationName;
}
string GenerateOperationMessages (string protocol, bool generateInput)
{
if (!IsOperationSupported (protocol)) return "";
Port port;
if (protocol != "Soap") port = FindPort (null, protocol);
else port = FindPort (CurrentOperationBinding, null);
Binding binding = descriptions.GetBinding (port.Binding);
OperationBinding obin = FindOperation (binding, CurrentOperationName);
PortType portType = descriptions.GetPortType (binding.Type);
Operation oper = FindOperation (portType, CurrentOperationName);
HtmlSampleGenerator sg = new HtmlSampleGenerator (descriptions, schemas);
string txt = sg.GenerateMessage (port, obin, oper, protocol, generateInput);
if (protocol == "Soap") txt = WrapText (txt,CodeTextColumns);
txt = ColorizeXml (txt);
txt = txt.Replace ("@placeholder!","<span class='literal-placeholder'>");
txt = txt.Replace ("!placeholder@","</span>");
return txt;
}
bool IsOperationSupported (string protocol)
{
if (CurrentPage != "op" || CurrentTab != "msg") return false;
if (protocol == "Soap") return true;
Port port = FindPort (null, protocol);
if (port == null) return false;
Binding binding = descriptions.GetBinding (port.Binding);
if (binding == null) return false;
return FindOperation (binding, CurrentOperationName) != null;
}
//
// Proxy code generation
//
string GetProxyCode ()
{
CodeNamespace codeNamespace = new CodeNamespace();
CodeCompileUnit codeUnit = new CodeCompileUnit();
codeUnit.Namespaces.Add (codeNamespace);
ServiceDescriptionImporter importer = new ServiceDescriptionImporter();
foreach (ServiceDescription sd in descriptions)
importer.AddServiceDescription(sd, null, null);
foreach (XmlSchema sc in schemas)
importer.Schemas.Add (sc);
importer.Import(codeNamespace, codeUnit);
string langId = Request.QueryString ["lang"];
if (langId == null || langId == "") langId = "cs";
CodeDomProvider provider = GetProvider (langId);
ICodeGenerator generator = provider.CreateGenerator();
CodeGeneratorOptions options = new CodeGeneratorOptions();
StringWriter sw = new StringWriter ();
generator.GenerateCodeFromCompileUnit(codeUnit, sw, options);
return Colorize (WrapText (sw.ToString (), CodeTextColumns), langId);
}
public string CurrentLanguage
{
get {
string langId = Request.QueryString ["lang"];
if (langId == null || langId == "") langId = "cs";
return langId;
}
}
public string CurrentProxytName
{
get {
string lan = CurrentLanguage == "cs" ? "C#" : "Visual Basic";
return lan + " Client Proxy";
}
}
private CodeDomProvider GetProvider(string langId)
{
switch (langId.ToUpper())
{
case "CS": return new CSharpCodeProvider();
case "VB": return new VBCodeProvider();
default: return null;
}
}
//
// Document generation
//
class UTF8StringWriter : StringWriter {
public override Encoding Encoding {
get { return Encoding.UTF8; }
}
}
string GenerateDocument ()
{
UTF8StringWriter sw = new UTF8StringWriter ();
if (CurrentDocType == "wsdl")
descriptions [CurrentDocInd].Write (sw);
else if (CurrentDocType == "schema")
schemas [CurrentDocInd].Write (sw);
return Colorize (WrapText (sw.ToString (), CodeTextColumns), "xml");
}
public string CurrentDocType
{
get { return Request.QueryString ["doctype"] != null ? Request.QueryString ["doctype"] : "wsdl"; }
}
public int CurrentDocInd
{
get { return Request.QueryString ["docind"] != null ? int.Parse (Request.QueryString ["docind"]) : 0; }
}
public string CurrentDocumentName
{
get {
if (CurrentDocType == "wsdl")
return "WSDL document for namespace \"" + descriptions [CurrentDocInd].TargetNamespace + "\"";
else
return "Xml Schema for namespace \"" + schemas [CurrentDocInd].TargetNamespace + "\"";
}
}
//
// Pages and tabs
//
bool firstTab = true;
ArrayList disabledTabs = new ArrayList ();
string CurrentTab
{
get { return Request.QueryString["tab"] != null ? Request.QueryString["tab"] : "main" ; }
}
string CurrentPage
{
get { return Request.QueryString["page"] != null ? Request.QueryString["page"] : "main" ; }
}
void WriteTabs ()
{
if (CurrentOperationName != null)
{
WriteTab ("main","Overview");
WriteTab ("test","Test Form");
WriteTab ("msg","Message Layout");
}
}
void WriteTab (string id, string label)
{
if (!firstTab) Response.Write(" | ");
firstTab = false;
string cname = CurrentTab == id ? "tabLabelOn" : "tabLabelOff";
Response.Write ("<a href='" + PageName + "?" + GetPageContext(null) + GetDataContext() + "tab=" + id + "' style='text-decoration:none'>");
Response.Write ("<span class='" + cname + "'>" + label + "</span>");
Response.Write ("</a>");
}
string GetTabContext (string pag, string tab)
{
if (tab == null) tab = CurrentTab;
if (pag == null) pag = CurrentPage;
if (pag != CurrentPage) tab = "main";
return "page=" + pag + "&tab=" + tab + "&";
}
string GetPageContext (string pag)
{
if (pag == null) pag = CurrentPage;
return "page=" + pag + "&";
}
class Tab
{
public string Id;
public string Label;
}
//
// Syntax coloring
//
static string keywords_cs =
"(\\babstract\\b|\\bevent\\b|\\bnew\\b|\\bstruct\\b|\\bas\\b|\\bexplicit\\b|\\bnull\\b|\\bswitch\\b|\\bbase\\b|\\bextern\\b|" +
"\\bobject\\b|\\bthis\\b|\\bbool\\b|\\bfalse\\b|\\boperator\\b|\\bthrow\\b|\\bbreak\\b|\\bfinally\\b|\\bout\\b|\\btrue\\b|" +
"\\bbyte\\b|\\bfixed\\b|\\boverride\\b|\\btry\\b|\\bcase\\b|\\bfloat\\b|\\bparams\\b|\\btypeof\\b|\\bcatch\\b|\\bfor\\b|" +
"\\bprivate\\b|\\buint\\b|\\bchar\\b|\\bforeach\\b|\\bprotected\\b|\\bulong\\b|\\bchecked\\b|\\bgoto\\b|\\bpublic\\b|" +
"\\bunchecked\\b|\\bclass\\b|\\bif\\b|\\breadonly\\b|\\bunsafe\\b|\\bconst\\b|\\bimplicit\\b|\\bref\\b|\\bushort\\b|" +
"\\bcontinue\\b|\\bin\\b|\\breturn\\b|\\busing\\b|\\bdecimal\\b|\\bint\\b|\\bsbyte\\b|\\bvirtual\\b|\\bdefault\\b|" +
"\\binterface\\b|\\bsealed\\b|\\bvolatile\\b|\\bdelegate\\b|\\binternal\\b|\\bshort\\b|\\bvoid\\b|\\bdo\\b|\\bis\\b|" +
"\\bsizeof\\b|\\bwhile\\b|\\bdouble\\b|\\block\\b|\\bstackalloc\\b|\\belse\\b|\\blong\\b|\\bstatic\\b|\\benum\\b|" +
"\\bnamespace\\b|\\bstring\\b)";
static string keywords_vb =
"(\\bAddHandler\\b|\\bAddressOf\\b|\\bAlias\\b|\\bAnd\\b|\\bAndAlso\\b|\\bAnsi\\b|\\bAs\\b|\\bAssembly\\b|" +
"\\bAuto\\b|\\bBoolean\\b|\\bByRef\\b|\\bByte\\b|\\bByVal\\b|\\bCall\\b|\\bCase\\b|\\bCatch\\b|" +
"\\bCBool\\b|\\bCByte\\b|\\bCChar\\b|\\bCDate\\b|\\bCDec\\b|\\bCDbl\\b|\\bChar\\b|\\bCInt\\b|" +
"\\bClass\\b|\\bCLng\\b|\\bCObj\\b|\\bConst\\b|\\bCShort\\b|\\bCSng\\b|\\bCStr\\b|\\bCType\\b|" +
"\\bDate\\b|\\bDecimal\\b|\\bDeclare\\b|\\bDefault\\b|\\bDelegate\\b|\\bDim\\b|\\bDirectCast\\b|\\bDo\\b|" +
"\\bDouble\\b|\\bEach\\b|\\bElse\\b|\\bElseIf\\b|\\bEnd\\b|\\bEnum\\b|\\bErase\\b|\\bError\\b|" +
"\\bEvent\\b|\\bExit\\b|\\bFalse\\b|\\bFinally\\b|\\bFor\\b|\\bFriend\\b|\\bFunction\\b|\\bGet\\b|" +
"\\bGetType\\b|\\bGoSub\\b|\\bGoTo\\b|\\bHandles\\b|\\bIf\\b|\\bImplements\\b|\\bImports\\b|\\bIn\\b|" +
"\\bInherits\\b|\\bInteger\\b|\\bInterface\\b|\\bIs\\b|\\bLet\\b|\\bLib\\b|\\bLike\\b|\\bLong\\b|" +
"\\bLoop\\b|\\bMe\\b|\\bMod\\b|\\bModule\\b|\\bMustInherit\\b|\\bMustOverride\\b|\\bMyBase\\b|\\bMyClass\\b|" +
"\\bNamespace\\b|\\bNew\\b|\\bNext\\b|\\bNot\\b|\\bNothing\\b|\\bNotInheritable\\b|\\bNotOverridable\\b|\\bObject\\b|" +
"\\bOn\\b|\\bOption\\b|\\bOptional\\b|\\bOr\\b|\\bOrElse\\b|\\bOverloads\\b|\\bOverridable\\b|\\bOverrides\\b|" +
"\\bParamArray\\b|\\bPreserve\\b|\\bPrivate\\b|\\bProperty\\b|\\bProtected\\b|\\bPublic\\b|\\bRaiseEvent\\b|\\bReadOnly\\b|" +
"\\bReDim\\b|\\bREM\\b|\\bRemoveHandler\\b|\\bResume\\b|\\bReturn\\b|\\bSelect\\b|\\bSet\\b|\\bShadows\\b|" +
"\\bShared\\b|\\bShort\\b|\\bSingle\\b|\\bStatic\\b|\\bStep\\b|\\bStop\\b|\\bString\\b|\\bStructure\\b|" +
"\\bSub\\b|\\bSyncLock\\b|\\bThen\\b|\\bThrow\\b|\\bTo\\b|\\bTrue\\b|\\bTry\\b|\\bTypeOf\\b|" +
"\\bUnicode\\b|\\bUntil\\b|\\bVariant\\b|\\bWhen\\b|\\bWhile\\b|\\bWith\\b|\\bWithEvents\\b|\\bWriteOnly\\b|\\bXor\\b)";
string Colorize (string text, string lang)
{
if (lang == "xml") return ColorizeXml (text);
else if (lang == "cs") return ColorizeCs (text);
else if (lang == "vb") return ColorizeVb (text);
else return text;
}
string ColorizeXml (string text)
{
text = text.Replace (" ", " ");
Regex re = new Regex ("\r\n|\r|\n");
text = re.Replace (text, "_br_");
re = new Regex ("<\\s*(\\/?)\\s*([\\s\\S]*?)\\s*(\\/?)\\s*>");
text = re.Replace (text,"{blue:<$1}{maroon:$2}{blue:$3>}");
re = new Regex ("\\{(\\w*):([\\s\\S]*?)\\}");
text = re.Replace (text,"<span style='color:$1'>$2</span>");
re = new Regex ("\"(.*?)\"");
text = re.Replace (text,"\"<span style='color:purple'>$1</span>\"");
text = text.Replace ("\t", " ");
text = text.Replace ("_br_", "<br>");
return text;
}
string ColorizeCs (string text)
{
text = text.Replace (" ", " ");
text = text.Replace ("<", "<");
text = text.Replace (">", ">");
Regex re = new Regex ("\"((((?!\").)|\\\")*?)\"");
text = re.Replace (text,"<span style='color:purple'>\"$1\"</span>");
re = new Regex ("//(((.(?!\"</span>))|\"(((?!\").)*)\"</span>)*)(\r|\n|\r\n)");
text = re.Replace (text,"<span style='color:green'>//$1</span><br/>");
re = new Regex (keywords_cs);
text = re.Replace (text,"<span style='color:blue'>$1</span>");
text = text.Replace ("\t"," ");
text = text.Replace ("\n","<br/>");
return text;
}
string ColorizeVb (string text)
{
text = text.Replace (" ", " ");
/* Regex re = new Regex ("\"((((?!\").)|\\\")*?)\"");
text = re.Replace (text,"<span style='color:purple'>\"$1\"</span>");
re = new Regex ("'(((.(?!\"\\<\\/span\\>))|\"(((?!\").)*)\"\\<\\/span\\>)*)(\r|\n|\r\n)");
text = re.Replace (text,"<span style='color:green'>//$1</span><br/>");
re = new Regex (keywords_vb);
text = re.Replace (text,"<span style='color:blue'>$1</span>");
*/
text = text.Replace ("\t"," ");
text = text.Replace ("\n","<br/>");
return text;
}
//
// Helper methods and classes
//
string GetDataContext ()
{
return "op=" + CurrentOperationName + "&bnd=" + CurrentOperationBinding + "&";
}
string GetOptionSel (string v1, string v2)
{
string op = "<option ";
if (v1 == v2) op += "selected ";
return op + "value='" + v1 + "'>";
}
string WrapText (string text, int maxChars)
{
text = text.Replace(" />","/>");
string linspace = null;
int lincount = 0;
int breakpos = 0;
int linstart = 0;
bool inquotes = false;
char lastc = ' ';
string sublineIndent = "";
System.Text.StringBuilder sb = new System.Text.StringBuilder ();
for (int n=0; n<text.Length; n++)
{
char c = text [n];
if (c=='\r' || c=='\n' || n==text.Length-1)
{
sb.Append (linspace + sublineIndent + text.Substring (linstart, n-linstart+1));
linspace = null;
lincount = 0;
linstart = n+1;
breakpos = linstart;
sublineIndent = "";
lastc = c;
continue;
}
if (lastc==',' || lastc=='(')
{
if (!inquotes) breakpos = n;
}
if (lincount > maxChars && breakpos >= linstart)
{
if (linspace != null)
sb.Append (linspace + sublineIndent);
sb.Append (text.Substring (linstart, breakpos-linstart));
sb.Append ("\n");
sublineIndent = " ";
lincount = linspace.Length + sublineIndent.Length + (n-breakpos);
linstart = breakpos;
}
if (c==' ' || c=='\t')
{
if (!inquotes)
breakpos = n;
}
else if (c=='"')
{
inquotes = !inquotes;
}
else
if (linspace == null) {
linspace = text.Substring (linstart, n-linstart);
linstart = n;
}
lincount++;
lastc = c;
}
return sb.ToString ();
}
class Parameter
{
string name;
string type;
string description;
public string Name { get { return name; } set { name = value; } }
public string Type { get { return type; } set { type = value; } }
public string Description { get { return description; } set { description = value; } }
}
public class HtmlSampleGenerator: SampleGenerator
{
public HtmlSampleGenerator (ServiceDescriptionCollection services, XmlSchemas schemas)
: base (services, schemas)
{
}
protected override string GetLiteral (string s)
{
return "@placeholder!" + s + "!placeholder@";
}
}
public class SampleGenerator
{
protected ServiceDescriptionCollection descriptions;
protected XmlSchemas schemas;
XmlSchemaElement anyElement;
ArrayList queue;
SoapBindingUse currentUse;
XmlDocument document = new XmlDocument ();
static readonly XmlQualifiedName anyType = new XmlQualifiedName ("anyType",XmlSchema.Namespace);
static readonly XmlQualifiedName arrayType = new XmlQualifiedName ("Array","http://schemas.xmlsoap.org/soap/encoding/");
static readonly XmlQualifiedName arrayTypeRefName = new XmlQualifiedName ("arrayType","http://schemas.xmlsoap.org/soap/encoding/");
const string SoapEnvelopeNamespace = "http://schemas.xmlsoap.org/soap/envelope/";
const string WsdlNamespace = "http://schemas.xmlsoap.org/wsdl/";
const string SoapEncodingNamespace = "http://schemas.xmlsoap.org/soap/encoding/";
class EncodedType
{
public EncodedType (string ns, XmlSchemaElement elem) { Namespace = ns; Element = elem; }
public string Namespace;
public XmlSchemaElement Element;
}
public SampleGenerator (ServiceDescriptionCollection services, XmlSchemas schemas)
{
descriptions = services;
this.schemas = schemas;
queue = new ArrayList ();
}
public string GenerateMessage (Port port, OperationBinding obin, Operation oper, string protocol, bool generateInput)
{
OperationMessage msg = null;
foreach (OperationMessage opm in oper.Messages)
{
if (opm is OperationInput && generateInput) msg = opm;
else if (opm is OperationOutput && !generateInput) msg = opm;
}
if (msg == null) return null;
switch (protocol) {
case "Soap": return GenerateHttpSoapMessage (port, obin, oper, msg);
case "HttpGet": return GenerateHttpGetMessage (port, obin, oper, msg);
case "HttpPost": return GenerateHttpPostMessage (port, obin, oper, msg);
}
return "Unknown protocol";
}
public string GenerateHttpSoapMessage (Port port, OperationBinding obin, Operation oper, OperationMessage msg)
{
string req = "";
if (msg is OperationInput)
{
SoapAddressBinding sab = port.Extensions.Find (typeof(SoapAddressBinding)) as SoapAddressBinding;
SoapOperationBinding sob = obin.Extensions.Find (typeof(SoapOperationBinding)) as SoapOperationBinding;
req += "POST " + new Uri (sab.Location).AbsolutePath + "\n";
req += "SOAPAction: " + sob.SoapAction + "\n";
req += "Content-Type: text/xml; charset=utf-8\n";
req += "Content-Length: " + GetLiteral ("string") + "\n";
req += "Host: " + GetLiteral ("string") + "\n\n";
}
else
{
req += "HTTP/1.0 200 OK\n";
req += "Content-Type: text/xml; charset=utf-8\n";
req += "Content-Length: " + GetLiteral ("string") + "\n\n";
}
req += GenerateSoapMessage (obin, oper, msg);
return req;
}
public string GenerateHttpGetMessage (Port port, OperationBinding obin, Operation oper, OperationMessage msg)
{
string req = "";
if (msg is OperationInput)
{
HttpAddressBinding sab = port.Extensions.Find (typeof(HttpAddressBinding)) as HttpAddressBinding;
HttpOperationBinding sob = obin.Extensions.Find (typeof(HttpOperationBinding)) as HttpOperationBinding;
string location = new Uri (sab.Location).AbsolutePath + sob.Location + "?" + BuildQueryString (msg);
req += "GET " + location + "\n";
req += "Host: " + GetLiteral ("string");
}
else
{
req += "HTTP/1.0 200 OK\n";
req += "Content-Type: text/xml; charset=utf-8\n";
req += "Content-Length: " + GetLiteral ("string") + "\n\n";
MimeXmlBinding mxb = (MimeXmlBinding) obin.Output.Extensions.Find (typeof(MimeXmlBinding)) as MimeXmlBinding;
if (mxb == null) return req;
Message message = descriptions.GetMessage (msg.Message);
XmlQualifiedName ename = null;
foreach (MessagePart part in message.Parts)
if (part.Name == mxb.Part) ename = part.Element;
if (ename == null) return req + GetLiteral("string");
StringWriter sw = new StringWriter ();
XmlTextWriter xtw = new XmlTextWriter (sw);
xtw.Formatting = Formatting.Indented;
currentUse = SoapBindingUse.Literal;
WriteRootElementSample (xtw, ename);
xtw.Close ();
req += sw.ToString ();
}
return req;
}
public string GenerateHttpPostMessage (Port port, OperationBinding obin, Operation oper, OperationMessage msg)
{
string req = "";
if (msg is OperationInput)
{
HttpAddressBinding sab = port.Extensions.Find (typeof(HttpAddressBinding)) as HttpAddressBinding;
HttpOperationBinding sob = obin.Extensions.Find (typeof(HttpOperationBinding)) as HttpOperationBinding;
string location = new Uri (sab.Location).AbsolutePath + sob.Location;
req += "POST " + location + "\n";
req += "Content-Type: application/x-www-form-urlencoded\n";
req += "Content-Length: " + GetLiteral ("string") + "\n";
req += "Host: " + GetLiteral ("string") + "\n\n";
req += BuildQueryString (msg);
}
else return GenerateHttpGetMessage (port, obin, oper, msg);
return req;
}
string BuildQueryString (OperationMessage opm)
{
string s = "";
Message msg = descriptions.GetMessage (opm.Message);
foreach (MessagePart part in msg.Parts)
{
if (s.Length != 0) s += "&";
s += part.Name + "=" + GetLiteral (part.Type.Name);
}
return s;
}
public string GenerateSoapMessage (OperationBinding obin, Operation oper, OperationMessage msg)
{
SoapOperationBinding sob = obin.Extensions.Find (typeof(SoapOperationBinding)) as SoapOperationBinding;
SoapBindingStyle style = (sob != null) ? sob.Style : SoapBindingStyle.Document;
MessageBinding msgbin = (msg is OperationInput) ? (MessageBinding) obin.Input : (MessageBinding)obin.Output;
SoapBodyBinding sbb = msgbin.Extensions.Find (typeof(SoapBodyBinding)) as SoapBodyBinding;
SoapBindingUse bodyUse = (sbb != null) ? sbb.Use : SoapBindingUse.Literal;
StringWriter sw = new StringWriter ();
XmlTextWriter xtw = new XmlTextWriter (sw);
xtw.Formatting = Formatting.Indented;
xtw.WriteStartDocument ();
xtw.WriteStartElement ("soap", "Envelope", SoapEnvelopeNamespace);
xtw.WriteAttributeString ("xmlns", "xsi", null, XmlSchema.InstanceNamespace);
xtw.WriteAttributeString ("xmlns", "xsd", null, XmlSchema.Namespace);
if (bodyUse == SoapBindingUse.Encoded)
{
xtw.WriteAttributeString ("xmlns", "soapenc", null, SoapEncodingNamespace);
xtw.WriteAttributeString ("xmlns", "tns", null, msg.Message.Namespace);
}
// Serialize headers
bool writtenHeader = false;
foreach (object ob in msgbin.Extensions)
{
SoapHeaderBinding hb = ob as SoapHeaderBinding;
if (hb == null) continue;
if (!writtenHeader) {
xtw.WriteStartElement ("soap", "Header", SoapEnvelopeNamespace);
writtenHeader = true;
}
WriteHeader (xtw, hb);
}
if (writtenHeader)
xtw.WriteEndElement ();
// Serialize body
xtw.WriteStartElement ("soap", "Body", SoapEnvelopeNamespace);
currentUse = bodyUse;
WriteBody (xtw, oper, msg, sbb, style);
xtw.WriteEndElement ();
xtw.WriteEndElement ();
xtw.Close ();
return sw.ToString ();
}
void WriteHeader (XmlTextWriter xtw, SoapHeaderBinding header)
{
Message msg = descriptions.GetMessage (header.Message);
if (msg == null) throw new InvalidOperationException ("Message " + header.Message + " not found");
MessagePart part = msg.Parts [header.Part];
if (part == null) throw new InvalidOperationException ("Message part " + header.Part + " not found in message " + header.Message);
currentUse = header.Use;
if (currentUse == SoapBindingUse.Literal)
WriteRootElementSample (xtw, part.Element);
else
WriteTypeSample (xtw, part.Type);
}
void WriteBody (XmlTextWriter xtw, Operation oper, OperationMessage opm, SoapBodyBinding sbb, SoapBindingStyle style)
{
Message msg = descriptions.GetMessage (opm.Message);
if (msg.Parts.Count > 0 && msg.Parts[0].Name == "parameters")