matreshka_league_21.0.0_0c8f4d47/source/xml/dom/matreshka-dom_documents.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
------------------------------------------------------------------------------
--                                                                          --
--                            Matreshka Project                             --
--                                                                          --
--                               XML Processor                              --
--                                                                          --
--                        Runtime Library Component                         --
--                                                                          --
------------------------------------------------------------------------------
--                                                                          --
-- Copyright © 2014-2020, 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: 5920 $ $Date: 2020-06-09 21:29:10 +0300 (Вт, 09 июн 2020) $
------------------------------------------------------------------------------
with Ada.Containers.Hashed_Maps;
with Ada.Tags.Generic_Dispatching_Constructor;

with League.Strings.Hash;

with Matreshka.DOM_Attributes;
with Matreshka.DOM_Elements;
with Matreshka.DOM_Texts;

package body Matreshka.DOM_Documents is

   procedure Split_Qualified_Name
    (Qualified_Name : League.Strings.Universal_String;
     Prefix         : out League.Strings.Universal_String;
     Local_Name     : out League.Strings.Universal_String);
   --  Splits qualified name into prefix and local name parts.

   type Qualified_Name is record
      Namespace_URI : League.Strings.Universal_String;
      Local_Name    : League.Strings.Universal_String;
   end record;

   function Hash (Item : Qualified_Name) return Ada.Containers.Hash_Type;
   --  Hash function to be used with standard containers.

   package Qualified_Name_Maps is
     new Ada.Containers.Hashed_Maps
          (Qualified_Name, Ada.Tags.Tag, Hash, "=", Ada.Tags."=");

   Attribute_Registry : Qualified_Name_Maps.Map;
   Element_Registry   : Qualified_Name_Maps.Map;

   ------------------
   -- Constructors --
   ------------------

   package body Constructors is

      ----------------
      -- Initialize --
      ----------------

      procedure Initialize (Self : not null access Document_Node'Class) is
      begin
         Matreshka.DOM_Nodes.Constructors.Initialize
          (Self, Matreshka.DOM_Nodes.Document_Access (Self));
      end Initialize;

   end Constructors;

   ----------------------
   -- Create_Attribute --
   ----------------------

   function Create_Attribute
    (Self          : not null access Document_Node'Class;
     Namespace_URI : League.Strings.Universal_String;
     Prefix        : League.Strings.Universal_String;
     Local_Name    : League.Strings.Universal_String)
       return not null XML.DOM.Attributes.DOM_Attribute_Access
   is

      function Constructor is
        new Ada.Tags.Generic_Dispatching_Constructor
             (Matreshka.DOM_Attributes.Abstract_Attribute_L2_Node,
              Matreshka.DOM_Attributes.Attribute_L2_Parameters,
              Matreshka.DOM_Attributes.Create);

      Parameters : aliased Matreshka.DOM_Attributes.Attribute_L2_Parameters
        := (Document      => Matreshka.DOM_Nodes.Document_Access (Self),
            Namespace_URI => Namespace_URI,
            Prefix        => Prefix,
            Local_Name    => Local_Name);
      Position   : Qualified_Name_Maps.Cursor;
      Tag        : Ada.Tags.Tag;
      Node       : Matreshka.DOM_Nodes.Node_Access;

   begin
      Position :=
        Attribute_Registry.Find
         ((Parameters.Namespace_URI, Parameters.Local_Name));

      if Qualified_Name_Maps.Has_Element (Position) then
         Tag := Qualified_Name_Maps.Element (Position);

      else
         Tag := Matreshka.DOM_Attributes.Attribute_L2_Node'Tag;
      end if;

      Node :=
        new Matreshka.DOM_Attributes.Abstract_Attribute_L2_Node'Class'
             (Constructor (Tag, Parameters'Access));

      return XML.DOM.Attributes.DOM_Attribute_Access (Node);
   end Create_Attribute;

   -------------------------
   -- Create_Attribute_NS --
   -------------------------

   overriding function Create_Attribute_NS
    (Self           : not null access Document_Node;
     Namespace_URI  : League.Strings.Universal_String;
     Qualified_Name : League.Strings.Universal_String)
       return not null XML.DOM.Attributes.DOM_Attribute_Access
   is
      Prefix     : League.Strings.Universal_String;
      Local_Name : League.Strings.Universal_String;
      Node       : constant not null Matreshka.DOM_Nodes.Node_Access
        := new Matreshka.DOM_Attributes.Attribute_L2_Node;

   begin
      Split_Qualified_Name (Qualified_Name, Prefix, Local_Name);

      return Self.Create_Attribute (Namespace_URI, Prefix, Local_Name);
   end Create_Attribute_NS;

   --------------------
   -- Create_Element --
   --------------------

   function Create_Element
    (Self          : not null access Document_Node'Class;
     Namespace_URI : League.Strings.Universal_String;
     Prefix        : League.Strings.Universal_String;
     Local_Name    : League.Strings.Universal_String)
       return not null XML.DOM.Elements.DOM_Element_Access
   is

      function Constructor is
        new Ada.Tags.Generic_Dispatching_Constructor
             (Matreshka.DOM_Elements.Abstract_Element_Node,
              Matreshka.DOM_Elements.Element_L2_Parameters,
              Matreshka.DOM_Elements.Create);

      Parameters : aliased Matreshka.DOM_Elements.Element_L2_Parameters
        := (Document      => Matreshka.DOM_Nodes.Document_Access (Self),
            Namespace_URI => Namespace_URI,
            Prefix        => Prefix,
            Local_Name    => Local_Name);
      Position   : Qualified_Name_Maps.Cursor;
      Tag        : Ada.Tags.Tag;
      Node       : Matreshka.DOM_Nodes.Node_Access;

   begin
      Position :=
        Element_Registry.Find
         ((Parameters.Namespace_URI, Parameters.Local_Name));

      if Qualified_Name_Maps.Has_Element (Position) then
         Tag := Qualified_Name_Maps.Element (Position);

      else
         Tag := Matreshka.DOM_Elements.Element_Node'Tag;
      end if;

      Node :=
        new Matreshka.DOM_Elements.Abstract_Element_Node'Class'
             (Constructor (Tag, Parameters'Access));

      return XML.DOM.Elements.DOM_Element_Access (Node);
   end Create_Element;

   -----------------------
   -- Create_Element_NS --
   -----------------------

   overriding function Create_Element_NS
    (Self           : not null access Document_Node;
     Namespace_URI  : League.Strings.Universal_String;
     Qualified_Name : League.Strings.Universal_String)
       return not null XML.DOM.Elements.DOM_Element_Access
   is
      Prefix     : League.Strings.Universal_String;
      Local_Name : League.Strings.Universal_String;

   begin
      Split_Qualified_Name (Qualified_Name, Prefix, Local_Name);

      return Self.Create_Element (Namespace_URI, Prefix, Local_Name);
   end Create_Element_NS;

   ----------------------
   -- Create_Text_Node --
   ----------------------

   overriding function Create_Text_Node
    (Self : not null access Document_Node;
     Data : League.Strings.Universal_String)
       return not null XML.DOM.Texts.DOM_Text_Access
   is
      Node : constant not null Matreshka.DOM_Nodes.Node_Access
        := new Matreshka.DOM_Texts.Text_Node;

   begin
      Matreshka.DOM_Texts.Constructors.Initialize
       (Matreshka.DOM_Texts.Text_Node'Class (Node.all)'Access,
        Matreshka.DOM_Nodes.Document_Access (Self),
        Data);

      return XML.DOM.Texts.DOM_Text_Access (Node);
   end Create_Text_Node;

   ----------------
   -- Enter_Node --
   ----------------

   overriding procedure Enter_Node
    (Self    : not null access Document_Node;
     Visitor : in out XML.DOM.Visitors.Abstract_Visitor'Class;
     Control : in out XML.DOM.Visitors.Traverse_Control) is
   begin
      Visitor.Enter_Document
       (XML.DOM.Documents.DOM_Document_Access (Self), Control);
   end Enter_Node;

   ----------------
   -- Error_Code --
   ----------------

   overriding function Error_Code
    (Self : not null access constant Document_Node)
       return XML.DOM.Error_Code is
   begin
      return Self.Diagnosis;
   end Error_Code;

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

   overriding function Error_String
    (Self : not null access constant Document_Node)
       return League.Strings.Universal_String is
   begin
      return League.Strings.Empty_Universal_String;
   end Error_String;

   -------------------
   -- Get_Node_Name --
   -------------------

   overriding function Get_Node_Name
    (Self : not null access constant Document_Node)
       return League.Strings.Universal_String
   is
      pragma Unreferenced (Self);

   begin
      return League.Strings.To_Universal_String ("#document");
   end Get_Node_Name;

   -------------------
   -- Get_Node_Type --
   -------------------

   overriding function Get_Node_Type
    (Self : not null access constant Document_Node)
       return XML.DOM.Node_Type
   is
      pragma Unreferenced (Self);

   begin
      return XML.DOM.Document_Node;
   end Get_Node_Type;

   ------------------------
   -- Get_Owner_Document --
   ------------------------

   overriding function Get_Owner_Document
    (Self : not null access constant Document_Node)
       return XML.DOM.Documents.DOM_Document_Access
   is
      pragma Unreferenced (Self);

   begin
      return null;
   end Get_Owner_Document;

   ----------
   -- Hash --
   ----------

   function Hash (Item : Qualified_Name) return Ada.Containers.Hash_Type is
      use type Ada.Containers.Hash_Type;

   begin
      return
        League.Strings.Hash (Item.Namespace_URI)
          + League.Strings.Hash (Item.Local_Name);
   end Hash;

   ----------------
   -- Leave_Node --
   ----------------

   overriding procedure Leave_Node
    (Self    : not null access Document_Node;
     Visitor : in out XML.DOM.Visitors.Abstract_Visitor'Class;
     Control : in out XML.DOM.Visitors.Traverse_Control) is
   begin
      Visitor.Leave_Document
       (XML.DOM.Documents.DOM_Document_Access (Self), Control);
   end Leave_Node;

   ------------------------
   -- Register_Attribute --
   ------------------------

   procedure Register_Attribute
    (Namespace_URI : League.Strings.Universal_String;
     Local_Name    : League.Strings.Universal_String;
     Tag           : Ada.Tags.Tag) is
   begin
      Attribute_Registry.Insert ((Namespace_URI, Local_Name), Tag);
   end Register_Attribute;

   ----------------------
   -- Register_Element --
   ----------------------

   procedure Register_Element
    (Namespace_URI : League.Strings.Universal_String;
     Local_Name    : League.Strings.Universal_String;
     Tag           : Ada.Tags.Tag) is
   begin
      Element_Registry.Insert ((Namespace_URI, Local_Name), Tag);
   end Register_Element;

   --------------------------
   -- Split_Qualified_Name --
   --------------------------

   procedure Split_Qualified_Name
    (Qualified_Name : League.Strings.Universal_String;
     Prefix         : out League.Strings.Universal_String;
     Local_Name     : out League.Strings.Universal_String)
   is
      Delimiter : constant Natural := Qualified_Name.Index (':');

   begin
      if Delimiter = 0 then
         Prefix := League.Strings.Empty_Universal_String;
         Local_Name := Qualified_Name;

      else
         Prefix := Qualified_Name.Head (Delimiter - 1);
         Local_Name := Qualified_Name.Tail_From (Delimiter + 1);
      end if;
   end Split_Qualified_Name;

   ----------------
   -- Visit_Node --
   ----------------

   overriding procedure Visit_Node
    (Self     : not null access Document_Node;
     Iterator : in out XML.DOM.Visitors.Abstract_Iterator'Class;
     Visitor  : in out XML.DOM.Visitors.Abstract_Visitor'Class;
     Control  : in out XML.DOM.Visitors.Traverse_Control) is
   begin
      Iterator.Visit_Document
       (Visitor, XML.DOM.Documents.DOM_Document_Access (Self), Control);
   end Visit_Node;

end Matreshka.DOM_Documents;