matreshka_league_21.0.0_0c8f4d47/source/web/tools/wsdl2ada/wsdl-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
------------------------------------------------------------------------------
--                                                                          --
--                            Matreshka Project                             --
--                                                                          --
--                               Web Framework                              --
--                                                                          --
--                              Tools Component                             --
--                                                                          --
------------------------------------------------------------------------------
--                                                                          --
-- Copyright © 2012-2013, Vadim Godunko <vgodunko@gmail.com>                --
-- All rights reserved.                                                     --
--                                                                          --
-- Redistribution and use in source and binary forms, with or without       --
-- modification, are permitted provided that the following conditions       --
-- are met:                                                                 --
--                                                                          --
--  * Redistributions of source code must retain the above copyright        --
--    notice, this list of conditions and the following disclaimer.         --
--                                                                          --
--  * Redistributions in binary form must reproduce the above copyright     --
--    notice, this list of conditions and the following disclaimer in the   --
--    documentation and/or other materials provided with the distribution.  --
--                                                                          --
--  * Neither the name of the Vadim Godunko, IE nor the names of its        --
--    contributors may be used to endorse or promote products derived from  --
--    this software without specific prior written permission.              --
--                                                                          --
-- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS      --
-- "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT        --
-- LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR    --
-- A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT     --
-- HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,   --
-- SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED --
-- TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR   --
-- PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF   --
-- LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING     --
-- NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS       --
-- SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.             --
--                                                                          --
------------------------------------------------------------------------------
--  $Revision: 4002 $ $Date: 2013-07-14 14:36:47 +0400 (Вс, 14 июл 2013) $
------------------------------------------------------------------------------
with Ada.Containers.Ordered_Maps;
with Ada.Wide_Wide_Text_IO; use Ada.Wide_Wide_Text_IO;

with League.Strings;

with WSDL.AST.Bindings;
pragma Unreferenced (WSDL.AST.Bindings);
--  GNAT Pro 7.2.0w (20130423): package is needed to access to type's
--  components.
with WSDL.AST.Descriptions;
pragma Unreferenced (WSDL.AST.Descriptions);
--  GNAT Pro 7.2.0w (20130423): package is needed to access to type's
--  components.
with WSDL.AST.Interfaces;
pragma Unreferenced (WSDL.AST.Interfaces);
--  XXX GNAT 20130108 reports unreferenced unit.
with WSDL.AST.Messages;
pragma Unreferenced (WSDL.AST.Messages);
--  GNAT Pro 7.2.0w (20130423): package is needed to access to type's
--  components.
with WSDL.AST.Operations;
pragma Unreferenced (WSDL.AST.Operations);
--  GNAT Pro 7.2.0w (20130423): package is needed to access to type's
--  components.
with WSDL.AST.Services;
with WSDL.Constants;
with WSDL.Generator.Naming_Conventions;

package body WSDL.Generator is

   use WSDL.Constants;

   package Operation_Maps is
     new Ada.Containers.Ordered_Maps
          (League.Strings.Universal_String,
           WSDL.AST.Interface_Operation_Access,
           League.Strings."<",
           WSDL.AST."=");

   procedure Put (Item : League.Strings.Universal_String);

   procedure Put_Line (Item : League.Strings.Universal_String);

   procedure Generate_Service
    (Service_Node : not null WSDL.AST.Services.Service_Access);

   procedure Lookup_Binding
    (Interface_Node : not null WSDL.AST.Interface_Access;
     Binding_Node   : out WSDL.AST.Binding_Access);

   function Compute_All_Operations
    (Interface_Node : not null WSDL.AST.Interface_Access)
       return Operation_Maps.Map;

   function Compute_SOAP_Action
    (Operation_Node : not null WSDL.AST.Interface_Operation_Access;
     Binding_Node   : WSDL.AST.Binding_Access)
       return League.Strings.Universal_String;

   ----------------------------
   -- Compute_All_Operations --
   ----------------------------

   function Compute_All_Operations
    (Interface_Node : not null WSDL.AST.Interface_Access)
       return Operation_Maps.Map
   is
      Result : Operation_Maps.Map;

      procedure Analyze_Interface
       (Interface_Node : not null WSDL.AST.Interface_Access);

      -----------------------
      -- Analyze_Interface --
      -----------------------

      procedure Analyze_Interface
       (Interface_Node : not null WSDL.AST.Interface_Access) is
      begin
         --  Fill result by own operations of the interface, but excluding
         --  all operations which names is in the result already because they
         --  come from extended interface.

         for Operation_Node of Interface_Node.Interface_Operations loop
            if not Result.Contains (Operation_Node.Local_Name) then
               Result.Insert (Operation_Node.Local_Name, Operation_Node);
            end if;
         end loop;

         for Extended_Interface_Node of Interface_Node.Extended_Interfaces loop
            Analyze_Interface (Extended_Interface_Node);
         end loop;
      end Analyze_Interface;

   begin
      Analyze_Interface (Interface_Node);

      return Result;
   end Compute_All_Operations;

   -------------------------
   -- Compute_SOAP_Action --
   -------------------------

   function Compute_SOAP_Action
    (Operation_Node : not null WSDL.AST.Interface_Operation_Access;
     Binding_Node   : WSDL.AST.Binding_Access)
       return League.Strings.Universal_String
   is
      use type WSDL.AST.Interface_Operation_Access;

   begin
      for Binding_Operation of Binding_Node.Binding_Operations loop
         if Binding_Operation.Interface_Operation = Operation_Node then
            return Binding_Operation.SOAP.Action;
         end if;
      end loop;

      return League.Strings.Empty_Universal_String;
   end Compute_SOAP_Action;

   --------------
   -- Generate --
   --------------

   procedure Generate
    (Description : not null WSDL.AST.Description_Access) is
   begin
      for Service_Node of Description.Services loop
         Generate_Service (Service_Node);
      end loop;
   end Generate;

   ----------------------
   -- Generate_Service --
   ----------------------

   procedure Generate_Service
    (Service_Node : not null WSDL.AST.Services.Service_Access)
   is
      use type League.Strings.Universal_String;
      use type WSDL.AST.Binding_Access;
      use type WSDL.AST.Message_Content_Models;
      use type WSDL.AST.Message_Directions;
      use type WSDL.AST.Interface_Message_Access;

      Binding_Node           : WSDL.AST.Binding_Access;
      Operations             : Operation_Maps.Map;
      Interface_Type_Name    : League.Strings.Universal_String;
      Interface_Package_Name : League.Strings.Universal_String;
      SOAP_Action            : League.Strings.Universal_String;
      Input_Message          : WSDL.AST.Interface_Message_Access;
      Output_Message         : WSDL.AST.Interface_Message_Access;
      First_Operation        : Boolean;

   begin
      --  Lookup for corresponding binding component.

      Lookup_Binding (Service_Node.Interface_Node, Binding_Node);

      if Binding_Node = null then
         --  There is no binding for interface element specified.

         raise Program_Error;
      end if;

      if Binding_Node.Binding_Type /= SOAP_Binding_Type then
         --  This binding type is not supported.

         raise Program_Error;
      end if;

      if Binding_Node.SOAP.Version /= SOAP_Version_12_Literal then
         --  This version of SOAP is not supported.

         raise Program_Error;
      end if;

      --  Compute set of all interface's operations, including inherited.

      Operations := Compute_All_Operations (Service_Node.Interface_Node);

      --  Generate specification of delegation package.

      Interface_Package_Name :=
        "Generic_"
          & Naming_Conventions.To_Ada_Identifier
             (Service_Node.Interface_Node.Local_Name);
      Interface_Type_Name :=
        Naming_Conventions.To_Ada_Identifier
         (Service_Node.Interface_Node.Local_Name);

      Put_Line
       ("--------------------------------------------------------------------"
          & "----------");
      Put_Line
       ("--  This compilation unit was generated by WSDL2Ada translator.");
      Put_Line
       ("--------------------------------------------------------------------"
          & "----------");
      Put_Line ("pragma Style_Checks (""NM32766"");");
      New_Line;
      Put_Line ("with Web_Services.SOAP.Payloads.Faults;");
      Put_Line ("with Payloads;");
      New_Line;
      Put_Line ("generic");

      First_Operation := True;

      for Operation_Node of Operations loop
         if Operation_Node.Message_Exchange_Pattern.IRI /= In_Out_MEP
           and Operation_Node.Message_Exchange_Pattern.IRI /= Robust_In_Only_MEP
         then
            --  Only in-out and robust-in-only MEPs are supported.

            raise Program_Error;
         end if;

         --  All supported MEPs has at most one input and at most one output
         --  placeholder. Lookup for input and output messages to simplify
         --  code.

         Input_Message := null;
         Output_Message := null;

         for Message_Node of Operation_Node.Interface_Message_References loop
            case Message_Node.Direction is
               when WSDL.AST.In_Message =>
                  Input_Message := Message_Node;

               when WSDL.AST.Out_Message =>
                  Output_Message := Message_Node;
            end case;
         end loop;

         if not First_Operation then
            New_Line;

         else
            First_Operation := False;
         end if;

         Put
          ("   with procedure "
             & Naming_Conventions.To_Ada_Identifier
                (Operation_Node.Local_Name));

         --  Generate input parameter, if any.

         if Input_Message /= null then
            if Input_Message.Message_Content_Model
              not in WSDL.AST.None | WSDL.AST.Element
            then
               --  Only '#none' and '#element' message content models are
               --  supported.

               raise Program_Error;
            end if;

            if Input_Message.Message_Content_Model = WSDL.AST.Element then
               New_Line;
               Put
                ("    (Input  : Payloads."
                   & Naming_Conventions.To_Ada_Identifier
                      (Input_Message.Element.Local_Name)
                   & "'Class");
            end if;
         end if;

         --  Generate output parameter, if any.

         if Output_Message /= null then
            if Input_Message /= null
              and then Input_Message.Message_Content_Model = WSDL.AST.Element
            then
               Put_Line (";");
               Put ("     ");

            else
               New_Line;
               Put ("    (");
            end if;

            Put
             ("Output : out Payloads."
                & Naming_Conventions.To_Ada_Identifier
                   (Output_Message.Element.Local_Name)
                & "_Access");
         end if;

         --  Generate output fault parameter, if eny.

         if not Operation_Node.Interface_Fault_References.Is_Empty then
            Put_Line (";");
            Put
             ("     Fault  :"
                & " out Web_Services.SOAP.Payloads.Faults.SOAP_Fault_Access");
         end if;

         Put_Line (");");
      end loop;

      New_Line;
      Put_Line ("package " & Interface_Package_Name & " is");
      New_Line;
      Put_Line ("   procedure Register_Interface;");
      New_Line;
      Put_Line ("end " & Interface_Package_Name & ";");

      --  Generate package for invocation dispatching.

      Put_Line
       ("--------------------------------------------------------------------"
          & "----------");
      Put_Line
       ("--  This compilation unit was generated by WSDL2Ada translator.");
      Put_Line
       ("--------------------------------------------------------------------"
          & "----------");
      Put_Line ("pragma Style_Checks (""NM32766"");");
      New_Line;
      Put_Line ("with League.Strings;");
      Put_Line ("with Web_Services.SOAP.Handler_Registry;");
      Put_Line ("with Web_Services.SOAP.Messages;");
      Put_Line ("with Web_Services.SOAP.Payloads;");
      New_Line;
      Put_Line ("package body " & Interface_Package_Name & " is");
      New_Line;
      Put_Line ("   use type League.Strings.Universal_String;");
      New_Line;
      Put_Line ("   function ""+""");
      Put_Line ("    (Item : Wide_Wide_String)");
      Put_Line ("       return League.Strings.Universal_String");
      Put_Line ("         renames League.Strings.To_Universal_String;");
      New_Line;
      Put_Line ("   procedure Dispatch");
      Put_Line ("    (Input   : Web_Services.SOAP.Messages.SOAP_Message;");
      Put_Line ("     Output  : out Web_Services.SOAP.Messages.SOAP_Message_Access;");
      Put_Line ("     Found   : in out Boolean);");
      New_Line;
      Put_Line ("   --------------");
      Put_Line ("   -- Dispatch --");
      Put_Line ("   --------------");
      New_Line;
      Put_Line ("   procedure Dispatch");
      Put_Line ("    (Input   : Web_Services.SOAP.Messages.SOAP_Message;");
      Put_Line ("     Output  : out Web_Services.SOAP.Messages.SOAP_Message_Access;");
      Put_Line ("     Found   : in out Boolean)");
      Put_Line ("   is");
      Put_Line ("      use type Web_Services.SOAP.Payloads.Faults.SOAP_Fault_Access;");
      New_Line;
      Put_Line ("      Fault : Web_Services.SOAP.Payloads.Faults.SOAP_Fault_Access;");
      New_Line;
      Put_Line ("   begin");

      First_Operation := True;

      for Operation_Node of Operations loop
         if Operation_Node.Message_Exchange_Pattern.IRI /= In_Out_MEP
           and Operation_Node.Message_Exchange_Pattern.IRI /= Robust_In_Only_MEP
         then
            --  Only in-out and robust-in-only MEPs are supported.

            raise Program_Error;
         end if;

         --  All supported MEPs has at most one input and at most one output
         --  placeholder. Lookup for input and output messages to simplify
         --  code.

         Input_Message := null;
         Output_Message := null;

         for Message_Node of Operation_Node.Interface_Message_References loop
            case Message_Node.Direction is
               when WSDL.AST.In_Message =>
                  Input_Message := Message_Node;

               when WSDL.AST.Out_Message =>
                  Output_Message := Message_Node;
            end case;
         end loop;

         SOAP_Action := Compute_SOAP_Action (Operation_Node, Binding_Node);

         if First_Operation then
            Put ("      if");

         else
            New_Line;
            Put ("      elsif");
         end if;

         --  Use SOAP Action to dispatch call when specified.

         if not SOAP_Action.Is_Empty then
            Put_Line (" Input.Action = +""" & SOAP_Action & """ then");

         else
            Put_Line
             (" Input.Namespace_URI = +"""
                & Input_Message.Element.Namespace_URI
                & '"');
            Put_Line
             ("        and Input.Local_Name = +"""
                & Input_Message.Element.Local_Name
                & '"');
            Put_Line ("      then");
         end if;

         First_Operation := False;

         Put_Line ("         declare");

         if Output_Message /= null then
            Put_Line
             ("            Aux : Payloads."
                & Naming_Conventions.To_Ada_Identifier
                   (Output_Message.Element.Local_Name)
                & "_Access;");
            New_Line;
         end if;

         Put_Line ("         begin");
         Put_Line ("            Found := True;");
         Put
          ("            "
             & Naming_Conventions.To_Ada_Identifier
                (Operation_Node.Local_Name));

         --  Generate input parameter, if any.

         if Input_Message /= null then
            if Input_Message.Message_Content_Model
              not in WSDL.AST.None | WSDL.AST.Element
            then
               --  Only '#none' and '#element' message content models are
               --  supported.

               raise Program_Error;
            end if;

            if Input_Message.Message_Content_Model = WSDL.AST.Element then
               New_Line;
               Put ("             (Payloads." 
                   & Naming_Conventions.To_Ada_Identifier
                      (Input_Message.Element.Local_Name)
                   & "'Class (Input.Payload.all)");
            end if;
         end if;

         --  Generate output parameter, if any.

         if Output_Message /= null then
            if Input_Message = null
              or else Input_Message.Message_Content_Model /= WSDL.AST.Element
            then
               New_Line;
               Put ("             (Aux");

            else
               Put_Line (",");
               Put ("              Aux");
            end if;
         end if;

         --  Generate output fault parameter, if eny.

         if not Operation_Node.Interface_Fault_References.Is_Empty then
            Put_Line (",");
            Put ("              Fault");
         end if;

         Put_Line (");");

         if Operation_Node.Message_Exchange_Pattern.IRI = In_Out_MEP then
            Put_Line
             ("            Output :="
                & " new Web_Services.SOAP.Messages.SOAP_Message;");

            if not Operation_Node.Interface_Fault_References.Is_Empty then
               New_Line;
               Put_Line ("            if Fault = null then");
               Put_Line ("               Output.Payload :=");
               Put_Line
                ("                 "
                   & "Web_Services.SOAP.Payloads.SOAP_Payload_Access (Aux);");
               New_Line;
               Put_Line ("            else");
               Put_Line ("               Output.Payload :=");
               Put_Line
                ("                 "
                   & "Web_Services.SOAP.Payloads.SOAP_Payload_Access (Fault);");
               Put_Line ("            end if;");

            else
               Put_Line
                ("            Output.Payload :=");
               Put_Line
                ("              "
                   & "Web_Services.SOAP.Payloads.SOAP_Payload_Access (Aux);");
            end if;

         elsif Operation_Node.Message_Exchange_Pattern.IRI = Robust_In_Only_MEP then
            --  Generate fault handling code when necessary.

            if not Operation_Node.Interface_Fault_References.Is_Empty then
               Put_Line
                ("            Output :="
                   & " new Web_Services.SOAP.Messages.SOAP_Message;");
               Put_Line ("            Output.Payload :=");
               Put_Line
                ("              "
                   & "Web_Services.SOAP.Payloads.SOAP_Payload_Access (Fault);");
            end if;
         end if;

         Put_Line ("         end;");
      end loop;

      Put_Line ("      end if;");

      Put_Line ("   end Dispatch;");
      New_Line;
      Put_Line ("   ------------------------");
      Put_Line ("   -- Register_Interface --");
      Put_Line ("   ------------------------");
      New_Line;
      Put_Line ("   procedure Register_Interface is");
      Put_Line ("   begin");
      Put_Line ("      Web_Services.SOAP.Handler_Registry.Register");
      Put_Line ("       (Dispatch'Unrestricted_Access);");
      Put_Line ("   end Register_Interface;");
      New_Line;
      Put_Line ("end " & Interface_Package_Name & ";");
   end Generate_Service;

   --------------------
   -- Lookup_Binding --
   --------------------

   procedure Lookup_Binding
    (Interface_Node : not null WSDL.AST.Interface_Access;
     Binding_Node   : out WSDL.AST.Binding_Access)
   is
      use type WSDL.AST.Interface_Access;

   begin
      Binding_Node := null;

      for Binding of Interface_Node.Parent.Bindings loop
         --  Use default binding when found. Default value will be overwritten
         --  when interface specific binding will be found.

         if Binding.Interface_Node = null then
            Binding_Node := Binding;
         end if;

         --  Use interface specific binding when found.

         if Binding.Interface_Node = Interface_Node then
            Binding_Node := Binding;

            exit;
         end if;
      end loop;
   end Lookup_Binding;

   ---------
   -- Put --
   ---------

   procedure Put (Item : League.Strings.Universal_String) is
   begin
      Put (Item.To_Wide_Wide_String);
   end Put;

   --------------
   -- Put_Line --
   --------------

   procedure Put_Line (Item : League.Strings.Universal_String) is
   begin
      Put_Line (Item.To_Wide_Wide_String);
   end Put_Line;

end WSDL.Generator;