1------------------------------------------------------------------------------
2--                                                                          --
3--                            Matreshka Project                             --
4--                                                                          --
5--                               XML Processor                              --
6--                                                                          --
7--                        Runtime Library Component                         --
8--                                                                          --
9------------------------------------------------------------------------------
10--                                                                          --
11-- Copyright © 2010-2011, Vadim Godunko <vgodunko@gmail.com>                --
12-- All rights reserved.                                                     --
13--                                                                          --
14-- Redistribution and use in source and binary forms, with or without       --
15-- modification, are permitted provided that the following conditions       --
16-- are met:                                                                 --
17--                                                                          --
18--  * Redistributions of source code must retain the above copyright        --
19--    notice, this list of conditions and the following disclaimer.         --
20--                                                                          --
21--  * Redistributions in binary form must reproduce the above copyright     --
22--    notice, this list of conditions and the following disclaimer in the   --
23--    documentation and/or other materials provided with the distribution.  --
24--                                                                          --
25--  * Neither the name of the Vadim Godunko, IE nor the names of its        --
26--    contributors may be used to endorse or promote products derived from  --
27--    this software without specific prior written permission.              --
28--                                                                          --
29-- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS      --
30-- "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT        --
31-- LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR    --
32-- A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT     --
33-- HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,   --
34-- SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED --
35-- TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR   --
36-- PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF   --
37-- LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING     --
38-- NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS       --
39-- SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.             --
40--                                                                          --
41------------------------------------------------------------------------------
42--  $Revision: 2031 $ $Date: 2011-07-24 16:07:15 +0400 (Sun, 24 Jul 2011) $
43------------------------------------------------------------------------------
44with League.Strings;
45with Matreshka.Internals.Strings;
46with Matreshka.Internals.Utf16;
47
48package Matreshka.Internals.XML.Entity_Tables is
49
50   pragma Preelaborate;
51
52   type Entity_Table is limited private;
53
54   procedure New_Internal_Parameter_Entity
55    (Self             : in out Entity_Table;
56     Enclosing_Entity : Entity_Identifier;
57     Name             : Matreshka.Internals.XML.Symbol_Identifier;
58     Replacement_Text :
59       not null Matreshka.Internals.Strings.Shared_String_Access;
60     Entity           : out Entity_Identifier);
61   --  Allocates space for the entity and returns identifier of the allocated
62   --  entity.
63
64   procedure New_External_Parameter_Entity
65    (Self               : in out Entity_Table;
66     Enclosing_Entity   : Entity_Identifier;
67     Name               : Matreshka.Internals.XML.Symbol_Identifier;
68     Public_Id          : League.Strings.Universal_String;
69     System_Id          : League.Strings.Universal_String;
70     Enclosing_Base_URI : League.Strings.Universal_String;
71     Entity             : out Entity_Identifier);
72   --  Allocates space for the entity and returns identifier of the allocated
73   --  entity.
74
75   procedure New_Internal_General_Entity
76    (Self             : in out Entity_Table;
77     Enclosing_Entity : Entity_Identifier;
78     Name             : Matreshka.Internals.XML.Symbol_Identifier;
79     Replacement_Text :
80       not null Matreshka.Internals.Strings.Shared_String_Access;
81     Entity           : out Entity_Identifier);
82   --  Allocates space for the entity and returns identifier of the allocated
83   --  entity.
84
85   procedure New_External_Parsed_General_Entity
86    (Self               : in out Entity_Table;
87     Enclosing_Entity   : Entity_Identifier;
88     Name               : Matreshka.Internals.XML.Symbol_Identifier;
89     Public_Id          : League.Strings.Universal_String;
90     System_Id          : League.Strings.Universal_String;
91     Enclosing_Base_URI : League.Strings.Universal_String;
92     Entity             : out Entity_Identifier);
93   --  Allocates space for the entity and returns identifier of the allocated
94   --  entity.
95
96   procedure New_External_Unparsed_General_Entity
97    (Self             : in out Entity_Table;
98     Enclosing_Entity : Entity_Identifier;
99     Name             : Matreshka.Internals.XML.Symbol_Identifier;
100     Notation         : Symbol_Identifier;
101     Entity           : out Entity_Identifier);
102   --  Allocates space for the entity and returns identifier of the allocated
103   --  entity.
104
105   procedure New_Document_Entity
106    (Self             : in out Entity_Table;
107     Public_Id        : League.Strings.Universal_String;
108     System_Id        : League.Strings.Universal_String;
109     Entity_Base_URI  : League.Strings.Universal_String;
110     Entity           : out Entity_Identifier);
111   --  Allocates space for the entity and returns identifier of the allocated
112   --  entity.
113
114   procedure New_External_Subset_Entity
115    (Self               : in out Entity_Table;
116     Enclosing_Entity   : Entity_Identifier;
117     Public_Id          : League.Strings.Universal_String;
118     System_Id          : League.Strings.Universal_String;
119     Enclosing_Base_URI : League.Strings.Universal_String;
120     Entity             : out Entity_Identifier);
121   --  Allocates space for the entity and returns identifier of the allocated
122   --  entity.
123
124   function Is_Document_Entity
125    (Self   : Entity_Table;
126     Entity : Entity_Identifier) return Boolean;
127   --  Returns True when entity is document entity.
128
129   function Is_External_Subset
130    (Self   : Entity_Table;
131     Entity : Entity_Identifier) return Boolean;
132   --  Returns True when entity is enternal subset of the DTD.
133
134   function Is_Internal_General_Entity
135    (Self   : Entity_Table;
136     Entity : Entity_Identifier) return Boolean;
137   --  Returns True when entity is internal general entity.
138
139   function Is_External_Parsed_General_Entity
140    (Self   : Entity_Table;
141     Entity : Entity_Identifier) return Boolean;
142   --  Returns True when entity is external parsed general entity.
143
144   function Is_External_Unparsed_General_Entity
145    (Self   : Entity_Table;
146     Entity : Entity_Identifier) return Boolean;
147   --  Returns True when entity is external unparsed general entity.
148
149   function Is_Parameter_Entity
150    (Self   : Entity_Table;
151     Entity : Entity_Identifier) return Boolean;
152   --  Returns True when entity is parameter entity (internal or external).
153
154   function Is_Parsed_General_Entity
155    (Self   : Entity_Table;
156     Entity : Entity_Identifier) return Boolean;
157   --  Returns True when entity is parsed general entity (internal or
158   --  external).
159
160   function Name
161    (Self   : Entity_Table;
162     Entity : Entity_Identifier)
163       return Matreshka.Internals.XML.Symbol_Identifier;
164
165   function Enclosing_Entity
166    (Self   : Entity_Table;
167     Entity : Entity_Identifier)
168       return Matreshka.Internals.XML.Entity_Identifier;
169   --  Returns entity which contains declaration of the entity.
170
171   function Public_Id
172    (Self   : Entity_Table;
173     Entity : Entity_Identifier)
174       return not null Matreshka.Internals.Strings.Shared_String_Access;
175
176   function System_Id
177    (Self   : Entity_Table;
178     Entity : Entity_Identifier)
179       return not null Matreshka.Internals.Strings.Shared_String_Access;
180
181   function Notation
182    (Self   : Entity_Table;
183     Entity : Entity_Identifier)
184       return Matreshka.Internals.XML.Symbol_Identifier;
185   --  Returns identifier of notation of the entity for unparsed entity and
186   --  No_Symbol for others.
187
188   function Enclosing_Base_URI
189    (Self   : Entity_Table;
190     Entity : Entity_Identifier)
191       return not null Matreshka.Internals.Strings.Shared_String_Access;
192   --  Returns base URI of entity's declaration element to be used to resolve
193   --  relative entity's system identifier.
194
195   function Entity_Base_URI
196    (Self   : Entity_Table;
197     Entity : Entity_Identifier)
198       return not null Matreshka.Internals.Strings.Shared_String_Access;
199   --  Returns base URI of the entity.
200
201   procedure Set_Entity_Base_URI
202    (Self            : in out Entity_Table;
203     Entity          : Entity_Identifier;
204     Entity_Base_URI : League.Strings.Universal_String);
205   --  Sets base URI of the entity.
206
207   function Is_Resolved
208    (Self   : Entity_Table;
209     Entity : Entity_Identifier) return Boolean;
210   --  Returns True when entity is marked as resolved.
211
212   procedure Set_Is_Resolved
213    (Self   : in out Entity_Table;
214     Entity : Entity_Identifier;
215     To     : Boolean);
216   --  Mark entity as resolved.
217
218   function Replacement_Text
219    (Self   : Entity_Table;
220     Entity : Entity_Identifier)
221       return Matreshka.Internals.Strings.Shared_String_Access;
222   --  Returns entity's replacement text.
223
224   procedure Set_Replacement_Text
225    (Self             : in out Entity_Table;
226     Entity           : Entity_Identifier;
227     Replacement_Text :
228       not null Matreshka.Internals.Strings.Shared_String_Access);
229   --  Sets replacement text for the entity.
230
231   function First_Position
232    (Self   : Entity_Table;
233     Entity : Entity_Identifier)
234       return Matreshka.Internals.Utf16.Utf16_String_Index;
235   --  Returns position of the first significant character after the text
236   --  declaration if any; otherwise returns zero.
237
238   procedure Set_First_Position
239    (Self     : in out Entity_Table;
240     Entity   : Entity_Identifier;
241     Position : Matreshka.Internals.Utf16.Utf16_String_Index);
242   --  Sets position of the first significant character after the text
243   --  declaration if any.
244
245   procedure Initialize (Self : in out Entity_Table);
246   --  Initializes entity table.
247
248   procedure Finalize (Self : in out Entity_Table);
249   --  Finalizes entity table.
250
251   procedure Reset (Self : in out Entity_Table);
252   --  Resets internal structures to initial state.
253
254   function First_Entity (Self : Entity_Table) return Entity_Identifier;
255   --  Returns first entity.
256
257   procedure Next_Entity
258    (Self   : Entity_Table;
259     Entity : in out Entity_Identifier);
260   --  Sets Entity to next entity identifier or to No_Entity.
261
262private
263
264   type Entity_Kinds is
265    (Document_Entity,
266     External_Subset_Entity,
267     Internal_Parameter_Entity,
268     External_Parameter_Entity,
269     Internal_General_Entity,
270     External_Parsed_General_Entity,
271     External_Unparsed_General_Entity);
272
273   type Entity_Record is record
274      Kind               : Entity_Kinds;
275      Enclosing          : Entity_Identifier;
276      Name               : Symbol_Identifier;
277      Notation           : Symbol_Identifier;
278      Public_Id          : Matreshka.Internals.Strings.Shared_String_Access;
279      System_Id          : Matreshka.Internals.Strings.Shared_String_Access;
280      Enclosing_Base_URI : Matreshka.Internals.Strings.Shared_String_Access;
281      Entity_Base_URI    : Matreshka.Internals.Strings.Shared_String_Access;
282      Is_Resolved        : Boolean;
283      Replacement_Text   : Matreshka.Internals.Strings.Shared_String_Access;
284      First_Position     : Matreshka.Internals.Utf16.Utf16_String_Index;
285   end record;
286
287   type Entity_Array is array (Entity_Identifier range <>) of Entity_Record;
288
289   type Entity_Array_Access is access all Entity_Array;
290
291   type Entity_Table is limited record
292      Data : Entity_Array_Access;
293      Last : Entity_Identifier := 0;
294   end record;
295
296   pragma Inline (First_Entity);
297   pragma Inline (Is_Document_Entity);
298   pragma Inline (Is_External_Subset);
299   pragma Inline (Is_Parameter_Entity);
300   pragma Inline (Is_Internal_General_Entity);
301   pragma Inline (Is_External_Parsed_General_Entity);
302   pragma Inline (Is_External_Unparsed_General_Entity);
303   pragma Inline (Next_Entity);
304
305end Matreshka.Internals.XML.Entity_Tables;
306