matreshka_league_21.0.0_0c8f4d47/tools/csmib/csmib-parser.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
------------------------------------------------------------------------------
--                                                                          --
--                            Matreshka Project                             --
--                                                                          --
--         Localization, Internationalization, Globalization for Ada        --
--                                                                          --
--                              Tools Component                             --
--                                                                          --
------------------------------------------------------------------------------
--                                                                          --
-- Copyright © 2013-2017, 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: 5774 $ $Date: 2017-09-15 15:53:25 +0300 (Пт, 15 сен 2017) $
------------------------------------------------------------------------------
with Ada.Containers.Hashed_Sets;

with League.Characters;
with League.Strings.Hash;
with League.String_Vectors;
with XML.SAX.Attributes;
with XML.SAX.Content_Handlers;
with XML.SAX.Input_Sources.Streams.Files;
with XML.SAX.Simple_Readers;

package body CSMIB.Parser is

   use type League.Strings.Universal_String;
 
   package String_Sets is
     new Ada.Containers.Hashed_Sets
          (League.Strings.Universal_String,
           League.Strings.Hash,
           League.Strings."=");

   type CSMIB_Parser is
     limited new XML.SAX.Content_Handlers.SAX_Content_Handler with record
      Text      : League.Strings.Universal_String;
      Collect   : Boolean := False;
      In_Record : Boolean := False;
      MIB       : Positive;
      Aliases   : League.String_Vectors.Universal_String_Vector;
      All_Names : String_Sets.Set;
   end record;

   overriding procedure Characters
    (Self    : in out CSMIB_Parser;
     Text    : League.Strings.Universal_String;
     Success : in out Boolean);

   overriding procedure End_Element
    (Self           : in out CSMIB_Parser;
     Namespace_URI  : League.Strings.Universal_String;
     Local_Name     : League.Strings.Universal_String;
     Qualified_Name : League.Strings.Universal_String;
     Success        : in out Boolean);

   overriding function Error_String
    (Self : CSMIB_Parser) return League.Strings.Universal_String;

   overriding procedure Start_Element
    (Self           : in out CSMIB_Parser;
     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);

   function Normalize
    (Item : League.Strings.Universal_String)
       return League.Strings.Universal_String;

   Alias_Element  : constant League.Strings.Universal_String
     := League.Strings.To_Universal_String ("alias");
   Name_Element   : constant League.Strings.Universal_String
     := League.Strings.To_Universal_String ("name");
   Record_Element : constant League.Strings.Universal_String
     := League.Strings.To_Universal_String ("record");
   Value_Element  : constant League.Strings.Universal_String
     := League.Strings.To_Universal_String ("value");

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

   overriding procedure Characters
    (Self    : in out CSMIB_Parser;
     Text    : League.Strings.Universal_String;
     Success : in out Boolean) is
   begin
      if Self.Collect then
         Self.Text.Append (Text);
      end if;
   end Characters;

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

   overriding procedure End_Element
    (Self           : in out CSMIB_Parser;
     Namespace_URI  : League.Strings.Universal_String;
     Local_Name     : League.Strings.Universal_String;
     Qualified_Name : League.Strings.Universal_String;
     Success        : in out Boolean)
   is
      Name : League.Strings.Universal_String;

   begin
      if Qualified_Name = Alias_Element then
         if Self.In_Record then
            Name := Normalize (Self.Text);

            if not Self.All_Names.Contains (Name) then
               Self.All_Names.Insert (Name);
               Self.Aliases.Append (Name);
            end if;
         end if;

         Self.Text.Clear;
         Self.Collect := False;

      elsif Qualified_Name = Name_Element then
         if Self.In_Record then
            Name := Normalize (Self.Text);

            if Self.All_Names.Contains (Name) then
               --  Name of the character set is in conflict with alias of
               --  someone else character set.

               raise Constraint_Error;
            end if;

            Self.All_Names.Insert (Name);
            Self.Aliases.Append (Name);
         end if;

         Self.Text.Clear;
         Self.Collect := False;

      elsif Qualified_Name = Record_Element then
         for J in 1 .. Self.Aliases.Length loop
            MIBs.Append ((Self.Aliases.Element (J), Self.MIB));
         end loop;

         Self.In_Record := False;
         Self.Aliases.Clear;

      elsif Qualified_Name = Value_Element then
         Self.MIB :=
           Positive'Wide_Wide_Value (Self.Text.To_Wide_Wide_String);
         Self.Text.Clear;
         Self.Collect := False;
      end if;
   end End_Element;

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

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

   ---------------
   -- Normalize --
   ---------------

   function Normalize
    (Item : League.Strings.Universal_String)
       return League.Strings.Universal_String
   is
      use type League.Characters.Universal_Character;

      Aux   : League.Strings.Universal_String;
      Digit : Boolean := False;

   begin
      for J in 1 .. Item.Length loop
         case Item.Element (J).To_Wide_Wide_Character is
            when 'A' .. 'Z' =>
               Aux.Append (Item.Element (J).Simple_Lowercase_Mapping);
               Digit := False;

            when 'a' .. 'z' =>
               Aux.Append (Item.Element (J));
               Digit := False;

            when '0' .. '9' =>
               if Item.Element (J) /= '0' or Digit then
                  Aux.Append (Item.Element (J));
                  Digit := True;
               end if;

            when others =>
               null;
         end case;
      end loop;

      return Aux;
   end Normalize;

   -----------
   -- Parse --
   -----------

   procedure Parse (File : League.Strings.Universal_String) is
      Source : aliased XML.SAX.Input_Sources.Streams.Files.File_Input_Source;
      Reader : aliased XML.SAX.Simple_Readers.Simple_Reader;
      Parser : aliased CSMIB_Parser;

   begin
      Reader.Set_Content_Handler (Parser'Unchecked_Access);
      Source.Open_By_File_Name (File);
      Reader.Parse (Source'Unchecked_Access);
      Source.Close;
   end Parse;

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

   overriding procedure Start_Element
    (Self           : in out CSMIB_Parser;
     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 Qualified_Name = Alias_Element then
         Self.Text.Clear;
         Self.Collect := True;

      elsif Qualified_Name = Name_Element then
         Self.Text.Clear;
         Self.Collect := True;

      elsif Qualified_Name = Record_Element then
         Self.Aliases.Clear;
         Self.MIB := Positive'Last;
         Self.In_Record := True;

      elsif Qualified_Name = Value_Element then
         Self.Text.Clear;
         Self.Collect := True;
      end if;
   end Start_Element;

end CSMIB.Parser;