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 | -- Copyright (C) 2024 Simon Wright <simon@pushface.org>
-- Licence: GPL-3.0-or-later
with Ada.Strings.Fixed;
with Ada.Strings.Maps;
with DOM.Core.Nodes;
with Unicode.CES;
with XIA;
package body Utilities is
use Ada.Strings.Fixed;
use Ada.Strings.Maps;
use Ada.Strings.Unbounded;
use DOM.Core;
use XIA;
function Print_Text_Node (T : Text; Indent : Boolean := False)
return Unbounded_String;
function Run_Query (Document : DOM.Readers.Tree_Reader;
Query : String;
Matches : out Natural)
return Unbounded_String
is
Queried_Nodes : Node_List;
N : Node;
Child : Node;
Children : Node_List;
Result : Unbounded_String;
begin
Queried_Nodes :=
XPath_Query (DOM.Readers.Get_Tree (Document), Query);
Matches := 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 Length (Result) > 0 then
Append (Result, ";");
end if;
if N.Node_Type = Element_Node then
Append (Result, "<");
Append (Result, Nodes.Node_Name (N));
Append (Result, ">");
Element_Content :
declare
Content : Unbounded_String;
begin
Children := Nodes.Child_Nodes (N);
for J in 0 .. Nodes.Length (Children) - 1 loop
if Length (Content) > 0 then
Append (Content, ";");
end if;
Child := Nodes.Item (Children, J);
if Child.Node_Type = Element_Node then
Append (Result,
" <"
& Nodes.Node_Name (Child)
& ">");
elsif Child.Node_Type = Text_Node then
Append (Result, Print_Text_Node (Child, Indent => True));
end if;
end loop;
Append (Result, Content);
end Element_Content;
Append (Result,
"</"
& Nodes.Node_Name (N)
& ">");
-- & ";");
elsif N.Node_Type = Attribute_Node then
Append (Result,
Nodes.Node_Name (N)
& "='"
& Nodes.Node_Value (N)
& "'");
-- & ";");
elsif N.Node_Type = Text_Node then
Append (Result, Print_Text_Node (N));
else
Append (Result, Nodes.Node_Value (N));
end if;
end loop Print_Nodes;
return Result;
end Run_Query;
function Print_Text_Node (T : Text; Indent : Boolean := False)
return Unbounded_String
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);
Result : Unbounded_String;
begin
if S'Length > 0 then
if Indent then
Append (Result, " ");
end if;
Append (Result, S);
end if;
return Result;
end Print_Text_Node;
end Utilities;
|