/usr/share/ada/adainclude/aws/soap-message-xml.adb is in libaws3.2.0-dev 3.2.0-4ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
| 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 1204 1205 1206 1207 1208 1209 | ------------------------------------------------------------------------------
--                              Ada Web Server                              --
--                                                                          --
--                     Copyright (C) 2000-2013, AdaCore                     --
--                                                                          --
--  This library 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 library 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.                    --
--                                                                          --
--                                                                          --
--                                                                          --
--                                                                          --
--                                                                          --
--  You should have received a copy of the GNU General Public License and   --
--  a copy of the GCC Runtime Library Exception along with this program;    --
--  see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see   --
--  <http://www.gnu.org/licenses/>.                                         --
--                                                                          --
--                                                                          --
--                                                                          --
--                                                                          --
--                                                                          --
--                                                                          --
--                                                                          --
------------------------------------------------------------------------------
pragma Ada_2012;
with Ada.Characters.Handling;
with Ada.Exceptions;
with Ada.Strings.Fixed;
with AWS.Client.XML.Input_Sources;
with DOM.Core.Nodes;
with Input_Sources.Strings;
with Sax.Readers;
with Unicode.CES.Utf8;
with SOAP.Message.Reader;
with SOAP.Message.Response.Error;
with SOAP.Name_Space;
with SOAP.Types.Untyped;
with SOAP.Utils;
with SOAP.XML;
package body SOAP.Message.XML is
   use Ada;
   use DOM.Core.Nodes;
   use SOAP.Message.Reader;
   NL : constant String := ASCII.CR & ASCII.LF;
   Max_Object_Size : constant := 2_048;
   --  This is the maximum number of items in a record or an array supported
   --  by this implementation.
   XML_Header : constant String := "<?xml version='1.0' encoding='UTF-8'?>";
   URL_Enc    : constant String := "http://schemas.xmlsoap.org/soap/encoding/";
   URL_Env    : constant String := "http://schemas.xmlsoap.org/soap/envelope/";
   URL_xsd    : constant String := "http://www.w3.org/1999/XMLSchema";
   URL_xsd_01 : constant String := "http://www.w3.org/2001/XMLSchema";
   URL_xsi    : constant String := "http://www.w3.org/1999/XMLSchema-instance";
   URL_xsi_01 : constant String := "http://www.w3.org/2001/XMLSchema-instance";
   --  Name spaces
   NS_SOAP_Env : constant SOAP.Name_Space.Object :=
                   SOAP.Name_Space.Create ("soap", URL_Env);
   NS_SOAP_Enc : constant SOAP.Name_Space.Object :=
                   SOAP.Name_Space.Create ("soapenc", URL_Enc);
   NS_XSD      : constant SOAP.Name_Space.Object :=
                   SOAP.Name_Space.Create ("xsd", URL_xsd);
   NS_XSI      : constant SOAP.Name_Space.Object :=
                   SOAP.Name_Space.Create ("xsi", URL_xsi);
   NS_Enc      : constant SOAP.Name_Space.Object :=
                   SOAP.Name_Space.Create
                     ("encodingStyle", URL_Enc, Prefix => "soap");
   Start_Env   : constant String := "<soap:Envelope";
   End_Env     : constant String := "</soap:Envelope>";
   Header      : constant String :=
                   Start_Env
                     & ' ' & SOAP.Name_Space.Image (NS_Enc)
                     & ' ' & SOAP.Name_Space.Image (NS_SOAP_Enc)
                     & ' ' & SOAP.Name_Space.Image (NS_SOAP_Env)
                     & ' ' & SOAP.Name_Space.Image (NS_XSD)
                     & ' ' & SOAP.Name_Space.Image (NS_XSI)
                     & '>';
   Start_Body : constant String := "<soap:Body>";
   End_Body   : constant String := "</soap:Body>";
   type Type_State is
     (Void, T_Undefined, T_Any_Type,
      T_Int, T_Float, T_Double, T_Long, T_Short, T_Byte,
      T_Unsigned_Long, T_Unsigned_Int, T_Unsigned_Short, T_Unsigned_Byte,
      T_String, T_Boolean, T_Time_Instant, T_Base64);
   type NS_Set is array (1 .. 10) of SOAP.Name_Space.Object;
   type Namespaces is record
      xsd   : SOAP.Name_Space.Object;
      xsi   : SOAP.Name_Space.Object;
      enc   : SOAP.Name_Space.Object;
      User  : NS_Set;
      Index : Natural := 0;
   end record;
   type State is record
      Name_Space   : SOAP.Name_Space.Object;
      Wrapper_Name : Unbounded_String;
      Parameters   : SOAP.Parameters.List;
      A_State      : Type_State := Void;
      NS           : Namespaces;
   end record;
   function To_Type
     (Type_Name : String;
      NS        : Namespaces)
      return Type_State;
   --  Given the Type_Name and the namespaces return the proper type
   procedure Load_XML
     (Input : in out Input_Sources.Input_Source'Class;
      S     : out State);
   --  Load XML document, set State and ensure the document is freed when an
   --  exception occurs. The Input source is closed before returning.
   procedure Parse_Namespaces
     (N  : DOM.Core.Node;
      NS : in out Namespaces);
   --  Read namespaces from node and set NS accordingly
   function Get_Namespace_Value
     (NS   : Namespaces;
      Name : String) return String;
   --  Returns the user's namspace value for the given namespace name
   procedure Parse_Document
     (N : DOM.Core.Node;
      S : in out State);
   procedure Parse_Envelope
     (N : DOM.Core.Node;
      S : in out State);
   procedure Parse_Header
     (N : DOM.Core.Node;
      S : in out State);
   procedure Parse_Body
     (N : DOM.Core.Node;
      S : in out State);
   procedure Parse_Wrapper
     (N : DOM.Core.Node;
      S : in out State);
   --  Parse routines for specific types
   function Parse_Any_Type
     (Name : String;
      N    : DOM.Core.Node) return Types.Object'Class;
   function Parse_Long
     (Name : String;
      N    : DOM.Core.Node) return Types.Object'Class;
   function Parse_Int
     (Name : String;
      N    : DOM.Core.Node) return Types.Object'Class;
   function Parse_Short
     (Name : String;
      N    : DOM.Core.Node) return Types.Object'Class;
   function Parse_Byte
     (Name : String;
      N    : DOM.Core.Node) return Types.Object'Class;
   function Parse_Float
     (Name : String;
      N    : DOM.Core.Node) return Types.Object'Class;
   function Parse_Double
     (Name : String;
      N    : DOM.Core.Node) return Types.Object'Class;
   function Parse_String
     (Name : String;
      N    : DOM.Core.Node) return Types.Object'Class;
   function Parse_Boolean
     (Name : String;
      N    : DOM.Core.Node) return Types.Object'Class;
   function Parse_Base64
     (Name : String;
      N    : DOM.Core.Node) return Types.Object'Class;
   function Parse_Unsigned_Long
     (Name : String;
      N    : DOM.Core.Node) return Types.Object'Class;
   function Parse_Unsigned_Int
     (Name : String;
      N    : DOM.Core.Node) return Types.Object'Class;
   function Parse_Unsigned_Short
     (Name : String;
      N    : DOM.Core.Node) return Types.Object'Class;
   function Parse_Unsigned_Byte
     (Name : String;
      N    : DOM.Core.Node) return Types.Object'Class;
   function Parse_Untyped
     (Name : String;
      N    : DOM.Core.Node) return Types.Object'Class;
   --  Parse a node whose type is unknown. This is used to workaround the fact
   --  that AWS parser is not aware of the WSDL schema.
   function Parse_Time_Instant
     (Name : String;
      N    : DOM.Core.Node) return Types.Object'Class;
   function Parse_Param
     (N : DOM.Core.Node;
      S : State) return Types.Object'Class;
   function Parse_Array
     (Name : String;
      N    : DOM.Core.Node;
      S    : State) return Types.Object'Class;
   function Parse_Record
     (Name : String;
      N    : DOM.Core.Node;
      S    : State) return Types.Object'Class;
   function Parse_Enumeration
     (Name : String;
      N    : DOM.Core.Node) return Types.Object'Class;
   procedure Error (Node : DOM.Core.Node; Message : String) with No_Return;
   --  Raises SOAP_Error with the Message as exception message
   type Parse_Type is access
     function (Name : String;
               N    : DOM.Core.Node) return Types.Object'Class;
   type Type_Handler is record
      Name    : access constant String;
      Handler : Parse_Type;
      Encoded : Boolean; --  True if based on soap-enc namespaces
   end record;
   Handlers : constant array (Type_State) of Type_Handler :=
                (Void           =>
                   (null, null, False),
                 T_Undefined    =>
                   (Types.XML_Undefined'Access, null, False),
                 T_Any_Type      =>
                   (Types.XML_Any_Type'Access, Parse_Any_Type'Access, False),
                 T_Long          =>
                   (Types.XML_Long'Access, Parse_Long'Access, False),
                 T_Int            =>
                   (Types.XML_Int'Access, Parse_Int'Access, False),
                 T_Short          =>
                   (Types.XML_Short'Access, Parse_Short'Access, False),
                 T_Byte           =>
                   (Types.XML_Byte'Access, Parse_Byte'Access, False),
                 T_Float          =>
                   (Types.XML_Float'Access, Parse_Float'Access, False),
                 T_Double         =>
                   (Types.XML_Double'Access, Parse_Double'Access, False),
                 T_String         =>
                   (Types.XML_String'Access, Parse_String'Access, False),
                 T_Boolean        =>
                   (Types.XML_Boolean'Access, Parse_Boolean'Access, False),
                 T_Base64         =>
                   (Types.XML_Base64'Access, Parse_Base64'Access, True),
                 T_Unsigned_Long  =>
                   (Types.XML_Unsigned_Long'Access,
                    Parse_Unsigned_Long'Access, False),
                 T_Unsigned_Int   =>
                   (Types.XML_Unsigned_Int'Access,
                    Parse_Unsigned_Int'Access, False),
                 T_Unsigned_Short =>
                   (Types.XML_Unsigned_Short'Access,
                    Parse_Unsigned_Short'Access, False),
                 T_Unsigned_Byte  =>
                   (Types.XML_Unsigned_Byte'Access,
                    Parse_Unsigned_Byte'Access, False),
                 T_Time_Instant   =>
                   (Types.XML_Time_Instant'Access,
                    Parse_Time_Instant'Access, False));
   -----------
   -- Error --
   -----------
   procedure Error (Node : DOM.Core.Node; Message : String) is
      Name : constant String := Local_Name (Node);
   begin
      raise SOAP_Error with Name & " - " & Message;
   end Error;
   -------------------------
   -- Get_Namespace_Value --
   -------------------------
   function Get_Namespace_Value
     (NS   : Namespaces;
      Name : String) return String is
   begin
      for K in 1 .. NS.Index loop
         if SOAP.Name_Space.Name (NS.User (K)) = Name then
            return SOAP.Name_Space.Value (NS.User (K));
         end if;
      end loop;
      return "";
   end Get_Namespace_Value;
   -----------
   -- Image --
   -----------
   function Image (O : Object'Class) return String is
   begin
      return To_String (XML.Image (O));
   end Image;
   -----------
   -- Image --
   -----------
   function Image (O : Object'Class) return Unbounded_String is
      Message_Body : Unbounded_String;
   begin
      --  Header
      Append (Message_Body, XML_Header & NL);
      Append (Message_Body, Header & NL);
      --  Body
      Append (Message_Body, Start_Body & NL);
      --  Wrapper
      Append (Message_Body, Message.XML_Image (O));
      --  End of Body and Envelope
      Append (Message_Body, End_Body & NL);
      Append (Message_Body, End_Env & NL);
      return Message_Body;
   end Image;
   ------------------
   -- Load_Payload --
   ------------------
   function Load_Payload (XML : String) return Message.Payload.Object is
      use Input_Sources.Strings;
      Str    : aliased String := XML;
      Source : String_Input;
      S      : State;
   begin
      Open (Str'Unchecked_Access,
            Unicode.CES.Utf8.Utf8_Encoding,
            Source);
      Load_XML (Source, S);
      Close (Source);
      return Message.Payload.Build
        (To_String (S.Wrapper_Name), S.Parameters, S.Name_Space);
   end Load_Payload;
   -------------------
   -- Load_Response --
   -------------------
   function Load_Response
     (Connection : AWS.Client.HTTP_Connection)
      return Message.Response.Object'Class
   is
      use AWS.Client.XML.Input_Sources;
      Source : HTTP_Input;
      S      : State;
   begin
      Create (Connection, Source);
      Load_XML (Source, S);
      Close (Source);
      if SOAP.Parameters.Exist (S.Parameters, "faultcode") then
         return Message.Response.Error.Build
           (Faultcode   =>
              Message.Response.Error.Faultcode
               (String'(SOAP.Parameters.Get (S.Parameters, "faultcode"))),
            Faultstring => SOAP.Parameters.Get (S.Parameters, "faultstring"));
      else
         return Message.Response.Object'
           (Message.Object'(S.Name_Space, S.Wrapper_Name, S.Parameters)
            with null record);
      end if;
   exception
      when E : others =>
         return Message.Response.Error.Build
           (Faultcode   => Message.Response.Error.Client,
            Faultstring => Exceptions.Exception_Message (E));
   end Load_Response;
   function Load_Response
     (XML : String) return Message.Response.Object'Class
   is
      use Input_Sources.Strings;
      Source : String_Input;
      S      : State;
   begin
      Open (XML'Unrestricted_Access,
            Unicode.CES.Utf8.Utf8_Encoding,
            Source);
      Load_XML (Source, S);
      Close (Source);
      if SOAP.Parameters.Exist (S.Parameters, "faultcode") then
         return Message.Response.Error.Build
           (Faultcode   =>
              Message.Response.Error.Faultcode
               (String'(SOAP.Parameters.Get (S.Parameters, "faultcode"))),
            Faultstring => SOAP.Parameters.Get (S.Parameters, "faultstring"));
      else
         return Message.Response.Object'
           (Message.Object'(S.Name_Space, S.Wrapper_Name, S.Parameters)
            with null record);
      end if;
   exception
      when E : others =>
         return Message.Response.Error.Build
           (Faultcode   => Message.Response.Error.Client,
            Faultstring => Exceptions.Exception_Message (E));
   end Load_Response;
   function Load_Response
     (XML : Unbounded_String) return Message.Response.Object'Class
   is
      S : String_Access := new String (1 .. Length (XML));
   begin
      --  Copy XML content to local S string
      for I in 1 .. Length (XML) loop
         S (I) := Element (XML, I);
      end loop;
      declare
         Result : constant Message.Response.Object'Class
           := Load_Response (S.all);
      begin
         Free (S);
         return Result;
      end;
   end Load_Response;
   --------------
   -- Load_XML --
   --------------
   procedure Load_XML
     (Input : in out Input_Sources.Input_Source'Class;
      S     : out State)
   is
      Reader : Tree_Reader;
      Doc    : DOM.Core.Document;
   begin
      --  If True, xmlns:* attributes will be reported in Start_Element
      Set_Feature (Reader, Sax.Readers.Namespace_Prefixes_Feature, True);
      Set_Feature (Reader, Sax.Readers.Validation_Feature, False);
      Parse (Reader, Input);
      Doc := Get_Tree (Reader);
      Parse_Document (Doc, S);
      Free (Doc);
   exception
      when others =>
      Doc := Get_Tree (Reader);
      Free (Doc);
      raise;
   end Load_XML;
   --------------------
   -- Parse_Any_Type --
   --------------------
   function Parse_Any_Type
     (Name : String;
      N    : DOM.Core.Node) return Types.Object'Class is
   begin
      --  ??? We have no type information, in this implementation we map the
      --  value into a xsd:string.
      return Parse_String (Name, N);
   end Parse_Any_Type;
   -----------------
   -- Parse_Array --
   -----------------
   function Parse_Array
     (Name : String;
      N    : DOM.Core.Node;
      S    : State) return Types.Object'Class
   is
      use SOAP.Types;
      use type DOM.Core.Node;
      function Item_Type (Name : String) return String with Inline;
      --  Returns the array's item type, remove [] if present
      LS : State := S;
      ---------------
      -- Item_Type --
      ---------------
      function Item_Type (Name : String) return String is
         N : constant Positive := Strings.Fixed.Index (Name, "[");
      begin
         return Name (Name'First .. N - 1);
      end Item_Type;
      OS    : Types.Object_Set (1 .. Max_Object_Size);
      K     : Natural := 0;
      Field : DOM.Core.Node;
      Atts  : constant DOM.Core.Named_Node_Map := Attributes (N);
   begin
      Parse_Namespaces (N, LS.NS);
      declare
         A_Name    : constant String :=
                       SOAP.Name_Space.Name (LS.NS.enc) & ":arrayType";
         --  Attribute name
         Type_Name : constant String :=
                       Item_Type (Node_Value (Get_Named_Item (Atts, A_Name)));
         A_Type    : constant Type_State := To_Type (Type_Name, LS.NS);
      begin
         Field := SOAP.XML.First_Child (N);
         while Field /= null loop
            K := K + 1;
            OS (K) := +Parse_Param
              (Field,
               (S.Name_Space, S.Wrapper_Name, S.Parameters, A_Type, S.NS));
            Field := Next_Sibling (Field);
         end loop;
         return Types.A
           (OS (1 .. K), Name, Utils.With_NS ("awsns", Type_Name));
      end;
   end Parse_Array;
   ------------------
   -- Parse_Base64 --
   ------------------
   function Parse_Base64
     (Name : String;
      N    : DOM.Core.Node) return Types.Object'Class
   is
      use type DOM.Core.Node;
      Value : DOM.Core.Node;
   begin
      Normalize (N);
      Value := First_Child (N);
      if Value = null then
         --  No node found, this is an empty Base64 content
         return Types.B64 ("", Name);
      else
         return Types.B64 (Node_Value (Value), Name);
      end if;
   end Parse_Base64;
   ----------------
   -- Parse_Body --
   ----------------
   procedure Parse_Body (N : DOM.Core.Node; S : in out State) is
   begin
      Parse_Wrapper (SOAP.XML.First_Child (N), S);
   end Parse_Body;
   -------------------
   -- Parse_Boolean --
   -------------------
   function Parse_Boolean
     (Name : String;
      N    : DOM.Core.Node) return Types.Object'Class
   is
      Value : constant DOM.Core.Node := First_Child (N);
   begin
      if Node_Value (Value) = "1"
        or else Node_Value (Value) = "true"
        or else Node_Value (Value) = "TRUE"
      then
         return Types.B (True, Name);
      else
         --  ??? we should check for wrong boolean value
         return Types.B (False, Name);
      end if;
   end Parse_Boolean;
   ----------------
   -- Parse_Byte --
   ----------------
   function Parse_Byte
     (Name : String;
      N    : DOM.Core.Node) return Types.Object'Class
   is
      Value : constant DOM.Core.Node := First_Child (N);
   begin
      return Types.B (Types.Byte'Value (Node_Value (Value)), Name);
   end Parse_Byte;
   --------------------
   -- Parse_Document --
   --------------------
   procedure Parse_Document (N : DOM.Core.Node; S : in out State) is
      NL : constant DOM.Core.Node_List := Child_Nodes (N);
   begin
      if Length (NL) = 1 then
         Parse_Envelope (SOAP.XML.First_Child (N), S);
      else
         Error (N, "Document must have a single node, found "
                & Natural'Image (Length (NL)));
      end if;
   end Parse_Document;
   ------------------
   -- Parse_Double --
   ------------------
   function Parse_Double
     (Name : String;
      N    : DOM.Core.Node) return Types.Object'Class
   is
      Value : constant DOM.Core.Node := First_Child (N);
   begin
      return Types.D (Long_Float'Value (Node_Value (Value)), Name);
   end Parse_Double;
   -----------------------
   -- Parse_Enumeration --
   -----------------------
   function Parse_Enumeration
     (Name : String;
      N    : DOM.Core.Node) return Types.Object'Class is
   begin
      return Types.E
        (Node_Value (First_Child (N)),
         Utils.No_NS (SOAP.XML.Get_Attr_Value (N, "type")),
         Name);
   end Parse_Enumeration;
   --------------------
   -- Parse_Envelope --
   --------------------
   procedure Parse_Envelope (N : DOM.Core.Node; S : in out State) is
      NL : constant DOM.Core.Node_List := Child_Nodes (N);
      LS : State := S;
   begin
      Parse_Namespaces (N, LS.NS);
      if Length (NL) = 1 then
         --  This must be the body
         Parse_Body (SOAP.XML.First_Child (N), LS);
      elsif Length (NL) = 2 then
         --  The first child must the header tag
         Parse_Header (SOAP.XML.First_Child (N), LS);
         --  The second child must be the body
         Parse_Body (SOAP.XML.Next_Sibling (First_Child (N)), LS);
      else
         Error (N, "Envelope must have at most two nodes, found "
                & Natural'Image (Length (NL)));
      end if;
      S := LS;
   end Parse_Envelope;
   -----------------
   -- Parse_Float --
   -----------------
   function Parse_Float
     (Name : String;
      N    : DOM.Core.Node) return Types.Object'Class
   is
      Value : constant DOM.Core.Node := First_Child (N);
   begin
      return Types.F (Float'Value (Node_Value (Value)), Name);
   end Parse_Float;
   ------------------
   -- Parse_Header --
   ------------------
   procedure Parse_Header (N : DOM.Core.Node; S : in out State) is
      pragma Unreferenced (S);
      Name : constant String := Local_Name (N);
   begin
      if Ada.Characters.Handling.To_Lower (Name) /= "header" then
         Error (N, "Header node expected, found " & Name);
      end if;
   end Parse_Header;
   ---------------
   -- Parse_Int --
   ---------------
   function Parse_Int
     (Name : String;
      N    : DOM.Core.Node) return Types.Object'Class
   is
      Value : constant DOM.Core.Node := First_Child (N);
   begin
      return Types.I (Integer'Value (Node_Value (Value)), Name);
   end Parse_Int;
   ----------------
   -- Parse_Long --
   ----------------
   function Parse_Long
     (Name : String;
      N    : DOM.Core.Node) return Types.Object'Class
   is
      Value : constant DOM.Core.Node := First_Child (N);
   begin
      return Types.L (Types.Long'Value (Node_Value (Value)), Name);
   end Parse_Long;
   ----------------------
   -- Parse_Namespaces --
   ----------------------
   procedure Parse_Namespaces
     (N  : DOM.Core.Node;
      NS : in out Namespaces)
   is
      Atts : constant DOM.Core.Named_Node_Map := Attributes (N);
   begin
      for K in 0 .. Length (Atts) - 1 loop
         declare
            N     : constant DOM.Core.Node := Item (Atts, K);
            Name  : constant String        := Node_Name (N);
            Value : constant String        := Node_Value (N);
         begin
            if Utils.NS (Name) = "xmlns" then
               if Value = URL_xsd or else Value = URL_xsd_01 then
                  NS.xsd := SOAP.Name_Space.Create (Utils.No_NS (Name), Value);
               elsif Value = URL_xsi or else Value = URL_xsi_01 then
                  NS.xsi := SOAP.Name_Space.Create (Utils.No_NS (Name), Value);
               elsif Value = URL_Enc then
                  NS.enc := SOAP.Name_Space.Create (Utils.No_NS (Name), Value);
               elsif NS.Index < NS.User'Last then
                  NS.Index := NS.Index + 1;
                  NS.User (NS.Index) :=
                    SOAP.Name_Space.Create (Utils.No_NS (Name), Value);
               end if;
            end if;
         end;
      end loop;
   end Parse_Namespaces;
   -----------------
   -- Parse_Param --
   -----------------
   function Parse_Param
     (N : DOM.Core.Node;
      S : State) return Types.Object'Class
   is
      use type DOM.Core.Node;
      use type DOM.Core.Node_Types;
      function Is_Array return Boolean;
      --  Returns True if N is an array node
      Name : constant String                  := Local_Name (N);
      Ref  : constant DOM.Core.Node           := SOAP.XML.Get_Ref (N);
      Atts : constant DOM.Core.Named_Node_Map := Attributes (Ref);
      LS   : State := S;
      --------------
      -- Is_Array --
      --------------
      function Is_Array return Boolean is
         XSI_Type : constant DOM.Core.Node :=
                      Get_Named_Item
                        (Atts, SOAP.Name_Space.Name (LS.NS.xsi) & ":type");
         SOAP_Enc : constant DOM.Core.Node :=
                      Get_Named_Item
                        (Atts,
                         SOAP.Name_Space.Name (LS.NS.enc) & ":arrayType");
      begin
         return
         --  Either we have xsi:type="soapenc:Array"
           (XSI_Type /= null
            and then Utils.No_NS (Node_Value (XSI_Type)) = "Array")
           or else
         --  or soapenc:arrayType="..."
             SOAP_Enc /= null;
      end Is_Array;
      S_Type   : constant DOM.Core.Node := Get_Named_Item (Atts, "type");
      XSI_Type : DOM.Core.Node;
   begin
      Parse_Namespaces (Ref, LS.NS);
      XSI_Type :=
        Get_Named_Item (Atts, SOAP.Name_Space.Name (LS.NS.xsi) & ":type");
      if To_String (S.Wrapper_Name) = "Fault" then
         return Parse_String (Name, Ref);
      elsif Is_Array then
         return Parse_Array (Name, Ref, LS);
      else
         if XSI_Type = null
           and then S.A_State in Void .. T_Undefined
         then
            --  No xsi:type attribute found
            if Get_Named_Item
              (Atts, SOAP.Name_Space.Name (LS.NS.xsi) & ":null") /= null
            then
               return Types.N (Name);
            elsif S_Type /= null
              and then First_Child (Ref).Node_Type = DOM.Core.Text_Node
            then
               --  Not xsi:type but a type information, the child being a text
               --  node, this is an enumeration.
               return Parse_Enumeration (Name, Ref);
            elsif First_Child (Ref) = null then
               --  We have an emtpy node, return an empty string in this case
               --  otherwise we will create an empty record in the else section
               --  below.
               return Types.Untyped.S ("", Name);
            elsif First_Child (Ref).Node_Type = DOM.Core.Text_Node then
               --  No xsi:type and no type information. Children are some kind
               --  of text data, so this is a data node with no type
               --  information. Note that this code is to workaround an
               --  interoperability problem found with gSOAP and Microsoft SOAP
               --  implementation based on WSDL were the type information is
               --  not provided into the payload but only on the WSDL file. As
               --  AWS/SOAP parser is not WSDL compliant at this point we
               --  record such type as undefined. Later the value will be
               --  converted to to right type when read by one of the
               --  Types.Get routine. Note that this code is only there to
               --  parse data received from a SOAP server. AWS/SOAP always send
               --  type information into the payload.
               return Parse_Untyped (Name, Ref);
            else
               --  This is a type defined in a schema, either a SOAP record
               --  or an enumeration, enumerations will be checked into
               --  Parse record.
               --  This is a SOAP record, we have no attribute and no
               --  type defined. We have a single tag "<name>" which can
               --  only be the start or a record.
               return Parse_Record (Name, Ref, LS);
            end if;
         else
            if S.A_State in Void .. T_Any_Type then
               --  No array type state
               declare
                  xsd    : constant String     := Node_Value (XSI_Type);
                  S_Type : constant Type_State := To_Type (xsd, LS.NS);
               begin
                  if S_Type = T_Undefined then
                     --  Not a known basic type, let's try to parse a
                     --  record object. This implemtation does not
                     --  support schema so there is no way to check
                     --  for the real type here.
                     return Parse_Record (Name, Ref, LS);
                  else
                     return Handlers (S_Type).Handler (Name, Ref);
                  end if;
               end;
            else
               return Handlers (S.A_State).Handler (Name, Ref);
            end if;
         end if;
      end if;
   end Parse_Param;
   ------------------
   -- Parse_Record --
   ------------------
   function Parse_Record
     (Name : String;
      N    : DOM.Core.Node;
      S    : State) return Types.Object'Class
   is
      use SOAP.Types;
      use type DOM.Core.Node;
      use type DOM.Core.Node_Types;
      OS    : Types.Object_Set (1 .. Max_Object_Size);
      K     : Natural := 0;
      Field : DOM.Core.Node := SOAP.XML.Get_Ref (N);
   begin
      if Name /= Local_Name (N)
        and then First_Child (Field).Node_Type = DOM.Core.Text_Node
      then
         --  This is not a record after all, it is an enumeration with an href
         --  A record can't have a text child node.
         return Types.E
           (Node_Value (First_Child (Field)),
            Utils.No_NS (SOAP.XML.Get_Attr_Value
                           (Field, SOAP.Name_Space.Name (S.NS.xsi) & ":type")),
            Name);
      else
         Field := SOAP.XML.First_Child (Field);
         while Field /= null loop
            K := K + 1;
            OS (K) := +Parse_Param (Field, S);
            Field := Next_Sibling (Field);
         end loop;
         return Types.R (OS (1 .. K), Name);
      end if;
   end Parse_Record;
   -----------------
   -- Parse_Short --
   -----------------
   function Parse_Short
     (Name : String;
      N    : DOM.Core.Node) return Types.Object'Class
   is
      Value : constant DOM.Core.Node := First_Child (N);
   begin
      return Types.S (Types.Short'Value (Node_Value (Value)), Name);
   end Parse_Short;
   ------------------
   -- Parse_String --
   ------------------
   function Parse_String
     (Name : String;
      N    : DOM.Core.Node) return Types.Object'Class
   is
      use type DOM.Core.Node;
      use type DOM.Core.Node_Types;
      L : constant DOM.Core.Node_List := Child_Nodes (N);
      S : Unbounded_String;
      P : DOM.Core.Node;
   begin
      for I in 0 .. Length (L) - 1 loop
         P := Item (L, I);
         if P.Node_Type = DOM.Core.Text_Node then
            Append (S, Node_Value (P));
         end if;
      end loop;
      return Types.S (S, Name);
   end Parse_String;
   ------------------------
   -- Parse_Time_Instant --
   ------------------------
   function Parse_Time_Instant
     (Name : String;
      N    : DOM.Core.Node) return Types.Object'Class
   is
      Value : constant DOM.Core.Node := First_Child (N);
      TI    : constant String        := Node_Value (Value);
   begin
      return Utils.Time_Instant (TI, Name);
   end Parse_Time_Instant;
   -------------------------
   -- Parse_Unsigned_Byte --
   -------------------------
   function Parse_Unsigned_Byte
     (Name : String;
      N    : DOM.Core.Node) return Types.Object'Class
   is
      Value : constant DOM.Core.Node := First_Child (N);
   begin
      return Types.UB (Types.Unsigned_Byte'Value (Node_Value (Value)), Name);
   end Parse_Unsigned_Byte;
   ------------------------
   -- Parse_Unsigned_Int --
   ------------------------
   function Parse_Unsigned_Int
     (Name : String;
      N    : DOM.Core.Node) return Types.Object'Class
   is
      Value : constant DOM.Core.Node := First_Child (N);
   begin
      return Types.UI (Types.Unsigned_Int'Value (Node_Value (Value)), Name);
   end Parse_Unsigned_Int;
   -------------------------
   -- Parse_Unsigned_Long --
   -------------------------
   function Parse_Unsigned_Long
     (Name : String;
      N    : DOM.Core.Node) return Types.Object'Class
   is
      Value : constant DOM.Core.Node := First_Child (N);
   begin
      return Types.UL (Types.Unsigned_Long'Value (Node_Value (Value)), Name);
   end Parse_Unsigned_Long;
   --------------------------
   -- Parse_Unsigned_Short --
   --------------------------
   function Parse_Unsigned_Short
     (Name : String;
      N    : DOM.Core.Node) return Types.Object'Class
   is
      Value : constant DOM.Core.Node := First_Child (N);
   begin
      return Types.US (Types.Unsigned_Short'Value (Node_Value (Value)), Name);
   end Parse_Unsigned_Short;
   -------------------
   -- Parse_Untyped --
   -------------------
   function Parse_Untyped
     (Name : String;
      N    : DOM.Core.Node) return Types.Object'Class
   is
      use type DOM.Core.Node;
      use type DOM.Core.Node_Types;
      L : constant DOM.Core.Node_List := Child_Nodes (N);
      S : Unbounded_String;
      P : DOM.Core.Node;
   begin
      for I in 0 .. Length (L) - 1 loop
         P := Item (L, I);
         if P.Node_Type = DOM.Core.Text_Node then
            Append (S, Node_Value (P));
         end if;
      end loop;
      return Types.Untyped.S (S, Name);
   end Parse_Untyped;
   -------------------
   -- Parse_Wrapper --
   -------------------
   procedure Parse_Wrapper (N : DOM.Core.Node; S : in out State) is
      use type DOM.Core.Node_Types;
      use type SOAP.Parameters.List;
      NL     : constant DOM.Core.Node_List      := Child_Nodes (N);
      Prefix : constant String                  := DOM.Core.Nodes.Prefix (N);
      Name   : constant String                  := Local_Name (N);
      Atts   : constant DOM.Core.Named_Node_Map := Attributes (N);
      LS     : State := S;
   begin
      Parse_Namespaces (N, LS.NS);
      if Prefix /= "" then
         --  The wrapper has a prefix
         if Length (Atts) /= 0 then
            declare
               use type DOM.Core.Node;
               xmlns : constant DOM.Core.Node :=
                         Get_Named_Item (Atts, "xmlns:" & Prefix);
            begin
               if xmlns /= null then
                  S.Name_Space :=
                    SOAP.Name_Space.Create (Prefix, Node_Value (xmlns));
               end if;
            end;
         else
            --  There is no attribute for this node, yet the wrapper is using
            --  a name space.
            S.Name_Space :=
              SOAP.Name_Space.Create
                (Prefix, Get_Namespace_Value (S.NS, Prefix));
         end if;
      end if;
      S.Wrapper_Name := To_Unbounded_String (Name);
      for K in 0 .. Length (NL) - 1 loop
         if Item (NL, K).Node_Type /= DOM.Core.Text_Node then
            S.Parameters := S.Parameters & Parse_Param (Item (NL, K), LS);
         end if;
      end loop;
   end Parse_Wrapper;
   -------------
   -- To_Type --
   -------------
   function To_Type
     (Type_Name : String;
      NS        : Namespaces) return Type_State
   is
      function Is_A
        (T1_Name, T2_Name : String;
         NS               : String) return Boolean
      is (T1_Name = Utils.With_NS (NS, T2_Name)) with Inline;
      --  Returns True if T1_Name is equal to T2_Name based on namespace
   begin
      for K in Handlers'Range loop
         if Handlers (K).Name /= null
           and then
             ((Handlers (K).Encoded
               and then Is_A (Type_Name, Handlers (K).Name.all,
                              SOAP.Name_Space.Name (NS.enc)))
              or else Is_A (Type_Name, Handlers (K).Name.all,
                            SOAP.Name_Space.Name (NS.xsd)))
         then
            return K;
         end if;
      end loop;
      return T_Undefined;
   end To_Type;
end SOAP.Message.XML;
 |