aws_24.0.0_2b75fe6d/tools/ada2wsdl-generator.adb

   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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
------------------------------------------------------------------------------
--                              Ada Web Server                              --
--                                                                          --
--                     Copyright (C) 2003-2020, AdaCore                     --
--                                                                          --
--  This is free software;  you can redistribute it  and/or modify it       --
--  under terms of the  GNU General Public License as published  by the     --
--  Free Software  Foundation;  either version 3,  or (at your option) any  --
--  later version.  This software is distributed in the hope  that it will  --
--  be useful, but WITHOUT ANY WARRANTY;  without even the implied warranty --
--  of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU     --
--  General Public License for  more details.                               --
--                                                                          --
--  You should have  received  a copy of the GNU General  Public  License   --
--  distributed  with  this  software;   see  file COPYING3.  If not, go    --
--  to http://www.gnu.org/licenses for a complete copy of the license.      --
------------------------------------------------------------------------------

with Ada.Calendar;
with Ada.Characters.Handling;
with Ada.Containers.Indefinite_Hashed_Maps;
with Ada.Containers.Vectors;
with Ada.Exceptions;
with Ada.Strings.Hash;
with Ada.Text_IO;

with GNAT.Calendar.Time_IO;

with AWS.Containers.String_Vectors;
with AWS.Utils;
with SOAP.Name_Space;
with SOAP.Types;
with SOAP.WSDL;

with Ada2WSDL.Options;

package body Ada2WSDL.Generator is

   use Ada;
   use SOAP;

   --  Data structure for API's description

   type Parameter;
   type Parameter_Access is access Parameter;

   type Parameter is record
      Name      : Unbounded_String;
      Type_Name : Unbounded_String;
      XSD_Name  : Unbounded_String;
      Next      : Parameter_Access;
   end record;

   type Mode is (Routine, Safe_Pointer_Definition,
                 Structure, Table, Simple_Type, Enumeration);

   type Definition (Def_Mode : Mode := Routine) is record
      Name       : Unbounded_String;
      NS         : Unbounded_String;
      Parameters : Parameter_Access;
      Last       : Parameter_Access;

      case Def_Mode is
         when Routine =>
            Return_Type : Parameter_Access;

         when Table =>
            Length : Natural;

         when Simple_Type =>
            Min, Max : Unbounded_String;
            Len      : Unbounded_String;

         when Structure | Enumeration =>
            null;

         when Safe_Pointer_Definition =>
            Type_Name, Access_Name : Unbounded_String;
      end case;
   end record;

   package Profiles is new Containers.Vectors (Positive, Definition);

   API : Profiles.Vector;
   --  All definitions found for the current API

   Schema_Needed : Boolean := False;
   --  Set to True if a WSDL schema is to be writed

   package NS_Maps is new Ada.Containers.Indefinite_Hashed_Maps
     (String, Positive, Ada.Strings.Hash, "=", "=");

   Name_Spaces : NS_Maps.Map;
   NS_Num      : Natural := 0;
   --  Each name-space is named n<N> where <N> is a number starting at 1
   --  and incrementing. NS_Num is this number. The Name_Spaces map keep the
   --  relation between the name-space value (string http://.../) and the
   --  actual number. The name-spaces are then written into the WSDL document
   --  header and referenced by the elements of the WSDL.

   procedure Insert_NS (Value : String);
   --  Insert a new namespace value into Name_Spaces table

   function NS_Prefix (Value : String) return String;
   --  Returns the name space prefix for the given name space value

   function "+" (S : String) return Unbounded_String
      renames To_Unbounded_String;

   function "-" (S : Unbounded_String) return String
      renames To_String;

   function To_XSD (NS, Ada_Type : String) return String;

   procedure Check_Routine (Name : String);
   --  Checks the routine name, no overloading is allowed, raises
   --  Constraint_Error if name already exits.

   -------------------
   -- Check_Routine --
   -------------------

   procedure Check_Routine (Name : String) is
      use Exceptions;
   begin
      for A of API loop
         if A.Def_Mode = Routine
           and then To_String (A.Name) = Name
         then
            Raise_Exception
              (Spec_Error'Identity,
               "routine " & Name & " already found, no overloading allowed.");
         end if;
      end loop;
   end Check_Routine;

   ---------------
   -- Insert_NS --
   ---------------

   procedure Insert_NS (Value : String) is
      P       : NS_Maps.Cursor;
      Success : Boolean;
   begin
      if not Name_Spaces.Contains (Value)
        and then Value /= SOAP.Name_Space.XSD_URL
      then
         NS_Num := NS_Num + 1;
         Name_Spaces.Insert (Value, NS_Num, P, Success);
      end if;
   end Insert_NS;

   -------------------
   -- New_Component --
   -------------------

   procedure New_Component (NS, Comp_Name, Comp_Type : String) is
      New_P : constant not null Parameter_Access :=
                new Parameter'
                  (+Comp_Name, +Comp_Type, +To_XSD (NS, Comp_Type), null);
   begin
      if Options.Verbose then
         Text_IO.Put_Line
           ("        " & Comp_Name & " : " & Comp_Type
              & " (" & (-New_P.XSD_Name) & ')');
      end if;

      Insert_NS (NS);

      if API (API.Last_Index).Parameters = null then
         API (API.Last_Index).Parameters := New_P;
      else
         API (API.Last_Index).Last.Next := New_P;
      end if;

      API (API.Last_Index).Last := New_P;
   end New_Component;

   ----------------
   -- New_Formal --
   ----------------

   procedure New_Formal (NS, Var_Name, Var_Type : String) is
      New_P : constant not null Parameter_Access :=
                new Parameter'
                  (+Var_Name, +Var_Type, +To_XSD (NS, Var_Type), null);
   begin
      if Options.Verbose then
         Text_IO.Put_Line
           ("        " & Var_Name & " : " & Var_Type
              & " (" & (-New_P.XSD_Name) & ')');
      end if;

      Insert_NS (NS);

      if API (API.Last_Index).Parameters = null then
         API (API.Last_Index).Parameters := New_P;
      else
         API (API.Last_Index).Last.Next := New_P;
      end if;

      API (API.Last_Index).Last := New_P;
   end New_Formal;

   -----------------
   -- New_Literal --
   -----------------

   procedure New_Literal (Name : String) is
      New_P : constant not null Parameter_Access :=
                new Parameter'(+Name, +"", +"", null);
   begin
      if Options.Verbose then
         Text_IO.Put_Line ("        " & Name);
      end if;

      if API (API.Last_Index).Parameters = null then
         API (API.Last_Index).Parameters := New_P;
      else
         API (API.Last_Index).Last.Next := New_P;
      end if;

      API (API.Last_Index).Last := New_P;
   end New_Literal;

   ---------------
   -- NS_Prefix --
   ---------------

   function NS_Prefix (Value : String) return String is
      use AWS;
   begin
      if Value = "" then
         return "tns";
      else
         return 'n' & Utils.Image (Name_Spaces.Element (Value));
      end if;
   end NS_Prefix;

   ----------------------
   -- Register_Derived --
   ----------------------

   procedure Register_Derived
     (NS, Name : String;
      Def      : Type_Data)
   is
      New_P : constant not null Parameter_Access :=
                new Parameter'
                  (+Name, Def.Name, +To_XSD (-Def.NS, -Def.Name), null);
      D     : Definition (Simple_Type);
   begin
      --  We need to write a schema for this derived type
      Schema_Needed := True;

      Insert_NS (NS);

      D.NS         := +NS;
      D.Name       := +Name;
      D.Parameters := New_P;
      D.Min        := Def.Min;
      D.Max        := Def.Max;
      D.Len        := Def.Len;

      if Name = "Character" and then -Def.Len = "1" then
         --  We special case the Character schema as we don't want it to
         --  interfer with the current defintion. As soon as we found it we
         --  prepend it into to API to ensure that it is generated in the
         --  final schema.

         if API (API.First_Index).Element.Name /= Name then
            API.Prepend (D);
         end if;

      else
         API.Append (D);
      end if;

      if not Options.Quiet then
         Text_IO.Put
           ("   - derived         " & Name & " is new " & (-Def.Name));

         if Options.Verbose then
            Text_IO.Put_Line (" (" & (-New_P.XSD_Name) & ')');
         else
            Text_IO.New_Line;
         end if;
      end if;
   end Register_Derived;

   ---------------------------
   -- Register_Safe_Pointer --
   ---------------------------

   procedure Register_Safe_Pointer (Name, Type_Name, Access_Name : String) is
      use Exceptions;
      D : Definition (Safe_Pointer_Definition);
   begin
      if Name /= Type_Name & "_Safe_Pointer" then
         Raise_Exception
           (Spec_Error'Identity,
            "Package Safe_Pointers instantiation must be named "
            & Type_Name & "_Safe_Pointer.");
      end if;

      D.Name        := +Name;
      D.Type_Name   := +Type_Name;
      D.Access_Name := +Access_Name;

      API.Append (D);

      if not Options.Quiet then
         Text_IO.Put_Line
           ("   - safe pointer    " & Name
              & " (" & Type_Name & ", " & Access_Name & ")");
      end if;
   end Register_Safe_Pointer;

   -------------------
   -- Register_Type --
   -------------------

   procedure Register_Type
     (NS, Name : String;
      Def      : Type_Data)
   is
      New_P : constant not null Parameter_Access :=
                new Parameter'
                  (+Name, Def.Name, +To_XSD (-Def.NS, -Def.Name), null);
      D     : Definition (Simple_Type);
   begin
      --  We need to write a schema for this derived type
      Schema_Needed := True;

      Insert_NS (NS);

      D.NS         := +NS;
      D.Name       := +Name;
      D.Parameters := New_P;
      D.Min        := Def.Min;
      D.Max        := Def.Max;

      API.Append (D);

      if not Options.Quiet then
         Text_IO.Put
           ("   - new type        " & Name & " is " & (-Def.Name));

         if Options.Verbose then
            Text_IO.Put_Line (" (" & (-New_P.XSD_Name) & ')');
         else
            Text_IO.New_Line;
         end if;
      end if;
   end Register_Type;

   -----------------
   -- Return_Type --
   -----------------

   procedure Return_Type (NS, Name, Spec_Name : String) is
      New_P : constant not null Parameter_Access :=
                new Parameter'(+Spec_Name & "_Result",
                               +Name, +To_XSD (NS, Name), null);
   begin
      Insert_NS (NS);

      if Options.Verbose then
         Text_IO.Put_Line
           ("        return " & Name & " (" & (-New_P.XSD_Name) & ')');
      end if;

      API (API.Last_Index).Return_Type := New_P;
   end Return_Type;

   -----------------
   -- Start_Array --
   -----------------

   procedure Start_Array
     (NS, Name                     : String;
      Component_NS, Component_Type : String;
      Length                       : Natural := 0)
   is
      New_P : constant not null Parameter_Access :=
                new Parameter'(+"item", +Component_Type,
                               +To_XSD (Component_NS, Component_Type), null);
      D     : Definition (Table);
   begin
      --  We need to write a schema for this record
      Schema_Needed := True;

      Insert_NS (NS);

      D.NS         := +NS;
      D.Name       := +Name;
      D.NS         := +NS;
      D.Parameters := New_P;
      D.Length     := Length;

      API.Append (D);

      if not Options.Quiet then
         Text_IO.Put ("   - array (");

         if Length = 0 then
            --  An unconstrained array
            Text_IO.Put ("<>");
         else
            Text_IO.Put (AWS.Utils.Image (Length));
         end if;

         Text_IO.Put (")");
         Text_IO.Set_Col (22);
         Text_IO.Put (Name & " of " & Component_Type);

         if Options.Verbose then
            Text_IO.Put_Line (" (" & (-New_P.XSD_Name) & ')');
         else
            Text_IO.New_Line;
         end if;
      end if;
   end Start_Array;

   -----------------------
   -- Start_Enumeration --
   -----------------------

   procedure Start_Enumeration (NS, Name : String) is
      D : Definition (Enumeration);
   begin
      --  We need to write a schema for this derived type
      Schema_Needed := True;

      Insert_NS (NS);

      D.NS         := +NS;
      D.Name       := +Name;
      D.Parameters := null;

      API.Append (D);

      if not Options.Quiet then
         Text_IO.Put_Line ("   - enumeration     " & Name);
      end if;
   end Start_Enumeration;

   ------------------
   -- Start_Record --
   ------------------

   procedure Start_Record (NS, Name : String) is
      D : Definition (Structure);
   begin
      --  We need to write a schema for this record
      Schema_Needed := True;

      Insert_NS (NS);

      D.NS   := +NS;
      D.Name := +Name;

      API.Append (D);

      if not Options.Quiet then
         Text_IO.Put_Line ("   - record          " & Name);
      end if;
   end Start_Record;

   -------------------
   -- Start_Routine --
   -------------------

   procedure Start_Routine (NS, Name, Comment : String) is
      D : Definition (Routine);
   begin
      Check_Routine (Name);

      D.NS := +NS;
      D.Name := +Name;

      API.Append (D);

      if not Options.Quiet then
         Text_IO.Put_Line ("   > " & Comment & "       " & Name);
      end if;
   end Start_Routine;

   ------------
   -- To_XSD --
   ------------

   function To_XSD (NS, Ada_Type : String) return String is
      P        : WSDL.Parameter_Type;
      Standard : Boolean;
   begin
      Insert_NS (NS);

      if Characters.Handling.To_Lower (Ada_Type) = "character" then
         --  First generate the Character derived type if needed

         declare
            Def : constant Type_Data :=
                    (NS   => +SOAP.Name_Space.XSD_URL,
                     Name => +"String",
                     Min  => Null_Unbounded_String,
                     Max  => Null_Unbounded_String,
                     Len  => +"1");
         begin
            Register_Derived
              (NS   => Name_Space.Value (Name_Space.AWS) & "Standard_pkg/",
               Name => "Character",
               Def  => Def);
         end;

         return NS_Prefix
           (Name_Space.Value (Name_Space.AWS) & "Standard_pkg/")
           & ":Character";

      elsif Ada_Type = "SOAP_Base64" then
         return Types.XML_Base64_Binary;
      end if;

      WSDL.From_Ada (Ada_Type, P, Standard);

      if Standard then
         return WSDL.To_XSD (P);

      else
         --  We suppose here that this is a composite type (record/array)
         --  and that a corresponding entry will be found in the schema.
         return NS_Prefix (NS) & ':' & Ada_Type;
      end if;
   end To_XSD;

   -----------------
   -- Type_Exists --
   -----------------

   function Type_Exists (NS, Name : String) return Boolean is
   begin
      for A of API loop
         if A.Def_Mode in Structure | Simple_Type | Enumeration | Table then
            if -A.Name = Name and then -A.NS = NS then
               return True;
            end if;
         end if;
      end loop;

      return False;
   end Type_Exists;

   -----------
   -- Write --
   -----------

   procedure Write (Filename : String) is

      use Ada.Text_IO;

      WS_Name : constant String := -Options.WS_Name;

      NS      : constant String :=
                  SOAP.Name_Space.Value
                    (SOAP.Name_Space.AWS) & WS_Name & "_def/";

      T_NS    : constant String :=
                  (SOAP.Name_Space.Value
                     (SOAP.Name_Space.AWS) & WS_Name & "_pkg/");

      procedure Write_Header;
      --  Write WSDL header

      procedure Write_Schema;
      --  Write WSDL schema, types tag

      procedure Write_Messages;
      --  Write WSDL message tags

      procedure Write_Port_Type;
      --  Write WSDL portType tag

      procedure Write_Binding;
      --  Write WSDL binding tag

      procedure Write_Service;
      --  Write WSDL service tag

      procedure Write_Footer;
      --  Write WSDL footer, close definition tag

      -------------------
      -- Write_Binding --
      -------------------

      procedure Write_Binding is

         procedure Write_Operation (R : Definition);

         ---------------------
         -- Write_Operation --
         ---------------------

         procedure Write_Operation (R : Definition) is

            procedure Write_SOAP_Body;

            ---------------------
            -- Write_SOAP_Body --
            ---------------------

            procedure Write_SOAP_Body is
            begin
               Put_Line ("            <soap:body");
               Put_Line ("               encodingStyle="""
                           & "http://schemas.xmlsoap.org/soap/encoding/""");

               if not (Options.Document and then Options.Literal) then
                  Put_Line ("               namespace=""" & NS & '"');
               end if;

               if Options.Literal then
                  Put_Line ("               use=""literal""/>");
               else
                  Put_Line ("               use=""encoded""/>");
               end if;
            end Write_SOAP_Body;

            Name : constant String := -R.Name;

         begin
            Put_Line ("      <wsdl:operation name=""" & Name & """>");
            Put_Line ("         <soap:operation soapAction="""
                        & Name & """/>");
            Put_Line ("         <wsdl:input>");
            Write_SOAP_Body;
            Put_Line ("         </wsdl:input>");
            Put_Line ("         <wsdl:output>");
            Write_SOAP_Body;
            Put_Line ("         </wsdl:output>");
            Put_Line ("      </wsdl:operation>");
         end Write_Operation;

      begin
         New_Line;
         Put_Line ("   <wsdl:binding name="""
                   & WS_Name & "_Binding"" type=""tns:"
                     & WS_Name & "_PortType"">");

         if Options.Document then
            Put_Line ("      <soap:binding style=""document""");
         else
            Put_Line ("      <soap:binding style=""rpc""");
         end if;

         Put_Line ("         transport="""
                     & "http://schemas.xmlsoap.org/soap/http""/>");

         --  Output all operations info

         for A of API loop
            if A.Def_Mode = Routine then
               New_Line;
               Write_Operation (A);
            end if;
         end loop;

         Put_Line ("   </wsdl:binding>");
      end Write_Binding;

      ------------------
      -- Write_Footer --
      ------------------

      procedure Write_Footer is
      begin
         Put_Line ("</wsdl:definitions>");
      end Write_Footer;

      ------------------
      -- Write_Header --
      ------------------

      procedure Write_Header is
         use AWS;
      begin
         Put_Line ("<?xml version=""1.0"" encoding=""UTF-8""?>");
         Put_Line ("<wsdl:definitions name=""" & WS_Name  & """");
         Put_Line ("   targetNamespace=""" & NS & '"');
         Put_Line ("   xmlns:tns=""" & NS & '"');

         Put_Line ("   " & Name_Space.Image (Name_Space.SOAP));
         Put_Line ("   " & Name_Space.Image (Name_Space.SOAPENC));
         Put_Line ("   " & Name_Space.Image (Name_Space.WSDL));
         Put_Line ("   " & Name_Space.Image (Name_Space.XSI));
         Put      ("   " & Name_Space.Image (Name_Space.XSD));

         if Options.Document then
            --  Ensure the main name-space is generated, this is needed if
            --  the schema is empty (no user defined types), yet the elements
            --  to be generated for the document style will reference this
            --  name-space.

            Insert_NS (T_NS);
         end if;

         --  Write all name spaces

         for P in Name_Spaces.Iterate loop
            New_Line;
            Put ("   xmlns:n" & Utils.Image (NS_Maps.Element (P))
                 & "=""" & NS_Maps.Key (P) & '"');
         end loop;

         --  Close definition

         Put_Line (">");

         --  Write AWS/Ada2WSDL tag

         New_Line;
         Put ("   <!-- Generated by AWS/Ada2WSDL v" & Version);

         if Options.Timestamp then
            New_Line;
            Put
              ("        on " & GNAT.Calendar.Time_IO.Image
                 (Ada.Calendar.Clock, "%A %d %B %Y at %T"));
         end if;

         Put_Line (" -->");
      end Write_Header;

      --------------------
      -- Write_Messages --
      --------------------

      procedure Write_Messages is

         procedure Write_Message (R : Definition)
           with Pre => R.Def_Mode = Routine;

         ---------------------
         -- Write_Operation --
         ---------------------

         procedure Write_Message (R : Definition) is

            Name : constant String := -R.Name;

            procedure Write_Part
              (Parameters : Boolean; P : not null access Parameter);

            ----------------
            -- Write_Part --
            ----------------

            procedure Write_Part
              (Parameters : Boolean; P : not null access Parameter)
            is
               A : access Parameter := P;
            begin
               --  Whether we have to generate a document style binding or
               --  an RPC one. A part for a document style is:
               --
               --     <part name="" element="" />
               --
               --  where element is referencing an element in the schema.
               --  Those elements are written by Generate_Element routine.
               --  A single part is allowed, if a routine is taking
               --  multiple parameters they are all inside an
               --  element/complexType/sequence schema structure.
               --
               --  For an RPC style we use multiple:
               --
               --     <part name="" type="" />

               if Options.Document then
                  declare
                     Prefix : constant String := NS_Prefix (T_NS);
                  begin
                     Put_Line
                       ("      <wsdl:part name=""parameters"" "
                        & "element="""
                        & Prefix & ':'
                        & (if Parameters then "Parameters" else "Result")
                        & '_' & Name & """/>");
                  end;

               else
                  while A /= null loop
                     Put_Line
                       ("      <wsdl:part name=""" & (-A.Name) & """ "
                        & "type=""" & (-A.XSD_Name) & """/>");
                     A := A.Next;
                  end loop;
               end if;
            end Write_Part;

         begin
            New_Line;

            if R.Parameters /= null then
               Put_Line ("   <wsdl:message name=""" & Name & "_Request"">");
               Write_Part (Parameters => True, P => R.Parameters);
               Put_Line ("   </wsdl:message>");
            end if;

            New_Line;

            if R.Return_Type /= null then
               Put_Line ("   <wsdl:message name=""" & Name & "_Response"">");
               Write_Part (Parameters => False, P => R.Return_Type);
               Put_Line ("   </wsdl:message>");
            end if;
         end Write_Message;

      begin
         for A of API loop
            if A.Def_Mode = Routine then
               Write_Message (A);
            end if;
         end loop;
      end Write_Messages;

      ---------------------
      -- Write_Port_Type --
      ---------------------

      procedure Write_Port_Type is

         procedure Write_Operation (R : Definition);

         ---------------------
         -- Write_Operation --
         ---------------------

         procedure Write_Operation (R : Definition) is

            Name : constant String := -R.Name;

         begin
            Put_Line ("      <wsdl:operation name=""" & Name & """>");

            if R.Parameters /= null then
               --  Notification operation
               Put_Line
                 ("         <wsdl:input message=""tns:"
                  & Name & "_Request""/>");
            end if;

            if R.Return_Type /= null then
               --  Request-response operation
               Put_Line
                 ("         <wsdl:output message=""tns:"
                  & Name & "_Response""/>");
            end if;

            Put_Line ("      </wsdl:operation>");
         end Write_Operation;

         Found : Boolean := False;

      begin
         New_Line;
         Put_Line ("   <wsdl:portType name=""" & WS_Name & "_PortType"">");

         --  Output all operations info

         for A of API loop
            if A.Def_Mode = Routine then
               if Found then
                  New_Line;
               else
                  Found := True;
               end if;
               Write_Operation (A);
            end if;
         end loop;

         Put_Line ("   </wsdl:portType>");
      end Write_Port_Type;

      ------------------
      -- Write_Schema --
      ------------------

      procedure Write_Schema is

         procedure Write_Array (E : Definition);
         --  Write array element tags

         procedure Write_Record (E : Definition);
         --  Write record element tags

         procedure Write_Type (E : Definition);
         --  Write a derived type (simpleType)

         procedure Write_Enumeration (E : Definition);
         --  Write an enumeration type definition (simpleType)

         procedure Generate_Element (E : Definition);
         --  Write the Element for document style binding

         procedure Write_Schema_For (NS : String);
         --  Write schema definitions for the given name-space

         ----------------------
         -- Generate_Element --
         ----------------------

         procedure Generate_Element (E : Definition) is

            procedure Check_Message (R : Definition)
              with Pre => R.Def_Mode = Routine;

            ---------------------
            -- Write_Operation --
            ---------------------

            procedure Check_Message (R : Definition) is

               Name : constant String := -R.Name;

               procedure Check_Part
                 (Name : String;
                  P    : not null access Parameter);

               ----------------
               -- Check_Part --
               ----------------

               procedure Check_Part
                 (Name : String;
                  P    : not null access Parameter)
               is
                  A : access Parameter := P;
               begin
                  Text_IO.Put_Line
                    ("         <element name=""" & Name & """>");
                  Text_IO.Put_Line ("            <complexType>");
                  Text_IO.Put_Line ("               <sequence>");

                  while A /= null loop
                     Text_IO.Put_Line
                       ("                  <xsd:element name="""
                        & (-A.Name) & """"
                        & " type=""" & (-A.XSD_Name) & """/>");
                     A := A.Next;
                  end loop;

                  Text_IO.Put_Line ("               </sequence>");
                  Text_IO.Put_Line ("            </complexType>");
                  Text_IO.Put_Line ("         </element>");
               end Check_Part;

            begin
               if R.Parameters /= null then
                  Check_Part ("Parameters_" & Name, R.Parameters);
               end if;

               if R.Return_Type /= null then
                  Check_Part ("Result_" & Name, R.Return_Type);
               end if;
            end Check_Message;

         begin
            Check_Message (E);
         end Generate_Element;

         -----------------
         -- Write_Array --
         -----------------

         procedure Write_Array (E : Definition) is
         begin
            New_Line;
            Put_Line
              ("         <xsd:complexType name=""" & (-E.Name) & """>");

            if Options.SEA then
               --  Generate old style SOAP-Encoded array. This is kept for
               --  compatibility reasons.

               Put_Line ("            <xsd:complexContent>");
               Put_Line ("               <xsd:restriction "
                         & "base=""soapenc:Array"">");
               Put_Line ("                  <xsd:attribute "
                         & "ref=""soapenc:arrayType"""
                         & " wsdl:arrayType=""" & (-E.Parameters.XSD_Name)
                         & (if E.Length = 0
                           then "[]"
                           else "[" & AWS.Utils.Image (E.Length) & "]")
                         & """/>");
               Put_Line ("               </xsd:restriction>");
               Put_Line ("            </xsd:complexContent>");

            else
               --  Generate WSDL schema style array

               Put_Line ("            <xsd:sequence>");

               Put_Line ("               <xsd:element name=""x"" type="""
                         & (-E.Parameters.XSD_Name) & '"');
               Put_Line ("                    minOccurs=""0"" maxOccurs="""
                         & (if E.Length = 0
                           then "unbounded"
                           else AWS.Utils.Image (E.Length)) & """/>");
               Put_Line ("            </xsd:sequence>");
            end if;

            Put_Line ("         </xsd:complexType>");
         end Write_Array;

         -----------------------
         -- Write_Enumeration --
         -----------------------

         procedure Write_Enumeration (E : Definition) is
            P : access Parameter := E.Parameters;
         begin
            New_Line;
            Put_Line ("         <xsd:simpleType name=""" & (-E.Name) & """>");
            Put_Line ("            <xsd:restriction base=""xsd:string"">");

            while P /= null loop
               Put_Line ("               <xsd:enumeration value="""
                           & (-P.Name) & """/>");
               P := P.Next;
            end loop;

            Put_Line ("            </xsd:restriction>");
            Put_Line ("         </xsd:simpleType>");
         end Write_Enumeration;

         ------------------
         -- Write_Record --
         ------------------

         procedure Write_Record (E : Definition) is
            P : access Parameter := E.Parameters;
         begin
            New_Line;
            Put_Line ("         <xsd:complexType name=""" & (-E.Name) & """>");
            Put_Line ("            <xsd:all>");

            while P /= null loop
               Put_Line ("               <xsd:element name=""" & (-P.Name)
                           & """ type=""" & (-P.XSD_Name) & """/>");
               P := P.Next;
            end loop;

            Put_Line ("            </xsd:all>");
            Put_Line ("         </xsd:complexType>");
         end Write_Record;

         ----------------------
         -- Write_Schema_For --
         ----------------------

         procedure Write_Schema_For (NS : String) is
         begin
            New_Line;
            Put
              ("      <schema"
               & " xmlns=""http://www.w3.org/2001/XMLSchema""");
            New_Line;
            Put ("         targetNamespace=""" & NS & '"');
            Put_Line (">");

            --  Output all structures

            for A of API loop
               if A.Def_Mode in Structure | Table | Simple_Type
                 | Enumeration | Routine
                 and then -A.NS = NS
               then
                  case A.Def_Mode is
                     when Structure   => Write_Record (A);
                     when Table       => Write_Array (A);
                     when Simple_Type => Write_Type (A);
                     when Enumeration => Write_Enumeration (A);
                     when Routine     =>
                        if Options.Document then
                           Generate_Element (A);
                        end if;

                     when Safe_Pointer_Definition =>
                        null;
                  end case;
               end if;
            end loop;

            Put_Line ("      </schema>");
         end Write_Schema_For;

         ----------------
         -- Write_Type --
         ----------------

         procedure Write_Type (E : Definition) is
            P : constant not null access Parameter := E.Parameters;
         begin
            New_Line;
            Put_Line ("         <xsd:simpleType name=""" & (-E.Name) & """>");
            Put_Line ("            <xsd:restriction base="""
                      & (-P.XSD_Name) & """>");

            if E.Min /= Null_Unbounded_String then
               Put_Line ("               <xsd:minInclusive value="""
                         & To_String (E.Min) & """/>");
            end if;

            if E.Max /= Null_Unbounded_String then
               Put_Line ("               <xsd:maxInclusive value="""
                         & To_String (E.Max) & """/>");
            end if;

            if E.Len /= Null_Unbounded_String then
               Put_Line ("               <xsd:length value="""
                         & To_String (E.Len) & """/>");
            end if;

            Put_Line ("            </xsd:restriction>");
            Put_Line ("         </xsd:simpleType>");
         end Write_Type;

         Schemas : AWS.Containers.String_Vectors.Vector;
         --  Record all schemas defined

      begin
         if Schema_Needed or else Options.Document then
            for A of API loop
               case A.Def_Mode is
                  when Structure | Table | Simple_Type | Enumeration =>
                     if not Schemas.Contains (-A.NS) then
                        Schemas.Append (-A.NS);
                     end if;

                  when Routine =>
                     if not Schemas.Contains (-A.NS) then
                        Schemas.Append (-A.NS);
                     end if;

                  when Safe_Pointer_Definition =>
                     null;
               end case;
            end loop;

            --  Now write all needed schemas

            New_Line;
            Put ("   <wsdl:types>");

            for S of Schemas loop
               Write_Schema_For (S);
            end loop;

            Put_Line ("   </wsdl:types>");
         end if;
      end Write_Schema;

      -------------------
      -- Write_Service --
      -------------------

      procedure Write_Service is
      begin
         New_Line;
         Put_Line ("   <wsdl:service name=""" & WS_Name & "_Service"">");
         Put_Line ("      <wsdl:port name=""" & WS_Name
                   & "_Port"" binding=""tns:" & WS_Name & "_Binding"">");
         Put_Line ("         <soap:address location="""
                     & To_String (Options.SOAP_Address) & """/>");
         Put_Line ("      </wsdl:port>");
         Put_Line ("   </wsdl:service>");
      end Write_Service;

      File : Text_IO.File_Type;

   begin
      Create (File, Out_File, Filename);

      Text_IO.Set_Output (File);

      Write_Header;

      Write_Schema;

      Write_Messages;

      Write_Port_Type;

      Write_Binding;

      Write_Service;

      Write_Footer;

      Set_Output (Current_Output);
   end Write;

end Ada2WSDL.Generator;