xia_1.4.1_b3cee557/test/src/test_xpath.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
-------------------------------------------------------------------------------
--                                                                           --
--            XPATH IN ADA Copyright (C) 2004, McKae Technologies            --
--         Copyright (C) 2021-2024, Simon Wright <simon@pushface.org>        --
--                                                                           --
--  XPath in  Ada (XIA) is  free software;  you can redistribute  it and/or  --
--  modify it under terms of the GNU General Public License as published by  --
--  the Free Software Foundation; either version 3, or (at your option) any  --
--  later version.  GNAT is distributed in the hope that it will be useful,  --
--  but  WITHOUT  ANY  WARRANTY;  without  even  the  implied  warranty  of  --
--  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                     --
--                                                                           --
--  As  a special  exception under  Section  7 of  GPL version  3, you  are  --
--  granted  additional permissions  described in  the GCC  Runtime Library  --
--  Exception, version 3.1, as published by the Free Software Foundation.    --
--                                                                           --
--  You should have received a copy of the GNU General Public License and a  --
--  copy of the GCC Runtime Library  Exception along with this program; see  --
--  the  files  COPYING3 and  COPYING.RUNTIME  respectively.   If not,  see  --
--  <http://www.gnu.org/licenses/>.                                          --
--                                                                           --
--  XIA is maintained at https://github.com/simonjwright/xia                 --
--                                                                           --
-------------------------------------------------------------------------------

with Ada.Strings.Fixed;
with Ada.Strings.Maps;
with DOM.Core.Nodes;
with DOM.Readers;
with Input_Sources.File;
with Unicode.CES;

with GNATCOLL.Readline;

with XIA;

with Ada.Text_IO; use Ada.Text_IO;

procedure Test_Xpath is

   use Ada.Strings.Fixed;
   use Ada.Strings.Maps;
   use DOM.Core;
   use DOM;
   use Input_Sources;
   use XIA;

   XML_Source_Reader : DOM.Readers.Tree_Reader;
   Queried_Nodes     : Node_List;
   N                 : Node;
   Child             : Node;
   Children          : Node_List;

   Children_Printed : Boolean := False;

   File_Source : File.File_Input;

   procedure Print_Text_Node (T : Text; Indent : Boolean := False) is
      White_Space : constant String := ' ' & ASCII.LF & ASCII.CR & ASCII.HT;
      White_Space_Set : constant Character_Set := To_Set (White_Space);

      S : constant Unicode.CES.Byte_Sequence :=
        Trim (Nodes.Node_Value (T), White_Space_Set, White_Space_Set);

   begin
      if S'Length > 0 then
         if Indent then
            Put ("  ");
         end if;
         Put_Line (S);
      end if;
   end Print_Text_Node;

begin

   GNATCOLL.Readline.Initialize (Appname => "test_xpath",
                                 History_File => "test_xpath.history");

   Get_Xml :
   declare
      Xml_File : constant String := GNATCOLL.Readline.Get_Line
        (Prompt => "Enter XML file name: ");
   begin

      File.Open (Trim (Xml_File, Ada.Strings.Both), File_Source);

      Readers.Parse (XML_Source_Reader, File_Source);
      File.Close (File_Source);

      Get_Commands :
      loop
         Get_Query :
         declare
            Query : constant String := GNATCOLL.Readline.Get_Line
              (Prompt => "Enter XPath query: ");
         begin
            exit Get_Commands when Query'Length = 0;
            if Query (1) /= '#' then
               New_Line;
               Put_Line ("Evaluating: " & Query);
               New_Line;
               Run_Query :
               begin
                  Queried_Nodes :=
                    XPath_Query (Readers.Get_Tree (XML_Source_Reader), Query);
                  Put_Line
                    ("Number of nodes:" &
                       Natural'Image (DOM.Core.Nodes.Length (Queried_Nodes)));

                  Print_Nodes :
                  for I in 0 .. Nodes.Length (Queried_Nodes) - 1 loop
                     N := DOM.Core.Nodes.Item (Queried_Nodes, I);

                     if N.Node_Type = Element_Node then
                        Put ("<");
                        Put (Nodes.Node_Name (N));
                        Put (">");

                        Children         := Nodes.Child_Nodes (N);
                        Children_Printed := False;
                        for J in 0 .. Nodes.Length (Children) - 1 loop
                           Child := Nodes.Item (Children, J);
                           if Child.Node_Type = Element_Node then
                              if not Children_Printed then
                                 New_Line;
                                 Children_Printed := True;
                              end if;
                              Put ("  <");
                              Put (Nodes.Node_Name (Child));
                              Put_Line (">");
                           elsif Child.Node_Type = Text_Node then
                              if not Children_Printed then
                                 New_Line;
                                 Children_Printed := True;
                              end if;
                              Print_Text_Node (Child, Indent => True);
                           end if;
                        end loop;

                        Put ("</");
                        Put (Nodes.Node_Name (N));
                        Put_Line (">");

                     elsif N.Node_Type = Attribute_Node then
                        Put (Nodes.Node_Name (N) & "=""");
                        Put (Nodes.Node_Value (N));
                        Put_Line ("""");

                     elsif N.Node_Type = Text_Node then
                        Print_Text_Node (N);
                     else
                        Put (Nodes.Node_Value (N));
                     end if;
                  end loop Print_Nodes;

               exception
                  when Malformed_XPath =>
                     Put_Line ("Malformed query");
               end Run_Query;
            end if;
         end Get_Query;
      end loop Get_Commands;

   end Get_Xml;

   GNATCOLL.Readline.Finalize (History_File => "test_xpath.history");

exception
   when others =>
      GNATCOLL.Readline.Finalize (History_File => "test_xpath.history");
      raise;
end Test_Xpath;