matreshka_league_21.0.0_0c8f4d47/testsuite/xml/xmlconf-canonical_writers.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
------------------------------------------------------------------------------
--                                                                          --
--                            Matreshka Project                             --
--                                                                          --
--                               XML Processor                              --
--                                                                          --
--                            Testsuite Component                           --
--                                                                          --
------------------------------------------------------------------------------
--                                                                          --
-- Copyright © 2010-2011, 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: 1484 $ $Date: 2011-02-17 12:59:46 +0300 (Чт, 17 фев 2011) $
------------------------------------------------------------------------------
with Ada.Characters.Wide_Wide_Latin_1;
with Ada.Containers.Ordered_Maps;
with Ada.Strings.Wide_Wide_Fixed;

package body XMLConf.Canonical_Writers is

   use Ada.Characters.Wide_Wide_Latin_1;
   use type League.Strings.Universal_String;

   package Universal_String_Integer_Maps is
     new Ada.Containers.Ordered_Maps
          (League.Strings.Universal_String, Positive);

   function Escape_Character_Data
    (Item    : League.Strings.Universal_String;
     Version : XML_Version)
       return League.Strings.Universal_String;
   --  Escape character data according to canonical form representation.
   --  '&', '<', '>' and '"' characters are replaced by general entity
   --  reference; TAB, CR and LF characters are replaced by character
   --  reference in hexadecimal format.

   procedure Set_Version (Self : in out Canonical_Writer);
   --  Sets version.

   ----------------
   -- Characters --
   ----------------

   overriding procedure Characters
    (Self    : in out Canonical_Writer;
     Text    : League.Strings.Universal_String;
     Success : in out Boolean) is
   begin
      Self.Result.Append (Escape_Character_Data (Text, Self.Version));
   end Characters;

   -------------
   -- End_DTD --
   -------------

   overriding procedure End_DTD
    (Self    : in out Canonical_Writer;
     Success : in out Boolean)
   is

      procedure Output_Notation (Position : Notation_Maps.Cursor);
      --  Outputs notation declaration.

      ---------------------
      -- Output_Notation --
      ---------------------

      procedure Output_Notation (Position : Notation_Maps.Cursor) is
         Notation : constant Notation_Information
           := Notation_Maps.Element (Position);

      begin
         if Notation.Public_Id.Is_Empty then
            Self.Result.Append
             ("<!NOTATION "
                & Notation.Name
                & " SYSTEM '"
                & Notation.System_Id
                & "'>"
                & LF);

         elsif Notation.System_Id.Is_Empty then
            Self.Result.Append
             ("<!NOTATION "
                & Notation.Name
                & " PUBLIC '"
                & Notation.Public_Id
                & "'>"
                & LF);

         else
            Self.Result.Append
             ("<!NOTATION "
                & Notation.Name
                & " PUBLIC '"
                & Notation.Public_Id
                & "' '"
                & Notation.System_Id
                & "'>"
                & LF);
         end if;
      end Output_Notation;

   begin
      if not Self.Notations.Is_Empty then
         Self.Result.Append ("<!DOCTYPE " & Self.Name & " [" & LF);

         Self.Notations.Iterate (Output_Notation'Access);

         Self.Result.Append
          (League.Strings.To_Universal_String ("]>" & LF));
      end if;
   end End_DTD;

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

   overriding procedure End_Element
    (Self           : in out Canonical_Writer;
     Namespace_URI  : League.Strings.Universal_String;
     Local_Name     : League.Strings.Universal_String;
     Qualified_Name : League.Strings.Universal_String;
     Success        : in out Boolean) is
   begin
      Self.Result.Append ("</" & Qualified_Name & ">");
   end End_Element;

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

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

   ---------------------------
   -- Escape_Character_Data --
   ---------------------------

   function Escape_Character_Data
    (Item    : League.Strings.Universal_String;
     Version : XML_Version)
       return League.Strings.Universal_String
   is
      Result : League.Strings.Universal_String := Item;
      C      : Wide_Wide_Character;

   begin
      for J in reverse 1 .. Item.Length loop
         C := Result.Element (J).To_Wide_Wide_Character;

         if C = '&' then
            Result.Replace (J, J, "&amp;");

         elsif C = '<' then
            Result.Replace (J, J, "&lt;");

         elsif C = '>' then
            Result.Replace (J, J, "&gt;");

         elsif C = '"' then
            Result.Replace (J, J, "&quot;");

         else
            case Version is
               when Unspecified =>
                  raise Program_Error;

               when XML_1_0 =>
                  if C = Wide_Wide_Character'Val (9) then
                     Result.Replace (J, J, "&#9;");

                  elsif C = Wide_Wide_Character'Val (10) then
                     Result.Replace (J, J, "&#10;");

                  elsif C = Wide_Wide_Character'Val (13) then
                     Result.Replace (J, J, "&#13;");
                  end if;

               when XML_1_1 =>
                  if C in Wide_Wide_Character'Val (16#01#)
                            .. Wide_Wide_Character'Val (16#1F#)
                    or C in Wide_Wide_Character'Val (16#7F#)
                              .. Wide_Wide_Character'Val (16#9F#)
                  then
                     Result.Replace
                      (J,
                       J,
                       "&#"
                         & Ada.Strings.Wide_Wide_Fixed.Trim
                            (Integer'Wide_Wide_Image
                              (Wide_Wide_Character'Pos (C)),
                             Ada.Strings.Both)
                         & ";");
                  end if;
            end case;
         end if;
      end loop;

      return Result;
   end Escape_Character_Data;

   --------------------------
   -- Ignorable_Whitespace --
   --------------------------

   overriding procedure Ignorable_Whitespace
    (Self    : in out Canonical_Writer;
     Text    : League.Strings.Universal_String;
     Success : in out Boolean) is
   begin
      Set_Version (Self);
      Self.Result.Append (Escape_Character_Data (Text, Self.Version));
   end Ignorable_Whitespace;

   --------------------------
   -- Notation_Declaration --
   --------------------------

   overriding procedure Notation_Declaration
    (Self      : in out Canonical_Writer;
     Name      : League.Strings.Universal_String;
     Public_Id : League.Strings.Universal_String;
     System_Id : League.Strings.Universal_String;
     Success   : in out Boolean) is
   begin
      Self.Notations.Insert (Name, (Name, Public_Id, System_Id));
   end Notation_Declaration;

   ----------------------------
   -- Processing_Instruction --
   ----------------------------

   overriding procedure Processing_Instruction
    (Self    : in out Canonical_Writer;
     Target  : League.Strings.Universal_String;
     Data    : League.Strings.Universal_String;
     Success : in out Boolean) is
   begin
      Set_Version (Self);
      Self.Result.Append ("<?" & Target & " " & Data & "?>");
   end Processing_Instruction;

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

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

   -----------------
   -- Set_Version --
   -----------------

   procedure Set_Version (Self : in out Canonical_Writer) is
      use League.Strings;

   begin
      if Self.Version = Unspecified then
         if Self.Locator.Version = To_Universal_String ("1.0") then
            Self.Version := XML_1_0;

         elsif Self.Locator.Version = To_Universal_String ("1.1") then
--            Self.Result.Prepend ("<?xml version=""1.1""?>");
            Self.Result := "<?xml version=""1.1""?>" & Self.Result;
            Self.Version := XML_1_1;

         else
            raise Program_Error;
         end if;
      end if;
   end Set_Version;

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

   overriding procedure Start_DTD
    (Self      : in out Canonical_Writer;
     Name      : League.Strings.Universal_String;
     Public_Id : League.Strings.Universal_String;
     System_Id : League.Strings.Universal_String;
     Success   : in out Boolean) is
   begin
      Set_Version (Self);
      Self.Name := Name;
   end Start_DTD;

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

   overriding procedure Start_Element
    (Self           : in out Canonical_Writer;
     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
      use League.Strings;
      use Universal_String_Integer_Maps;

      Map      : Universal_String_Integer_Maps.Map;
      Position : Universal_String_Integer_Maps.Cursor;
      Index    : Positive;

   begin
      Set_Version (Self);
      Self.Result.Append ("<" & Qualified_Name);

      for J in 1 .. Attributes.Length loop
         Map.Insert (Attributes.Qualified_Name (J), J);
      end loop;

      Position := Map.First;

      while Has_Element (Position) loop
         Index := Element (Position);
         Self.Result.Append
          (" "
             & Attributes.Qualified_Name (Index)
             & "="""
             & Escape_Character_Data (Attributes.Value (Index), Self.Version)
             & '"');
         Next (Position);
      end loop;

      Self.Result.Append ('>');
   end Start_Element;

   ----------
   -- Text --
   ----------

   function Text
    (Self : Canonical_Writer) return League.Strings.Universal_String is
   begin
      return Self.Result;
   end Text;

end XMLConf.Canonical_Writers;