matreshka_league_21.0.0_0c8f4d47/source/xml/catalogs/matreshka-xml_catalogs-handlers.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
------------------------------------------------------------------------------
--                                                                          --
--                            Matreshka Project                             --
--                                                                          --
--                               XML Processor                              --
--                                                                          --
--                        Runtime Library Component                         --
--                                                                          --
------------------------------------------------------------------------------
--                                                                          --
-- Copyright © 2011-2012, 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: 3157 $ $Date: 2012-10-08 21:07:31 +0400 (Пн, 08 окт 2012) $
------------------------------------------------------------------------------
with Ada.Wide_Wide_Text_IO;

with League.IRIs;
with Matreshka.XML_Catalogs.Normalization;

package body Matreshka.XML_Catalogs.Handlers is

   use Matreshka.XML_Catalogs.Entry_Files;
   use type League.Strings.Universal_String;

   XML_Catalogs_Namespace     : constant League.Strings.Universal_String
     := League.Strings.To_Universal_String
         ("urn:oasis:names:tc:entity:xmlns:xml:catalog");
   XML_Catalogs_1_0_Public_Id : constant League.Strings.Universal_String
     := League.Strings.To_Universal_String
         ("-//OASIS//DTD Entity Resolution XML Catalog V1.0//EN");
   XML_Catalogs_1_1_Public_Id : constant League.Strings.Universal_String
     := League.Strings.To_Universal_String
         ("-//OASIS//DTD XML Catalogs V1.1//EN");

   --  Tags names

   Catalog_Tag_Name           : constant League.Strings.Universal_String
     := League.Strings.To_Universal_String ("catalog");
   Delegate_Public_Tag_Name   : constant League.Strings.Universal_String
     := League.Strings.To_Universal_String ("delegatePublic");
   Delegate_System_Tag_Name   : constant League.Strings.Universal_String
     := League.Strings.To_Universal_String ("delegateSystem");
   Delegate_URI_Tag_Name      : constant League.Strings.Universal_String
     := League.Strings.To_Universal_String ("delegateURI");
   Group_Tag_Name             : constant League.Strings.Universal_String
     := League.Strings.To_Universal_String ("group");
   Next_Catalog_Tag_Name      : constant League.Strings.Universal_String
     := League.Strings.To_Universal_String ("nextCatalog");
   Public_Tag_Name            : constant League.Strings.Universal_String
     := League.Strings.To_Universal_String ("public");
   Rewrite_System_Tag_Name    : constant League.Strings.Universal_String
     := League.Strings.To_Universal_String ("rewriteSystem");
   Rewrite_URI_Tag_Name       : constant League.Strings.Universal_String
     := League.Strings.To_Universal_String ("rewriteURI");
   System_Tag_Name            : constant League.Strings.Universal_String
     := League.Strings.To_Universal_String ("system");
   System_Suffix_Tag_Name     : constant League.Strings.Universal_String
     := League.Strings.To_Universal_String ("systemSuffix");
   URI_Tag_Name               : constant League.Strings.Universal_String
     := League.Strings.To_Universal_String ("uri");
   URI_Suffix_Tag_Name        : constant League.Strings.Universal_String
     := League.Strings.To_Universal_String ("uriSuffix");

   --  Attributes names

   Catalog_Attribute_Name          : constant League.Strings.Universal_String
     := League.Strings.To_Universal_String ("catalog");
   Name_Attribute_Name             : constant League.Strings.Universal_String
     := League.Strings.To_Universal_String ("name");
   Prefer_Attribute_Name           : constant League.Strings.Universal_String
     := League.Strings.To_Universal_String ("prefer");
   Public_Id_Attribute_Name        : constant League.Strings.Universal_String
     := League.Strings.To_Universal_String ("publicId");
   Public_Id_Start_String_Attribute_Name :
     constant League.Strings.Universal_String
       := League.Strings.To_Universal_String ("publicIdStartString");
   Rewrite_Prefix_Attribute_Name   : constant League.Strings.Universal_String
     := League.Strings.To_Universal_String ("rewritePrefix");
   System_Id_Attribute_Name        : constant League.Strings.Universal_String
     := League.Strings.To_Universal_String ("systemId");
   System_Id_Start_String_Attribute_Name :
     constant League.Strings.Universal_String
       := League.Strings.To_Universal_String ("systemIdStartString");
   System_Id_Suffix_Attribute_Name : constant League.Strings.Universal_String
       := League.Strings.To_Universal_String ("systemIdSuffix");
   URI_Attribute_Name              : constant League.Strings.Universal_String
     := League.Strings.To_Universal_String ("uri");
   URI_Start_String_Attribute_Name : constant League.Strings.Universal_String
     := League.Strings.To_Universal_String ("uriStartString");
   URI_Suffix_Attribute_Name       : constant League.Strings.Universal_String
     := League.Strings.To_Universal_String ("uriSuffix");

   --  Attribute values images

   System_Image : constant League.Strings.Universal_String
     := League.Strings.To_Universal_String ("system");
   Public_Image : constant League.Strings.Universal_String
     := League.Strings.To_Universal_String ("public");

   procedure Error
    (Self    : in out XML_Catalog_Handler'Class;
     Message : League.Strings.Universal_String;
     Success : out Boolean);
   --  Sets Success to False and save Message as last error message.

   procedure Error
    (Self    : in out XML_Catalog_Handler'Class;
     Message : Wide_Wide_String;
     Success : out Boolean);
   --  Sets Success to False and save Message as last error message.

   procedure Check_No_Fragment_Identifier
    (Self    : in out XML_Catalog_Handler'Class;
     URI     : League.Strings.Universal_String;
     Success : in out Boolean);
   --  Check that URI is not include fragment identifier.

   procedure Process_Catalog_Start_Element
    (Self       : in out XML_Catalog_Handler'Class;
     Attributes : XML.SAX.Attributes.SAX_Attributes;
     Success    : in out Boolean);
   --  Processes start of "catalog" element.

   procedure Process_Group_Start_Element
    (Self       : in out XML_Catalog_Handler'Class;
     Attributes : XML.SAX.Attributes.SAX_Attributes;
     Success    : in out Boolean);
   --  Processes start of "group" element.

   procedure Process_Group_End_Element
    (Self    : in out XML_Catalog_Handler'Class;
     Success : in out Boolean);
   --  Processes end of "group" element.

   procedure Process_Public_Start_Element
    (Self       : in out XML_Catalog_Handler'Class;
     Attributes : XML.SAX.Attributes.SAX_Attributes;
     Success    : in out Boolean);
   --  Processes start of "public" element.

   procedure Process_System_Start_Element
    (Self       : in out XML_Catalog_Handler'Class;
     Attributes : XML.SAX.Attributes.SAX_Attributes;
     Success    : in out Boolean);
   --  Processes start of "system" element.

   procedure Process_Rewrite_System_Start_Element
    (Self       : in out XML_Catalog_Handler'Class;
     Attributes : XML.SAX.Attributes.SAX_Attributes;
     Success    : in out Boolean);
   --  Processes start of "rewriteSystem" element.

   procedure Process_System_Suffix_Start_Element
    (Self       : in out XML_Catalog_Handler'Class;
     Attributes : XML.SAX.Attributes.SAX_Attributes;
     Success    : in out Boolean);
   --  Processes start of "systemSuffix" element.

   procedure Process_Delegate_Public_Start_Element
    (Self       : in out XML_Catalog_Handler'Class;
     Attributes : XML.SAX.Attributes.SAX_Attributes;
     Success    : in out Boolean);
   --  Processes start of "delegatePublic" element.

   procedure Process_Delegate_System_Start_Element
    (Self       : in out XML_Catalog_Handler'Class;
     Attributes : XML.SAX.Attributes.SAX_Attributes;
     Success    : in out Boolean);
   --  Processes start of "delegateSystem" element.

   procedure Process_URI_Start_Element
    (Self       : in out XML_Catalog_Handler'Class;
     Attributes : XML.SAX.Attributes.SAX_Attributes;
     Success    : in out Boolean);
   --  Processes start of "uri" element.

   procedure Process_Rewrite_URI_Start_Element
    (Self       : in out XML_Catalog_Handler'Class;
     Attributes : XML.SAX.Attributes.SAX_Attributes;
     Success    : in out Boolean);
   --  Processes start of "rewriteURI" element.

   procedure Process_URI_Suffix_Start_Element
    (Self       : in out XML_Catalog_Handler'Class;
     Attributes : XML.SAX.Attributes.SAX_Attributes;
     Success    : in out Boolean);
   --  Processes start of "uriSuffix" element.

   procedure Process_Delegate_URI_Start_Element
    (Self       : in out XML_Catalog_Handler'Class;
     Attributes : XML.SAX.Attributes.SAX_Attributes;
     Success    : in out Boolean);
   --  Processes start of "delegateURI" element.

   procedure Process_Next_Catalog_Start_Element
    (Self       : in out XML_Catalog_Handler'Class;
     Attributes : XML.SAX.Attributes.SAX_Attributes;
     Success    : in out Boolean);
   --  Processes start of "nextCatalog" element.

   ----------------------------------
   -- Check_No_Fragment_Identifier --
   ----------------------------------

   procedure Check_No_Fragment_Identifier
    (Self    : in out XML_Catalog_Handler'Class;
     URI     : League.Strings.Universal_String;
     Success : in out Boolean) is
   begin
      if URI.Index ('#') /= 0 then
         Error
          (Self,
           "URI reference should not include a fragment identifier",
           Success);
      end if;
   end Check_No_Fragment_Identifier;

   -----------------
   -- End_Element --
   -----------------

   overriding procedure End_Element
    (Self           : in out XML_Catalog_Handler;
     Namespace_URI  : League.Strings.Universal_String;
     Local_Name     : League.Strings.Universal_String;
     Qualified_Name : League.Strings.Universal_String;
     Success        : in out Boolean) is
   begin
      if Namespace_URI = XML_Catalogs_Namespace
        and Local_Name = Group_Tag_Name
      then
         Process_Group_End_Element (Self, Success);
      end if;
   end End_Element;

   -----------
   -- Error --
   -----------

   procedure Error
    (Self    : in out XML_Catalog_Handler'Class;
     Message : Wide_Wide_String;
     Success : out Boolean) is
   begin
      Self.Diagnosis := League.Strings.To_Universal_String (Message);
      Success := False;
   end Error;

   -----------
   -- Error --
   -----------

   procedure Error
    (Self    : in out XML_Catalog_Handler'Class;
     Message : League.Strings.Universal_String;
     Success : out Boolean) is
   begin
      Self.Diagnosis := Message;
      Success := False;
   end Error;

   -----------
   -- Error --
   -----------

   overriding procedure Error
    (Self       : in out XML_Catalog_Handler;
     Occurrence : XML.SAX.Parse_Exceptions.SAX_Parse_Exception;
     Success    : in out Boolean) is
   begin
      Ada.Wide_Wide_Text_IO.Put_Line
       (Ada.Wide_Wide_Text_IO.Standard_Error,
        "(error) " & Occurrence.Message.To_Wide_Wide_String);
   end Error;

   ------------------
   -- Error_String --
   ------------------

   overriding function Error_String
    (Self : XML_Catalog_Handler) return League.Strings.Universal_String is
   begin
      return Self.Diagnosis;
   end Error_String;

   -----------------
   -- Fatal_Error --
   -----------------

   overriding procedure Fatal_Error
    (Self       : in out XML_Catalog_Handler;
     Occurrence : XML.SAX.Parse_Exceptions.SAX_Parse_Exception) is
   begin
      Ada.Wide_Wide_Text_IO.Put_Line
       (Ada.Wide_Wide_Text_IO.Standard_Error,
        "(fatal) " & Occurrence.Message.To_Wide_Wide_String);
   end Fatal_Error;

   ----------------------------
   -- Get_Catalog_Entry_File --
   ----------------------------

   function Get_Catalog_Entry_File
    (Self : XML_Catalog_Handler)
       return Matreshka.XML_Catalogs.Entry_Files.Catalog_Entry_File_Access is
   begin
      return Self.Entry_File;
   end Get_Catalog_Entry_File;

   -----------------------------------
   -- Process_Catalog_Start_Element --
   -----------------------------------

   procedure Process_Catalog_Start_Element
    (Self       : in out XML_Catalog_Handler'Class;
     Attributes : XML.SAX.Attributes.SAX_Attributes;
     Success    : in out Boolean)
   is
      --  [XML Catalogs] 6.5.1. The catalog Entry
      --
      --  "Each XML Catalog entry file consists of a single catalog element.
      --  This element may set the global prefer value and global base URI. It
      --  is otherwise just a container for the other elements.
      --
      --  <catalog
      --    id = id
      --    prefer = "system" | "public"
      --    xml:base = uri-reference >
      --      <!-- Content: (group | public | system | rewriteSystem
      --      | systemSuffix | delegatePublic | delegateSystem | uri
      --      | rewriteURI | uriSuffix | delegateURI | nextCatalog)+ -->
      --  </catalog>"

      Prefer_Index : constant Natural
        := Attributes.Index (Prefer_Attribute_Name);

   begin
      --  Analyze 'prefer' attribute when present.

      if Prefer_Index /= 0 then
         if Attributes.Value (Prefer_Index) = Public_Image then
            Self.Current_Prefer_Mode := Public;

         elsif Attributes.Value (Prefer_Index) = System_Image then
            Self.Current_Prefer_Mode := System;

         else
            Self.Error ("Invalid value of 'prefer' attribute", Success);
         end if;
      end if;

      --  Create new catalog entry file object.

      Self.Entry_File :=
        new Matreshka.XML_Catalogs.Entry_Files.Catalog_Entry_File;
      Self.Entry_File.Default_Prefer_Mode := Self.Default_Prefer_Mode;
   end Process_Catalog_Start_Element;

   -------------------------------------------
   -- Process_Delegate_Public_Start_Element --
   -------------------------------------------

   procedure Process_Delegate_Public_Start_Element
    (Self       : in out XML_Catalog_Handler'Class;
     Attributes : XML.SAX.Attributes.SAX_Attributes;
     Success    : in out Boolean)
   is
      --  [XML Catalogs] 6.5.7. The delegatePublic Element
      --
      --  "The delegatePublic element associates an alternate catalog with a
      --  partial public identifier.
      --
      --  <delegatePublic
      --    id = id
      --    publicIdStartString = public-identifier-prefix
      --    catalog = uri-reference
      --    xml:base = uri-reference />
      --
      --  A delegatePublic entry matches a public identifier if the normalized
      --  value (Section 6.2, “Public Identifier Normalization”) of the public
      --  identifier begins precisely with the normalized value of the
      --  publicIdStartString attribute of the entry."
      --
      --  "If the value of the catalog attribute is relative, it must be made
      --  absolute with respect to the base URI currently in effect."

      Public_Id : constant League.Strings.Universal_String
        := Matreshka.XML_Catalogs.Normalization.Normalize_Public_Identifier
            (Attributes.Value (Public_Id_Start_String_Attribute_Name));
      Catalog   : constant League.Strings.Universal_String
        := Self.Locator.Base_URI.Resolve
            (League.IRIs.From_Universal_String
              (Attributes.Value (Catalog_Attribute_Name))).To_Universal_String;

   begin
      --  Check that 'publicIdStartString' is not empty.

      if Public_Id.Is_Empty then
         Error (Self, "publicIdStartString is empty", Success);

         return;
      end if;

      Self.Entry_File.Append
       (new Delegate_Public_Entry'
             (Public_Id, Catalog, Self.Current_Prefer_Mode));
   end Process_Delegate_Public_Start_Element;

   -------------------------------------------
   -- Process_Delegate_System_Start_Element --
   -------------------------------------------

   procedure Process_Delegate_System_Start_Element
    (Self       : in out XML_Catalog_Handler'Class;
     Attributes : XML.SAX.Attributes.SAX_Attributes;
     Success    : in out Boolean)
   is
      --  [XML Catalogs] 6.5.8. The delegateSystem Element
      --
      --  "The delegateSystem element associates an alternate catalog with a
      --  partial system identifier.
      --
      --  <delegateSystem
      --    id = id
      --    systemIdStartString = string
      --    catalog = uri-reference
      --    xml:base = uri-reference />
      --
      --  A delegateSystem entry matches a system identifier if the normalized
      --  value (Section 6.3, “System Identifier and URI Normalization”) of the
      --  system identifier begins precisely with the normalized value of the
      --  systemIdStartString attribute of the entry."
      --
      --  "If the value of the catalog attribute is relative, it must be made
      --  absolute with respect to the base URI currently in effect."

      System_Id : constant League.Strings.Universal_String
        := Matreshka.XML_Catalogs.Normalization.Normalize_System_Identifier
            (Attributes.Value (System_Id_Start_String_Attribute_Name));
      Catalog   : constant League.Strings.Universal_String
        := Self.Locator.Base_URI.Resolve
            (League.IRIs.From_Universal_String
              (Attributes.Value (Catalog_Attribute_Name))).To_Universal_String;

   begin
      --  Check that 'systemIdStartString' is not empty.

      if System_Id.Is_Empty then
         Error (Self, "systemIdStartString is empty", Success);

         return;
      end if;

      Self.Entry_File.Append
       (new Delegate_System_Entry'(System_Id, Catalog));
   end Process_Delegate_System_Start_Element;

   ----------------------------------------
   -- Process_Delegate_URI_Start_Element --
   ----------------------------------------

   procedure Process_Delegate_URI_Start_Element
    (Self       : in out XML_Catalog_Handler'Class;
     Attributes : XML.SAX.Attributes.SAX_Attributes;
     Success    : in out Boolean)
   is
      --  [XML Catalogs] 6.5.12. The delegateURI Element
      --
      --  "The delegateURI element associates an alternate catalog with a
      --  partial URI reference.
      --
      --  <delegateURI
      --    id = id
      --    uriStartString = string
      --    catalog = uri-reference
      --    xml:base = uri-reference />
      --
      --  A delegateURI entry matches a URI reference if the normalized value
      --  (Section 6.3, “System Identifier and URI Normalization”) of the URI
      --  reference begins precisely with the normalized value of the
      --  uriStartString attribute of the entry.
      --
      --  If the value of the catalog attribute is relative, it must be made
      --  absolute with respect to the base URI currently in effect."

      URI     : constant League.Strings.Universal_String
        := Matreshka.XML_Catalogs.Normalization.Normalize_URI
            (Attributes.Value (URI_Start_String_Attribute_Name));
      Catalog : constant League.Strings.Universal_String
        := Self.Locator.Base_URI.Resolve
            (League.IRIs.From_Universal_String
              (Attributes.Value (Catalog_Attribute_Name))).To_Universal_String;

   begin
      --  Check that 'uriStartString' is not empty.

      if URI.Is_Empty then
         Error (Self, "uriStartString is empty", Success);

         return;
      end if;

      Self.Entry_File.Append (new Delegate_URI_Entry'(URI, Catalog));
   end Process_Delegate_URI_Start_Element;

   -------------------------------
   -- Process_Group_End_Element --
   -------------------------------

   procedure Process_Group_End_Element
    (Self    : in out XML_Catalog_Handler'Class;
     Success : in out Boolean) is
   begin
      --  Restore preference mode on leave of "group" element.

      Self.Current_Prefer_Mode := Self.Previous_Prefer_Mode;
   end Process_Group_End_Element;

   ---------------------------------
   -- Process_Group_Start_Element --
   ---------------------------------

   procedure Process_Group_Start_Element
    (Self       : in out XML_Catalog_Handler'Class;
     Attributes : XML.SAX.Attributes.SAX_Attributes;
     Success    : in out Boolean)
   is
      --  [XML Catalogs] 6.5.2. The group Entry
      --
      --  "The group element is a convenience wrapper for specifying a prefer
      --  setting or base URI for a set of catalog entries.  It has no
      --  semantics other than scoping these settings.
      --
      --  <group
      --    id = id
      --    prefer = "system" | "public"
      --    xml:base = uri-reference >
      --      <!-- Content: (public | system | rewriteSystem | systemSuffix |
      --      delegatePublic | delegateSystem | uri | rewriteURI | uriSuffix |
      --      delegateURI | nextCatalog)+ -->
      --  </group>"

      Prefer_Index : constant Natural
        := Attributes.Index (Prefer_Attribute_Name);

   begin
      --  Save current preference mode, analyze 'prefer' attribute when
      --  present, otherwise preserve current mode.

      Self.Previous_Prefer_Mode := Self.Current_Prefer_Mode;

      if Prefer_Index /= 0 then
         if Attributes.Value (Prefer_Index) = Public_Image then
            Self.Current_Prefer_Mode := Public;

         elsif Attributes.Value (Prefer_Index) = System_Image then
            Self.Current_Prefer_Mode := System;

         else
            Self.Error ("Invalid value of 'prefer' attribute", Success);
         end if;
      end if;
   end Process_Group_Start_Element;

   ----------------------------------------
   -- Process_Next_Catalog_Start_Element --
   ----------------------------------------

   procedure Process_Next_Catalog_Start_Element
    (Self       : in out XML_Catalog_Handler'Class;
     Attributes : XML.SAX.Attributes.SAX_Attributes;
     Success    : in out Boolean)
   is
      --  [XML Catalogs] 6.5.13. The nextCatalog Element
      --
      --  "The nextCatalog elements indicate additional catalog entry file(s)
      --  to be considered during the process of resolution.
      --
      --  <nextCatalog
      --    id = id
      --    catalog = uri-reference
      --    xml:base = uri-reference />
      --
      --  If the value of the catalog attribute is relative, it must be made
      --  absolute with respect to the base URI currently in effect.
      --
      --  Catalogs loaded due to a nextCatalog directive have an initial base
      --  URI that is dependent on the location of the loaded catalog entry
      --  file. No xml:base information is inherited from the originating
      --  catalog.

      Catalog : constant League.Strings.Universal_String
        := Self.Locator.Base_URI.Resolve
            (League.IRIs.From_Universal_String
              (Attributes.Value (Catalog_Attribute_Name))).To_Universal_String;

   begin
      Self.Entry_File.Append
       (new Next_Catalog_Entry'(Catalog, Self.Default_Prefer_Mode, null));
   end Process_Next_Catalog_Start_Element;

   ----------------------------------
   -- Process_Public_Start_Element --
   ----------------------------------

   procedure Process_Public_Start_Element
    (Self       : in out XML_Catalog_Handler'Class;
     Attributes : XML.SAX.Attributes.SAX_Attributes;
     Success    : in out Boolean)
   is
      --  [XML Catalogs] 6.5.3. The public Entry
      --
      --  "The public element associates a URI reference with the public
      --  identifier portion of an external identifier.
      --
      --  <public
      --    id = id
      --    publicId = public-identifier
      --    uri = uri-reference
      --    xml:base = uri-reference />
      --
      --  A public entry matches a public identifier if the normalized value
      --  (Section 6.2, “Public Identifier Normalization”) of the public
      --  identifier is lexically identical to the normalized value of the
      --  publicId attribute of the entry.
      --
      --  If the value of the uri attribute is relative, it must be made
      --  absolute with respect to the base URI currently in effect. The URI
      --  reference should not include a fragment identifier."

      Public_Id : constant League.Strings.Universal_String
        := Matreshka.XML_Catalogs.Normalization.Normalize_Public_Identifier
            (Attributes.Value (Public_Id_Attribute_Name));
      URI       : constant League.Strings.Universal_String
        := Self.Locator.Base_URI.Resolve
            (League.IRIs.From_Universal_String
              (Attributes.Value (URI_Attribute_Name))).To_Universal_String;

   begin
      --  Check that 'publicId' is not empty.

      if Public_Id.Is_Empty then
         Error (Self, "publicId is empty", Success);

         return;
      end if;

      --  Check that URI is not include a fragment identifier.

      Check_No_Fragment_Identifier (Self, URI, Success);

      if not Success then
         return;
      end if;

      Self.Entry_File.Append
       (new Public_Entry'(Public_Id, URI, Self.Current_Prefer_Mode));
   end Process_Public_Start_Element;

   ------------------------------------------
   -- Process_Rewrite_System_Start_Element --
   ------------------------------------------

   procedure Process_Rewrite_System_Start_Element
    (Self       : in out XML_Catalog_Handler'Class;
     Attributes : XML.SAX.Attributes.SAX_Attributes;
     Success    : in out Boolean)
   is
      --  [XML Catalogs] 6.5.5. The rewriteSystem Element
      --
      --  The rewriteSystem element rewrites the beginning of a system
      --  identifier.
      --
      --  <rewriteSystem
      --    id = id
      --    systemIdStartString = string
      --    rewritePrefix = uri-reference />
      --
      --  A rewriteSystem entry matches a system identifier if the normalized
      --  value (Section 6.3, “System Identifier and URI Normalization”) of the
      --  system identifier begins precisely with the normalized value of the
      --  systemIdStartString attribute of the entry.
      --
      --  If the value of the rewritePrefix attribute is relative, it must be
      --  made absolute with respect to the base URI currently in effect.

      System_Id_Start_String : constant League.Strings.Universal_String
        := Matreshka.XML_Catalogs.Normalization.Normalize_System_Identifier
            (Attributes.Value (System_Id_Start_String_Attribute_Name));
      Rewrite_Prefix         : constant League.Strings.Universal_String
        := Self.Locator.Base_URI.Resolve
            (League.IRIs.From_Universal_String
              (Attributes.Value
                (Rewrite_Prefix_Attribute_Name))).To_Universal_String;

   begin
      --  Check that 'systemIdStartString' is not empty.

      if System_Id_Start_String.Is_Empty then
         Error (Self, "'systemIdStartString' is empty", Success);

         return;
      end if;

      Self.Entry_File.Append
       (new Rewrite_System_Entry'(System_Id_Start_String, Rewrite_Prefix));
   end Process_Rewrite_System_Start_Element;

   ---------------------------------------
   -- Process_Rewrite_URI_Start_Element --
   ---------------------------------------

   procedure Process_Rewrite_URI_Start_Element
    (Self       : in out XML_Catalog_Handler'Class;
     Attributes : XML.SAX.Attributes.SAX_Attributes;
     Success    : in out Boolean)
   is
      --  [XML Catalogs] 6.5.10. The rewriteURI Element
      --
      --  The rewriteURI element rewrites the beginning of a URI reference that
      --  is not part of an external identifier.
      --
      --  <rewriteURI
      --    id = id
      --    uriStartString = string
      --    rewritePrefix = uri-reference />
      --
      --  A rewriteURI entry matches a URI reference if the normalized value
      --  (Section 6.3, “System Identifier and URI Normalization”) of the URI
      --  reference begins precisely with the normalized value of the
      --  uriStartString attribute of the entry.
      --
      --  If the value of the rewritePrefix attribute is relative, it must be
      --  made absolute with respect to the base URI currently in effect.

      URI_Start_String : constant League.Strings.Universal_String
        := Matreshka.XML_Catalogs.Normalization.Normalize_URI
            (Attributes.Value (URI_Start_String_Attribute_Name));
      Rewrite_Prefix   : constant League.Strings.Universal_String
        := Self.Locator.Base_URI.Resolve
            (League.IRIs.From_Universal_String
              (Attributes.Value
                (Rewrite_Prefix_Attribute_Name))).To_Universal_String;

   begin
      --  Check that 'uriStartString' is not empty.

      if URI_Start_String.Is_Empty then
         Error (Self, "'uriStartString' is empty", Success);

         return;
      end if;

      Self.Entry_File.Append
       (new Rewrite_URI_Entry'(URI_Start_String, Rewrite_Prefix));
   end Process_Rewrite_URI_Start_Element;

   ----------------------------------
   -- Process_System_Start_Element --
   ----------------------------------

   procedure Process_System_Start_Element
    (Self       : in out XML_Catalog_Handler'Class;
     Attributes : XML.SAX.Attributes.SAX_Attributes;
     Success    : in out Boolean)
   is
      --  [XML Catalogs] 6.5.4. The system Element
      --
      --  "The system element associates a URI reference with the system
      --  identifier portion of an external identifier.
      --
      --  <system
      --    id = id
      --    systemId = string
      --    uri = uri-reference
      --    xml:base = uri-reference />
      --
      --  A system entry matches a system identifier if the normalized value
      --  (Section 6.3, “System Identifier and URI Normalization”) of the
      --  system identifier is lexically identical to the normalized value of
      --  the systemId attribute of the entry.
      --
      --  If the value of the uri attribute is relative, it must be made
      --  absolute with respect to the base URI currently in effect. The URI
      --  reference should not include a fragment identifier."

      System_Id : constant League.Strings.Universal_String
        := Matreshka.XML_Catalogs.Normalization.Normalize_System_Identifier
            (Attributes.Value (System_Id_Attribute_Name));
      URI       : constant League.Strings.Universal_String
        := Self.Locator.Base_URI.Resolve
            (League.IRIs.From_Universal_String
              (Attributes.Value (URI_Attribute_Name))).To_Universal_String;

   begin
      --  Check that 'systemId' is not empty.

      if System_Id.Is_Empty then
         Error (Self, "systemId is empty", Success);

         return;
      end if;

      --  Check that URI is not include a fragment identifier.

      Check_No_Fragment_Identifier (Self, URI, Success);

      if not Success then
         return;
      end if;

      Self.Entry_File.Append (new System_Entry'(System_Id, URI));
   end Process_System_Start_Element;

   -----------------------------------------
   -- Process_System_Suffix_Start_Element --
   -----------------------------------------

   procedure Process_System_Suffix_Start_Element
    (Self       : in out XML_Catalog_Handler'Class;
     Attributes : XML.SAX.Attributes.SAX_Attributes;
     Success    : in out Boolean)
   is
      --  [XML Catalogs] 6.5.6. The systemSuffix Element
      --
      --  "The systemSuffix element associates a URI reference with the suffix
      --  portion of a system identifier.
      --
      --  <systemSuffix
      --    id = id
      --    systemIdSuffix = string
      --    uri = uri-reference
      --    xml:base = uri-reference />
      --
      --  A systemSuffix entry matches a system identifier if the normalized
      --  value (Section 6.3, “System Identifier and URI Normalization”) of the
      --  system identifier ends precisely with the normalized value of the
      --  systemIdSuffix attribute of the entry.
      --
      --  If the value of the uri attribute is relative, it must be made
      --  absolute with respect to the base URI currently in effect. The URI
      --  reference should not include a fragment identifier."

      System_Id_Suffix : constant League.Strings.Universal_String
        := Matreshka.XML_Catalogs.Normalization.Normalize_System_Identifier
            (Attributes.Value (System_Id_Suffix_Attribute_Name));
      URI              : constant League.Strings.Universal_String
        := Self.Locator.Base_URI.Resolve
            (League.IRIs.From_Universal_String
              (Attributes.Value (URI_Attribute_Name))).To_Universal_String;

   begin
      --  Check that 'systemIdSuffix' is not empty.

      if System_Id_Suffix.Is_Empty then
         Error (Self, "systemIdSuffix is empty", Success);

         return;
      end if;

      --  Check that URI is not include a fragment identifier.

      Check_No_Fragment_Identifier (Self, URI, Success);

      if not Success then
         return;
      end if;

      Self.Entry_File.Append (new System_Suffix_Entry'(System_Id_Suffix, URI));
   end Process_System_Suffix_Start_Element;

   -------------------------------
   -- Process_URI_Start_Element --
   -------------------------------

   procedure Process_URI_Start_Element
    (Self       : in out XML_Catalog_Handler'Class;
     Attributes : XML.SAX.Attributes.SAX_Attributes;
     Success    : in out Boolean)
   is
      --  [XML Catalogs] 6.5.9. The uri Element
      --
      --  The uri element associates an alternate URI reference with a URI
      --  reference that is not part of an external identifier.
      --
      --  <uri
      --    id = id
      --    name = string
      --    uri = uri-reference
      --    xml:base = uri-reference />
      --
      --  A uri entry matches a URI reference if the normalized value (Section
      --  6.3, “System Identifier and URI Normalization”) of the URI reference
      --  is lexically identical to the normalized value of the name attribute
      --  of the entry."
      --
      --  "If the value of the uri attribute is relative, it must be made
      --  absolute with respect to the base URI currently in effect."

      Name : constant League.Strings.Universal_String
        := Matreshka.XML_Catalogs.Normalization.Normalize_URI
            (Attributes.Value (Name_Attribute_Name));
      URI  : constant League.Strings.Universal_String
        := Self.Locator.Base_URI.Resolve
            (League.IRIs.From_Universal_String
              (Attributes.Value (URI_Attribute_Name))).To_Universal_String;

   begin
      --  Check that 'name' is not empty.

      if Name.Is_Empty then
         Error (Self, "name is empty", Success);

         return;
      end if;

      Self.Entry_File.Append (new URI_Entry'(Name, URI));
   end Process_URI_Start_Element;

   --------------------------------------
   -- Process_URI_Suffix_Start_Element --
   --------------------------------------

   procedure Process_URI_Suffix_Start_Element
    (Self       : in out XML_Catalog_Handler'Class;
     Attributes : XML.SAX.Attributes.SAX_Attributes;
     Success    : in out Boolean)
   is
      --  [XML Catalogs] 6.5.11. The uriSuffix Element
      --
      --  "The uriSuffix element associates a URI reference with the suffix
      --  portion of a URI reference that is not part of an external
      --  identifier.
      --
      --  <uriSuffix
      --    id = id
      --    uriSuffix = string
      --    uri = uri-reference
      --    xml:base = uri-reference />
      --
      --  A uriSuffix entry matches a URI if the normalized value (Section 6.3,
      --  “System Identifier and URI Normalization”) of the URI ends precisely
      --  with the normalized value of the uriSuffix attribute of the entry.
      --
      --  If the value of the uri attribute is relative, it must be made
      --  absolute with respect to the base URI currently in effect.

      URI_Suffix : constant League.Strings.Universal_String
        := Matreshka.XML_Catalogs.Normalization.Normalize_URI
            (Attributes.Value (URI_Suffix_Attribute_Name));
      URI        : constant League.Strings.Universal_String
        := Self.Locator.Base_URI.Resolve
            (League.IRIs.From_Universal_String
              (Attributes.Value (URI_Attribute_Name))).To_Universal_String;

   begin
      --  Check that 'uriSuffix' is not empty.

      if URI_Suffix.Is_Empty then
         Error (Self, "uriSuffix is empty", Success);

         return;
      end if;

      Self.Entry_File.Append (new URI_Suffix_Entry'(URI_Suffix, URI));
   end Process_URI_Suffix_Start_Element;

   -----------------------------
   -- Set_Default_Prefer_Mode --
   -----------------------------

   procedure Set_Default_Prefer_Mode
    (Self : in out XML_Catalog_Handler'Class;
     Mode : Matreshka.XML_Catalogs.Entry_Files.Prefer_Mode) is
   begin
      Self.Default_Prefer_Mode := Mode;
   end Set_Default_Prefer_Mode;

   --------------------------
   -- Set_Document_Locator --
   --------------------------

   overriding procedure Set_Document_Locator
    (Self    : in out XML_Catalog_Handler;
     Locator : XML.SAX.Locators.SAX_Locator) is
   begin
      Self.Locator := Locator;
   end Set_Document_Locator;

   --------------------
   -- Start_Document --
   --------------------

   overriding procedure Start_Document
    (Self    : in out XML_Catalog_Handler;
     Success : in out Boolean) is
   begin
      Self.Diagnosis := League.Strings.Empty_Universal_String;
      Self.Current_Prefer_Mode := Self.Default_Prefer_Mode;
   end Start_Document;

   ---------------
   -- Start_DTD --
   ---------------

   overriding procedure Start_DTD
    (Self      : in out XML_Catalog_Handler;
     Name      : League.Strings.Universal_String;
     Public_Id : League.Strings.Universal_String;
     System_Id : League.Strings.Universal_String;
     Success   : in out Boolean) is
   begin
      --  Check whether processed document is XML Catalogs entry file.

      if Public_Id /= XML_Catalogs_1_0_Public_Id
        and Public_Id /= XML_Catalogs_1_1_Public_Id
      then
         Self.Error ("Unknown XML Catalogs DTD", Success);
      end if;
   end Start_DTD;

   -------------------
   -- Start_Element --
   -------------------

   overriding procedure Start_Element
    (Self           : in out XML_Catalog_Handler;
     Namespace_URI  : League.Strings.Universal_String;
     Local_Name     : League.Strings.Universal_String;
     Qualified_Name : League.Strings.Universal_String;
     Attributes     : XML.SAX.Attributes.SAX_Attributes;
     Success        : in out Boolean) is
   begin
      if Namespace_URI /= XML_Catalogs_Namespace then
         --  Ignore all non-XML Catalogs elements.

         return;
      end if;

      if Local_Name = Catalog_Tag_Name then
         Process_Catalog_Start_Element (Self, Attributes, Success);

      elsif Local_Name = Delegate_Public_Tag_Name then
         Process_Delegate_Public_Start_Element (Self, Attributes, Success);

      elsif Local_Name = Delegate_System_Tag_Name then
         Process_Delegate_System_Start_Element (Self, Attributes, Success);

      elsif Local_Name = Delegate_URI_Tag_Name then
         Process_Delegate_URI_Start_Element (Self, Attributes, Success);

      elsif Local_Name = Group_Tag_Name then
         Process_Group_Start_Element (Self, Attributes, Success);

      elsif Local_Name = Next_Catalog_Tag_Name then
         Process_Next_Catalog_Start_Element (Self, Attributes, Success);

      elsif Local_Name = Public_Tag_Name then
         Process_Public_Start_Element (Self, Attributes, Success);

      elsif Local_Name = Rewrite_System_Tag_Name then
         Process_Rewrite_System_Start_Element (Self, Attributes, Success);

      elsif Local_Name = Rewrite_URI_Tag_Name then
         Process_Rewrite_URI_Start_Element (Self, Attributes, Success);

      elsif Local_Name = System_Tag_Name then
         Process_System_Start_Element (Self, Attributes, Success);

      elsif Local_Name = System_Suffix_Tag_Name then
         Process_System_Suffix_Start_Element (Self, Attributes, Success);

      elsif Local_Name = URI_Tag_Name then
         Process_URI_Start_Element (Self, Attributes, Success);

      elsif Local_Name = URI_Suffix_Tag_Name then
         Process_URI_Suffix_Start_Element (Self, Attributes, Success);
      end if;
   end Start_Element;

   -------------
   -- Warning --
   -------------

   overriding procedure Warning
    (Self       : in out XML_Catalog_Handler;
     Occurrence : XML.SAX.Parse_Exceptions.SAX_Parse_Exception;
     Success    : in out Boolean) is
   begin
      Ada.Wide_Wide_Text_IO.Put_Line
       (Ada.Wide_Wide_Text_IO.Standard_Error,
        "(warning) " & Occurrence.Message.To_Wide_Wide_String);
   end Warning;

end Matreshka.XML_Catalogs.Handlers;